How the message queue processing duplicate messages

First, the message duplication

  In MQTT protocol given quality of service can be provided when three kinds of standard delivery message:

      At most once: at most once, this situation will lose some data, this is not strictly the general log collection can be used for data

      At least once: at least once, this will lead to a repeatedly transmitted message

  Exactly once: just once, a message will only be consumed once

      

  At least once RocketMQ, Rabbit MQ, Kafka are used, although the message is repeated, but not lost. Exactly once it does not use this, because this is a time to be sent before sending check whether the message has been successfully sent, greatly reducing the performance of MQ.

     

Second, the solution

  That message is repeated, and how to solve it? Usually in the consumer end of the guarantee idempotency to resolve.

  Idempotent: f (f (x)) = f (x), a plurality of times and the execution result of the execution is the same, which we call idempotent.

  For example, now there is a demand: to increase the balance of account A 100.

  Program: controlling by a unique constraint

    1. Database unique index:

      Transaction order number and account established water table unique index, duplicate insert when the violation is unique, so the only successful once.

    2.redis of setnx

      redis has the key operation can not be repeated

  Option II: Prerequisites

    1. Database Query

      Plus distributed lock, there is no running water and then query the order number, you can not insert.

    2. The database version number

      Check out the current record and which version number, the update time to update based on the version number.      

  Option Three: Global id

    Producer to a global increase in data id, the consumer side to query this id has been no consumption, no processing is performed.

Guess you like

Origin www.cnblogs.com/ITyannic/p/12241861.html