KatInterview

Thread-Safe Tag Index and Online Bookstore Architecture

System DesignModel answer
HardModel answerSystem Designconcurrencyobject-oriented-designdistributed-systems

Last updated

Design a concurrent in-memory tagging system with correct multi-index atomicity, then architect a scalable bookstore handling high-read browsing and consistent purchases.

The Problem

This question has two independent parts. Budget roughly 15–20 minutes on Part 1 and 25–30 minutes on Part 2.


Part 1: Concurrent Document-Tag Index

Build an in-memory library that associates arbitrary string tags with documents. Multiple threads will call it simultaneously.

Your design must support:

  • Attaching or detaching a tag from a document
  • Retrieving all tags on a given document
  • Retrieving all documents that carry a given tag
  • (Bonus) Retrieving documents that carry every tag in a given set

What the interviewer is looking for:

  • A clean class structure that separates entity representation from index management
  • Correct identification of the atomicity problem that arises when two indexes must stay in sync
  • At least two locking strategies with their trade-offs, including how to avoid deadlock when striping
  • Returning safe, immutable views to callers
  • Complexity analysis for the intersection query

Part 2: Scalable Online Bookstore

Design the back-end of a bookstore that lets users browse a catalog and place orders for physical or digital books.

Core capabilities:

  • Full-text and category search across a catalog of millions of titles
  • Viewing book details, pricing, and stock availability
  • A shopping cart
  • Checkout with payment, no overselling, no duplicate charges
  • Order history

What the interviewer is looking for:

  • Explicit scale assumptions backed by rough numbers
  • A clear API surface
  • A data model that separates read-heavy catalog data from strongly-consistent inventory and order data
  • Caching strategy with stated TTLs and invalidation approach
  • How the checkout flow prevents race conditions (concurrent buyers, duplicate submissions)
  • Where the system degrades gracefully versus where it must not

Hints

Hint 1

For Part 1, think about what 'atomic' means when a single logical operation touches two separate maps — per-map thread safety is necessary but not sufficient. Name the specific failure mode a reader can observe if you don't guard both maps together.

Hint 2

For lock striping in Part 1, the key question is: what rule prevents two threads from deadlocking when they each need two stripes? Write that rule down before you code anything.

Hint 3

For Part 2, the read:write ratio on a bookstore is roughly 1000:1. Let that ratio drive every architectural decision: where you cache, what consistency level you need, and which storage engine you pick for each data type.