NoSQL Databases
Scale-out stores for specific access shapes
NoSQL services (DynamoDB, Cosmos DB, Firestore/Bigtable) trade joins and ACID breadth for horizontal scale and flexible schemas. Choosing the right NoSQL store means matching access patterns, not just picking a name.
What you will learn
- Categorize document, key-value, wide-column and graph stores
- Explain partition keys and hot partitions
- Choose NoSQL over relational with evidence
NoSQL abandons joins for pre-shaped access: one primary-key get, range scans, or per-attribute queries. DynamoDB scales to any throughput with a partition key; Firestore is a serverless document store; Bigtable is a wide-column timeseries engine.
What is this?
AWS DynamoDB: managed key-value + document, on-demand/provisioned capacity, global tables, stream triggers. Azure Cosmos DB: multi-model (document, table, graph, columnar) with consistency levels and global distribution. GCP Firestore: serverless document store with realtime sync; Bigtable: wide-column petabyte timeseries. All scale horizontally by partition key; hot keys and poor access patterns cause throttling.
Why do you need it?
- Predictable sub-10ms single-key reads at any scale.
- No schema migration for evolving documents.
- Serverless billing (DynamoDB on-demand, Firestore) fits spiky apps.
- Global distribution (global tables, multi-region Cosmos) with strong consistence choice.
Mental model
A coat check where every tag maps straight to a hook
Key-value NoSQL
Fast when you know the tag (primary key). Searching 'all coats by color' (a scan/query without the key) is possible but costs a rack of hangers — exactly why access calls are designed up front.
How it works
- Data model: item/partition key (HASH) + optional sort key (RANGE) define the index.
- Partitions each hold a key range; a hot key concentrates traffic onto one partition and throttles it (partition key design is everything).
- Queries read by key (GetItem) or across a sort-key range (Query); scans touch everything and cost reads linearly.
- Consistency: eventually/strong options per store; Cosmos exposes consistency levers.
- DAX/Cache proxies buffer hotspots; exponential backoff with retries mitigates, not fixes, hot keys.
Partitioned access path.
Examples
Design for partition access
# DynamoDB: item-by-primary-key plus range queryItem: {"pk": "user#42", "sk": "order#2026-09-22T01:00:00Z", "amount": 9.99}# Query recent orders for a user (uses pk=sk range):# GetItem {pk: user#42, sk: order#2026-...}# Bigtable: row key = time-bucketed metric -> time series# row key "sensor#2026-09-22#01"Composite keys bake in the access pattern: user# → their orders as a range scan.
In production
- A chat app stores messages as sk=timestamp ranges per conversation on Firestore.
- A logger round-trips metrics through Bigtable keyed by host#minute for fast scans.
- Cosmos DB distributes a shopping-cart service across two regions with session consistency.
Best practices
- Design access patterns before schema: the key IS the index.
- Use high-cardinality partition keys; avoid 'user' on a table you'll 'scan all'.
- Balance reads/writes: know your consistent needs and pick capacity mode accordingly.
- Enable TTL for expiring sessions/logs instead of deleting manually.
Common mistakes
- Querying 'by email' without an email index → full scan on every login.
- Hot partition from one celebrity key throttling the whole table.
- Believing 'NoSQL = no schemas' — access patterns still need design.
- Choosing NoSQL for strongly-acid financial balance semantics then bolting on hacks.
Common issues
One key throttles everythingPartition key with low cardinality or skewed popularity
Redesign the key to spread load (add salt/entity suffix), or warm a secondary index for reads.
Expensive scans in the runner billAccess pattern without a supporting index
Model a GSI or second table keyed for the query; never scan a large table habitually.
Debugging tips
- Throttled 'ProvisionedThroughputExceeded': inspect hot keys — Enable CloudWatch metric on item-level throttling.
- Scans slow: add a GSI (Global Secondary Index) for the query column.
- Latency spikes: check partition distribution — skew shows in table resource metrics.
- Consistency confusion: read-after-write lag → bump consistency level or design around eventual reads.
Interview checkpoint
Hands-on lab
- Model items with a good PK/SK scheme
- Simulate hot-key throttling
- Fix the design with a high-cardinality key
Design and Stress a NoSQL Access Pattern
30 min · intermediate
Quick quiz
What makes a DynamoDB partition key 'good'?