Designing an E-Commerce Platform
Difficulty: Advanced Topics: Inventory Consistency, Search, Order Orchestration, Idempotency Asked at: Amazon, Flipkart, Walmart, Meesho · Prerequisites: Idempotency and Transactions and Isolation
1. Understanding the Problem
An e-commerce platform lets people find a product among a hundred million of them, put it in a cart, pay, and receive it. The browsing half is a read-scaling problem and it is largely solved by caching. The buying half is where the design earns its difficulty rating, because it has to be correct about two things that cannot be approximated: how many units exist, and how much money changed hands.
The one sentence to say out loud early: you can sell a unit you do not have exactly zero times. Everything structural in this design follows from that. Overselling one PlayStation during a flash sale is not a rounding error you reconcile later, it is a cancelled order, a refund, a support ticket and a customer who buys from someone else next time.
This page is the end-to-end platform. Two neighbouring designs go deeper on their own slice and are not repeated here: Cart Management covers cart persistence, guest-cart merging and cross-device sync, and Payment System covers the payment rails, PSP integration and settlement. Read them alongside this.
Real examples: Amazon, Flipkart, Walmart, Meesho, Shopify, Zalando.
2. Naive First Cut
flowchart LR
USER["Shopper"]
APP["Monolith<br/>catalog cart orders"]
DB[("Postgres<br/>products stock orders")]
USER -->|"1. Browse and search"| APP
APP -->|"2. SELECT and UPDATE"| DB
USER -->|"3. Place order"| APP
One application, one database. Products in a table, a stock integer on each row, orders in another table. Search is WHERE title LIKE '%query%'. Checkout reads the stock, checks it in application code, and writes the decremented value.
Why this breaks:
- The stock check is a race. Two buyers hit checkout at the same moment on a product with one unit left. Both read
stock = 1, both decide it is fine, both writestock = 0. Two orders, one unit. This is the lost-update problem and it will happen on your most popular item, under load, in public.
💡 Lost update = two requests read the same value, each works out a new value from it, and the second write quietly erases the first. Nothing errors, which is why this bug reaches production. Learn more → LIKE '%query%'is not search. No relevance ranking, no typo tolerance, no faceting by brand or price, and the leading wildcard means no index can be used - it is a sequential scan of 100M rows per query, at 50K queries/sec.- One schema cannot hold the catalog. A book has an ISBN and a page count; a t-shirt has a size and a colour; a fridge has a capacity in litres. Modelling that relationally gives you either a table with 400 mostly-null columns or an attribute-value table that every query has to pivot.
- Checkout has no retry story. The user double-clicks Place Order, or their phone drops the connection and the app retries. Two orders, two charges.
- Flash sales concentrate everything on one row. Ten thousand concurrent buyers for one SKU (stock keeping unit) all queue on the same row lock.
💡 A SKU is one specific purchasable variant - “this shoe, size 9, black” - as opposed to the product, which is “this shoe”. Stock is counted per SKU because that is the thing a warehouse can actually hand over. Throughput collapses to the speed of one lock handoff, and every other order on the site waits behind it.
The rest of this doc evolves that into a platform that cannot oversell, searches properly, and survives a partial failure mid-checkout.
3. Prior Art We’re Drawing From
- Amazon’s service-oriented split and the Dynamo paper - the 2007 Dynamo paper is the origin of a lot of the industry’s thinking on partitioned, highly available stores, and the broader Amazon lesson is harder-won: separate services by the consistency they require rather than by org chart. Inventory and orders need strong consistency, the catalog does not. (Dynamo: Amazon’s Highly Available Key-value Store, SOSP 2007)
- Shopify’s flash-sale engineering - published extensively on surviving a single-product traffic spike that is orders of magnitude above baseline, including queueing at the edge and sharding hot inventory counters. Contributes the framing that a flash sale is a load-shedding problem, not just a locking problem. (Shopify Engineering)
- Temporal and its Cadence lineage from Uber - durable execution for multi-step business processes. Contributes the idea that an order is a long-running workflow with timeouts and compensations, and that the workflow’s state should survive the process that is running it. (Temporal)
- Debezium - log-based change data capture, which is how the search index and the warehouse learn about catalog changes without the application dual-writing. (Debezium)
- Elasticsearch - the inverted index, analysis pipeline and relevance scoring that make product search behave the way shoppers expect. (Elasticsearch)
4. Functional Requirements
Core (Top 3)
- Browse and search the catalog - find a product among 100M by text query, filter by brand, price and category
- Place an order without overselling - a unit is sold at most once, ever, under any amount of concurrency
- Track order state - the buyer sees where their order is from placement through to delivery
Discussion
Newest first