Database schemas
E-commerce orders
Orders, line items and the price that must not change after checkout.
What this board gets wrong on purpose
The tension
inventory is a single quantity_on_hand with no reservation, so two concurrent checkouts both pass the stock check and oversell the last unit
Start here
Why this design
An order schema is the most common database-design question there is, and it is mostly a test of whether you can tell history from state. A price, a product name and an address are facts about today; an order is a record of what was agreed on one particular day, and the schema has to keep those apart or last quarter’s revenue changes every time marketing runs a sale. Interviewers use it to see whether you reach for constraints and transactions before you reach for indexes; real teams meet it as a refund that pays back a number the customer never paid, or as the last unit of a flash-sale item sold twice.
What you will learn
- Why order_lines copies unit_price_minor, sku_at_purchase and name_at_purchase at checkout instead of joining to product_variants for them, and why that makes the variant FK ON DELETE SET NULL.
- Why money is a bigint of minor units with a currency beside it, never a float, and why JPY and KWD break the "divide by 100" assumption.
- Why a cart is its own table rather than an order with status = 'cart': the NOT NULLs an order needs are exactly the columns a guest cart cannot fill.
- How each of the eleven foreign keys picks CASCADE, RESTRICT or SET NULL, and what policy each choice encodes.
- Why a single quantity_on_hand oversells under concurrency, the one-statement conditional decrement that stops stock going negative, and the reservation model that the board deliberately leaves out.
- How to change this schema on a live database: expand/contract, NOT VALID constraints, batched backfills, and partitioning orders by month when the composite key it forces ripples into every child table.
How to read this board
- Schema, top to bottom: CATALOGUE (products, product_variants, inventory), then CUSTOMER (customers, addresses), then CART (carts, cart_items).
- Then the right-hand region, ORDER AND MONEY: orders, order_lines and payments - the permanent half. Press play for the eight-step walkthrough; it lights each table and the FK that joins it.
- Page 1 · Requirements - the guarantees the schema must give, written as sentences a constraint can enforce.
- Pages 2 and 3 · Capacity and Storage - orders per second, the hot inventory row, bytes per row and index size per year.
- Page 4 · Availability and integrity - constraints, the checkout transaction, isolation for the stock decrement, and what contention does to it.
- Page 5 · How it works - every table walked from catalogue to payment, and why each FK has the ON DELETE it has.
- Page 6 · Deployment and migrations - online schema change, backfills, partitioning and archiving. Then open Generate SQL and compare the DDL with page 5.
1 · Requirements
Ask these before drawing anything
- Can a guest check out? Yes here - but orders.customer_id is NOT NULL, so checkout creates (or claims, by email) a customers row. A guest is a customer with no password, not a missing foreign key.
- Is the price the customer saw the price they pay? The cart reprices on every view; the order freezes. Say where the freeze happens: inside the checkout transaction, and nowhere else.
- One currency per order, or mixed? One. orders.currency is NOT NULL and every line and payment must match it.
- Can stock go negative (backorders)? Not on this board. If the business sells on backorder, that is a flag on the variant and a different constraint, decided up front.
- Do we reserve stock while a payment is being authorised? This board does not - that is its stated hole. Decide whether the business can live with an occasional oversell and an apology email.
- How long must orders be kept, and can a customer be erased? Tax law says years (six to ten depending on country); privacy law says erase on request. The answer is redaction, which is why there is no CASCADE from customers to orders.
What the schema must guarantee
- Price frozen at checkout: an order line’s unit_price_minor, sku and name never change after the order is placed, whatever happens to the catalogue.
- Totals reproducible: orders.total_minor = subtotal_minor + shipping_minor + tax_minor, and subtotal_minor = the sum of its line_total_minor, checked rather than trusted.
- Stock never negative: quantity_on_hand >= 0 is a CHECK, and the decrement is a single conditional UPDATE so two checkouts cannot both take the last unit.
- Cart is not order: a cart may be anonymous, unpriced and expiring; an order has a customer, an address, a currency and stored totals, all NOT NULL.
- Money is exact: integers of minor units with an ISO 4217 currency on every amount.
- Payments are idempotent and append-only: provider_ref is UNIQUE, so a replayed webhook cannot double-credit; refunds are new negative rows, never edits.
- History survives catalogue edits: delisting or deleting a variant never deletes a paid line; editing an address never moves an old parcel.
Non-functional
- Checkout (cart to placed order, excluding the payment provider’s call): p99 under 150 ms in the database.
- Product and cart reads: p95 under 20 ms, served largely from replicas and cache.
- Zero lost orders or payments on a failover (RPO 0 for the order half); checkout back within 2 minutes (RTO).
- Every schema change applied online - no table lock longer than a couple of seconds on orders, order_lines, payments or inventory.
Out of scope
- Promotions and discount codes (they land in line_total_minor, but the rules engine is another board).
- Multi-warehouse stock, shipments and returns as their own entities.
- Tax calculation itself - tax_minor stores its result.
The rest of this board
Available on Tier Slate
This page publishes the question. The answer — 5 more written pages, an 8-step narrated walkthrough and a 10-table schema — is the board itself, and it opens in Tierslate.
- 5written pages
- 8walkthrough steps
- 10tables64 columns
5 pages behind this one
- Capacity estimation
- Storage estimation
- Availability and integrity
- How it works
- Deployment and migrations