Video in progress

Strong Middle 📨 Kafka and messaging

What do the consumer's `fetch.min.bytes`, `fetch.max.wait.ms`, `max.partition.fetch.bytes` and `max.poll.records` control, and when do you change them?

The first three control how data is pulled from the broker (how long to wait, how much), while max.poll.records only controls how many records a single poll() returns to the application. For throughput I increase the fetch sizes; if I have a poll timeout problem, I decrease max.poll.records.

📖 Answer

These settings often get mixed up, but they work at two different layers.

Network layer (fetch):

  • fetch.min.bytes (default 1): the broker waits until at least this many bytes have accumulated before responding;
  • fetch.max.wait.ms (default 500): but it won't wait longer than this;
  • max.partition.fetch.bytes (default 1 MB): the most from a single partition in one fetch;
  • fetch.max.bytes (default 50 MB): the limit for the whole fetch response.

Application layer:

  • max.poll.records (default 500): fetched data sits in the client buffer, and poll() hands out at most this many records from it. It has no effect on the network at all.
spring:
  kafka:
    consumer:
      max-poll-records: 100
      fetch-min-size: 65536      # accumulate 64 KB
      fetch-max-wait: 200ms

What I change and when:

  • An analytics or log consumer with lots of small messages: I raise fetch.min.bytes. Fewer requests to the broker, less CPU.
  • Each record needs heavy processing and rebalances are happening: I lower max.poll.records.
  • Latency matters (a payment confirmation): I leave fetch.min.bytes=1, otherwise a message may wait up to fetch.max.wait.ms.

One subtle point: a message larger than max.partition.fetch.bytes still comes through (Kafka doesn't split a single message), so it's a "target size", not a "limit".