TExcelTAD
Home › AWS Cloud › Amazon DynamoDB

Amazon DynamoDB

Amazon DynamoDB is a fully managed, serverless key-value and document database. There are no servers or instances to size: you create a table, and it delivers single-digit-millisecond reads and writes at any scale. It is the database behind many high-traffic AWS applications and a natural partner for Lambda.

Tables, items and keys

  • A table holds items (like rows); an item is a set of attributes (like columns, but each item can have different ones).
  • Every item is identified by its primary key:
    • a partition key alone (e.g. user_id), or
    • a partition key + sort key (e.g. customer_id + order_date), which groups related items and lets you query ranges.
  • Secondary indexes (GSI, LSI) give you other ways to query.
bash
aws dynamodb create-table --table-name Orders \
    --attribute-definitions AttributeName=customer_id,AttributeType=S AttributeName=order_date,AttributeType=S \
    --key-schema AttributeName=customer_id,KeyType=HASH AttributeName=order_date,KeyType=RANGE \
    --billing-mode PAY_PER_REQUEST
aws dynamodb put-item --table-name Orders \
    --item '{"customer_id":{"S":"c-17"},"order_date":{"S":"2026-03-02"},"total":{"N":"59.90"}}'
aws dynamodb query --table-name Orders \
    --key-condition-expression "customer_id = :c AND begins_with(order_date, :m)" \
    --expression-attribute-values '{":c":{"S":"c-17"},":m":{"S":"2026-03"}}'

Sample output of the query:

text
{
    "Items": [
        {
            "customer_id": {"S": "c-17"},
            "order_date": {"S": "2026-03-02"},
            "total": {"N": "59.90"}
        }
    ],
    "Count": 1,
    "ScannedCount": 1
}

Model: how partition keys spread data

DynamoDB hashes the partition key to choose a partition (a storage node). A key with many distinct values spreads load evenly; a key with few values (like status) creates hot partitions. This model uses MD5 as the hash, like DynamoDB uses an internal hash function:

python · try it yourself
import hashlib
from collections import Counter

PARTITIONS = 4

def partition_for(key):
    digest = hashlib.md5(key.encode()).hexdigest()
    return int(digest, 16) % PARTITIONS

orders = [(f"c-{n}", "shipped" if n % 10 else "pending") for n in range(1, 401)]

by_customer = Counter(partition_for(customer) for customer, _ in orders)
by_status = Counter(partition_for(status) for _, status in orders)

print("partition key = customer_id:", dict(sorted(by_customer.items())))
print("partition key = status     :", dict(sorted(by_status.items())))
partition key = customer_id: {0: 90, 1: 96, 2: 121, 3: 93}
partition key = status     : {0: 40, 2: 360}

With customer_id the 400 items land roughly evenly on the four partitions. With status, every "shipped" order hashes to the same partition, which then takes 90% of the traffic. Choose a high-cardinality partition key that spreads both data and requests.

Capacity modes

Mode You pay for Good for
On-demand (PAY_PER_REQUEST) Each read and write request Unpredictable or new workloads
Provisioned Read and write capacity units per second (+ auto scaling) Steady, predictable traffic (cheaper at scale)

Capacity units:

  • 1 WCU = one write per second of an item up to 1 KB (larger items use more, rounded up).
  • 1 RCU = one strongly consistent read per second of up to 4 KB, or two eventually consistent reads. Transactional reads/writes cost double.
python · try it yourself
import math

def wcu(item_kb, writes_per_s, transactional=False):
    units = math.ceil(item_kb / 1) * writes_per_s
    return units * (2 if transactional else 1)

def rcu(item_kb, reads_per_s, consistency="eventual"):
    per_read = math.ceil(item_kb / 4)
    factor = {"eventual": 0.5, "strong": 1, "transactional": 2}[consistency]
    return math.ceil(per_read * reads_per_s * factor)

print("100 writes/s of 2.5 KB items          :", wcu(2.5, 100), "WCU")
print("100 transactional writes/s of 0.5 KB  :", wcu(0.5, 100, transactional=True), "WCU")
print("500 eventually consistent reads of 6 KB:", rcu(6, 500), "RCU")
print("500 strongly consistent reads of 6 KB :", rcu(6, 500, "strong"), "RCU")
print("80 strongly consistent reads of 3 KB  :", rcu(3, 80, "strong"), "RCU")
100 writes/s of 2.5 KB items          : 300 WCU
100 transactional writes/s of 0.5 KB  : 200 WCU
500 eventually consistent reads of 6 KB: 500 RCU
500 strongly consistent reads of 6 KB : 1000 RCU
80 strongly consistent reads of 3 KB  : 80 RCU

Note the rounding: a 2.5 KB item costs 3 WCU per write, and a 6 KB item costs 2 RCU per strong read. Small items and eventually consistent reads are much cheaper.

Query vs Scan

  • Query finds items by partition key (and optionally a sort key condition). It reads only what it needs.
  • Scan reads the whole table and filters afterwards. It consumes capacity for every item read, so avoid it on large tables.

More features

  • Global Secondary Index (GSI): query by another key, e.g. status + order_date.
  • DynamoDB Streams: a change log that can trigger Lambda.
  • TTL: delete items automatically after a timestamp.
  • Global tables: multi-Region, multi-active replication.
  • DAX: an in-memory cache in front of DynamoDB (microsecond reads).
  • Point-in-time recovery: restore to any second in the last 35 days.

Test yourself

Why is status (with values like 'shipped'/'pending') a poor partition key?

  1. It is too long
  2. It has few distinct values, creating hot partitions
  3. Strings are not allowed
  4. It cannot be indexed

How many WCUs does one write per second of a 3.2 KB item need?

  1. 1
  2. 3
  3. 4
  4. 0.5

Which operation reads the entire table?

  1. Query
  2. GetItem
  3. Scan
  4. PutItem