Skip to content
Tutorials Center
My learning

Topics

Lesson 1 of 30 ยท Wide-Column and Graph Databases

The NoSQL families and when to choose them

Tour the four NoSQL families, learn CAP and PACELC in plain language, and see when a wide-column or graph database beats a relational one.

"NoSQL" is not one technology. It is a label for databases that chose a different shape for your data than the relational table, and each shape makes some questions cheap and others painful. There are four main families: key-value, document, wide-column and graph. This track teaches two of them in depth: Apache Cassandra (wide-column) and Neo4j (graph).

Before you learn either, you need a map. Why do these families exist? What do you give up when you leave SQL? And how do you decide, for a real project, whether a table, a map, a partitioned store or a graph fits best? This lesson answers those questions, introduces the two famous trade-off rules called CAP and PACELC in plain language, and ends with small runnable stand-ins.

A note on honesty that applies to every lesson in this track: Cassandra and Neo4j are server programs, and they cannot run inside a web page. Their commands appear in read-only code blocks. Next to them you get runnable stand-ins, small programs written in JavaScript or Python that imitate the idea so you can poke at it. A stand-in shows how a concept works; it is not the real product and makes no claims about real speed.

Why not just use SQL?

A relational database is excellent. It gives you joins, transactions, constraints and a flexible query language, so you can ask questions you did not plan for. If you have not met the design rules behind it, read normalization first: relational design stores each fact once and joins tables back together at query time.

That flexibility has a price when data gets very large. A single machine can only hold so much, and joining tables that live on different machines is slow. The NoSQL families each give up some of the relational toolbox in exchange for something specific:

  • Scale across many machines by only supporting queries that can be answered from one place.
  • A shape that matches the data, so you avoid awkward conversions (a social network is not naturally rows).
  • Predictable speed by making expensive operations impossible to write.

None of this makes SQL obsolete. Most applications should start with a relational database. NoSQL becomes interesting when you can name the specific problem you have.

The four families

flowchart TD
    A["Data model families"] --> KV["Key-value<br/>key to opaque value"]
    A --> DOC["Document<br/>key to nested JSON"]
    A --> WC["Wide-column<br/>partition key to sorted rows"]
    A --> G["Graph<br/>nodes and relationships"]
    KV --> E1["Redis, DynamoDB"]
    DOC --> E2["MongoDB, CouchDB"]
    WC --> E3["Cassandra, ScyllaDB"]
    G --> E4["Neo4j, Neptune"]

The four NoSQL families and a few well-known products in each.

Key-value. The simplest model: a giant dictionary. You give a key and get a value. Lookups by key are extremely fast and the store scales easily, because any key can live on any machine. You cannot ask "find all values where the age is over 30" without scanning everything. It is the natural idea behind a hash table, spread over many servers.

Document. Like key-value, but the value is a structured document (usually JSON) that the database understands. You can query inside documents and index their fields. It fits data that arrives as nested objects, such as product catalogs or content pages.

Wide-column. Data is divided into partitions by a key, and each partition holds many rows kept in sorted order. Cassandra is the best-known example. It is built for huge write volumes spread over many machines, with queries designed ahead of time: "give me the last 50 events for device 42". You will spend most of this track on it.

Graph. The data is nodes (things) and relationships (connections between things), and the relationships are stored as first-class data. Neo4j is the best-known example. Questions like "who are the friends of my friends who like jazz?" are natural, because following a relationship is a cheap, direct hop instead of a join.

CAP in plain language

When data is copied to several machines, the network between them can fail. Imagine two copies of a record, one in Berlin and one in Tokyo, and the cable between them is cut. A client in each city now talks to its local copy. What should the system do?

  • It can stay consistent: refuse requests that could produce disagreement. Everybody sees the same data, but some requests fail.
  • It can stay available: answer every request using whatever copy is nearby. Everybody gets an answer, but the two copies may disagree for a while.

The CAP theorem says that during such a network partition you cannot have both perfect Consistency and full Availability. Partition tolerance is not optional, because networks do fail, so the real choice is between C and A when something breaks.

CAP is often over-simplified into "pick two of three". A better reading: when the network is healthy you can have both, and when it is broken you must choose. Also remember that consistency in CAP means a strict "every read sees the latest write" guarantee, which is stronger than the "consistency" in ACID.

PACELC: the rule for the normal days too

CAP says nothing about the 99.9% of the time when nothing is broken. PACELC fills the gap. It reads: if there is a Partition, choose A or C; Else (no partition), choose Latency or Consistency.

Even on a healthy day, waiting for every copy to confirm a write makes the write slower. Waiting for fewer copies is faster but a reader might see an older value. So there is a constant dial between speed and strictness.

Cassandra is usually described as PA/EL: it prefers availability and low latency, and it gives you a dial (called a consistency level) to ask for more strictness per query. You will meet that dial in Replication and tunable consistency. A single-node Neo4j database, by contrast, behaves like a classic ACID database. Neo4j clusters add copies for reliability and read scaling while keeping transactions.

What changes when you pick one

Question Relational Key-value Wide-column Graph
Ad hoc queries Excellent Poor Poor, plan them Good for connections
Joins Built in None None Relationship hops
Scale writes across machines Hard Easy Easy Harder
Typical design rule Normalize Choose good keys Design tables per query Model relationships

Read the table as tendencies, not laws. Newer products blur the lines, and every row has exceptions.

Stand-in 1: the same question in three shapes

The program below stores the same tiny "who follows whom" data three ways: a relational-style list of rows, a key-value map, and a graph-style adjacency list. Then it asks "who does Ada follow?" and "who follows Cy?" in each shape and counts how many items it had to look at. The counts are items touched by this toy code, not a benchmark of any real database.

JS One question, three data shapes

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

No output.

The key-value shape is perfect for the question its key was designed for, and falls back to a scan for the reverse question. The graph shape stores both directions, so both questions are cheap. That is the central lesson of NoSQL: the shape of the data decides which questions are cheap.

Stand-in 2: choosing consistency or availability

This program has two copies of one value and a broken link between them. A "CP" store refuses a write it cannot copy everywhere. An "AP" store accepts the write on the copy it can reach, and a reader on the other side then sees stale data until the link heals.

JS A network partition, two policies

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

No output.

The CP store keeps both cities in agreement at the cost of an error. The AP store answered everyone, and Tokyo served the old price until the link healed. Neither is wrong; they are answers to different business needs. A bank balance usually wants the first, a "likes" counter the second.

How to choose

Start from your questions, not from the product:

  1. Do you need ad hoc queries, joins and transactions across many tables? Use a relational database.
  2. Do you only ever fetch by one key? Key-value fits.
  3. Is each record naturally one nested object? Document.
  4. Do you write enormous volumes, spread across regions, and know your queries in advance? Wide-column, such as Cassandra.
  5. Are the connections themselves the interesting part, with questions several hops deep? Graph, such as Neo4j.

Many real systems use more than one: a relational database for orders, Cassandra for activity history, a graph for recommendations. The last lessons of this track, Cassandra, Neo4j, or something else?, return to this decision with more experience.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which family stores relationships between things as first-class data?

    Graph databases such as Neo4j store nodes and relationships directly, so following a connection is a cheap hop.
  2. 2. During a network partition, a system that keeps answering every request even though copies may disagree has chosen...

    CAP says that when the network splits you either refuse some requests (consistency) or answer all of them with possibly stale data (availability).
  3. 3. What does the E in PACELC add that CAP does not cover?

    PACELC says that even without a partition there is a trade-off: waiting for more copies is more consistent but slower.
  4. 4. Why is a plain key-value store poor at 'find all users older than 30'?

    Key-value stores are fast for fetching by key, but any question that is not about the key has to look at every value unless you build your own index.

Summary

  • NoSQL means several data shapes: key-value, document, wide-column and graph. Each makes some questions cheap and others expensive.
  • Relational databases remain the right default; pick a NoSQL family when you can name the problem it solves.
  • CAP: during a network partition you choose between consistency and availability. PACELC adds the latency versus consistency dial for normal days.
  • Cassandra is wide-column and leans toward availability and low latency; Neo4j is a graph database built for relationship questions.
  • In this track the real products are shown in read-only blocks, and stand-ins in JavaScript and Python show how the ideas work.

Next: Cassandra architecture in one picture.