blog by OSO

How to deploy Kafka consumers on Kubernetes

Sion Smith
Sion Smith 2 August 2023
OSO guide to deploying Kafka consumers on Kubernetes with smart health checks, with white title text on a navy panel and a lime Blogs label.

Updated November 2026: We have tidied this guide and clarified how Kubernetes startup probes work, where Kafka stores committed offsets and how to avoid restarts during rebalances.

Deploying Kafka consumers on Kubernetes looks simple until a consumer sits idle while its pod reports healthy. OSO has used Kafka in production since 2014 and has delivered more than 30 enterprise Kafka projects. Our applications handle millions of messages per second and have processed over a trillion messages in total. This guide shares what we learned about keeping Kafka consumers healthy on Kubernetes.

Why are Kafka consumers on Kubernetes hard to keep healthy?

Distributed systems like Kafka are hard to manage because their components run independently. One component can keep running while another fails, for example because of a transient network issue, a configuration error or a failed external dependency. A consumer pod can therefore look alive while it has stopped processing messages, which builds up lag and can lead to incidents.

Kubernetes gives you liveness, readiness and startup probes to detect unhealthy containers. The challenge for Kafka consumers is deciding what “healthy” actually means.

What do Kubernetes probes do for Kafka consumers?

Kubernetes has three types of probe:

ProbeWhat Kubernetes does when it failsHow it applies to Kafka consumers
LivenessRestarts the containerCatches stuck or deadlocked consumers that are running but not processing
ReadinessRemoves the pod from Service endpoints, so it gets no trafficMatters for consumers that also serve HTTP, such as APIs; pure consumers pull from Kafka and receive no routed traffic
StartupHolds off liveness and readiness checks until the container has startedStops slow-starting consumers, for example those restoring state, being killed before they are up

For most Kafka consumers, the liveness probe does the important work, so it needs to reflect whether the consumer is really making progress.

How do you build a smart health check for a Kafka consumer?

Our first approach was naive. The health check listed topics on the broker to confirm the Kafka connection, and failed the liveness probe if the call failed. It caught some problems, such as TLS connectivity issues, but missed consumers that were connected and sitting idle while lag grew on their partitions.

We replaced it with a smart health check inspired by PagerDuty’s Kafka health checks. It uses two values for each partition the consumer is assigned:

  • The latest offset: the log end offset, which is the offset of the next message to be written to the partition.
  • The committed offset: the offset the consumer has committed for that partition. Kafka stores committed offsets in the __consumer_offsets topic, and the consumer also tracks the value it last committed in memory.

The check works like this:

  1. Fetch the latest offset from Kafka. If this fails, there is a connectivity problem and the check fails.
  2. Read the consumer’s last committed offset from memory. This should never fail. If it does, the consumer is in an unexpected state and the check fails.
  3. If the committed offset equals the latest offset, there is nothing left to process, so the check passes.
  4. If they differ, check whether the committed offset has changed since the previous probe. If it has not, the consumer is stuck and the check fails, so Kubernetes restarts the pod.
  5. If the committed offset has moved, the consumer is processing messages and the check passes.

This check cut Kafka-related incidents and reduced false alerts. Be careful with step 1: if the Kafka cluster itself is down, restarting every consumer pod will not fix it and can add load. Consider tolerating a few failed connection checks before you fail the probe.

What problems did we hit and how did we fix them?

ProblemWhat happenedFix
Cascading restartsOne pod restarted for an unrelated reason. The rebalance moved partitions between replicas, so other pods saw offsets that had not moved for their newly assigned partitions and failed their probes too.Track offsets per assigned partition and reset that state when partitions are revoked or assigned, so a rebalance never looks like a stuck consumer
Intermittent liveness failuresAn email delivery service read messages in batches and called external services. When a batch took longer than the probe interval, the committed offset had not moved yet, so the probe failed.Set the probe’s initial delay and interval (and failure threshold) longer than the time it takes to process your slowest batch

You can also make rebalances less disruptive. Static membership (a stable group.instance.id per pod) stops a quick pod restart from triggering a rebalance. The cooperative sticky assignor, or the new consumer rebalance protocol in Kafka 4.0, moves only the partitions that need to move. See our guide to the new consumer rebalance protocol for details.

What are the lessons for running Kafka consumers on Kubernetes?

  • Naive health checks give a false sense of security. A consumer can be connected to Kafka and still not process anything.
  • Good health checks make applications self-healing and reduce how often engineers are paged for trivial issues.
  • Smart health checks reduce false alerts and improve quality of life for the on-call team.
  • Design health checks around how your service behaves, and decide what “unhealthy” really means for it.
  • Log the partitions being consumed, the offsets being committed and how long health checks take. This makes troubleshooting much faster.
  • Monitor the health checks themselves as closely as the application.

Key takeaway: A Kafka consumer is healthy when it is making progress, not just when it is connected. Base the liveness probe on whether the committed offset moves while messages are waiting, track it per assigned partition so rebalances do not cause restarts, and size probe timings to your slowest batch.

Frequently asked questions

How do you health check a Kafka consumer?

Compare each assigned partition’s latest offset with the consumer’s committed offset. If they are equal, there is nothing to process and the consumer is healthy. If messages are waiting and the committed offset has not moved since the last check, the consumer is stuck and the health check should fail.

Should a Kafka consumer use a liveness or a readiness probe?

Use a liveness probe based on processing progress so Kubernetes restarts stuck consumers. Readiness probes only control whether a pod receives traffic through a Service, so they matter only if the consumer also serves HTTP requests. Add a startup probe if the consumer takes a long time to start.

Why do Kafka consumer pods restart during a rebalance?

During a rebalance, partitions move between pods. If the health check compares offsets across partitions a pod has just been given, it can look as if the consumer is not making progress. Track offsets per assigned partition and reset them when partitions are assigned or revoked.

How do you stop liveness probes failing on slow batches?

Set the probe’s initial delay, period and failure threshold so the total time before a restart is longer than your slowest batch takes to process. Otherwise, the committed offset will not have moved when the probe runs, and Kubernetes will restart a healthy consumer.

How can you reduce rebalances for Kafka consumers on Kubernetes?

Use static membership by giving each pod a stable group.instance.id, so short restarts do not trigger a rebalance. Use the cooperative sticky assignor, or the new consumer rebalance protocol from Kafka 4.0, so only partitions that need to move are reassigned.

Make your Kafka consumers self-healing

Talk to OSO's Kafka engineers about health checks, probe tuning and rebalance settings for Kafka consumers running on Kubernetes.

CONTACT US