NoSQL Families
Overview
"NoSQL" is not one technology. It is a name for several kinds of databases that give up some relational features (joins, multi-table transactions, a fixed schema) to gain something else: easy horizontal scale, flexible data shape, or speed on one specific access pattern.
There are four families: key-value, document, wide-column and graph. Each one is built around a particular question, and the skill interviewers look for is matching your access pattern to the right family. This lesson explains each family, shows the same data in each, and gives a decision rule.
Detailed Notes
What is it?
A NoSQL database is any database that does not use the relational table-and-join model as its main interface. The common thread is not the missing SQL; it is a design choice: model the data around how you will read it, and spread it across many machines.
The four families:
- Key-value store. The simplest: a giant dictionary. You
put(key, value)andget(key). The database does not look inside the value. Examples: Redis, DynamoDB (in its simplest use), Riak. - Document store. The value is a structured document (usually JSON) and the database can look inside it, so you can query and index fields. Examples: MongoDB, Couchbase.
- Wide-column store. Data is grouped by a partition key, and inside a partition rows are sorted by a clustering key. It is built for huge write volumes and queries like "the latest 50 items for this key". Examples: Cassandra, HBase, Bigtable.
- Graph database. Stores nodes (things) and edges (relationships) and is fast at following connections several steps deep. Examples: Neo4j, Amazon Neptune.
Why do we need it?
Relational databases are excellent, but three situations push teams away from them:
- Scale beyond one node. Joins and transactions across machines are slow. If every query touches only one key, you can spread keys over hundreds of machines with no coordination. NoSQL stores do this partitioning for you.
- Flexible or varied data. A product catalogue where shoes have sizes and laptops have RAM would need many nullable columns, or many tables. A document holds exactly the fields each product needs.
- Predictable speed on one access pattern. A key lookup that always takes about 1-5 ms, regardless of data size, is easier to promise than a join over five tables.
The price is real. You lose ad-hoc queries (you must decide the questions in advance), you usually lose multi-row transactions, and you denormalise: store the same fact in several places so one read returns everything. When the fact changes you must update every copy yourself.
How does it work?
The same data in each family
Take one example: a user, their profile, and their activity feed.
- PKkeystring
session:9f3a
- valueblob
opaque to the database
- ttlseconds
auto-expire in 1800
Only get/put by exact key.
- PK_idstring
- namestring
- addressobject
embedded, no join
- IDXinterestsarray
queryable inside the doc
One read returns the whole user.
- PKuser_idbigint
partition key
- SKevent_timetimestamp
clustering key, newest first
- typetext
- payloadtext
Latest N events of a user = one sequential read.
- FKfrom_usernode
- FKto_usernode
- sincedate
edge property
Edges are first-class, so multi-hop walks stay fast.
The same information stored the way each family wants to read it.
- Key-value:
session:9f3amaps to an opaque blob. Super fast, but you can only ask for a session by its exact key. You cannot ask "all sessions of user 42" unless you also store that list under another key. - Document: one JSON document per user, with the address and a list of interests inside it. One read returns everything and no join is needed. A rule of thumb: embed data that is read together and rarely changes; reference (store an id) data that is large, shared, or changes often.
- Wide-column: partition key
user_id, clustering keyevent_timedescending. All events of one user live together on disk in time order, so "latest 50 events of user 42" is one sequential read from one machine, even with billions of events overall. - Graph: nodes are people, edges are
FOLLOWS. The query "friends of friends who like jazz" walks edges. In a relational database it would be repeated self-joins, which get slow as depth grows.
Access patterns first
The central discipline: write down your queries, then design the data to answer them. With SQL you design tables first and think about queries later. With NoSQL it is reversed.
Query 1: get profile by user_id -> document or key-value
Query 2: latest 50 events for a user -> wide-column (partition user_id, sort event_time)
Query 3: is user A connected to user B within 3 hops -> graph
Query 4: lookup session token -> key-value, with TTL
Often one system uses several stores, each for the query it is best at. This is called polyglot persistence.
Picking a family
Key-value
get / put by key
- 1
Hash the key to a node
O(1) lookup
- 2
Return the opaque value
no inspection
- 3
Optional TTL expiry
sessions, tokens
- Fastest and simplest
- Scales out trivially
- No queries by value
- No relationships
Sessions, caches, counters, feature flags
Document
JSON with queries
- 1
Store a nested JSON doc
shape may vary
- 2
Index chosen fields
query by field
- 3
Read the whole object
no joins
- Flexible schema
- Whole object in one read
- Duplicated data to keep in sync
- Weak cross-document joins
Catalogues, profiles, content
Wide-column
partition + sorted rows
- 1
Partition key picks the node
e.g. user_id
- 2
Rows sorted by clustering key
e.g. time
- 3
Range read inside one partition
latest N
- Huge write throughput
- Time-ordered reads are cheap
- Queries must match the key design
- Ad-hoc queries are painful
Feeds, messages, metrics, event logs
Start from the dominant query, not from the product name.
Partition key: the most important choice
In key-value, document and wide-column stores, the partition key decides which machine holds a record (the partitioning lesson explains how). A good partition key has many distinct values and spreads traffic evenly. user_id is good. country is bad: a few countries hold most users, so a few machines do most of the work. A key like date is bad for writes because all of today's writes hit one partition.
Worked example: a feed table with 100 million users and a partition key of user_id. Each user has about 1,000 events of 200 bytes, so 100M x 1,000 x 200 B = 20 TB. Partitioned over 40 nodes, each holds about 500 GB, and any single user's feed is read from one node.
Interview tip. Do not say "NoSQL because it scales". Say which access pattern you have and which family fits it. Equally, do not claim NoSQL has no transactions at all: many stores now offer limited ones (for example within one partition or one document), so state the limit rather than a blanket rule.
Consistency and queries you give up
Most NoSQL stores replicate data and offer tunable consistency, meaning you choose per request how many copies must agree (the consistency lesson covers this). Queries that cross partitions, such as "count users by country", are slow or impossible without an extra index or a separate analytics system. Plan for that up front.
Real-world example
A game company keeps three kinds of data. Player sessions are looked up by a token and expire after 30 minutes: a key-value store with a time-to-live. Player inventory varies per player (swords, skins, quest items with different attributes): a document store, one document per player. Match history grows forever and is always read as "last 20 matches of this player": a wide-column store partitioned by player_id and sorted by match time. Each store is the simplest tool for its query.
System design example
Design a chat message store. The only important query is "load the most recent 50 messages of a conversation, then older ones on scroll".
- Partition key
conversation_id, clustering keymessage_id(time-ordered, descending). The newest 50 messages are the first 50 rows of one partition. - Load: 10 million conversations x 100 messages/day x 200 bytes = 200 GB per day, about 73 TB per year. Writes are append-only with a steady 10 M x 100 / 100,000 s = 10,000 writes per second. That suits a wide-column store, and one relational node would struggle with the volume.
- Hot partition check: a group chat with 100,000 members and rapid messages can overload one partition. Mitigation: add a time bucket to the key (
conversation_id, day) so a partition never grows without limit. - Things we give up: joining messages with user profiles. We store the sender's display name inside each message, and accept it may be slightly stale.
Interview discussion. Say: "The dominant query is the newest messages of one conversation, which is a single-partition range read. A wide-column store with conversation_id as partition key and message_id as clustering key serves that directly, and it scales out with no cross-node joins. I'd use a relational store for accounts, where I want constraints."
Trade-off thinking. NoSQL buys horizontal scale and a fast, predictable path for the queries you planned. It costs flexibility: a new query shape may need a new table, a new index, or a data migration. If the product is still discovering its queries, a relational database is the safer start.
Key interview takeaway
Name the access pattern, then the family: lookup by key (key-value), whole-object reads with varied shape (document), huge append-heavy data read by key and time (wide-column), deep relationship traversal (graph). Always state the partition key and why it spreads load evenly, and what you gave up.
Real World Example
A game keeps sessions in a key-value store with a 30-minute TTL, player inventories as documents, and match history in a wide-column store partitioned by player_id and sorted by time.
Common Interview Questions
Finished this lesson?
0 of 44 lessons done · 0% of the course

