0Pricing
AWS Solutions Architect · Lesson

Tables, Items, and Primary Keys

Design DynamoDB tables with partition keys and composite primary keys, and understand item-level storage limits.

Tables, Items, and Primary Keys is a free AWS Solutions Architect lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the AWS Solutions Architect learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

DynamoDB: NoSQL Key-Value Store

Amazon DynamoDB is a fully managed, serverless key-value and document database designed for single-digit millisecond performance at any scale. Unlike relational databases, DynamoDB is schema-less—each item can have a different set of attributes as long as the primary key is present.

DynamoDB stores data in tables, which are the top-level container analogous to a SQL table. Tables are spread across multiple storage nodes across AZs automatically, providing built-in redundancy without any configuration required from you.

Tables and Items

A DynamoDB table holds a collection of items, each of which is a collection of attributes. Attributes are typed values: String (S), Number (N), Binary (B), Boolean (BOOL), Null (NULL), List (L), Map (M), and Set types (SS, NS, BS).

Every item in a table must include the primary key attributes—all other attributes are optional and can differ between items. A single item can be at most 400 KB in size, including all its attribute names and values.

# Example DynamoDB item structure (JSON)
{
  'UserId': {'S': 'user-abc-123'},
  'Timestamp': {'N': '1719000000'},
  'Username': {'S': 'alice'},
  'Score': {'N': '4200'},
  'Tags': {'SS': ['premium', 'verified']}
}

Simple Primary Key: Partition Key Only

A simple primary key consists of a single attribute called the partition key (also called the hash key). DynamoDB applies an internal hash function to the partition key value to determine which storage partition holds the item. All items with the same partition key value are stored together.

With a simple primary key, no two items in the table can have the same partition key value—it uniquely identifies each item. This design suits tables where you always access data by a unique identifier, such as a user ID or order ID.

# Create a table with a simple (partition key only) primary key
aws dynamodb create-table \
  --table-name Users \
  --attribute-definitions AttributeName=UserId,AttributeType=S \
  --key-schema AttributeName=UserId,KeyType=HASH \
  --billing-mode PAY_PER_REQUEST

Composite Primary Key: Partition + Sort Key

A composite primary key uses both a partition key and a sort key (also called the range key). Items with the same partition key are stored together and sorted by the sort key value, enabling range queries within a partition.

This design is extremely flexible: multiple items can share the same partition key as long as their sort keys differ. For example, a Orders table might use CustomerId as partition key and OrderDate as sort key, allowing you to query all orders for a customer sorted by date.

# Create a table with a composite primary key
aws dynamodb create-table \
  --table-name Orders \
  --attribute-definitions \
    AttributeName=CustomerId,AttributeType=S \
    AttributeName=OrderDate,AttributeType=S \
  --key-schema \
    AttributeName=CustomerId,KeyType=HASH \
    AttributeName=OrderDate,KeyType=RANGE \
  --billing-mode PAY_PER_REQUEST

Partition Key Design and Hot Partitions

Choosing the right partition key is the most important DynamoDB design decision. A good partition key has high cardinality (many distinct values) and distributes access evenly across partitions. Poor choices lead to hot partitions where one partition receives disproportionate traffic, causing throttling.

Anti-patterns to avoid: using a boolean flag (only two values), a date that groups all today's writes together, or a low-cardinality status field. Good choices: user ID, device ID, random UUID, or composite values like tenantId#entityType.

PutItem, GetItem, and DeleteItem

The three fundamental DynamoDB item operations are:

  • PutItem: writes a new item or completely replaces an existing one with the same primary key
  • GetItem: retrieves a single item by its exact primary key (requires the full key—partition key and, if composite, sort key)
  • DeleteItem: removes an item by its exact primary key

All three operations are atomic at the item level. By default, GetItem uses eventually consistent reads; adding --consistent-read forces a strongly consistent read that always returns the latest written value.

# PutItem
aws dynamodb put-item \
  --table-name Users \
  --item '{"UserId":{"S":"user-123"},"Name":{"S":"Alice"}}'

# GetItem
aws dynamodb get-item \
  --table-name Users \
  --key '{"UserId":{"S":"user-123"}}' \
  --consistent-read

UpdateItem and Conditional Expressions

UpdateItem modifies specific attributes of an existing item without replacing it entirely, unlike PutItem. You can add attributes, remove attributes, or perform arithmetic on Number attributes atomically (e.g., increment a counter).

Conditional expressions let you specify that an operation should only succeed if a condition is true. For example, only update an item's status if it is currently PENDING. This implements optimistic locking patterns without transactions and is a key DynamoDB design technique.

# Atomically increment a counter, only if item exists
aws dynamodb update-item \
  --table-name Orders \
  --key '{"CustomerId":{"S":"c-123"},"OrderDate":{"S":"2026-06-20"}}' \
  --update-expression 'SET ItemCount = ItemCount + :inc' \
  --condition-expression 'attribute_exists(CustomerId)' \
  --expression-attribute-values '{":inc":{"N":"1"}}'

Query vs Scan

Query retrieves items that share the same partition key value, optionally filtered by sort key conditions. Query is efficient—it reads only the targeted partition. You can use sort key conditions like begins_with, between, =, <, > to narrow results within the partition.

Scan reads every item in the table and then applies an optional filter expression. Scans are expensive for large tables and should be avoided in production query patterns. If you find yourself needing frequent Scans, reconsider your table design or add a Global Secondary Index.

# Query: get all orders for customer c-123 after a date
aws dynamodb query \
  --table-name Orders \
  --key-condition-expression 'CustomerId = :cid AND OrderDate >= :dt' \
  --expression-attribute-values \
    '{":cid":{"S":"c-123"},":dt":{"S":"2026-01-01"}}'

Strongly Consistent vs Eventually Consistent Reads

DynamoDB stores three copies of your data across multiple AZs. Eventually consistent reads (the default) may return a slightly stale value if a recent write has not propagated to all copies yet—but they consume half the read capacity units of strongly consistent reads.

Strongly consistent reads always return the latest committed write but cost twice the RCUs and are not available on Global Secondary Indexes. Choose eventually consistent reads for high-throughput read-heavy workloads and strongly consistent reads only when your application requires the absolute latest data.

DynamoDB Transactions

DynamoDB supports ACID transactions via TransactWriteItems and TransactGetItems. A transaction can group up to 100 write operations across multiple items and even multiple tables, ensuring all succeed or all are rolled back atomically.

Use transactions for scenarios like transferring money between accounts (debit one item, credit another) or booking a seat (check availability and reserve it atomically). Transactions cost twice the normal RCUs/WCUs, so use them only when atomicity across multiple items is genuinely required.

Item Size Limit and Data Modelling Tips

DynamoDB's 400 KB per-item limit influences data modelling. For large payloads (e.g., images, large documents), store the binary data in S3 and store only the S3 object key in DynamoDB. For deeply nested hierarchical data, model each node type with its own partition key pattern using single-table design—one table holds multiple entity types differentiated by the partition key prefix and sort key pattern.

Single-table design minimises the number of tables and enables efficient access patterns by co-locating related items in the same partition. It is an advanced technique that reduces operational overhead and improves performance for complex access patterns.

Quick Check

Test your understanding of AWS Solutions Architect (SAA-C03) concepts from this lesson.

Lesson Recap

In this lesson you learned: DynamoDB tables hold schema-less items limited to 400 KB, simple primary keys use a partition key alone while composite keys add a sort key for range queries, and high-cardinality partition keys prevent hot partitions. Use Query instead of Scan for efficient access. Next up we explore provisioned vs on-demand capacity modes.

Frequently asked questions

Is the “Tables, Items, and Primary Keys” lesson free?

Yes — the full text of “Tables, Items, and Primary Keys” is free to read here on the web, and the AWS Solutions Architect course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the AWS Solutions Architect course, upgrade to CoddyKit PRO.

What will I learn in “Tables, Items, and Primary Keys”?

Design DynamoDB tables with partition keys and composite primary keys, and understand item-level storage limits. You practise AWS Solutions Architect with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start AWS Solutions Architect?

No prior experience is required. AWS Solutions Architect on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Tables, Items, and Primary Keys” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this AWS Solutions Architect lesson?

Yes. Every AWS Solutions Architect lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Tables, Items, and Primary Keys
  2. Provisioned vs On-Demand Capacity
  3. Global Secondary Indexes and Local Secondary Indexes
  4. DynamoDB Streams and Global Tables
← Back to AWS Solutions Architect