Overview
Curated: · Written: · Reviewed:
NoSQL, Graph & Key-Value Data Stores
In one line
Reach for a NoSQL store when a known access pattern, a genuinely variable data shape, or a write volume one node cannot take is the hard constraint that relational defaults cannot meet. Relational stays the default, so a NoSQL choice needs a stated reason.
Key definitions
- Access pattern: the queries the store must serve. The design starts here, not with the data.
- Partition key: decides which node holds an item. Queries without it are rejected or scan the cluster.
- Embed vs reference: embedding nests child data inside the parent document; referencing stores children separately and links them by ID.
- Index-free adjacency: each graph node holds pointers to its neighbours, so a hop follows stored links instead of a global join lookup.
- Read-your-writes: a user always sees their own last write. The minimum guarantee for user-facing state.
- Hot partition: one key takes a disproportionate share of reads or writes and saturates the node that owns it.
Formulas and complexity
A read quorum overlaps the write quorum when W + R > N, so a read sees the latest acknowledged write. W = 1 gives fast acks and possibly stale reads.
Graph traversal cost grows with the nodes and edges the query visits, not with total graph size. Quote it that way.
How it works
A write carries its partition key. The store hashes that key to a token range, and the owning node and its replicas take the write. Reads that include the key go to the same place. A read without the key has to ask every partition, which is why such queries are rejected or become full scans.
When to use and when not to
| Situation | Reach for | Avoid when |
|---|---|---|
| Lookup by known key, latency dominates | Key-value (Redis) | You need queries by content or multi-key atomicity |
| Self-contained aggregate, variable shape, known read paths | Document (MongoDB) | Ad-hoc cross-document queries or frequent multi-document transactions dominate |
| Write-heavy events with fixed partition access | Wide-column (Cassandra) | Query patterns change often or you need ad-hoc analytics |
| Variable-depth traversal is the core query | Graph | One or two joins would answer it |
| Multi-entity transactions, constraints, ad-hoc SQL | Relational (PostgreSQL) | Rarely a reason to leave, only a reason to check first |
Traps interviewers probe
- Choosing for scale you do not have. Fix: measure a well-indexed relational baseline first, then name the pain it cannot meet.
- Treating Redis as a durable system of record. Fix: state the persistence mode and replication. With asynchronous replication, confirmed writes can be lost on failover.
- Calling a store schemaless. Fix: there is always a schema. Enforce it with
$jsonSchemavalidation so readers do not handle every historical shape. - Embedding an unbounded array. Fix: bound the child count, or reference the children and keep only a summary in the parent.
- Hash partitioning by a key one tenant dominates. Fix: add a time bucket or random suffix to the key, or cache the hot key in front of the store.
Interview tips
Open with the access pattern, the consistency requirement, and why Postgres is not enough. Expect these follow-ups: "Why not just Postgres?", "What consistency does this feature need?", "A key goes viral, what happens?", and "The read paths changed, now what?" The last one is the re-shaping cost: a new table or embedding change, a dual-write backfill, then cutover.
30-second outline: "Access is lookup by session ID at 50k/s with a sub-millisecond p99. Consistency is eventual, with read-your-writes on the same session. A relational table with a secondary index adds a second structure to maintain on every write. I would use a key-value store and accept that queries by user_id now need a separate index I build and keep in sync. If one key goes hot, I salt it or cache it in front."
Feedback on this overview
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.