KatInterview

Messaging Backend with a Tiered, Partitioned Cache Layer

System DesignModel answer
MediumModel answerSystem Designsystem-designcachingmessaging

Last updated

Design a durable, ordered, deduplicated messaging backend for DMs and small groups, and explain how a local and distributed cache is partitioned and kept correct under failures.

Problem

Design the backend of a team messaging product that supports one-to-one conversations and small group conversations (at most 50 members). Every acknowledged message must be durable, ordered identically for everyone in the conversation, and delivered quickly to anyone who is online. People who were offline must be able to catch up without seeing duplicates, and a sender who retries after a timeout must not create a second copy.

Caching is the main theme. You should say what belongs in per-server memory and what belongs in a shared distributed cache, how cache keys are split across nodes, and how you keep results correct during invalidation, failover, and traffic spikes on a single busy conversation.

Functional requirements

  • Send text messages to one person or to a group of up to 50.
  • Within one conversation, all participants see messages in the same strictly increasing order.
  • A returning user can sync what they missed, with no duplicates.
  • Retries caused by network timeouts must be deduplicated.

Non-functional requirements

  • 50 million daily active users and 500 million messages per day (about 6,000 writes/s on average, about 20,000 writes/s at peak).
  • p99 delivery latency under 100 ms for online recipients; p99 history fetch under 150 ms.
  • No acknowledged message may ever be lost (RPO = 0), and the service must stay highly available.

What to cover

  1. Architecture and data flow: ingestion, persistence, real-time fan-out over persistent connections such as WebSockets, and offline inbox sync.
  2. Data model and ordering: how per-conversation sequence numbers are produced and how idempotency keys are handled.
  3. Caching design:
    • the roles of local in-process caches (gateway and service nodes) versus a shared cache such as Redis or Memcached;
    • the partitioning scheme (for example by conversation ID with consistent hashing);
    • eviction, TTLs, and write-through versus write-around behavior.
  4. Failure handling: cache misses, stale data after a partition rebalance, stampedes on a hot group, and a network partition between the cache and the durable store.

Hints

Hint 1

Do the capacity math first: peak writes times average recipients gives delivery rate, and concurrent sockets divided by sockets per node gives the gateway count. Those numbers tell you the hot read set is small and cacheable.

Hint 2

Never use wall clocks for ordering. Keep a per-conversation counter on the same shard as the messages and allocate the sequence number, the message row and the idempotency record in a single transaction before acknowledging.

Hint 3

Treat the cache as pure acceleration that never acknowledges writes. Co-locate a conversation's keys with a hash tag, make appends conditional on sequence contiguity, and make readers compare the cached max sequence to a known last sequence so any hole turns into a miss.

Hint 4

For hot conversations combine single-flight loading on misses, short jittered local tail caches, and request coalescing. When the database is down, reject writes rather than acknowledge from the cache.