Skip to content

Kafka and message queue performance testing

B2Bnetwork team · · 6 min

System performance

In brief

A Kafka performance test checks three things: how many messages per second the cluster accepts with the required write durability, how long it takes from sending a message to its processing by a consumer, and whether consumers keep up, meaning their lag does not grow. The broker itself is rarely the bottleneck. More often it is producer settings, how keys spread across partitions and processing time in consumers. The most valuable tests are those with a broker failure, a consumer deployment and catching up on a backlog.

What to measure: four metrics

A message queue system has no single response time. The producer gets a write acknowledgment, and processing happens later, in another process. So you measure separately:

MetricWhat it tells youWhere it comes from
Producer throughputhow many messages and MB per second the cluster acceptsthe test tool, producer metrics
Write latencyhow long the producer waits for acknowledgmentproducer metrics (request-latency)
End-to-end latencyfrom sending to processing in the consumera timestamp in the message and in the consumer
Consumer laghow many messages are waiting to be processedthe records-lag-max metric, monitoring tools

On top of that come broker metrics: request handling time split into queue and processing, under-replicated partitions (UnderReplicatedPartitions, 0 in normal operation), disk and network usage. Growing lag at steady traffic is the simplest signal that consumers are falling behind, even when every other indicator looks fine.

Settings that change the result

A Kafka test result depends on settings more than on hardware. Before comparing numbers, write them down:

  1. acks and min.insync.replicas. Acknowledgment from all replicas guarantees durability but makes writes longer. Test with the setting used in production, not the fastest one.
  2. batch.size and linger.ms. A producer that waits a few milliseconds to build a batch sends fewer, larger requests. Throughput grows, and single-message latency grows a little too.
  3. Compression. It reduces network traffic and disk at the cost of producer and consumer CPU. With large JSON messages it usually pays off.
  4. Number of partitions and keys. Partitions are the unit of consumer parallelism. A key that collects most of the traffic (for example one large customer) creates a hot partition that one consumer cannot process, while the rest sit idle.
  5. Message size. A test with 100-byte messages says nothing about a system that sends 50 KB documents.
  6. Consumer settings. The number of records fetched at once, the time allowed to process a batch and the way offsets are committed decide whether a consumer keeps up and whether it drops out of the group.

How to choose topics, partitions and keys before the first deployment is covered in Apache Kafka in the enterprise.

Two levels of testing: the cluster and the application

A test of the cluster alone answers how much the infrastructure can take. Kafka ships with tools for this, for example:

bin/kafka-producer-perf-test.sh --topic test-platnosci --num-records 5000000 \
  --record-size 2048 --throughput -1 \
  --producer-props bootstrap.servers=broker1:9092 acks=all linger.ms=5 compression.type=lz4

Such a test gives you throughput and write latency percentiles for the given settings. It says nothing about the application, though: how long a consumer takes to process a message, how many times it calls the database and whether it keeps up at peak.

So the second level is an application test. A test producer sends messages with realistic structure, size and key distribution, and the real consumers process them as in production. You measure end-to-end latency, lag and resource usage of consumers and of the systems they write to. The same pattern applies to other message queue systems. For RabbitMQ the vendor provides the PerfTest tool, and at application level queue depth, acknowledgments and the prefetch count matter. The differences between these systems are covered in Kafka or RabbitMQ.

Failure and catch-up tests

A message queue system shows its worth when something goes wrong. Add these cases to the test plan:

  • Broker failure under load. Do producers switch to the new partition leader, how long is the gap, and are messages neither lost nor duplicated?
  • Consumer deployment. Restarting instances triggers a rebalance in the group. How long does it take, how much does lag grow, and does processing stall for long?
  • A slow system behind the consumer. The database or API the consumer writes to slows down. Does the consumer stay within the batch processing time limit, or does it keep dropping out of the group?
  • Catching up on a backlog. Consumers are stopped for an hour, then restarted. How quickly do they work off the lag, and do they overload the systems they write to while doing so?
  • A hot key. Most traffic goes to one key. Is there a plan for when one partition cannot keep up?

After every failure test, reconcile the numbers: messages sent, written, processed and rejected to the dead letter queue. "Zero lost and zero duplicated" has to be demonstrated, not assumed. How to design retries and idempotency is covered in Message queues and asynchronous processing.

Thresholds and the report

Thresholds for an event-driven system combine business and technical terms: for example, p99 end-to-end latency for payments below an agreed threshold, consumer lag returning to zero within minutes after the peak, and zero lost messages in failure tests. The report shows throughput with production settings, a chart of lag over time, failure test results with reconciled numbers and recommendations: partition count, producer settings, number of consumer instances.

An example of such a set of goals, with resilience tests, is the case study Replacing the ESB with a Kafka event platform for payments. At B2Bnetwork, performance testing of event-driven systems covers the cluster, producers, consumers and the systems behind them.

Questions and answers

How many messages per second can Kafka handle?

There is no single number. It depends on message size, durability settings, the number of partitions and replicas, disks and network. That is why the test runs on hardware and settings close to production, with realistic message sizes. Numbers from other people's benchmarks rarely carry over to your system.

Why does consumer lag grow when the cluster is lightly loaded?

Most often consumers process messages more slowly than they arrive: they wait for a database or API, process one at a time instead of in batches, or there are fewer of them than needed. The second cause is a hot partition handled by a single consumer. Metrics for processing time in the consumer and lag per partition help.

Can Kafka performance tests run on a managed cloud service?

Yes, but you need to know its limits: throughput per partition or per cluster, the number of connections and the cost of data transfer. The test shows whether the chosen service tier is enough for the peak. Repeat it after every change of tier.

How do you check that messages are not lost?

Every test message gets a unique ID and a sequence number. After the test, compare the list of sent messages with the list of processed ones and with the dead letter queue. Gaps and duplicates are analyzed one by one. Without that reconciliation a failure test only tells you that the system came back up.

What it looks like in a project

Your system slows down under load? We'll find where it loses time.

Let's talk

Read more: System performance

All in System performance →