Updated 24 October 2026
Data modelling for Cassandra and DynamoDB
How to design tables for wide-column and key-value stores: query-first modelling, partition keys and sort keys, one table per query versus single-table design, time-bucketed partitions, secondary indexes, avoiding hot and huge partitions, and consistency options.
Try it yourself, in a playground:LSM trees and compactionLocal vs global indexes
Cassandra, ScyllaDB, DynamoDB, Bigtable and HBase scale writes and storage almost without limit, which is why they appear in chat, feed, metrics and URL shortener designs. The price is that you cannot query them freely: there are no joins and limited filtering, and every efficient query must name a partition. Modelling for them works backwards from the queries. Getting the partition key right is the difference between a design that scales and one that falls over on its first hot key.
The model
- A table's primary key has two parts:
- the partition key, hashed to choose which nodes store the row; - the clustering or sort key, which orders rows inside the partition.
- Efficient reads: get one row by full key, or range-scan one partition by sort key ("the latest 50 messages in conversation 42").
- Inefficient or impossible: queries that do not specify the partition key, joins, and arbitrary filters.
In DynamoDB terms: partition key and sort key. In Cassandra: PRIMARY KEY ((partition columns), clustering columns).
Query-first design
- List the access patterns with their frequencies: "get messages for a conversation, newest first", "get conversations for a user ordered by last activity".
- For each, design a table (or an item type) whose partition key is what the query looks up by and whose sort key is the order it needs.
- Denormalise: store the same data in several tables shaped for different queries, written together.
Example for chat:
| Table | Partition key | Sort key | Serves |
|---|---|---|---|
| messages | conversation_id | message_id (time-ordered) | messages in a conversation, paged |
| user_conversations | user_id | last_activity, conversation_id | a user's inbox |
| conversations | conversation_id | (none) | conversation metadata |
See Design WhatsApp and data modelling and denormalisation.
Partition sizing and time buckets
Partitions must stay bounded. A conversation with millions of messages, or a sensor writing every second forever, creates a partition that grows without limit: slow to read, hard to repair, and stuck on a few nodes. Fix with bucketing: add a time bucket to the partition key.
- Metrics: partition by
(metric_id, day), sort by timestamp. - Busy chats: partition by
(conversation_id, month).
Queries then read the current bucket and walk back to older ones as needed. Aim for partitions in the tens to low hundreds of megabytes at most. See time-series data and Design a Monitoring System.
Hot partitions
Each partition is served by a limited set of nodes with limited throughput (DynamoDB enforces per-partition limits). A celebrity's timeline, a global counter or a date-only partition key concentrates traffic. Use high-cardinality keys, add a random suffix for write-heavy keys, and cache hot reads. See hot keys and skew.
Secondary indexes
- DynamoDB global secondary indexes are asynchronously maintained copies with a different key; great for alternative access patterns, eventually consistent.
- Cassandra secondary indexes are local to each node and query every node unless combined with the partition key; usually prefer a separate denormalised table.
- Materialised views exist but have operational caveats; many teams maintain their own tables.
Single-table design (DynamoDB)
A DynamoDB style where many entity types share one table with generic keys (PK = USER#42, SK = ORDER#2026-10-05#981), so related items live in one partition and one query fetches a user and their recent orders. It minimises requests and suits stable, well-known access patterns, at the cost of readability and flexibility. Multiple tables are fine when patterns are simpler or still changing.
Writes, updates and deletes
- Writes are upserts and very fast (LSM storage). See storage engines.
- Deletes create tombstones; many deletes in one partition (queues modelled as tables) slow reads badly. Avoid queue-like patterns; use TTLs for expiry.
- Lightweight transactions (Cassandra) and conditional writes or transactions (DynamoDB) exist for compare-and-set, at higher cost.
Consistency
- Cassandra offers tunable consistency per query:
QUORUMreads and writes (with replication factor 3) give read-your-writes;ONEis faster and weaker. UseLOCAL_QUORUMin multi-region setups. See replication and consistency. - DynamoDB reads are eventually consistent by default, with strongly consistent reads available on the base table.
When not to use them
Ad-hoc queries, complex relationships, multi-row transactions across entities and frequently changing access patterns fit a relational database better. Many systems combine both: relational for core entities and money, wide-column for high-volume append-heavy data. See SQL vs NoSQL.
Checklist
- Access patterns listed first; a table or item type per pattern.
- Partition key for lookup, sort key for order.
- Time buckets to bound partition size.
- High-cardinality keys; hot keys mitigated.
- Denormalised tables over secondary indexes for important queries.
- No queue-like delete patterns; TTLs for expiry.
- Consistency level chosen per query.
Questions people ask
What is query-first data modelling for Cassandra and DynamoDB?
Query-first data modelling means designing tables backwards from the queries, because wide-column and key-value stores have no joins, limited filtering, and every efficient query must name a partition. List the access patterns with their frequencies, then give each one a table or item type whose partition key is what the query looks up by and whose sort key is the order it needs. The same data is denormalised into several tables shaped for different queries and written together.
Partition key vs sort key: what is the difference?
The partition key is hashed to choose which nodes store a row, while the sort key (the clustering key in Cassandra) orders rows inside that partition. Together they form the primary key. Efficient reads either get one row by its full key or range-scan one partition by sort key, such as "the latest 50 messages in conversation 42" with conversation_id as the partition key and a time-ordered message_id as the sort key.
How do you stop Cassandra partitions from growing too large?
Add a time bucket to the partition key so every partition stays bounded. A conversation with millions of messages, or a sensor writing every second forever, creates a partition that is slow to read, hard to repair and stuck on a few nodes. Partition metrics by (metric_id, day) or busy chats by (conversation_id, month), read the current bucket and walk back to older ones, and aim for partitions in the tens to low hundreds of megabytes at most.
What causes hot partitions and how do you fix them?
A hot partition happens when a key concentrates traffic on one partition, which is served by a limited set of nodes with limited throughput (DynamoDB enforces per-partition limits). A celebrity's timeline, a global counter or a date-only partition key are typical causes. The fixes are high-cardinality partition keys, a random suffix on write-heavy keys, and caching hot reads.
Single-table design vs multiple tables in DynamoDB: which should you use?
Single-table design stores many entity types in one DynamoDB table with generic keys, such as PK = USER#42 and SK = ORDER#2026-10-05#981, so related items share a partition and one query fetches a user and their recent orders. It minimises requests and suits stable, well-known access patterns, at the cost of readability and flexibility. Multiple tables are fine when access patterns are simpler or still changing.
When should you use a relational database instead of Cassandra or DynamoDB?
A relational database fits better when you need ad-hoc queries, complex relationships, multi-row transactions across entities, or access patterns that change often. Wide-column stores such as Cassandra and DynamoDB have no joins and only query efficiently when the partition is named. Many systems combine both: relational for core entities and money, wide-column for high-volume, append-heavy data.