How to maintain consumption of news idempotency

Idempotence: a request, no matter how many times to repeat the result will not change.

Each message will have a unique message id.
1), first check and then save
every time you save the data, you have to check if the data existed then do not save. The situation is complicated by not high.

2) the business table to add constraints
if your database in the future will not be sub-library sub-table, you can add a unique constraint (UNIQUE) table in the business field, so that the same data is not stored in more than one.

3) adding the message table
further inside the database, adding a message consumption record table, table field plus a unique constraint (UNIQUE), then insert a table aboard consumed the data. Because the addition of a unique constraint, saved a second time, MySQL will error, it is not inserted into; repeated consumption can be limited by the database.

4) using Redis
if your system is distributed, and made a sub-library sub-table, you can use Redis to do the record, there is the message id Redis, the next time there is a duplicate message id in the consumer's time, if Redis can not be found inside had to consume.

5), a highly concurrent
if your system very complicated, it may be used or distributed Redis zookeeper message id to lock, then the above method is performed using several control idempotent

 

Guess you like

Origin www.cnblogs.com/zk-blog/p/12364844.html