Scrivio Interview library
ScenarioAdvanced7 min read · updated 13 November 2026

The Prometheus stack that scrapes 40 brokers keeps falling over: the JMX exporter emits per-partition and per-client-id metrics, about 6 million time series, scrapes take 90 seconds and dashboards time out. Teams rely on some per-topic graphs.

How do you restructure Kafka metrics collection, and which rules do you set across the platform?

We first measure where the 6 million series come from, which is almost always per-partition metrics, per-topic broker metrics with all their attributes, and request and quota metrics per client id, each multiplied by 40 brokers. Then we keep broker-level aggregates at full resolution, drop per-partition series in the exporter allowlist, build per-topic views from recording rules at lower resolution, move lag into a dedicated exporter that aggregates per group and topic, and require stable client ids. Platform rules (a series budget per cluster, review of new metrics, longer scrape intervals for slow metrics, and storage sized for the budget) keep it from growing back.

Why this question is asked

When a monitoring system collapses, it usually takes the alerts down with it, so a cluster that is fine is suddenly being run blind. Kafka is a classic cause because its JMX metrics multiply: by broker, by topic, by partition and by client. Interviewers ask this scenario to see whether we can reason about cardinality with numbers and fix it without taking away the graphs teams depend on. In this answer, we will recap what a time series is, measure where the series come from, trim the exporter, rebuild per-topic views, move lag elsewhere, deal with client ids, and set the rules that keep it healthy.

1A Quick Recap: What Makes a Time Series

In Prometheus, a time series is one metric name plus one exact combination of label values, such as bytes_in{broker="7",topic="orders"}. Every distinct combination is stored, indexed and scraped separately. So cardinality, the number of series, is the product of the label values: a metric with labels for 40 brokers, 2,000 topics and 50 partitions each could produce millions of series by itself.

The JMX exporter adds one more multiplier. A JMX meter such as BytesInPerSec has several attributes, Count plus the one, five and fifteen minute rates and a mean rate, and by default each becomes its own series. Since Prometheus computes rates from the Count, the other four are mostly waste.

2Finding Where the Cardinality Comes From

We do not guess; we ask Prometheus which metric names have the most series. Even a struggling Prometheus can usually answer this from its own index status page, or we query one broker's scrape directly.

Properties
# top metric names by series count
topk(10, count by (__name__) ({job="kafka"}))
# kafka_server_quota and request metrics 2,300,000 per user and client id x broker
# kafka_server_brokertopicmetrics_* 2,000,000 per topic x meter x attribute x broker
# kafka_cluster_partition_* 960,000 per partition replica, 6 metrics
# kafka_log_log_* 640,000 per partition replica, 4 metrics
# everything else (broker aggregates) 60,000

The pattern is typical. The per-client metrics are the largest, because pods that set no client.id, or one with the pod name in it, create new series on every deploy, and each is repeated on all 40 brokers. Per-topic broker metrics come next: about ten meters per topic, five attributes each, on every broker that hosts the topic, so 1,000 topics per broker gives 1,000 x 10 x 5 x 40 = 2 million. Per-partition metrics add 1.6 million more for 160,000 partition replicas. We attack them in order of size, and none of them is needed at that resolution.

3Keep Aggregates, Drop Per-Partition Series

Broker-level aggregates, such as total bytes in per broker, UnderReplicatedPartitions as a count, request latency and thread idle ratios, are a few hundred series per broker. They power every alert, so we keep them at full resolution, scraped every 30 seconds. Per-partition series are what we drop, by leaving kafka.log:type=Log and the per-partition cluster metrics out of the exporter allowlist, and by exporting only the Count attribute of meters.

YAML
# jmx_exporter: rules, after the includeObjectNames allowlist
rules:
- pattern: 'kafka.server<type=BrokerTopicMetrics, name=(BytesInPerSec|BytesOutPerSec), topic=(.+)><>Count'
name: kafka_topic_$1_total
type: COUNTER
labels: {topic: "$2"}
- pattern: 'kafka.server<type=(.+), name=(.+)><>(Value|Count)'
name: kafka_server_$1_$2
# OneMinuteRate, FiveMinuteRate, FifteenMinuteRate and MeanRate match no rule -> not exported

Under-replication per partition is still available when we need it, from kafka-topics.sh --describe --under-replicated-partitions, which lists exactly which partitions are affected when the aggregate alert fires.

4Per-Topic Views From Recording Rules

Teams rely on per-topic graphs, so we keep per-topic bytes in and out but change how they are stored. A recording rule sums the per-broker series into one series per topic every minute, and only the recorded series go into long-term storage; the raw per-broker, per-topic series are kept for a short window or scraped by a separate job at a longer interval.

Java
groups:
- name: kafka-topic-views
interval: 1m
rules:
- record: topic:kafka_bytes_in:rate5m
expr: sum by (cluster, topic) (rate(kafka_topic_bytesinpersec_total[5m]))
- record: topic:kafka_bytes_out:rate5m
expr: sum by (cluster, topic) (rate(kafka_topic_bytesoutpersec_total[5m]))
# 2,000 topics -> 4,000 recorded series; the 80,000 raw ones are kept only briefly

Together with the client-id fix below, this takes us from about 6 million series to under 200,000. The graphs teams use look the same, because they always showed the topic total, not one line per broker. Notice the trade: a one-minute resolution on per-topic graphs, which is plenty for capacity and trend questions.

5Lag and Client Ids

Lag per partition is the other large source, and nobody needs every partition's lag stored forever. A dedicated lag exporter computes lag from committed and end offsets and publishes it aggregated per group and topic, typically the sum and the maximum. When a team investigates, the partition-level detail is queried on demand from the exporter or with kafka-consumer-groups.sh, rather than stored.

Client ids are the cardinality we do not control directly. Kafka's request and quota metrics are keyed by client id and user, so a service that leaves client.id empty or puts the pod name in it creates new series on every deploy. The rule is a stable client.id per application, for example spring.kafka.client-id=checkout in Spring Boot, the same on every pod. Spring Kafka appends a small index per consumer thread, such as checkout-0 and checkout-1, which keeps the number bounded. Teams then also get readable per-application graphs.

6Platform Rules That Keep It Healthy

Fixes decay unless rules protect them. We set a series budget per cluster, for example 300,000, sized so Prometheus scrapes in a few seconds, and show the current count on the platform dashboard. New metrics go through a short review that asks what decision the metric supports and what its cardinality will be. Slow-changing metrics such as log sizes or certificate expiry are scraped every few minutes instead of every 30 seconds. And storage is sized for the agreed budget with headroom, so growth is a planned change rather than an outage.

Before and after
SourceBeforeAfter
Per-partition log and replica metrics1.6 million0 (on demand via CLI)
Per-topic broker metrics2 million4,000 recorded, 80,000 raw kept briefly
Per-client request and quota metrics2.3 millionabout 20,000
Broker aggregates60,00060,000
Lagper partitionper group and topic
Scrape time90 secondsa few seconds

7Follow-up questions

Will we lose anything important by dropping per-partition series?

We lose the history of individual partitions, which is rarely used. Alerts work from aggregates, and when one fires the CLI or the lag exporter gives the partition detail at that moment. If a specific case needs history, such as a known hot partition, we can allow that one series explicitly.

Why not just give Prometheus more memory?

Because cardinality grows with topics, partitions and pods, which grow with the business, so more memory only buys months. It also does nothing for the 90-second scrapes and slow queries. Fixing what we collect makes the cost follow the decisions we actually make.

How do you enforce stable client ids across 80 teams?

Through the platform's client defaults and checks: the shared Spring Boot starter sets client.id from the application name, and a weekly report lists client ids that look random or per-pod, sent to the owning teams.

8Conclusion

In this answer, we saw that 6 million series come from multiplying per-partition, per-topic and per-client metrics by 40 brokers, and that we can find the largest sources with a simple count by metric name. We kept broker aggregates, dropped per-partition series and unused meter attributes, rebuilt per-topic views with recording rules, moved lag into a dedicated exporter, required stable client ids, and set a series budget with review, scrape intervals and storage sized to match.

What to say in the interviewAbout 90 seconds aloud

Open with

I would start by measuring, not guessing: count series by metric name in Prometheus. With Kafka the answer is almost always per-partition metrics like log size and end offset, per-topic broker metrics with every meter attribute, and request and quota metrics per client id, all multiplied by 40 brokers.

Then, if they want more

Then I would cut in order of size. Broker-level aggregates stay at full resolution because the alerts depend on them. Per-partition series come out of the exporter allowlist, and I export only the Count attribute of meters, since Prometheus computes rates itself.

For the per-topic graphs teams rely on, recording rules sum per topic every minute and only those go to long-term storage, which leaves a few thousand long-term series. Lag moves to a dedicated exporter that publishes sum and max per group and topic, with partition detail queried on demand from the exporter or the CLI.

Client ids need a rule: a stable client.id per application, not per pod or random, because request and quota metrics are keyed by it. Finally I would set platform rules: a series budget per cluster shown on a dashboard, a short review for new metrics, longer scrape intervals for slow-changing ones, and storage sized for the budget. That should bring scrapes from 90 seconds to a few seconds.

About this answer

Last updated
13 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Apache Kafka documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "How do you restructure Kafka metrics collection, and which rules do you set across the...": https://scrivio-interview-library.netlify.app/spring-boot/kafka-operations/0df14d8eb1/