Scrivio Interview library
DebuggingIntermediate8 min read · updated 13 November 2026

A stream processing job joins 200,000 click events per second with user profiles, keyed by user id across 48 partitions. One task runs 40 minutes behind while the others keep up; its input partition receives about 30 percent of all records, and the lagging records all have an empty user id.

Explain how this skew arises and how you would fix it, both in the job and at the source of the events.

The skew comes from the partitioner: it hashes the record key, so every click with an empty user id gets the same hash and lands on the same partition and the same task. One synthetic key became the hottest key in the system. We fix it in the job by splitting anonymous clicks off before the keyed join (and salting the key if they still need aggregation), and at the source by finding why producers send empty ids and giving those events a well-spread key such as a session id.

Why this question is asked

Skew is one of the most common reasons a streaming job falls behind while its cluster looks half idle. Interviewers like this scenario because the fix that comes to mind first, adding partitions or consumers, does nothing at all, and seeing why tells them whether we understand how keyed partitioning really works. In this answer, we will recap how records are assigned to partitions, work out the numbers from the overview, then fix the job with a split and with key salting, fix the producers, and finally add the monitoring that catches the next sentinel key quickly.

1A Quick Recap of Keyed Partitioning

In Kafka, a topic is split into partitions, and each partition is an ordered log. When a producer sends a record with a key, the default partitioner hashes the key bytes (with murmur2) and takes the result modulo the number of partitions. The same key therefore always goes to the same partition, which is exactly what we want for a join: all clicks of user 42 and the profile of user 42 meet in the same place.

A Kafka Streams application runs one task per input partition. Task 17 reads partition 17 of the clicks topic and partition 17 of the profiles topic, and keeps the profile table for those users in its local state store. A task is processed by a single thread at a time, so the work of one partition cannot be shared by several threads. That is the property that turns a bad key into a lagging job.

2How an Empty User Id Becomes the Hottest Key

Now look at the numbers. The job reads 200,000 clicks per second over 48 partitions, so a fair share is about 4,200 records per second per partition. The lagging partition receives 30 percent of everything, which is 60,000 records per second, while the other 47 share the remaining 140,000, about 3,000 each. One task has twenty times the work of its neighbours.

The reason is in the last detail of the overview: the lagging records all have an empty user id. An empty string is still a key. It hashes to one fixed number, so every anonymous click from every logged-out visitor goes to the same partition. The same happens with other sentinel values such as 'unknown', '0' or 'null' written as a string. Real user ids are spread evenly by the hash; the sentinel is one key with millions of records behind it.

Notice also that these records are useless for the join. An anonymous click has no profile to join with, so the hot task spends most of its time looking up a key that never matches.

Step throughWhy one task falls behind
  1. Producer sends click with key ""30 percent of all clicks are anonymous
  2. Partitioner hashes the keymurmur2("") is always the same number
  3. Same partition every timepartition 17 gets 60,000 records per second
  4. One task owns partition 17a task is processed by one thread
  5. Can the task keep up?
    YesHealthyNoLag grows: 40 minutes behind

5 steps in order. Play them, or step through them one at a time.

Every other partition gets about 3,000 records per second, so the cluster looks mostly idle.

3Why Adding Partitions or Consumers Does Not Help

The first instinct is to scale out. But with 96 partitions, the empty key still hashes to exactly one of them, and that one still receives 60,000 records per second. Adding consumer instances only moves tasks between machines; the hot task remains a single task on a single thread. The maximum parallelism for one key is always one, because the partitioner promises that all records with that key stay together and in order.

So the fix must change the keys, not the cluster. Either the hot records stop going through the keyed step, or their key stops being a single value.

4Fix 1 in the Job: Split Anonymous Clicks Off Before the Join

The cleanest fix is to route anonymous clicks around the join. In Kafka Streams, split() divides a stream into named branches by a predicate, and each branch continues on its own. We must do this before any operation that repartitions or joins by user id.

Java
KStream<String, Click> clicks = builder.stream("clicks");
KTable<String, Profile> profiles = builder.table("profiles");
Map<String, KStream<String, Click>> parts = clicks
.split(Named.as("clicks-"))
.branch((userId, click) -> userId == null || userId.isBlank(), Branched.as("anon"))
.defaultBranch(Branched.as("known"));
parts.get("clicks-anon")
.mapValues(AnonymousClick::from) // no per-user state needed
.to("clicks-anonymous"); // 60,000/s, read by a stateless consumer
parts.get("clicks-known")
.join(profiles, EnrichedClick::of) // only real user ids reach the join
.to("clicks-enriched"); // about 3,000/s per task, evenly spread

Notice that this does not remove the hot partition on the input topic itself; the producers still write anonymous clicks to one partition. What it removes is the expensive work: the anonymous branch does a cheap stateless transformation, so one task can easily handle 60,000 records per second of it. The real fix for the input partition comes from the source, which we look at below.

5Fix 2: Salt the Key When Anonymous Clicks Still Need Aggregation

Sometimes we do need to count anonymous clicks, for example per page and minute. Grouping them all by the empty key would recreate the hot task. Instead we salt the key: we append a random suffix between 0 and N-1, so one logical key becomes N physical keys spread across partitions. Each salted key is aggregated separately, and a second, much smaller step strips the salt and adds the partial results together.

Java
int SALTS = 16;
anonymous
.selectKey((k, c) -> c.page() + "#" + ThreadLocalRandom.current().nextInt(SALTS)) // "home#7"
.groupByKey()
.windowedBy(TimeWindows.ofSizeWithNoGrace(Duration.ofMinutes(1)))
.count() // 16 partial counts per page and minute
.toStream()
.selectKey((wk, n) -> wk.key().substring(0, wk.key().lastIndexOf('#'))) // "home"
.groupByKey(Grouped.with(Serdes.String(), Serdes.Long()))
.reduce(Long::sum); // running sum; 16 small inputs per page and minute

The first step carries the heavy volume, now spread over 16 tasks. The second step sees only 16 records per page per window, which is tiny. The cost is an extra repartition topic and the fact that ordering per logical key is lost, which is fine for counts and sums because addition does not care about order. In production we would keep the window in the second step too so the totals stay per minute; the snippet keeps it short.

6Fixing the Source of the Events

The job fixes are defensive. The lasting fix is upstream: find out why producers send an empty id at all. Often it is expected, because logged-out visitors have no user id, but sometimes it is a tracking bug, such as a mobile release that reads the id before login completes. The producer team should know which, and the data contract for the topic should say what the key means when there is no user.

For logged-out visitors, a session id or device id makes a good key. It is well distributed, and it keeps per-session ordering, which is usually what analytics of anonymous behaviour needs. Producers should never fall back to a constant. If no key is meaningful at all, sending a null key lets Kafka spread records across partitions rather than piling them onto one.

7Monitoring So the Next Sentinel Key Is Caught in Minutes

This job was 40 minutes behind before anyone looked. We want two signals on the dashboard. The first is the input rate per partition, taken from the broker metrics or from the consumer, shown as a heat map so one bright partition stands out. The second is lag per task, not just the total lag of the consumer group, because a total can look acceptable while one task is far behind.

The alert itself should compare the busiest partition with the average: if the max-to-mean ratio of input rate stays above, say, 3 for ten minutes, page the owner. A ratio catches skew no matter how busy the system is overall, and in this incident it would have read 20 within minutes of the bad release.

8Follow-up questions

Why does adding partitions or consumers not help here?

Because the partitioner maps one key to exactly one partition, however many partitions exist, and one partition is processed by one task on one thread. With 96 partitions the empty key still lands on a single partition carrying 60,000 records per second. Extra consumers only redistribute the other, healthy tasks. The load of one key can be spread only by changing the key, by salting it, or by taking those records out of the keyed step.

Would a null key behave differently from an empty string?

Yes. A null key is not hashed; the producer spreads keyless records across partitions (the sticky partitioner fills a batch for one partition, then moves to another), so they do not pile onto one partition. But a null key also means the records cannot take part in a keyed join, and Kafka Streams drops or treats null-key records specially in many keyed operations, so check your version. An empty string is a real key and goes to one partition.

How large should the salt range be?

Large enough that each salted key carries an ordinary share of load. Here the hot key has about 20 times the average rate, so 16 to 32 salts brings each piece close to normal. More salts make the second, combining step larger and add more partial records, so we pick the smallest number that removes the hotspot.

9Conclusion

In this answer, we saw that keyed partitioning sends every record with the same key to one partition and one task, so a sentinel value such as an empty user id quietly becomes the hottest key. More partitions or consumers cannot help because one key has a parallelism of one. We split anonymous clicks off before the join, salted the key when they still need aggregation, fixed producers to send a session id instead of a constant, and added per-partition rates and per-task lag with a max-to-mean alert.

What to say in the interviewAbout 90 seconds aloud

Open with

The skew comes from the key, not from the cluster. Kafka hashes the record key to pick a partition, so every click with an empty user id gets the same hash and lands on one partition.

Then, if they want more

Thirty percent of 200,000 per second is 60,000 records per second on one partition, about twenty times the average, and one Streams task owns that partition on a single thread, so it falls behind while the others idle. Adding partitions or consumers does nothing, because one key always maps to one partition.

In the job, I would split anonymous clicks off with split and a branch before the join, since they have no profile to join anyway, and send them down a stateless path.

If they still need aggregation, I salt the key with a random suffix from 0 to 15, aggregate per salted key, then strip the salt and sum the partial results in a small second step.

At the source, I would find out why producers send an empty id, whether it is logged-out users or a tracking bug, and have them key those events by session id, never by a constant.

Finally I would add per-partition input rates and per-task lag to the dashboard with an alert when the busiest partition runs more than about three times the mean, so the next sentinel key is caught in minutes rather than after 40 minutes of lag.

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 official documentation for the version you use. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
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 "Explain how this skew arises and how you would fix it, both in the job and at the source...": https://scrivio-interview-library.netlify.app/architecture/partitioning-and-replication/a7894eb81c/