Skip to content
Tutorials Center
My learning

Topics

Lesson 66 of 79 ยท Distributed Databases, NoSQL and Analytics

NoSQL Databases: Key-Value, Document, Column and Graph

Compare the four NoSQL families, learn how their data models differ from tables, try JSON documents and graph traversal in SQLite, and see polyglot persistence.

"NoSQL" is a loose label for databases that do not use the classic relational table model as their main idea. The name is better read as "not only SQL". These systems appeared because some workloads, such as huge web-scale traffic, flexible records or heavily connected data, were awkward or expensive to handle with a single relational server. Each NoSQL family makes a different trade: it gives up some relational features (joins, strict schemas, multi-row transactions) in return for speed, scale or a model that fits the data better.

In this lesson you learn the four main families, how modelling changes in each, and how to choose. Real NoSQL servers (Redis, MongoDB, Cassandra, Neo4j) cannot run in your browser, so their commands are shown as read-only code. What does run live is SQLite, which can store JSON documents, act as a key-value store and even walk a graph. Those runnable stand-ins show the ideas honestly, but they are not the real products. For the practical SQL side of this topic see SQL and NoSQL. The previous lesson, CAP, BASE and Consistency Models, explains why many of these systems relax consistency.

Four families, four shapes

Key-value One key, one opaque value.
Document Nested JSON-like records.
Wide column Rows grouped by partition key.
Graph Nodes and relationships.

Choose by access pattern, not by fashion.

1 / 2

Key-value stores

A key-value store is the simplest model: a giant dictionary. You SET a value under a key and GET it back. The database does not look inside the value; to it, the value is just bytes. Because the only operation is lookup by key, these systems are extremely fast and easy to spread over many machines. Typical uses are caches, user sessions, shopping carts, rate limiters and leaderboards. Redis is the best-known example, and it adds useful value types such as lists, sets and sorted sets.

The cost is that you cannot ask "which sessions belong to Ana?" unless you built a second key for that question. The access pattern must be known in advance.

TEXT
# Read-only: Redis commands (not runnable on this site)
SET session:9f3a '{"user_id": 17, "cart": [3, 8]}' EX 1800
GET session:9f3a
INCR page:views:home
ZADD leaderboard 4200 "ana" 3900 "boris"
ZREVRANGE leaderboard 0 2 WITHSCORES

Document stores

A document store keeps self-describing records, usually JSON or a binary form of it, in collections. A document can contain nested objects and arrays, and two documents in the same collection may have different fields. The database does look inside, so you can filter and index on nested fields. MongoDB and CouchDB are the typical examples.

The central modelling idea is embedding: data that is read together is stored together. In a relational design an order and its lines live in two tables joined by a key. In a document design the lines sit inside the order document, so one read returns everything. The price is duplication (a product name copied into many orders) and the effort of keeping copies consistent. When something is shared and changes often, documents reference each other by id instead, just like foreign keys, but the database may not enforce them.

JavaScript
// Read-only: MongoDB shell (not runnable on this site)
db.orders.insertOne({
  customer: "Ana",
  lines: [{ sku: "B-12", qty: 2 }, { sku: "A-07", qty: 1 }],
  status: "paid"
});
db.orders.find({ "lines.sku": "B-12", status: "paid" });
db.orders.aggregate([{ $group: { _id: "$customer", orders: { $sum: 1 } } }]);

JSON in SQLite, live

Relational systems have borrowed the document idea. SQLite stores JSON as text and offers functions to read and change it: json_extract, json_each, json_set, json_array_length. The first example stores orders as documents and queries into them.

SQL Documents inside a SQL table

Press Run (or Ctrl + Enter) to see the result here.

Look at the result: Chen's document has a note field and no lines, Ana's has two lines and no note. The table has one column, yet the records have different shapes. That is the flexibility of the document model. The CHECK (json_valid(doc)) constraint is a small example of the price: with no schema, you must add the validation.

Wide-column stores

A wide-column (or column-family) store such as Cassandra or HBase organises data into tables whose rows are found by a partition key. Within a partition, rows are kept sorted by a clustering key, and each row may carry many columns. The data model follows the query: you design one table per question you will ask, even if that duplicates data. Writes are very fast and the system scales across many machines, but you cannot join, and ad hoc queries on columns outside the key are limited or slow.

Do not confuse this with columnar storage (the next lesson), which is a physical layout for analytics. Wide-column stores are still row-oriented in spirit.

SQL
-- Read-only: Cassandra CQL (not runnable on this site)
CREATE TABLE readings_by_sensor (
  sensor_id  text,
  taken_at   timestamp,
  celsius    double,
  PRIMARY KEY ((sensor_id), taken_at)   -- partition key, then clustering key
) WITH CLUSTERING ORDER BY (taken_at DESC);

SELECT taken_at, celsius FROM readings_by_sensor
WHERE sensor_id = 's-17' LIMIT 10;      -- the latest 10 readings of one sensor

Graph databases

A graph database stores nodes (things), edges (relationships) and properties on both. It is built for questions about connections: friends of friends, shortest routes, fraud rings, recommendations. In a relational database, each hop is a join, and deep traversals become slow and hard to write. In a graph database, a node points directly to its neighbours, so a hop costs about the same however large the graph is. Neo4j and its query language Cypher are the standard example.

TEXT
// Read-only: Cypher (Neo4j). Not runnable on this site.
MATCH (me:Person {name: "Ana"})-[:FOLLOWS*1..2]->(other:Person)
WHERE other <> me
RETURN DISTINCT other.name;

Standard SQL can do the same job with a recursive CTE, only with more typing and, on huge graphs, less speed. This runs live on a tiny follower graph:

SQL A graph as edges, walked with a recursive query

Press Run (or Ctrl + Enter) to see the result here.

Boris and Chen are one hop away; Dara and Eli are two. Fay needs three hops, so she is not listed. Notice the hops < 2 guard: without a limit, the cycle Ana, Chen, Eli, Ana would make a naive recursion run forever. Graph engines handle this for you.

Choosing a family

Family Strong at Weak at Typical use
Key-value Lookup by key, speed, caching Any query that is not by key Sessions, caches, counters
Document Flexible nested records, read an aggregate in one go Many-to-many links, cross-document consistency Catalogs, content, user profiles
Wide column Massive write rates, time series, known queries Ad hoc queries, joins Telemetry, messaging, event logs
Graph Deep relationship queries Bulk analytics over all rows Social networks, fraud, recommendations
Relational General queries, integrity, transactions Extreme horizontal scale, schema-less data Most business systems

Start with the question "how will the data be read?" If you cannot answer, a relational database is the safest default: its flexible queries forgive you when requirements change. Pick a specialised engine when a measured need appears.

Polyglot persistence

Few real systems use only one database. Polyglot persistence means using several engines, each for what it does best: a relational database as the system of record for orders and payments, Redis for sessions and caching, a search engine for text queries, a graph database for recommendations. The trade-off is operational: more systems to run, back up, secure and keep in sync. The usual tool for sync is an event stream that carries each change from the primary database to the others. Whatever you add, decide which store is the source of truth and treat the others as derived copies that can be rebuilt.

The second runnable stand-in shows the idea in miniature: a key-value cache in front of a relational table, where the cache stores a prepared JSON answer.

SQL Cache table next to the source of truth

Press Run (or Ctrl + Enter) to see the result here.

The two columns differ after the update: the cached copy still says 4.5 and the truth says 5.0. Keeping derived copies fresh (expiry times, invalidation on write) is the main cost of this design.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which NoSQL family is the best fit for the question "who are the friends of friends of Ana?"

    Relationship traversal over several hops is exactly what graph databases are built for. In other models each hop needs a join or extra lookups.
  2. 2. In a document store, what does "embedding" mean?

    Embedding keeps data that is read together in one document, for example order lines inside an order. It trades some duplication for fewer lookups.
  3. 3. Why must a key-value store's access patterns be known in advance?

    The database does not look inside values, so a question such as "all sessions of Ana" needs a second key you maintain yourself.
  4. 4. What is polyglot persistence?

    Polyglot persistence mixes engines, for example relational plus cache plus search. It needs a clear source of truth, and the other stores are derived copies.
  5. 5. In the JSON example, what does CHECK (json_valid(doc)) show?

    Flexible records are not checked by default. Validation can live in the database (as here) or in the application.

Summary

  • NoSQL means "not only SQL": several data models that trade relational features for scale, speed or fit.
  • Key-value stores give fast lookup by key; document stores keep nested JSON-like records; wide-column stores partition data by key and model one table per query; graph stores make relationships first-class.
  • Document modelling favours embedding data that is read together, at the price of duplication.
  • SQLite's JSON functions (json_extract, json_each, json_set) and recursive CTEs imitate documents and graphs, but they are stand-ins, not the real engines.
  • Choose by access pattern. A relational database is a safe default; add specialised stores for measured needs.
  • Polyglot persistence uses several engines, with one source of truth and derived copies.

Next: OLTP vs OLAP, Columnar Storage and Data Warehouses.