KatInterview

Stock Ledger and Support Chat: Two Backend Services

System DesignModel answer
MediumModel answerSystem Designsystem-designconsistencywebsockets

Last updated

Design an oversell-proof book stock service with search, plus a real-time one-to-one support chat with delivery receipts, history and offline handling.

Two Services for an Online Book Retailer

An online book retailer wants two separate backend services. Describe the design of each and justify the technology choices.

Service A: Stock and Catalog

Build a service that keeps track of book titles, their authors, and how many copies sit in each warehouse.

  • Stock must never go negative or be sold twice during checkout, so decrements need strong consistency.
  • Customers can search by title or author.
  • Scale: about 10 million SKUs and about 5,000 requests per second overall.

Service B: Support Chat

Build a service where a customer talks to a support agent in real time.

  • One-to-one conversations only.
  • Each message shows a status of sent, delivered, or read.
  • Past messages in a conversation can be retrieved.
  • Scale: about 100,000 users connected at the same time.

What to cover for each service

  1. Interface: the main operations (for example a checkout call and a send-message call), with their inputs and outputs.
  2. Storage: which database type you pick (relational or NoSQL), and the schema.
  3. Architecture: how a request flows end to end, including load balancing, caching, and where state is kept.
  4. Failure handling and trade-offs: for instance, what happens to a chat message sent to someone who is offline, or to a checkout request that is retried after a timeout.

Back your choices with rough capacity numbers.

Hints

Hint 1

Ask which service needs correctness above all and which needs low-latency fan-out and write throughput; let that decide storage and protocol for each.

Hint 2

For stock, avoid read-then-write. Think about a single conditional UPDATE, a short-lived reservation with an expiry, and an idempotency key so a retried checkout cannot decrement twice.

Hint 3

For chat, a persistent connection lets the server push. Persist a message before acknowledging it, track read/delivered state as a per-user watermark rather than per-message rows, and keep connection routing in a fast shared store.