Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kafka ordering guarantees

I was going through kafka documentation and came across

Guarantees

At a high-level, Kafka gives the following guarantees:

Messages sent by a producer to a particular topic partition will be appended in the order they are sent. That is, if a record M1 is sent by the same producer as a record M2, and M1 is sent first, then M1 will have a lower offset than M2 and appear earlier in the log. A consumer instance sees records in the order they are stored in the log. For a topic with replication factor N, we will tolerate up to N-1 server failures without losing any records committed to the log.

I had few questions.

  1. Is it always guaranteed that M1 will have a lower offset than M2 ? what if M1 is retried later than M2 ?
  2. I also understood from various documentations that ordering is not guaranteed, and the consumer has to deal with it.
like image 373
Nag Avatar asked Jan 03 '23 12:01

Nag


1 Answers

A possible scenario even with a single partition is:

  • Producer sends M1
  • Producer sends M2
  • M1 is not ack'ed on the first try due to some failure
  • M2 is delivered
  • M1 is delivered in a subsequent try.

One easy way to avoid this is through the producer config max.in.flight.requests.per.connection=1.

This of course has performance implications, so it should be used with caution.

like image 139
vahid Avatar answered Jan 12 '23 18:01

vahid