Skip to content
Kuza Ndoto Academy

Amazon DynamoDB

Beginner → IntermediateChapter 4 · Page 18 of 304 min readUpdated 15 Nov 2026

Amazon DynamoDB is a fully managed, serverless NoSQL database that stores data as items with keys. It delivers consistent single-digit millisecond responses at almost any scale, with no servers to size or patch, which makes it a favourite for serverless apps, mobile back-ends and high-traffic systems.

Tables, items and keys

  • A table holds items (like rows), and each item has attributes (like columns). Items in the same table can have different attributes; only the key is required.
  • The primary key uniquely identifies each item. It is either a partition key alone, or a partition key plus a sort key.
  • DynamoDB uses the partition key to decide where an item is stored. Items sharing a partition key are kept together, ordered by the sort key, so you can fetch a related group in one fast query.
Table: Results studentId (PK) term (SK) maths english school S0012026-T17865Kuza High S0012026-T28270Kuza High S0012026-T385(none)Kuza High S0022026-T15574Kuza High S0022026-T26179Kuza High Items with the same partition key are stored together, sorted by the sort key
Figure 1. PK is the partition key and SK the sort key; together, (studentId, term) is the primary key. All of S001's items form one group (shaded) that a single query can fetch in term order. The 2026-T3 item has no english attribute, which DynamoDB allows.

Capacity modes and pricing

  • On-demand: you pay per read and write request, with no capacity planning. Ideal for new apps, spiky traffic and learning, and idle tables cost only storage.
  • Provisioned: you set read and write capacity units (optionally with auto scaling). Cheaper for steady, predictable traffic.

Storage is charged per GB-month, and features such as backups, streams and global tables have their own charges. Check the DynamoDB pricing page.

Using DynamoDB from Python

Create the table from Figure 1 with on-demand capacity:

aws dynamodb create-table --table-name Results \
    --attribute-definitions AttributeName=studentId,AttributeType=S AttributeName=term,AttributeType=S \
    --key-schema AttributeName=studentId,KeyType=HASH AttributeName=term,KeyType=RANGE \
    --billing-mode PAY_PER_REQUEST

Only key attributes are declared up front; HASH means partition key and RANGE means sort key. Now write and read items with the boto3 Table resource:

import boto3
from boto3.dynamodb.conditions import Key

table = boto3.resource("dynamodb").Table("Results")

table.put_item(Item={"studentId": "S001", "term": "2026-T1", "maths": 78, "english": 65, "school": "Kuza High"})
table.put_item(Item={"studentId": "S001", "term": "2026-T2", "maths": 82, "english": 70, "school": "Kuza High"})
table.put_item(Item={"studentId": "S001", "term": "2026-T3", "maths": 85, "school": "Kuza High"})

# Get one item by its full primary key
response = table.get_item(Key={"studentId": "S001", "term": "2026-T2"})
item = response.get("Item")
print(item["maths"], type(item["maths"]).__name__)

# Query all of S001's items for 2026, in sort-key order
response = table.query(
    KeyConditionExpression=Key("studentId").eq("S001") & Key("term").begins_with("2026")
)
for item in response["Items"]:
    print(item["term"], item.get("english", "no english mark"))
82 Decimal 2026-T1 65 2026-T2 70 2026-T3 no english mark

Two things to notice. First, get_item returns no "Item" key at all if nothing matches, so use response.get("Item"). Second, numbers come back as Python Decimal objects, not int or float. Convert them before passing them to json.dumps, and use Decimal rather than float when writing non-whole numbers.

Query vs scan, and indexes

  • A query finds items by partition key (and optionally a sort key condition). It is fast and reads only what it needs.
  • A scan reads the entire table and then filters. On a big table it is slow and expensive. Avoid scans in normal app code.
  • To query by another attribute, such as all results for a school, add a global secondary index (GSI) with school as its partition key.

This is the big mindset shift: in DynamoDB you design keys around the questions your app will ask, rather than designing tables first and writing any query later.

Other useful features: TTL deletes items automatically after a timestamp (handy for sessions or one-time codes), DynamoDB Streams can trigger a Lambda function on every change, and reads are eventually consistent by default with an option for strongly consistent reads.

RDS or DynamoDB?

Amazon RDS / AuroraAmazon DynamoDB
Data modelTables with fixed schemas, joins, SQLKey-value and document items, flexible attributes
QueriesAny SQL query, ad hoc reportingBy key or index; design keys around access patterns
ScalingBigger instances, read replicasScales automatically to very high request rates
ServersYou choose instance sizes (except Aurora Serverless)None; fully serverless
Idle costPay for instance hours even when idleOn-demand: storage only when idle
Best forComplex relationships, transactions across many tables, reportingSimple, high-volume access patterns; serverless apps
Example

A mobile-money style payments notification service records millions of short events, each looked up by transaction ID: a good DynamoDB fit. The same company's finance team needs monthly reports joining customers, agents and fees: a good fit for a relational database.

Common mistake

Choosing a partition key with few distinct values, such as county or status. Most traffic then lands on a few "hot" partitions. Prefer keys with many distinct values, such as user IDs or order IDs.

Key points

  • DynamoDB is a serverless key-value and document database with consistent low latency.
  • The primary key is a partition key, optionally with a sort key that orders related items.
  • Use queries, not scans; add GSIs for other access patterns.
  • boto3 returns numbers as Decimal, and get_item omits Item when nothing matches.
  • Choose RDS for relational, SQL-heavy work and DynamoDB for simple, massive-scale access patterns.
Up next · New chapter: Networking & DeliveryAmazon VPCYour private network in the cloud: subnets, routing and gateways.
→

Tip: use the ← and → keys to move between pages.