DynamoDB Key Design and Capacity

Topics Covered

Modelling for Access Patterns

Tables, items and keys

The two read operations that scale

Start from access patterns

Single table design

When to use several tables

Denormalisation and its cost

When DynamoDB is the wrong choice

DynamoDB is a key value and document database that serves single digit millisecond reads and writes at almost any scale, with no servers to manage. It gets that predictability by refusing to do what relational databases do: there are no joins, no ad hoc queries across arbitrary attributes, and every efficient read goes through a key the team designed in advance. Teams that model DynamoDB like a relational database end up with scans, hot partitions and bills that grow faster than traffic. Teams that model it from their access patterns get a system that behaves the same at ten requests a second and at ten million.

Tables, items and keys

A table holds items, each a set of attributes up to 400 kilobytes. Every item has a primary key, which is either a partition key alone or a partition key plus a sort key. The partition key is hashed to decide which physical partition stores the item. The sort key orders items that share a partition key, and items with the same partition key form an item collection that can be read together in one query, in sort key order, with conditions such as begins with, between, greater than or less than. Every other attribute is schemaless: items in one table can have entirely different attributes.

Item size limits shape designs early. The 400 kilobyte cap includes attribute names and values, so documents that can grow without bound, such as a list of every comment on a post, do not belong in a single item. The usual answer is to store each element as its own item in the same item collection, with the parent ID as partition key and a sort key per element, so the collection grows freely and can be read in pages. Large binary data, such as images or exported reports, goes to S3, with the item storing the object key and a few attributes describing it.

The two read operations that scale

DynamoDB offers GetItem, which fetches one item by its full primary key, and Query, which reads items from one partition key, optionally narrowed by a sort key condition. Both touch only the data they return, so their cost and latency do not grow with table size. Scan reads the whole table, or a segment of it, and filters afterwards, so its cost grows with the table and it is almost never right for a request path. Filter expressions on queries and scans apply after items are read, which means they reduce the data returned but not the capacity consumed. The core of DynamoDB design is making every request path a GetItem or a Query.

BatchGetItem and BatchWriteItem group up to 100 reads or 25 writes into one call, saving round trips, but each item inside still costs what a single request would, and a batch can partially fail, returning unprocessed items that the caller must retry. Pagination applies to every query: a single response returns at most one megabyte of data read, with a LastEvaluatedKey to continue from, so code that reads an item collection must loop until no key comes back rather than assuming one call returns everything.

Start from access patterns

Relational design starts with entities and normalises them, then writes queries. DynamoDB design starts with a list of every access pattern the application needs, such as get a user by ID, list a user's orders newest first, get an order with its line items, or find open orders for a warehouse, with expected rates and sizes. The keys and indexes are then chosen so that each pattern is one request. This inverts the usual order, and it means a new access pattern discovered later may need a new index or a data migration, which is the main cost of the approach.

Every access pattern on the list mapped to one GetItem or Query. The one that needed a Scan was redesigned before launch, not after.

A practical way to run this exercise is a table with one row per access pattern and columns for the operation, the key or index it uses, the key values, the expected rate at peak, and the item sizes involved. Filling it in exposes gaps quickly: a pattern with no key that serves it, a rate high enough to make a key hot, or an item size that will make writes expensive. The same table becomes the documentation that later engineers need to understand why the keys look the way they do, and the checklist for any change.

Single table design

Because there are no joins, related data that is read together is stored together. Single table design puts several entity types, such as customers, orders and line items, in one table with generic key attribute names like PK and SK. A customer item might have PK = CUSTOMER#42 and SK = PROFILE, and their orders PK = CUSTOMER#42 and SK = ORDER#2026-11-03#981. One query on CUSTOMER#42 returns the profile and all orders, sorted by date, in a single round trip. The pattern is powerful for high throughput applications with stable access patterns, but makes the table hard to read without documentation, and harder to evolve.

When to use several tables

Single table design is a tool, not a rule. Entities with different access patterns, lifecycles, capacity needs, backup requirements or owners often belong in separate tables, and many teams find several focused tables easier to operate, especially when using DynamoDB with infrastructure as code and per table permissions. A sensible default is to store together what is read together, and separate what has nothing in common. The argument for a single table matters most where one request must fetch heterogeneous items, which is exactly when a join would be needed in a relational database.

Sort key design deserves as much thought as the partition key. Hierarchical sort keys such as COUNTRY#CA#CITY#Toronto#STORE#12 let one partition answer queries at several levels with begins with conditions, so one query lists every store in a country and another every store in a city. Timestamps in ISO 8601 form sort correctly as strings and support range queries for time windows. Prefixes that distinguish entity types, such as ORDER# and INVOICE#, let one query select a single type from a mixed collection. Zero padded numbers, 0000042 rather than 42, sort correctly where string comparison would put 100 before 42.

Denormalisation and its cost

DynamoDB designs often copy data, such as a customer's name onto each order item, so a query can return everything it needs without a second request. Copies must be kept consistent when the original changes, either in the same transaction, through DynamoDB Streams updating copies asynchronously, or by accepting that some copies show values as of when they were written, which for an order's shipping address is usually correct anyway. Deciding which copies must be updated and which are historical records is part of the design.

Schema changes in DynamoDB are application changes rather than migrations. Adding an attribute is free, since items are schemaless, but changing what a key means, or adding a new entity layout, requires writing the new form for new items and backfilling old ones, usually with a script that scans the table once in parallel segments, at a controlled rate, and rewrites each item. Code must read both forms during the transition. Planning for this, rather than hoping the first model is perfect, is the honest way to design, and is why teams keep a version attribute on items whose layout may change.

When DynamoDB is the wrong choice

Workloads that need ad hoc queries, reporting across many attributes, complex transactions across many entities, or frequently changing access patterns fit a relational database better. Analytics over DynamoDB data belongs in a warehouse or lake, fed by exports to S3 or zero ETL integration, not in scans. DynamoDB is at its best for high volume, predictable access, such as sessions, carts, user profiles, device state, leaderboards and event metadata, where its fixed latency and lack of servers to manage outweigh its modelling constraints.

The real skill is noticing early when DynamoDB is being asked to be something else. A design that needs five GSIs, each projecting all attributes, to answer questions that keep changing month to month is a relational workload wearing a key value costume. A design whose table holds a few hot items read millions of times a second needs a cache more than better keys. Recognising those shapes in the access pattern table, before the first item is written, saves the months of rework that a wrong database choice otherwise costs.

Design keys from the questions

DynamoDB serves GetItem and Query at constant cost regardless of table size, and everything else scans. List every access pattern with its rate first, then choose partition keys, sort keys and indexes so each pattern is one request, storing together what is read together.