Skip to content
Tutorials Center
My learning

Topics

Lesson 28 of 30 ยท Practice

Cassandra, Neo4j, or something else?

A practical decision guide for Cassandra, Neo4j and relational databases: workloads, scaling, hybrid designs, common mistakes and migrating from SQL.

After the projects and exercises, one question remains: when should you actually use these databases? The honest answer is that most applications should start with a relational database. Cassandra and Neo4j are specialists. Each is excellent for a particular shape of problem and a poor fit for others, and picking one because it is fashionable is among the most expensive mistakes in data architecture.

This lesson gives you a decision method: describe the workload first, then match it to a database. You will see the shapes where Cassandra wins, the shapes where Neo4j wins, why a relational database is often the right default, how to combine them, and how to move data over from SQL. The stand-ins here are small programs you can run: a toy workload scorer and a rough capacity estimator. They help you structure your thinking. They are not benchmarks, and the numbers in them are assumptions you should replace with your own measurements.

Start from the workload, not the product

Before you compare databases, answer these questions about your data and traffic:

  1. What are the main queries? Lookups by key, time slices, ad-hoc reports, or multi-hop relationship questions?
  2. How fast does the data grow, and how much is written per second?
  3. How much consistency do you need? Is a briefly stale read acceptable? Do you need multi-row transactions?
  4. How will it scale? One big server for years, or many servers, maybe in several regions?
  5. Is the access pattern stable? Do you know all the queries in advance, or will analysts invent new ones?

The answers point to a family of databases, as you saw in The NoSQL families and when to choose them.

flowchart TD
    S["What is your main question?"] --> A{"Mostly multi-hop<br/>relationship questions?"}
    A -->|"yes"| G["Graph database<br/>(Neo4j)"]
    A -->|"no"| B{"Huge write volume,<br/>known queries,<br/>multi-node or multi-region?"}
    B -->|"yes"| W["Wide-column<br/>(Cassandra)"]
    B -->|"no"| C{"Ad-hoc queries,<br/>joins, transactions?"}
    C -->|"yes"| R["Relational<br/>(PostgreSQL, MySQL)"]
    C -->|"no"| D["Document, key-value<br/>or a relational default"]

A first-pass decision tree. Real choices have more branches, but the order of the questions is the useful part.

When Cassandra is a good fit

Cassandra is built for large, always-on, write-heavy systems whose queries you know in advance. Typical signs:

  • A continuous stream of writes: events, messages, sensor readings, activity feeds, audit trails.
  • Reads that are lookups of a key or a time slice of a partition: "the latest 50 for this user".
  • A need to keep working through the loss of machines or a whole data center, with data replicated across regions.
  • Data volumes large enough that one machine, even a large one, is not the plan.
  • Predictable queries, so that you can design one table per query (Query-first data modeling).

It is a poor fit when you need joins, flexible ad-hoc filtering, strong multi-row transactions, or frequent updates and deletes of the same rows (tombstones and compaction cost add up). A small dataset that fits comfortably on one server gets all of Cassandra's operational complexity and none of its benefit.

When Neo4j is a good fit

Neo4j is built for connected data where the relationships carry the meaning, and the questions are about paths and neighborhoods:

  • Social networks: mutual friends, suggestions, influence.
  • Recommendations: people who liked this also liked that.
  • Fraud and identity: accounts sharing phones, addresses or devices.
  • Networks and dependencies: IT assets, supply chains, permissions, routes.
  • Knowledge graphs (Project: a knowledge graph).

The signal is a question like "within four hops" or "what connects these two things". In SQL that would be a self-join per hop, and the cost grows quickly with depth; in a graph the traversal starts at one node and follows its relationships. It is a weaker fit for bulk aggregation across the whole dataset, for very high write rates of simple records, and for data with few relationships. Scaling a single graph across many machines is also harder than scaling Cassandra, because graphs are hard to cut without breaking relationships. Check the current documentation of the version you plan to use for its clustering and sharding options.

When a relational database is still the answer

PostgreSQL or MySQL handle more than most teams expect: tens of thousands of simple queries per second on one machine, replication for reads, partitioning for big tables, JSON columns, full-text search, and recursive queries for modest graphs. They give you transactions, constraints and joins, and analysts can ask new questions without a redesign. If the data fits on one server (and a server can be large), and you have no special availability or scaling requirement, a relational database is simpler, cheaper to run and easier to hire for. See Normalization for the modeling style and Joins for how relational queries combine tables.

A good rule is: adopt a specialist database when a measured problem with the general-purpose one demands it, not before.

Stand-in 1: a workload scorer

The program below turns your answers into a rough ranking. Each workload trait is a number from 0 to 3, and each database has a hand-written profile of how well it serves that trait. It is a thinking aid with deliberately simple arithmetic: change the profiles and you change the answer, which is the point. Nobody should pick a database from a score like this.

JS A toy workload scorer

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

No output.

The three workloads land on three different winners, which is what you expect from a sensible decision method. Look at the HR tool: Cassandra scores low because its strengths (write volume, many regions) are traits that tool does not need, while its weak spots (ad-hoc queries, transactions) are exactly what it does need.

Scaling: capacity thinking

Both databases scale, in different ways. Cassandra scales out by adding nodes: data and traffic are spread over the ring, and capacity grows roughly in step with node count. Neo4j scales reads by adding read replicas; a single large machine serves the writes, and very large graphs need careful planning. Whatever you pick, estimate before you buy.

The stand-in below estimates the number of Cassandra nodes needed from data size, write rate and replication factor. The per-node limits are assumptions you must replace with numbers from your own load tests; they are not product limits.

Python A rough Cassandra capacity estimate

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

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The "Small app" row is the one to remember. Replication factor 3 means at least three nodes before the first row is stored, so a tiny dataset pays the full operational price. In the other two rows, data volume sets the node count under these assumptions (48 nodes by data against 9 by writes, and 360 against 120); change the write rate or the per-node write limit and the writes column can take over. Keep in mind that this arithmetic ignores compaction headroom, repair traffic and read load, which push the real numbers up.

Hybrid architectures

Large systems rarely use one database. A common pattern is a relational system of record with specialist stores fed from it:

flowchart LR
    App["Application"] --> PG["PostgreSQL<br/>orders, customers<br/>(system of record)"]
    App --> CS["Cassandra<br/>events, timelines"]
    PG -->|"change stream"| Q["Message queue"]
    Q --> N4["Neo4j<br/>relationship queries"]
    Q --> SE["Search index"]

One source of truth, several derived stores, each optimized for one kind of question.

The risks are real: more systems to operate, data that can lag behind the source, and duplicated business rules. Reduce them by choosing one system of record, treating the others as rebuildable projections, and making the feed idempotent (a replayed message must not corrupt anything). That is the same discipline you saw in the order store project.

Migrating from SQL

Moving from a relational database means changing how you think, not just the syntax.

In SQL you have In Cassandra In Neo4j
A table per entity A table per query A node label per entity
Foreign keys and joins Duplicated data, read by key Relationships traversed from a node
Normalized data Denormalized data Normalized, with relationships as first-class data
Ad-hoc WHERE on any column WHERE on the key only Index lookup, then patterns
Transactions across tables Single-partition atomicity, LWT for conditions ACID transactions

Practical steps:

  1. List the queries the application really runs (query logs help) before touching the target.
  2. Design the target model from those queries. For Cassandra, one table per query; for Neo4j, turn join tables into relationships (a table follows(user_id, followed_id) becomes a FOLLOWS relationship, and its extra columns become relationship properties).
  3. Migrate in slices. Pick one feature (the activity feed, the recommendations), build it on the new store and keep the rest in SQL.
  4. Dual-write or stream, then compare. Write to both, compare results, then switch reads.
  5. Keep the old system until you trust the new one, and practice a rollback.

Common mistakes

  • Choosing by hype or rรฉsumรฉ rather than by the measured workload.
  • Using Cassandra as a relational database: secondary indexes everywhere, ALLOW FILTERING, unbounded partitions, and heavy delete patterns.
  • Using Neo4j as a key-value store for records with no relationships, or loading every attribute as a node.
  • Skipping the data model. In both systems the model is the performance plan (Graph data modeling and Partition keys and clustering keys).
  • Ignoring operations: repairs, backups, monitoring and upgrades need an owner. A managed service can take much of this on.
  • Premature scale: designing for a billion users before you have a thousand.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which workload most clearly points toward Cassandra?

    High write volume, known key-and-time queries and replication across regions are Cassandra's strengths.
  2. 2. Which question is a strong signal for a graph database?

    Multi-hop relationship questions are what graph traversal is built for; the other three are key lookups or aggregations.
  3. 3. Why is a relational database often the best default?

    Unless you have a measured need for a specialist, the flexibility and simplicity of a relational database usually wins.
  4. 4. In a hybrid architecture, what makes the derived stores safe to rebuild?

    If one store is the truth and the others are fed idempotently from it, a derived store can be replayed or rebuilt without data loss.

Summary

  • Choose from the workload: main queries, write volume, consistency needs, scaling plan and stability of access patterns.
  • Cassandra fits write-heavy, always-on, known-query systems; Neo4j fits multi-hop relationship questions; a relational database fits most everything else.
  • A replication factor of 3 means at least three Cassandra nodes, so small datasets do not benefit.
  • Hybrid designs use one system of record and rebuildable, idempotently fed specialist stores.
  • Migrate from SQL by listing queries, redesigning the model, moving one feature at a time and keeping a rollback.

Next: Interview questions and answers.