Skip to content

feat(export): stream graph.json into the DB push — kill the load-time memory wall - #9

Merged
Azeem1985 merged 1 commit into
v8from
feat/streaming-db-push
Aug 24, 2026
Merged

Azeem1985 merged 1 commit into
v8from
feat/streaming-db-push

Conversation

@Azeem1985

Copy link
Copy Markdown

The OOM evidence

graphify export falkordb --push --batch-size 500 on a real 1.87GB graph.json was OOM-killed during LOADING on a 7.6GB box (unit capped at 6G RAM + 5G swap). An earlier run measured the pusher at ~5.3GB peak. Root cause: the CLI json.loads-ed the entire file and rebuilt a full NetworkX graph before push_to_falkordb/push_to_neo4j ever iterated it — but a push never needs the graph object; it only walks nodes, then edges.

The fix

For the --push paths (falkordb + neo4j), graph.json is now stream-parsed by a stdlib-only incremental reader for the known node-link shape (ijson is not a dependency, so none was added): one fast offset scan locating the top-level "nodes" and "links"/legacy "edges" arrays (plus the multigraph flag), then one row pass per array — each with a fixed 64KiB buffer — feeding batches straight to the existing batched UNWIND writers. No NetworkX graph is ever constructed on a push.

Before / after memory

peak (tracemalloc, same 30.5MB / 50k-node / 50k-link file)
in-memory push (old) 135.4MB (~4.4x file size — scales with the graph)
streaming push (new) 0.66MB (~2% of file size — batch-scale, flat in graph size)

~204x less. Extrapolated to the production 1.87GB file, the old path's ~4.4x factor reproduces the measured ~5.3GB peak; the streaming path stays at batch scale regardless of file size.

Parity kept (pinned by tests against the old path)

  • identical Cypher text and row payload shapes; per-label/relation row order and chunking preserved
  • nodes fully precede edges — even when "links" physically precedes "nodes" in the file (offsets are scanned first)
  • --batch-size, URI/auth parsing, upsert (MERGE) semantics, returned counts: unchanged
  • GRAPHIFY_MAX_GRAPH_BYTES still caps the push path — now guarding disk-read volume, not RAM (noted in code)
  • the no---push cypher.txt route keeps the old in-memory code on purpose
  • only cross-label batch interleaving differs, which MERGE makes irrelevant

Tests

tests/test_streaming_push.py (12 new): end-state parity vs the in-memory push with fake drivers (rows, order, chunking, Cypher text, counts, communities-sidecar override), nodes-before-edges ordering with a links-first file, legacy "edges" key, empty graph, tracemalloc assertion on a 50k-node file (peak < 8MB and < file/4; measured 0.66MB), malformed-file error tone (library + CLI), CLI streams without touching node_link_graph, --password still required for neo4j.

Full sweep: test_streaming_push + test_batched_push + test_extract_cli + test_cli_export + test_falkordb_integration + test_export → 159 passed, 1 skipped (live-server integration test). ruff and py_compile clean.

🤖 Generated with Claude Code

… memory wall

A --push never needs the graph object: it only iterates nodes, then
edges. Yet the CLI json.loads-ed the ENTIRE graph.json and rebuilt a
full NetworkX graph before push_to_falkordb/neo4j walked it. On a real
1.87GB graph.json that load was OOM-killed on a 7.6GB box (unit capped
6G RAM + 5G swap) before a single row went out; an earlier run measured
the pusher at ~5.3GB peak.

The --push paths (falkordb + neo4j) now stream-parse the file with a
stdlib-only incremental reader for the known node-link shape (ijson is
not a dependency): one fast offset scan, then one row pass per array,
each with a fixed 64KiB buffer, feeding batches straight to the existing
batched UNWIND writers without constructing NetworkX. Measured on a
30.5MB / 50k-node synthetic file: 0.66MB tracemalloc peak vs 135.4MB
for the old path (~204x, ~2% of file size, flat in graph size).

Wire parity is pinned by tests against the in-memory push: identical
Cypher text, identical row payloads, per-label/relation row order and
chunking preserved, nodes fully precede edges (even when "links"
physically precedes "nodes" in the file - offsets are scanned first),
legacy "edges" key honoured, MERGE upserts idempotent, --batch-size and
URI/auth parsing unchanged. Only cross-label batch interleaving differs,
which MERGE makes irrelevant. The GRAPHIFY_MAX_GRAPH_BYTES cap still
guards the push path - now bounding disk read, not RAM. The no-push
cypher.txt route keeps the old in-memory code on purpose.

Co-Authored-By: Claude Fable 5 <[email protected]>
@Azeem1985
Azeem1985 merged commit c6d092a into v8 Aug 24, 2026

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

yield rel, {"src": item["source"], "tgt": item["target"], "props": props}

P1 Badge Avoid reversing existing undirected database edges

For graphify's default directed: false output, the old path rebuilt an nx.Graph, whose G.edges() emits each pair according to node insertion order rather than the stored source/target order. This line now sends the raw endpoints instead, so a stored link such as z -> a can change from the old pushed a -> z to z -> a; rerunning against an existing database then creates the opposite relationship while leaving the previous one in place. Either preserve the old orientation or explicitly migrate/remove the stale relationships before claiming wire parity.


bucket = pending.setdefault(group, [])

P2 Badge Bound the total pending-row buffer across groups

When a graph contains many distinct sanitized labels or relationship types, each group can remain below batch_size, so pending retains one or more rows for every group until EOF. With unique relations per edge, this again holds the entire edge set in memory and can reproduce the OOM that the streaming path is intended to eliminate; impose a global pending-row bound or flush partial buckets as that bound is reached.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant