KatInterview

Build an E-Commerce Book Marketplace Backend

System DesignModel answer
MediumModel answerSystem Designe-commerceinventory-managementdistributed-transactions

Last updated

Architect a scalable platform for browsing, purchasing, and tracking book orders with high-availability search and transactional inventory correctness.

Problem

You're building the backend for a large-scale book marketplace. Customers need to browse a massive catalog, search for titles, manage shopping carts, complete purchases, and track their orders. The platform must handle heavy read traffic while guaranteeing that no book is oversold and no customer is charged twice.

Functional Scope

Your system must support:

  1. Discovery and catalog browsing: Search by title, author, genre, or ISBN. Display book metadata, pricing, reviews, and current availability.
  2. Shopping cart and checkout: Let users build a cart, temporarily hold inventory while they complete payment, and finalize orders.
  3. Payment processing: Integrate with external payment providers, handle callbacks, and confirm transactions.
  4. Order management: Track orders through their lifecycle—created, paid, being prepared, shipped, delivered, or cancelled.

Non-Functional Requirements

  • Partition tolerance: Catalog browsing and search should remain available even during partial outages; purchasing flows require strong consistency.
  • Performance: Search responses in under 100 ms at p95; checkout completion within 500 ms at p95.
  • Reliability: Prevent duplicate charges, duplicate orders, and inventory races. Recover gracefully when payment providers are slow or unavailable.

Expected Scale

  • 50 million book titles in the catalog
  • 10 million daily active users
  • 25,000 search queries per second on average, spiking to 50,000 QPS
  • 500 orders per second normally, up to 2,000 orders per second during peak events

What to Address

  1. API surface: Sketch the key endpoints for search, checkout, payment callbacks, and order status.
  2. Data layer: Choose storage technologies and design schemas for books, inventory, carts, and orders. Discuss relational vs. document/search trade-offs.
  3. Inventory correctness: Explain how to reserve stock during checkout without holding database locks for minutes or allowing overselling.
  4. Order orchestration: Describe the workflow from checkout initiation through payment to fulfillment, and how to handle partial failures.
  5. Search synchronization: Keep the search index fresh as inventory and catalog change.
  6. Fault handling: Address idempotency, retries, dead-letter queues, and monitoring for failures in the checkout pipeline.

Hints

Hint 1

Separate read-heavy catalog browsing from write-heavy transactional purchasing; eventual consistency is acceptable for search but not for inventory updates.

Hint 2

Use optimistic concurrency or conditional updates to reserve inventory atomically without long-lived locks; a version column or compare-and-set prevents race conditions.

Hint 3

Model order processing as a saga with compensating actions rather than distributed two-phase commit, since external payment APIs cannot participate in 2PC and holding locks across network calls is brittle.