Where should an online store keep its orders, carts and stock counts?
Applications like this: a shop, a marketplace checkout, a food-ordering app. This page is written the way I write a database design doc: the workload first, then where each kind of data goes and why, then how it grows, what breaks, and what I rejected.
| Dimension | This application |
|---|---|
| Writes | Bursty. Quiet most of the day, then 10 to 50 times normal during a sale. |
| Reads | Mostly by key: an order, a customer's orders, a product's stock. |
| Read to write | Around 10 to 1 on the order tables, much higher on the catalog. |
| Consistency | Strict for stock and orders. Two customers must not buy the last item. |
| Latency | Checkout under a few hundred milliseconds end to end. |
| Growth | Steady. Orders are rarely deleted, so the tables only grow. |
These incidents are why the choices below matter. Each one is written up with its numbers and sources.
Solid arrows: the application writes there. Dashed arrows: a copy fed from the source of truth (change data capture, an outbox, or a cache fill), which can be rebuilt from it.
| Data | Where it goes | Why |
|---|---|---|
| Orders, order lines, customers, stock | PostgreSQL or MySQL | Transactions across several tables, foreign keys, and a check that stock never goes below zero. This is what a relational database is built for. |
| Carts and sessions | Redis or Valkey, or the same database | A cart is read and rewritten on every click and can be rebuilt if lost. Keeping it out of the order tables keeps them small and hot. |
| Product search | Elasticsearch or OpenSearch | Full-text search with filters. The database stays the source of truth; the index is a copy. |
| Sales reports | A column store, fed by change data capture | Reports scan millions of order lines. Running them on the primary competes with checkout. |
How each family of store behaves under traffic: Database families.
CREATE TABLE orders ( id bigint PRIMARY KEY, customer_id bigint NOT NULL REFERENCES customers(id), status text NOT NULL, -- placed, paid, shipped, refunded created_at timestamptz NOT NULL DEFAULT now(), total_cents bigint NOT NULL ); CREATE INDEX ON orders (customer_id, created_at DESC); CREATE TABLE stock ( sku text PRIMARY KEY, available int NOT NULL CHECK (available >= 0) ); -- reserve stock inside the checkout transaction UPDATE stock SET available = available - 1 WHERE sku = $1 AND available >= 1; -- 0 rows updated = sold out
Step through checkout, then the race for the last item. Press Play, or use the arrows.
One product with one unit in stock. The checkout takes stock with a conditional UPDATE, so it never has to read first and hope nothing changed.
UPDATE products SET stock = stock - 1 WHERE id = 1 AND stock > 0 RETURNING id, stock;
UPDATE products SET stock = stock - 1 WHERE id = 1 AND stock > 0 RETURNING id, stock;
UPDATE products SET stock = stock - 1 WHERE id = 1;
Pick a stage, then throw something at it. The numbers are a model to show the shape of each change, not a measurement; the measured numbers are in the lab notebooks.
Units sold of a 1,000-unit stock, 1,600 attempts
Sixteen clients at once. Measured in Notebook 6.
| What happens | What you see | What I do |
|---|---|---|
| The hot SKU during a sale | Lock waits on one stock row, connection pool exhausted | Keep the stock transaction short, and reserve stock before payment, with a timeout to release it. See the lock pile-up lab. |
| Reports on the primary | CPU spikes at the top of the hour, checkout latency follows | Move reports to a replica or a column store. |
| OFFSET paging in the admin order list | Page 1 is fast, page 500 times out | Page by key. See paging. |
Cost. Most of the bill is the primary and its standby. A read replica that nobody reads, and reports running on the primary, are the usual waste. See the cost page.
| Stage | Where it runs |
|---|---|
| Where I would start | Managed relational with a standby in another zone |
| When it grows | Cloud-native managed (Aurora, AlloyDB) for read replicas, or self-managed on VMs if the steady bill justifies a team |
For a team without a dedicated database team. Who does which job in each option, and the full-cost comparison: Where to run the database.
The back-of-the-envelope numbers I would want before choosing anything. Move the assumptions to match your application. The link keeps your numbers, so a colleague sees the same ones.
Which database does my application need? What kind of application data goes in which database, and why, as design docs, plus the anti-patterns and the decisions behind them.