Designing for access patterns

Topic 3 of 10 in Week 3: Databases10 min readIntermediateFree course

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

Designing a table from a query. The query is: get the latest 50 messages in chat 42, newest first. The table messages_by_chat uses chat_id as the partition key and sent_at plus msg_id as clustering columns, ordered by sent_at descending. All messages for chat 42 live together in one partition, stored sorted by time: 10:42:07 see you soon, 10:41:55 on my way, 10:40:12 where are you, 10:38:01 hi. One sequential read returns the page. The partition key decides where data lives, the clustering key decides the order inside it, and one table per query is normal.

  • 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.

Exercise

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.

Text
Query 1 table:
  partition key:
  sort key:

Query 2 table:

Query 3 table:

Hot-partition risks:

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.