Lesson 229 of 296
NoSQL and DynamoDB
What you will learn
- What NoSQL means and why it exists
- The main kinds of NoSQL databases
- SQL vs NoSQL: how to choose
- DynamoDB: tables, partition keys and sort keys
- Access patterns and single-table design
- Secondary indexes, capacity and consistency
On this page
What is NoSQL?
NoSQL ("not only SQL") covers databases that do not use the relational table model. They became popular for huge scale, flexible data and very high throughput, usually by giving up some relational features (joins, multi-row transactions, a flexible query language).
| Kind | Stores | Examples | Good for |
|---|---|---|---|
| Document | JSON-like documents | MongoDB, Firestore, Couchbase | Content, catalogues, flexible records |
| Key-value | A value per key | Redis, DynamoDB (also wide-column-like) | Sessions, caching, very fast lookups |
| Wide-column | Rows with flexible columns grouped by partition | Cassandra, ScyllaDB | Massive write volumes, time series |
| Graph | Nodes and relationships | Neo4j, Amazon Neptune | Social networks, recommendations, fraud detection |
| Search | Inverted indexes over text | Elasticsearch, OpenSearch | Full-text search, logs |
| Vector | Embeddings | pgvector, Pinecone | AI similarity search (Lesson 276) |
SQL or NoSQL?
| Choose relational (PostgreSQL) when | Consider NoSQL when |
|---|---|
| Data is relational with many joins | Access patterns are known and simple (get by key, list by key) |
| You need flexible, ad-hoc queries and reports | You need predictable millisecond latency at any scale |
| Strong consistency and multi-row transactions matter (money, bookings) | Very high write throughput or global distribution |
| The schema is fairly stable | Data shape varies a lot between records |
Honest default: start with PostgreSQL unless there is a specific reason not to; it also offers JSONB, full-text search and vectors. Many systems use both (PostgreSQL for core data, Redis for caching, OpenSearch for search, DynamoDB for a high-volume feature).
DynamoDB basics
Amazon DynamoDB is AWS's fully managed key-value and document database: no servers to manage, single-digit-millisecond responses at any scale, pay per request or per provisioned capacity. It appears often in AWS-focused job adverts.
- A table holds items (like rows); each item is a set of attributes (like columns), and items can have different attributes.
- The primary key is either:
- a partition key alone (
userId), or - a partition key + sort key (
userId+orderDate): a composite primary key.
- a partition key alone (
- The partition key decides which storage partition holds the item (it is hashed). Items with the same partition key are stored together, sorted by the sort key.
Queries are by key
DynamoDB efficiently supports:
- GetItem: one item by its full primary key.
- Query: items with one partition key, optionally a sort key condition (
begins_with,between,>), in sort key order, paginated. - Scan: reads the whole table: slow and expensive; avoid in normal application paths.
There are no joins and no ad-hoc WHERE on arbitrary attributes (filters exist but apply after reading, so you still pay for the reads). So you design the table around your access patterns first: the opposite of relational design, where you normalise first and query later.
Single-table design
A common DynamoDB technique stores several entity types in one table, using generic key names (PK, SK) and prefixes, so related data can be fetched in one Query:
| PK | SK | Attributes |
|---|---|---|
CUSTOMER#1 | PROFILE | name, email |
CUSTOMER#1 | ORDER#2026-06-01#101 | status, total |
CUSTOMER#1 | ORDER#2026-06-15#102 | status, total |
ORDER#101 | ITEM#1 | product, quantity |
- "Get customer 1 and all their orders":
Query PK = "CUSTOMER#1". - "Customer 1's orders in June, newest first":
Query PK = "CUSTOMER#1" AND begins_with(SK, "ORDER#2026-06"),ScanIndexForward = false.
Secondary indexes
To query by something other than the primary key (for example "all orders with status pending"), add an index:
- Global Secondary Index (GSI): a different partition and sort key, can be added any time, eventually consistent, has its own capacity. E.g.
GSI1PK = "STATUS#pending",GSI1SK = orderDate. - Local Secondary Index (LSI): same partition key, different sort key; must be defined when the table is created.
Design pitfalls and features
- Hot partitions: if one partition key gets most of the traffic (all writes to
STATUS#pending), it hits per-partition throughput limits. Choose high-cardinality keys, or add a suffix to spread writes ("write sharding"). - Item size limit: 400 KB per item. Store big files in S3 (Lesson 254) and keep a reference.
- Consistency: reads are eventually consistent by default (may miss a write from the last moment); request strongly consistent reads on the base table when needed (not available on GSIs).
- Conditional writes:
ConditionExpression: "attribute_not_exists(PK)"(create only if new) orversion = :expected(optimistic locking). - Transactions:
TransactWriteItemsfor up to 100 items across tables, all or nothing. - TTL: an attribute that makes DynamoDB delete items automatically after a time (sessions, temporary data).
- Streams: a change log of item changes that can trigger Lambda functions (Lesson 255), for example to update a search index or send events (similar to the outbox, Lesson 204).
- Capacity modes: on-demand (pay per request, no planning) or provisioned (set read and write capacity, cheaper at steady load, with auto scaling).
How to say it in an interview
"NoSQL databases trade relational features like joins and flexible queries for scale, flexibility or speed: document stores, key-value stores, wide-column, graph and search engines. I default to PostgreSQL for relational data, ad-hoc queries and transactions, and use NoSQL where access patterns are simple and scale or latency demands it. In DynamoDB the primary key is a partition key, optionally with a sort key; you design around access patterns, using Query on one partition with sort key conditions, single-table design with prefixed keys, and global secondary indexes for other patterns, avoiding scans and hot partitions. I use conditional writes for optimistic locking, TTL for expiry and Streams to react to changes."
Key words
| Word | Meaning |
|---|---|
| NoSQL | Databases that do not use the relational model |
| Document / key-value / wide-column / graph | Kinds of NoSQL databases |
| Partition key | Decides where a DynamoDB item is stored |
| Sort key | Orders items within a partition |
| Query vs Scan | Read one partition by key vs read the whole table |
| Access pattern | A specific way the application reads or writes data |
| Single-table design | Several entity types in one DynamoDB table |
| GSI / LSI | Global / local secondary index |
| Hot partition | One partition receiving too much traffic |
| Eventually consistent | Reads may briefly miss the latest writes |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.