Backup, Restore, Import, Clustering and Routing

Article · 8:00

Backup and restore for both editions, the bulk import tools, clustering with Raft, routing and read replicas, and the edition boundary.

Backup depends on the edition, and the difference matters operationally. Community Edition has offline neo4j-admin database dump and load, which require the database to be stopped - so your RTO includes a shutdown, and you cannot take a backup without an outage unless you have a replica to dump. Enterprise and Aura have neo4j-admin database backup, which runs online and supports incremental backups, plus neo4j-admin database check to verify a backup's consistency before you need it.

The honest consequence: a single-instance Community deployment cannot be backed up without downtime, and the usual mitigation is a second instance kept in sync by replaying the transaction log or by periodic dumps during a maintenance window. That is a real constraint and belongs in a deployment decision, not a footnote.

Bulk import has two tools and they are not interchangeable.

neo4j-admin database import full builds a store from CSV offline, into an empty database, and it is fast - billions of relationships in hours. It is how you do an initial load. It cannot add to an existing database (though import incremental exists in Enterprise).

LOAD CSV runs as a Cypher query against a live database, which makes it right for incremental loads and wrong for the initial one. The rules for using it without exhausting the heap: use CALL { ... } IN TRANSACTIONS, create your constraints before loading so MERGE has an index, and set MERGE on identifying properties only.

Clustering (Enterprise and Aura) uses Raft. Primaries participate in consensus; a write is acknowledged when a majority of primaries have it. Secondaries are asynchronous read-only copies that do not vote, which is how you scale reads. The arithmetic: three primaries survive one failure, five survive two, and losing a majority of primaries means no writes - the cluster stays readable and refuses writes, which is the consistency-over-availability choice.

Routing is the driver's job and it is important. The neo4j:// scheme makes the driver fetch a routing table and send writes to the leader and reads to followers or secondaries. The bolt:// scheme connects to one instance with no routing - which works and means a leader election leaves your application talking to a node that will refuse writes. Using neo4j:// and execute_read/execute_write correctly is what makes failover transparent.

Consistency in a cluster: a write is acknowledged by a majority, but a read from a follower or secondary may not include it yet. For read-your-writes, use bookmarks - the driver tracks them automatically within a session, and across sessions you pass them explicitly. That is the mechanism, and an application that reads from replicas without bookmarks will occasionally not see its own write.

Laboratory exercise 5 - break it, then get it back.

On Community Edition, take a dump and restore it, timing the required downtime and recording RPO and RTO. Then state what that means for a production deployment.

Perform an initial bulk load of at least ten million nodes with neo4j-admin database import, timing it. Then load the same data with LOAD CSV and compare - the difference is the point.

Demonstrate the LOAD CSV failure modes: load without creating constraints first and measure the slowdown; load without IN TRANSACTIONS and exhaust the heap. Then do it correctly.

Perform a destructive change at a recorded time, restore, and verify with assertions - node and relationship counts by label and type, plus your capstone invariants.

For clustering, if you have Enterprise or Aura: build a three-primary cluster, kill the leader under write load and measure the election and the failed-write window; kill two and show writes refused while reads continue; and demonstrate the bolt://-against-neo4j:// difference by failing over with each. Then demonstrate the stale read: write and immediately read from a secondary without a bookmark, and record the miss rate; then with a bookmark, and show zero.

If you cannot run a cluster, produce the design exercise: topology, primary and secondary counts, the routing configuration, the bookmark strategy, the failure matrix, and the Aura-against-self-managed comparison.

Deliverable: the Community dump and restore with measured downtime, the two import tools compared, both LOAD CSV failure modes, the restore verified by assertions, and either the cluster measurements including the stale-read miss rate or the full design exercise.

// ==================================================================
// *** BACKUP DEPENDS ON THE EDITION, and it matters. ***
// ==================================================================
// COMMUNITY: OFFLINE only.
//   neo4j stop
//   neo4j-admin database dump neo4j --to-path=/backups
//   neo4j start
//   -- restore:
//   neo4j stop
//   neo4j-admin database load neo4j --from-path=/backups --overwrite-destination
//   neo4j start
//
// *** SO A SINGLE-INSTANCE COMMUNITY DEPLOYMENT CANNOT BE BACKED UP
// *** WITHOUT DOWNTIME. *** The usual mitigation is a second
// instance kept in sync, dumped instead. That is a REAL CONSTRAINT
// and belongs in the deployment decision, not a footnote. TIME THE
// DOWNTIME in the lab; it is your RTO floor.

// ENTERPRISE / AURA: ONLINE, and incremental.
//   neo4j-admin database backup neo4j --to-path=/backups --type=full
//   neo4j-admin database backup neo4j --to-path=/backups --type=diff
//   neo4j-admin database check neo4j --from-path=/backups
//                        ^^^^^ VERIFY THE BACKUP BEFORE YOU NEED IT.
//   neo4j-admin database restore neo4j --from-path=/backups/neo4j-2026-11-30

SHOW DATABASES;
// Enterprise supports several databases per instance, which is also
// how you restore alongside and cut over:
// CREATE DATABASE restored;
// START DATABASE restored;

// ==================================================================
// BULK IMPORT: TWO TOOLS, NOT INTERCHANGEABLE.
// ==================================================================
// 1. neo4j-admin database import - OFFLINE, INTO AN EMPTY DATABASE,
//    and FAST. Billions of relationships in hours. This is how you
//    do an INITIAL load.
//
//   neo4j stop
//   neo4j-admin database import full neo4j \
//     --nodes=Person=persons_header.csv,persons.csv \
//     --nodes=Account=accounts_header.csv,accounts.csv \
//     --relationships=OWNS=owns_header.csv,owns.csv \
//     --relationships=TRANSFERRED=transfers_header.csv,transfers.csv \
//     --skip-bad-relationships --skip-duplicate-nodes \
//     --high-parallel-io=on
//   neo4j start
//
//   headers:  id:ID(Person),name,email:string
//             :START_ID(Person),:END_ID(Account),since:date
//
//   It CANNOT add to an existing database. (Enterprise has
//   `import incremental` for that.)

// 2. LOAD CSV - a Cypher query against a LIVE database. Right for
//    INCREMENTAL loads, WRONG for the initial one.
//
// *** CREATE THE CONSTRAINTS FIRST. *** Without an index, every
// MERGE scans - a scan PER ROW, which is quadratic and looks like
// "the import got slow":
CREATE CONSTRAINT person_id IF NOT EXISTS
  FOR (p:Person) REQUIRE p.id IS UNIQUE;
CALL db.awaitIndexes(600);

LOAD CSV WITH HEADERS FROM 'file:///persons.csv' AS row
CALL {
  WITH row
  MERGE (p:Person {id: row.id})           // IDENTIFYING property only
    ON CREATE SET p.name = row.name, p.email = row.email
    ON MATCH  SET p.email = row.email
} IN TRANSACTIONS OF 10000 ROWS;
//  ^^^ WITHOUT THIS, the whole file is ONE TRANSACTION and the heap
//      is gone. Demonstrate both failures, then the correct form.

LOAD CSV WITH HEADERS FROM 'file:///transfers.csv' AS row
CALL {
  WITH row
  MATCH (a:Account {id: row.from}), (b:Account {id: row.to})
  CREATE (a)-[:TRANSFERRED {amount: toFloat(row.amount),
                            at: datetime(row.at)}]->(b)
} IN TRANSACTIONS OF 10000 ROWS;
//  MATCH, not MERGE, for the endpoints - MERGE on a relationship
//  pattern would create duplicate accounts (Module 2, trap 2).
//  And CREATE rather than MERGE for the relationship, because two
//  genuine transfers of the same amount at the same time are two
//  transfers.

// ==================================================================
// CLUSTERING (Enterprise / Aura): RAFT.
// ==================================================================
//   PRIMARIES participate in consensus. A write is acknowledged when
//   a MAJORITY of primaries have it.
//   SECONDARIES are ASYNCHRONOUS read-only copies that DO NOT VOTE -
//   which is how you scale reads.
//
//   3 primaries survive 1 failure.  5 survive 2.
//   *** LOSING A MAJORITY OF PRIMARIES MEANS NO WRITES *** - the
//   cluster stays READABLE and REFUSES WRITES. Consistency over
//   availability, deliberately.

SHOW SERVERS;
SHOW DATABASE neo4j;         // per-server role: PRIMARY / SECONDARY
CALL dbms.cluster.overview();
CALL dbms.routing.getRoutingTable({}, 'neo4j');

// ==================================================================
// *** ROUTING IS THE DRIVER'S JOB, AND IT IS IMPORTANT. ***
// ==================================================================
//   neo4j://host:7687    the driver FETCHES A ROUTING TABLE and
//                        sends WRITES to the leader, READS to
//                        followers and secondaries. Failover is
//                        TRANSPARENT.
//
//   bolt://host:7687     ONE instance, NO routing. It works - and
//                        *** A LEADER ELECTION LEAVES YOUR
//                        APPLICATION TALKING TO A NODE THAT WILL
//                        REFUSE WRITES. ***
//                        Neo.ClientError.Cluster.NotALeader
//
//   Demonstrate BOTH by failing over with each. Using neo4j:// plus
//   execute_read / execute_write correctly is what makes failover
//   invisible, and it is one line of configuration.

// ==================================================================
// *** CONSISTENCY IN A CLUSTER: BOOKMARKS. ***
// ==================================================================
// A write is acknowledged by a MAJORITY. A READ FROM A FOLLOWER OR
// SECONDARY MAY NOT INCLUDE IT YET.
//
//   write to the leader -> read from a secondary -> NOT FOUND
//
// Within ONE SESSION the driver tracks bookmarks automatically, so
// read-your-writes holds. ACROSS SESSIONS you must pass them:
//   bookmarks = session1.last_bookmarks()
//   session2 = driver.session(bookmarks=bookmarks)
//   -> session2 waits until the server it reads from has caught up
//
// MEASURE THE MISS RATE: write then immediately read from a
// secondary, 10,000 times, WITHOUT a bookmark - record the
// percentage that miss. Then WITH a bookmark: zero. *** That
// percentage is the cost of reading from replicas, in your
// deployment, and an application that does it without bookmarks
// will occasionally not see its own write. ***

// ==================================================================
// VERIFY A RESTORE WITH ASSERTIONS.
// ==================================================================
MATCH (n) RETURN labels(n) AS labels, count(*) AS nodes ORDER BY nodes DESC;
MATCH ()-[r]->() RETURN type(r) AS type, count(*) AS rels ORDER BY rels DESC;
CALL db.schema.visualization();
SHOW CONSTRAINTS;
SHOW INDEXES;
//   ... then the capstone invariants. "The database started" is not
//   verification - and note that a restore does NOT guarantee your
//   indexes finished building, so await them before measuring.
CALL db.awaitIndexes(600);

Discussion

No questions on this topic yet. Sign in to ask the first one.