Start from questions, not tables
Most beginners open a design by listing nouns: users, orders, products. That is a fine start, but a data model is judged by the questions it can answer quickly, not by how tidy the nouns look. So before drawing any table, write down the access patterns.
For Snapcart, our running example store, the first list looks like this:
| # | Access pattern | Frequency | Latency need |
|---|---|---|---|
| A1 | Show a product page by product_id |
Very high (50k/s) | < 50 ms |
| A2 | List products in a category, sorted by popularity | High | < 150 ms |
| A3 | Place an order (items, address, payment) | Medium (500/s peak) | < 500 ms, must be correct |
| A4 | Show "my orders", newest first | Medium | < 200 ms |
| A5 | Warehouse: all unshipped orders for a warehouse | Low | Seconds are fine |
| A6 | Finance: revenue per day per category | Very low | Minutes are fine, batch |
Three things fall out of this table immediately:
- A1 and A2 dominate traffic, so the product model must be cheap to read and easy to cache.
- A3 must be correct above all else: money and stock move, so it belongs in a store with transactions.
- A6 is analytics, which should not run on the same database as A3. It belongs in a warehouse fed by change events (Stream Processing and Change Data Capture (CDC) Explained).
Designing a schema without access patterns is like building a kitchen without knowing what you'll cook. You can buy the nicest cupboards in the shop and still find the frying pan is three rooms away from the stove.
Entities, relationships and cardinality
Once you know the questions, sketch the entities and how many of each relate to the other. The three shapes you'll meet constantly:
| Relationship | Example | Typical relational model | Typical NoSQL model |
|---|---|---|---|
| One-to-one | User ↔ profile settings | Same table, or a side table with the same PK | Same document |
| One-to-many | Customer → orders | Foreign key on the "many" side (orders.customer_id) |
Partition key = customer, sort key = order time |
| Many-to-many | Products ↔ categories, users ↔ groups | Join table (product_categories) |
Two lookup tables, one per direction |
Cardinality numbers matter as much as arrows. "A customer has many orders" could mean 5 or 50,000. A celebrity user with 80 million followers breaks models that work perfectly for a user with 300. Always ask, "what is the biggest possible many?"
Normalisation: one fact, one place
A normalised schema stores each fact exactly once. A customer's email lives only in customers; orders reference the customer by ID. The benefits are real:
- No update anomalies. Change the email in one row and every view is correct.
- Smaller writes. You update a single row, not hundreds of copies.
- Constraints work. Foreign keys, unique indexes and check constraints guard correctness for free.
The cost is joins at read time. Showing an order page means joining orders, items, products and addresses. For a transactional system at moderate scale, this is exactly the right trade.
A practical rule: normalise the source of truth, denormalise the read models. Keep the transactional database clean and boring; build fast, pre-joined copies (caches, search indexes, materialised views) on top, fed by events.
Denormalisation: copies on purpose
Denormalising means storing the same fact in more than one place so a read needs fewer lookups. Done deliberately, it's one of the most powerful tools you have.
| Technique | Example | Price you pay |
|---|---|---|
| Embed a snapshot | Store price_at_purchase on each order item |
None: this is actually more correct, since prices change |
| Copy a display field | Store author_name on each comment |
Renames must fan out to every copy |
| Pre-computed counts | posts.like_count |
Must update on every like; hot rows |
| Materialised view | category_top_products rebuilt every minute |
Staleness; rebuild cost |
| Read model per screen | A "my orders" document per customer | A pipeline to keep it in sync |
Denormalising "because joins are slow" without measuring. A well-indexed join on a few thousand rows takes a millisecond or two. Denormalise when profiling shows the join is the bottleneck, or when the data lives in a store that can't join at all.
The question to ask about every copy is: what happens when the original changes? If the answer is "nothing, it's a snapshot" (like a price on an invoice), you're done. If the copy must follow the original, you need a way to propagate the change: a transaction, an event, or CDC.
Keys: the most important decision in the model
Primary keys
| Key style | Example | Good | Watch out |
|---|---|---|---|
| Auto-increment integer | 42, 43 |
Small, fast, ordered | Leaks volume; one generator; hard to merge shards |
| UUIDv4 | f47ac10b-… |
Generated anywhere, no coordination | Random inserts fragment B-trees; 16 bytes |
| Time-ordered (UUIDv7, ULID, Snowflake) | 01HF3K… |
Generated anywhere and roughly sorted by time | Reveals creation time |
| Natural key | email, ISBN | Meaningful | Natural keys change (people change email) |
The modern default is a time-ordered ID (Unique ID Generation in Distributed Systems). It gives you the coordination-free generation of a UUID with the insert-friendliness and natural sort order of an auto-increment.
Partition and sort keys
In distributed stores (DynamoDB, Cassandra, Bigtable), you choose:
- a partition key, which decides which machine holds the row, and
- a sort key, which decides the order of rows within that partition.
Together they define what you can query efficiently: "give me everything for partition X, in sort-key order, between A and B". Anything else is a full scan.