Catalog My learning Blog
System Design Roadmap: 100-Day Course for Engineers · Phase 3 · Lesson 23 · 4 min read

NoSQL Database Types: Key-Value, Document, Graph

On this page
  1. NoSQL: one label, very different databases
  2. The NoSQL database types
  3. Worked example: choosing databases for a social app
  4. SQL vs NoSQL: the line is blurrier than it used to be
  5. Check yourself

NoSQL: one label, very different databases

"Should we use NoSQL?" is like asking "should we use a vehicle?" Redis, MongoDB, Cassandra and Neo4j are all called NoSQL, and they have almost nothing in common except that they aren't built around tables and joins. The useful question is which access pattern a feature has, because each NoSQL family is designed around one.

The NoSQL database types

Key-value stores (Redis, DynamoDB, Memcached). Give a key, get a value. That's nearly the whole API, which is why they're extremely fast and easy to spread across machines. Great for sessions, caches, counters and rate limits. You can't ask "which sessions belong to users in Berlin?" unless you built a key for that question in advance.

Document stores (MongoDB, Couchbase, Firestore). Each record is a JSON-like document, and documents in one collection can have different fields. Data that's naturally one nested object, like a product with varying attributes or a user profile, is read and written as a unit. Queries on fields and secondary indexes are supported; joins across collections are limited or slow.

Wide-column stores (Cassandra, HBase, Bigtable). Rows are grouped into partitions by a partition key and sorted within the partition. They're built for very high write rates spread across many machines and data centers, with predictable reads by partition key. You design tables around the queries you'll run, often storing the same data several times for different queries.

Graph databases (Neo4j, Amazon Neptune). Nodes and the relationships between them are stored directly, so following connections is cheap. "Friends of friends who like this band" is a few hops of traversal instead of several self-joins.

Search engines (Elasticsearch, OpenSearch) are a fifth family you'll meet in almost every case study. They're built on an inverted index: for every word, a list of the documents that contain it, like the index at the back of a book. A query for "red running shoes" looks up three short lists and intersects them, then ranks the matches by relevance. A relational WHERE name LIKE '%shoe%' can't use a normal index and scans every row. Search engines are usually a secondary copy, fed from the main database, so results lag writes by a second or so.

Four NoSQL data models, each built for a different access pattern: key-value, document, wide-column and graph.
Four NoSQL data models, each built for a different access pattern: key-value, document, wide-column and graph.
FamilyBuilt forWeak at
Key-valueLookups by known key at huge speedQueries by anything but the key
DocumentSelf-contained, varied records read as a wholeJoins and transactions across documents
Wide-columnMassive write volume, multi-region availabilityAd-hoc queries; table design is query-specific
GraphTraversing relationships many hops deepBulk aggregation over all records
SearchFull-text relevance, typo tolerance, facetsBeing the source of truth; transactions

Worked example: choosing databases for a social app

FeatureStoreWhy
Sessions, rate-limit countersRedisSimple lookups by key, very high rate
Profiles with varied attributesMongoDBFlexible, nested, read as a unit
Activity feed, millions of writes a minuteCassandraWrite throughput and availability across regions
"People you may know"Neo4jThe query is a relationship traversal
Searching posts and peopleElasticsearchFull-text relevance over millions of documents
Accounts, billing, paymentsPostgreSQLTransactions and correctness

The relational database stays. NoSQL stores are added for specific features whose access pattern it serves poorly. Netflix, for example, uses Cassandra for high-volume data such as viewing history, while relational databases remain common for transactional data.

SQL vs NoSQL: the line is blurrier than it used to be

PostgreSQL has a jsonb type with indexes, so a document-shaped column inside a relational table often covers "we need flexible attributes" without a second database. MongoDB supports multi-document transactions. DynamoDB supports secondary indexes. Start with the access pattern, then check whether a store you already run handles it well enough before adding another one to operate. On Azure, Cosmos DB offers several of these models behind one service: document, key-value, wide-column through a Cassandra-compatible API, and graph through Gremlin. That saves running separate systems, but not designing for each access pattern.

Check yourself

A graph database. The query is a multi-hop traversal of relationships, which graph stores do cheaply and relational databases do with several self-joins.

A leading wildcard can't use a normal index, so every row is scanned. A search engine's inverted index finds matching documents directly and ranks them.

Each table is designed for one query, keyed and sorted for it. Supporting another query efficiently usually means another table with a different partition key.

Not necessarily. A jsonb column in PostgreSQL, with an index on the fields you query, often covers it without adding a new database.

Looking up a value by a known key very fast: sessions, caches, feature flags, rate-limit counters or shopping carts. Key-value stores like Redis and DynamoDB are simple and fast for that pattern, but aren't built for ad-hoc queries across values.