Q: How do you diagnose a runaway Neo4j Cypher query that is causing Java heap OutOfMemory errors?
Neo4j interview questions on runaway Cypher: Cartesian products, EXPLAIN vs PROFILE, indexes, transaction memory limits and TERMINATE TRANSACTIONS.
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 memory trouble usually come down to one query with an unbounded intermediate result. The classic cause is a Cartesian product: two MATCH patterns with no relationship between them, so the planner pairs every row from one with every row from the other. Ten thousand nodes against ten thousand nodes becomes one hundred million rows, and the transaction runs out of heap. I describe the response in three layers. First, stop the damage by finding and terminating the offending transaction. Second, understand the cause by reading the query plan. Third, prevent a repeat with indexes, rewritten queries and transaction memory limits. I also separate heap used by running queries from the page cache, which is off-heap storage cache and is tuned separately. Setting names differ between 4.x and 5.x, and I call that out when I give them.
Find and stop the runaway transaction
The first move is to see what is running and how much memory it holds. SHOW TRANSACTIONS lists running transactions, including the current query, elapsed time and allocated bytes on recent versions. TERMINATE TRANSACTIONS stops one by id. In 4.x, SHOW TRANSACTIONS exists from 4.3 onwards, and older versions used dbms.listQueries and dbms.killQuery. This is an incident step, so I record the query text before killing it.
// Neo4j 5 (also 4.3+ for SHOW TRANSACTIONS)
SHOW TRANSACTIONS YIELD transactionId, currentQuery, elapsedTime, allocatedBytes
ORDER BY allocatedBytes DESC LIMIT 5;
TERMINATE TRANSACTIONS 'neo4j-transaction-1234';
// Older 4.x without SHOW TRANSACTIONS
// CALL dbms.listQueries(); CALL dbms.killQuery('query-123');
Use EXPLAIN first, because PROFILE actually runs the query
EXPLAIN builds the plan without executing, so it is safe on a suspect query. PROFILE executes the query and reports real row counts and database hits, which can recreate the outage if I run it on the bad query against production data. A CartesianProduct operator in the plan, with a very large estimated row count, is the signal. I run EXPLAIN on the live system and PROFILE only on a small copy or with a LIMIT.
EXPLAIN
MATCH (c:Customer), (o:Order)
WHERE c.region = 'EU' AND o.status = 'OPEN'
RETURN c.id, o.id;
// Plan shows: CartesianProduct <- NodeByLabelScan (c) , NodeByLabelScan (o)
// Safe execution on a bounded sample
PROFILE MATCH (c:Customer) WITH c LIMIT 100 MATCH (c)-[:PLACED]->(o:Order) RETURN count(o);
Rewrite disconnected patterns and add indexes
The fix is usually to connect the patterns with a relationship so the planner expands from one node to its neighbours instead of pairing everything. A NodeByLabelScan reads every node with a label, so I add an index on the property I filter by and the planner can use NodeIndexSeek. In some cases I can force the choice with USING INDEX, but I check the plan first, since hints can make plans worse as data changes.
CREATE INDEX customer_region IF NOT EXISTS FOR (c:Customer) ON (c.region);
CREATE INDEX order_status IF NOT EXISTS FOR (o:Order) ON (o.status);
PROFILE
MATCH (c:Customer)-[:PLACED]->(o:Order)
USING INDEX c:Customer(region)
WHERE c.region = 'EU' AND o.status = 'OPEN'
RETURN c.id, o.id;
// Expect NodeIndexSeek + Expand(All), no CartesianProduct
Set transaction memory limits so one query cannot take the server
Neo4j can cap memory per transaction. In Neo4j 5, db.memory.transaction.max limits a single transaction, db.memory.transaction.total.max limits the total per database, and dbms.memory.transaction.total.max limits the whole instance. A transaction that exceeds its cap fails with a memory limit error instead of taking the JVM down. In 4.x the equivalent names are dbms.memory.transaction.max_size and dbms.memory.transaction.global_max_size, so I check the manual for the exact version before editing neo4j.conf.
# neo4j.conf (Neo4j 5)
db.memory.transaction.max=2GiB
db.memory.transaction.total.max=8GiB
dbms.memory.transaction.total.max=12GiB
# Neo4j 4.x equivalents
# dbms.memory.transaction.max_size=2GiB
# dbms.memory.transaction.global_max_size=12GiB
Size the heap and page cache from data, not guesses
Heap holds query execution state, and the page cache holds graph store files off-heap, so growing the heap to fix a bad query starves the page cache and slows everything. I use the memory recommendation tool for a starting point, then set initial and max heap to the same value. Neo4j 5 names are server.memory.heap.initial_size and server.memory.heap.max_size, and 4.x uses dbms.memory.heap.initial_size and dbms.memory.heap.max_size.
neo4j-admin server memory-recommendation --memory=32g # Neo4j 5
# neo4j-admin memrec --memory=32g # Neo4j 4.x
# neo4j.conf (Neo4j 5)
server.memory.heap.initial_size=12g
server.memory.heap.max_size=12g
server.memory.pagecache.size=14g
- Disconnected MATCH patterns create a Cartesian product, and the intermediate rows exhaust heap.
- I find the transaction with SHOW TRANSACTIONS, terminate it, and read the plan with EXPLAIN because PROFILE runs the query.
- Indexes replace label scans with seeks, and connecting the patterns with a relationship removes the CartesianProduct.
- Transaction memory limits stop one query from crashing the server, and heap is sized separately from the page cache.
Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:
- Running PROFILE on the suspect query in production PROFILE executes the query, so it can trigger the same out-of-memory failure you are investigating.
- Fixing the incident by only increasing heap Larger heap delays the failure and takes memory from the page cache, which slows normal queries.
- Adding USING INDEX hints everywhere Hints freeze a plan choice that may be wrong after data changes, so use them only after checking the plan.
- Ignoring the difference between 4.x and 5.x setting names A wrong name in neo4j.conf can be silently unused or stop startup, depending on version, leaving no memory cap in place.