Designing for access patterns
Overview
In relational databases you can design tables around entities and write almost any query later; indexes make the common ones fast. Large-scale NoSQL stores such as Cassandra, DynamoDB and Bigtable work the other way round: you design tables around the queries, because the store can only answer efficiently the questions its keys were built for. This lesson shows how.
Start from the queries
Write the access patterns down before designing anything:
| # | Query | Key | Order | Frequency |
|---|---|---|---|---|
| 1 | Latest messages in a chat | chat_id | newest first | very high |
| 2 | A user’s chat list | user_id | last activity | high |
| 3 | One message by ID | msg_id | — | low |
Every column of this table becomes a design decision.
Partition keys and sort keys
- The partition key is hashed to choose which node stores the data. All rows with the same partition key live together.
- The sort key (clustering key in Cassandra, range key in DynamoDB) orders rows inside a partition.
A query that gives the partition key and a range on the sort key is cheap: go to one node, read a contiguous slice. A query that doesn’t include the partition key has to scan every node, which these stores either forbid or make very expensive.
Good partition keys
A partition key should:
- Match the query: you must know it when you ask.
- Spread load: many distinct values, accessed roughly evenly (user ID, chat ID, device ID).
- Keep partitions bounded: a partition that grows forever (all events for one sensor since 2019) gets slow and hard to move.
Common fixes:
- Time bucketing: put the period in the key, such as
(sensor_id, day), so each partition holds one day. - Write sharding: add a bucket suffix
(store_id, day, 0..9)to split a hot key across ten partitions, and read all ten when querying.
One table per query
If the same data must be read by different keys, store it more than once:
messages_by_chat(partition: chat_id) for reading a conversation,chats_by_user(partition: user_id, sort: last_activity) for the inbox list.
This is deliberate denormalization. Writes update several tables, either in a batch or through an event-driven process, and you accept that the copies may briefly disagree. DynamoDB’s secondary indexes automate some of this, with eventual consistency for global indexes.
Hot partitions
Even with good average distribution, one key can be far busier than the rest: a celebrity’s profile, a viral post, a huge store on a sale day. That partition’s node becomes the bottleneck while others sit idle. Defences:
- cache hot reads in front of the store,
- split hot keys with buckets,
- for counters, keep several sub-counters and add them up on read,
- move very hot entities to special handling (the news feed case study treats celebrities differently).
What about relational databases?
Thinking in access patterns helps there too:
- Create indexes that match queries: an index on
(chat_id, sent_at DESC)serves query 1 directly. - Use covering indexes that include all needed columns, so the table itself isn’t read.
- Avoid queries that can’t use an index on large tables.
The difference is flexibility: a relational database can still run an unplanned query, just slowly; a partitioned NoSQL store may not run it at all.
In an interview
After the data model, write the two or three main queries next to it and show the key for each: “Messages are partitioned by chat_id and clustered by sent_at descending, so loading a chat is a single partition read.” Then mention the hot-key risk and how you’d handle it.
Recap
Design partitioned stores from the queries backwards. The partition key decides where data lives and must be known at query time, spread load and keep partitions bounded; the sort key decides order within a partition. Store data once per query shape when needed, watch for hot partitions, and give relational tables indexes that match the same access patterns.
Design keys for an order history
An e-commerce app stores orders in a wide-column or key-value store (such as Cassandra or DynamoDB). It must support: (1) a customer's orders, newest first, 20 per page; (2) one order by its ID; (3) all orders placed at one store on a given day, for the store manager. Design the table(s), naming the partition key and sort key for each, and point out any hot-partition risk.
Query 1 table:
partition key:
sort key:
Query 2 table:
Query 3 table:
Hot-partition risks:Query 1: orders_by_customer
partition key: customer_id
sort key: created_at DESC (plus order_id to make it unique)
-> one partition read returns the newest 20; page with the last (created_at, order_id).
Query 2: orders_by_id
partition key: order_id (no sort key)
-> a direct key lookup. (In DynamoDB this could be the base table, with
query 1 as a secondary index.)
Query 3: orders_by_store_day
partition key: (store_id, order_date)
sort key: created_at, order_id
-> the date in the partition key keeps partitions bounded in size.
Hot-partition risks:
- A huge store on a sale day writes all orders into one partition. If that's too much,
add a bucket number: (store_id, order_date, bucket 0-9) and read all 10 buckets.
- A customer partition is naturally small, so query 1 is safe.
- Writes go to 3 tables, so write them together (batch/transaction where supported)
or from an event, and accept they may be briefly out of step.Summary
- List the queries first: what is looked up, by which key, in what order, and how often.
- In key-value and wide-column stores, the partition key decides where data lives and the sort key decides its order within a partition.
- A good partition key spreads load evenly and keeps partitions bounded in size.
- It is normal to store the same data in several tables, one per query, and keep them in sync.
- Relational databases are more flexible with ad-hoc queries, but still need indexes that match the access patterns.