# Consumer idempotency

* [Consumer idempotency](#consumer-idempotency)
  * [When local transaction is transaction](#when-local-transaction-is-transaction)
  * [When local transaction not available](#when-local-transaction-not-available)
* [Reduce load stress on unique index](#reduce-load-stress-on-unique-index)
  * [Flowchart of adding bloomfilter and redis](#flowchart-of-adding-bloomfilter-and-redis)
  * [Update order](#update-order)
  * [Improvements with local bloomfilter and consistent hashing](#improvements-with-local-bloomfilter-and-consistent-hashing)

## Consumer idempotency

* The most straightforward way to guarantee consumer idempotency is to use unique index.
* There are two ways to implement unique index depends on whether local transaction is available.

### When local transaction is transaction

![](https://1010073591-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mk8dv8Mfudl_6ziUzDf%2Fuploads%2Fgit-blob-53f212a4da0cfbe58512b5e7b6b082fa4170d69d%2FmessageQueue_consumerIdempotency_uniqueIndex.png?alt=media)

### When local transaction not available

![](https://1010073591-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mk8dv8Mfudl_6ziUzDf%2Fuploads%2Fgit-blob-d6546465fddf60a4c85c6a89bb51a223f800e2b9%2FmessageQueue_consumerIdempotency_uniqueIndex_asyncDetection.png?alt=media)

## Reduce load stress on unique index

### Flowchart of adding bloomfilter and redis

![](https://1010073591-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mk8dv8Mfudl_6ziUzDf%2Fuploads%2Fgit-blob-826688e882840dffeb9db20320374d0a7db7e9d8%2FmessageQueue_consumerIdempotency.png?alt=media)

### Update order

1. Unique index because source of truth is always the first option.
2. Then update bloomfilter or redis.

### Improvements with local bloomfilter and consistent hashing

* If consistent hashing is used, then the distributed bloomfilter could be moved to local machines, this will help reduce hash collision and performance (reduce network traffic).

![](https://1010073591-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-Mk8dv8Mfudl_6ziUzDf%2Fuploads%2Fgit-blob-af3bdfeb7133509dbf6b183b1ef88543f2e1eb78%2FmessageQueue_localbloomfilter.png?alt=media)
