KatInterview

Membership Service for a Growing ID Set: Latency, Memory, Durability

System DesignModel answer
MediumModel answerSystem Designbloom-filterlsm-treeconcurrency

Last updated

Design a check-and-insert service for ~1 million user IDs, covering data structure choice, atomic concurrency, probabilistic filters, and crash-safe on-disk storage.

Membership Service for a Growing ID Set

A backend service maintains a registry of user IDs. Every incoming request carries one ID. The service must answer a single question: has this ID been seen before? If the answer is no, the ID must be recorded so future requests can find it.

Your job is to design that service end-to-end, starting from the simplest working solution and evolving it as constraints tighten. Be explicit about your assumptions, your numbers, and the trade-offs you accept at each step.


Constraints and clarifications

  • The ID format, the request rate, and whether state must survive a restart are left open. State your assumptions before designing.
  • Check-and-insert is one logical operation. A caller must get a consistent answer: if the ID was absent, it must be present after the call returns.

Good clarifying questions to raise

  • What do IDs look like — 64-bit integers, UUIDs, opaque strings? Are integer IDs dense within a bounded range?
  • What is the request rate, and what share of requests carry a brand-new ID?
  • Is an occasional wrong answer acceptable? If so, which error direction is tolerable: falsely reporting "already seen" or falsely reporting "new"?
  • Must the registry survive a process restart? Is it shared across multiple service instances?
  • Will the registry grow well beyond 1 million entries, and can IDs ever be removed?

Part 1 — Single-machine baseline

Design the simplest solution that correctly handles 1 million IDs on one machine.

Cover:

  • A concrete data structure, its time complexity per operation, and a memory estimate for the ID format you assumed
  • How you make check-and-insert atomic when requests arrive concurrently, both within a single process and when multiple processes share the same store
  • Any alternative structures that become attractive for special ID shapes or heavily read-biased workloads

Part 2 — Approximate membership when memory is scarce

Assume memory is limited, or the set is expected to grow by several orders of magnitude. An exact in-memory structure holding every ID is no longer viable. How do you keep most lookups fast while accepting some imprecision?

Cover:

  • A compact probabilistic structure, the specific kind of error it can produce, and how to tune its size to hit a target error rate
  • How the overall system still produces an exact final answer — or what the consequences are if it does not
  • What happens to error rates as the set expands past its initial design capacity, and how deletions complicate things

Part 3 — Persistent storage that scales

The authoritative copy of the registry must now live on disk, survive crashes, and keep accepting inserts at a steady rate without making lookups slow.

Cover:

  • The write path: how you achieve durability, how you buffer writes in memory before flushing to disk
  • The read path: what you search first, what you search next, and how you avoid reading most of it most of the time
  • Background work that keeps read performance from degrading over time, and how the system recovers after a crash

Hints

Hint 1

For Part 1, nail down the ID format before estimating memory — a 128-bit UUID stored as a raw string costs 3–4× more than one stored as bytes. Then think about what 'atomic' means at the process level versus the host level.

Hint 2

For Part 2, a Bloom filter can tell you an ID is definitely absent, but it can never tell you it is definitely present. Work out which error direction is safe for this operation, and what must sit behind the filter to guarantee correctness.

Hint 3

For Part 3, think about why appending to a log and sorting in the background is friendlier to disks than in-place updates, and what small index structure lets you skip most on-disk files without reading them.