Scrivio Interview library
DebuggingIntermediate6 min read · updated 13 November 2026

A Java consumer service deduplicates device events by event id, keeping for each assigned partition an in-memory map of ids seen in the last 10 minutes. After the topic grew from 12 to 24 partitions the duplicate rate rose from zero to 0.4 percent, and it also spikes after every deploy.

Explain both effects and how you would redesign the deduplication.

Doubling the partitions changed the partition of about half the keys, because the default partitioner takes the key's hash modulo the partition count, so a duplicate could land on a different partition from the original and miss that partition's in-memory map; and every deploy or rebalance moves partitions to instances whose maps start empty. The fix is durable deduplication keyed by event id and independent of partitions, such as Redis SET NX with a 10-minute expiry or a unique constraint at the sink, or changelog-backed state in Kafka Streams, with an idempotent sink as the last line of defence.

Why this question is asked

In-memory deduplication per partition is fast, simple and works perfectly in a test environment. It quietly assumes two things: that every copy of an event lands on the same partition, and that a partition's memory stays with it. Interviewers like this question because both assumptions break in ordinary operations, a partition increase and a deploy. In this answer, we will recap how keys map to partitions, explain each effect with numbers, and then redesign the deduplication so it survives both.

1A Quick Recap of How Keys Choose Partitions

When a Kafka producer sends a record with a key, the default partitioner hashes the key's bytes with murmur2 and takes the result modulo the number of partitions. The same key therefore always lands on the same partition, which is what lets our consumer keep a separate map of recently seen event ids per partition. But "always" silently means "as long as the partition count does not change".

Java
int partition = Utils.toPositive(Utils.murmur2(keyBytes)) % numPartitions;
// device-4411: hash 2,117,839,045 -> 12 partitions: partition 1 24 partitions: partition 13
// device-0920: hash 1,994,402,306 -> 12 partitions: partition 2 24 partitions: partition 2

The hash values in the comments are illustrative, but the arithmetic is the point: a key whose hash modulo 24 is 12 or more moves to a new partition, and one whose hash modulo 24 is below 12 stays. Going from 12 to 24 partitions therefore moves about half of all keys.

2Effect 1: The Partition Increase

Suppose device-4411 sends an event, and a few minutes later its retry arrives as a duplicate. Before the change, both went to partition 1, the consumer handling partition 1 had the id in its map, and the duplicate was dropped. During and after the change, the original may have gone to partition 1 and the duplicate to partition 13, handled perhaps by another instance with a different map. The duplicate is not found and is processed.

Producers do not all switch at the same moment either. Each producer learns the new partition count when its metadata refreshes, which can take up to metadata.max.age.ms (5 minutes by default), so for a while different producers place the same key differently. If the 0.4 percent rate does not decay once every pair of copies falls on the same side of the change, the next thing to check is whether all producers key by the same field and use the same partitioner; a client in another language with a different default hash would place keys differently all the time.

Why a duplicate escapes after the change
CopySentPartitionMap checked
Original of evt-9c1before the change (12 partitions)1map of partition 1: records evt-9c1
Duplicate of evt-9c1after the change (24 partitions)13map of partition 13: no entry, processed

3Effect 2: Deploys and Rebalances

The second effect has nothing to do with the partition count. Each map lives in the memory of one instance. During a deploy, an instance stops, its partitions are reassigned to other instances, and those instances start with empty maps for the partitions they just received. For the next 10 minutes, any duplicate whose original was seen by the old instance passes through. That is why the rate spikes after every deploy and then settles, and it would happen with any rebalance: a crash, a scale-out or a slow consumer kicked out of the group.

4Redesign: Deduplication That Does Not Depend on Partitions

The root problem is that the deduplication state is tied to a partition and to an instance, while duplicates are tied to neither. So the state must be keyed only by event id and stored where every instance can see it and where it survives restarts. The simplest version uses Redis: a SET with NX succeeds only for the first instance to see the id, and the expiry implements the 10-minute window.

Java
// SET dedup:evt-9c1 1 NX EX 600
Boolean first = redis.opsForValue().setIfAbsent("dedup:" + eventId, "1", Duration.ofMinutes(10));
if (Boolean.TRUE.equals(first)) {
handle(event); // first sighting anywhere in the group
} else {
duplicates.increment(); // seen by some instance in the last 10 minutes: skip
}

If the sink is a database, an even better option is to let the sink enforce it: a unique constraint on event_id, with INSERT ... ON CONFLICT DO NOTHING or an upsert. That costs no extra system, covers any time window and makes duplicates harmless even if every other layer fails. Note one subtlety with the Redis version: if we set the key and then crash before handling the event, the redelivered event is skipped. Setting the key after a successful handle, or relying on the idempotent sink, avoids that.

5When State Must Stay Local

Sometimes the throughput is too high for a network call per event. Then the state can stay local, but it must move with its partition. Kafka Streams keeps state in local stores backed by a changelog topic; when a partition moves, the new owner restores the store from the changelog before processing. Flink does the same with checkpoints. Both make deploys safe.

Even then, state is partitioned by key, so a partition count change still breaks the mapping. Treat it as a migration: create a new topic with 24 partitions, have producers switch to it, and let consumers drain the old topic before reading the new one, so every key's state is consistent. And whatever we choose, make the sink idempotent with an upsert by event id, so stream-level deduplication only reduces load and is never the last line of defence.

Deduplication options
OptionSurvives partition changeSurvives deploy
In-memory map per partitionnono
Redis SET NX EX 600 by event idyesyes
Unique constraint at the sinkyesyes
Kafka Streams changelog storeno (migrate topic)yes

6Follow-up questions

Could we just avoid changing the partition count?

Choosing enough partitions up front avoids most changes, and many teams overprovision for that reason. But growth eventually forces a change, so the design should either not depend on partitions or treat the change as a planned topic migration.

Is Redis a single point of failure for deduplication?

If Redis is down, we must choose to process without deduplication (risking duplicates) or pause. With an idempotent sink behind it, processing without Redis is safe, which is another reason the sink should be the last line of defence.

Why did the rate spike only for about 10 minutes after each deploy?

Because the window is 10 minutes. After that, the originals of any new duplicates were seen by the new instance itself, so its map is warm again and duplicates are caught.

7Conclusion

In this answer, we saw two ways per-partition in-memory deduplication fails. Doubling partitions moved about half the keys, because the partitioner hashes modulo the partition count, so duplicates landed on partitions whose maps had never seen the original. Deploys and rebalances moved partitions to instances whose maps started empty. We redesigned the state to be keyed by event id and stored durably, with Redis SET NX and a 10-minute expiry or a unique constraint at the sink, kept local state only with changelog-backed stores and topic migrations, and made the sink idempotent so it is always the final protection.

What to say in the interviewAbout 100 seconds aloud

Open with

Both effects come from tying the deduplication state to a partition and an instance. The default partitioner hashes the key with murmur2 modulo the partition count, so going from twelve to twenty-four partitions moves about half the keys.

Then, if they want more

An original sent before the change and its duplicate sent after can land on different partitions, and the duplicate's partition map has never seen the id.

Producers also pick up the new count at different times through metadata refresh, so for a while they disagree; if the rate didn't decay afterwards, I'd check that every producer keys by the same field and uses the same partitioner.

The deploy spikes are simpler: when partitions move to another instance, its in-memory maps start empty, so for the next ten minutes duplicates pass through.

My redesign keys the state only by event id and stores it durably: for example Redis SET NX with a ten-minute expiry, so the first instance anywhere wins, or better, a unique constraint on event id in the sink database with an upsert or ON CONFLICT DO NOTHING.

If throughput forces local state, I'd use Kafka Streams or Flink so state is backed by a changelog and moves with its partition, and I'd treat any partition count change as a migration to a new topic.

Above all, the sink should be idempotent by event id, so stream-level dedup only reduces load and is never the last line of defence.

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 both effects and how you would redesign the deduplication.": https://scrivio-interview-library.netlify.app/architecture/distributed-systems/6c56094185/