> For the complete documentation index, see [llms.txt](https://eric-zhang-seattle.gitbook.io/mess-around/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://eric-zhang-seattle.gitbook.io/mess-around/message-queue/kafka-overview/msgbacklog.md).

# Message backlog

* [Partition to consumer mapping](#partition-to-consumer-mapping)
* [Number of partitions](#number-of-partitions)
* [Message backlog solutions](#message-backlog-solutions)
  * [Add partition](#add-partition)
  * [Add topic](#add-topic)
  * [Optimize consumer performances](#optimize-consumer-performances)
    * [Downgrade consumers](#downgrade-consumers)
    * [Batch messages](#batch-messages)
    * [Asynchronous processing](#asynchronous-processing)
      * [Message loss](#message-loss)
      * [Partial failure](#partial-failure)

## Partition to consumer mapping

* 1 partition corresponds to only 1 consumer.
* 1 consumer corresponds to more than 1 partition.

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

## Number of partitions

* The following QPS numbers are all average QPS rather than peak QPS because message queue is used to smooth the traffic in the beginning.
* Depends on two factors:
  * The ratio of producing msgs: Suppose QPS 1000 and each partition could support 100 write request, then we need 10 partitions at least.
  * The ratio of consuming msgs: Suppose each consumer consumes 100 request, then we need 20 paritions at least.
  * Take the bigger of the two (10, 20)

## Message backlog solutions

### Add partition

* Sometimes devops team does not allow to add partition

### Add topic

* Add a new topic with more consumers.

### Optimize consumer performances

#### Downgrade consumers

* For example, original consumers need to call several downstream services for response. In downgraded cases, consumers could rely on cache.

#### Batch messages

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

#### Asynchronous processing

* Consumer thread only pulls the msg from msg queue.
* And then worker thread will actually perform tasks.

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

**Message loss**

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

**Partial failure**

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