Q: How does a Neo4j Causal Cluster stay consistent across primaries and secondaries, and what happens during leader election?
Neo4j interview questions on clustering: Raft leader election for primaries, quorum, secondary catch-up, bookmarks and neo4j:// routing in 5.x.
Want to master this scenario in a live sandbox? KodeKloud's PostgreSQL Database Administration & High Availability Course covers this exact problem with hands-on terminal drills.
🛠️ Production Runbook & Step-by-Step Resolution
Direct Answer: Neo4j interview questions about clustering often use the older term Causal Cluster. That name comes from Neo4j 3.x and 4.x, where servers were core members and read replicas. Neo4j 5 describes the same idea as a cluster of servers hosting databases in primary and secondary roles. I answer using the 5.x terms and map them back to the older ones so the interviewer sees I know both. The core of the answer is this. Primaries form a Raft group per database, elect one leader, and commit a transaction only when a majority has it. Secondaries pull committed transactions asynchronously and serve reads, so they can lag. Causal consistency for clients comes from bookmarks, which let a read wait until the server has applied a specific earlier write. Routing sends writes to the leader and spreads reads across the rest. I also describe how to inspect the topology during an incident.
Map the terminology: primaries and secondaries in 5.x
In Neo4j 5, each database is hosted on a set of servers, with a chosen number of primaries and secondaries. Primaries take part in Raft and accept writes through the leader. Secondaries replicate from primaries asynchronously and serve read queries. In 4.x and 3.x the same roles were called core servers and read replicas. I create databases with an explicit topology so the roles are intentional.
// Neo4j 5: host a database on 3 primaries and 2 secondaries
CREATE DATABASE orders TOPOLOGY 3 PRIMARIES 2 SECONDARIES;
SHOW SERVERS;
SHOW DATABASES YIELD name, address, role, currentStatus;
// orders | server1:7687 | primary | online
// orders | server4:7687 | secondary | online
Raft consensus, quorum and leader election for primaries
Each database has its own Raft group across its primaries. One primary is the leader. A write is committed only after a majority of primaries have appended it to their Raft log, so with 3 primaries I can lose 1 and still commit, and with 5 I can lose 2. If the leader fails, followers wait for an election timeout, a candidate requests votes for a new term, and the node with a majority becomes leader. Without a majority, the database becomes read-only or unavailable for writes, which protects against split brain.
// Which server leads each database right now
SHOW DATABASES YIELD name, address, role, writer
WHERE role = 'primary';
// writer = true marks the leader that accepts writes
// Neo4j 4.x equivalent
// CALL dbms.cluster.overview();
How secondaries catch up and why they can lag
Secondaries do not vote and do not slow commits. They fetch committed transactions from primaries on a pull schedule and apply them in order. This keeps write latency low and lets me add many read servers, but a read on a secondary can miss a write that was committed a moment ago. I watch for lag in monitoring metrics and treat a persistently lagging secondary as a capacity or network issue, not a correctness bug.
// Check that secondaries are online and hosting the database
SHOW DATABASES YIELD name, address, role, currentStatus, statusMessage
WHERE role = 'secondary';
// Detailed metrics are exposed via Prometheus if enabled in neo4j.conf
// server.metrics.prometheus.enabled=true
Use bookmarks to get causal consistency in the application
A bookmark identifies the last transaction a session committed. When a later session passes that bookmark, the server it connects to waits until it has applied that transaction before running the query. That gives read-your-own-writes even when the read lands on a secondary. Drivers chain bookmarks automatically inside a single session, and I pass them explicitly between separate sessions.
from neo4j import GraphDatabase
driver = GraphDatabase.driver('neo4j://cluster.example.internal:7687', auth=('neo4j', 'secret'))
with driver.session(database='orders') as s1:
s1.execute_write(lambda tx: tx.run('CREATE (:Order {id: $id})', id=1001))
bookmarks = s1.last_bookmarks()
with driver.session(database='orders', bookmarks=bookmarks) as s2:
print(s2.execute_read(lambda tx: tx.run('MATCH (o:Order {id: 1001}) RETURN o.id').single()[0]))
Route traffic with the neo4j:// scheme and inspect the routing table
The neo4j:// URI scheme makes the driver fetch a routing table from the cluster. Writes go to the leader, reads are spread across eligible servers, and the driver refreshes the table when topology changes. A bolt:// URI connects to a single server and gives no failover, so I use it only for admin tasks on a specific node. I can check what routing hands out with the routing procedure.
// Show the routing table a driver would receive for a database
CALL dbms.cluster.routing.getRoutingTable({}, 'orders');
// returns servers grouped as WRITE, READ and ROUTE
# Connection strings
# neo4j://cluster.example.internal:7687 -> routing, failover aware
# bolt://server1.example.internal:7687 -> one node only
- Neo4j 5 calls the roles primaries and secondaries, which replace the core servers and read replicas of Causal Clustering in 3.x and 4.x.
- Primaries form a Raft group per database, elect a leader, and commit writes when a majority has them.
- Secondaries pull committed transactions asynchronously, so they scale reads but can lag slightly.
- Bookmarks make a later read wait for an earlier write, and the neo4j:// scheme routes writes to the leader.
Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:
- Running an even number of primaries A 4-primary group tolerates only 1 failure, the same as 3, so the extra server adds cost without more fault tolerance.
- Using bolt:// in application connection strings for a cluster The driver pins to one server and cannot follow leader changes, so a failover becomes an application outage.
- Assuming reads from secondaries are always current Secondaries replicate asynchronously, so without bookmarks a read can miss a recent write.
- Spreading primaries across two sites with no third site Losing the site with the majority removes quorum, and the database stops accepting writes.