KatInterview

In-Memory Event Deduplication with a Sliding Time Window

System DesignModel answer
MediumModel answerSystem Designdeduplicationin-memoryconcurrency

Last updated

Design a low-latency, high-throughput in-memory service that suppresses duplicate events within a configurable 10-minute window at 100k events/sec.

Background

Distributed systems that use at-least-once delivery guarantees — message queues, webhook retriers, event buses — routinely emit the same logical event more than once. Upstream retries after a network hiccup, publisher crashes mid-flush, and consumer-side reprocessing on restart all produce bursts of identical messages. Downstream services need a fast, cheap gate that absorbs those bursts before they propagate.

You are asked to design an in-memory deduplication service that sits in front of downstream consumers. It receives a stream of events, each identified by a stable deduplication key, and tells the caller whether the event is new or a duplicate of something already seen within the last 10 minutes.

What to Design

Functional Scope

  • Single atomic operation: check_and_record(key) → NEW | DUPLICATE. A caller must not have to issue a separate read and then a write; those two steps must be fused into one atomic operation.
  • 10-minute deduplication window. Support and be able to discuss two variants:
    • Fixed from first seen: the key expires exactly 10 minutes after the first time it was recorded, regardless of how many duplicates arrive in between.
    • Sliding refresh: each new duplicate resets the 10-minute clock, keeping the key alive as long as duplicates keep arriving.
  • Predictable memory reclamation: expired keys must be cleaned up continuously. Large, bursty eviction sweeps that cause latency spikes are unacceptable.

Non-Functional Requirements

PropertyTarget
p99 check latency< 2 ms
Peak throughput100,000 events / second
ConcurrencyThread-safe; no lock-convoy bottlenecks on multi-core hardware
MemoryBounded and predictable; sized at startup
Clock resilienceImmune to wall-clock adjustments and NTP steps

Scale Assumptions

  • Event rate: 100,000 / second sustained at peak.
  • Window: 600 seconds.
  • Maximum live keys in memory at any moment: ~60 million.
  • Key size: 128-bit hash (16 bytes).

What Is Expected

Walk through your design as you would in a real interview. Cover:

  1. Clarifying questions you would ask before drawing anything.
  2. A back-of-envelope memory estimate that motivates your data structure choices.
  3. The core data structures and how they handle both window policies correctly.
  4. How and when expired keys are reclaimed without causing latency spikes.
  5. Your concurrency model — how you achieve thread safety at 100k ops/sec.
  6. How you handle the clock — which clock source, and why.
  7. The availability trade-off: pure volatile state vs. some form of persistence.

You do not need to write production code, but pseudocode for the core check-and-record logic is expected.

Hints

Hint 1

Before choosing a data structure, work out how many bytes 60 million entries would require under a naive approach (e.g., a standard concurrent hash map) and under a flat open-addressing table. The gap is large enough to drive your entire design.

Hint 2

Correctness and reclamation can be decoupled: the expiry check on every read guarantees exact semantics, while a separate bucketed structure handles bulk cleanup lazily. This separation also makes it easy to support both window policies with the same code path.

Hint 3

A single global lock is obviously wrong, but a single lock per shard at 64 shards and 100k ops/sec means roughly 1,500 ops/sec per lock — low enough that striped locking is simple and effective. Mention what you would do differently at 10× scale.