Library

Volume 2 · Knowledge Graphs

Notebook
Sign in

Section 38

Graph Ingestion Pipelines

720 words · about 3 min

38.1 Batch Ingestion Architecture

┌──────────────┐    ┌────────────┐    ┌───────────────┐    ┌──────────┐
│  Data Sources │───▶│  Extract   │───▶│  Transform    │───▶│  Load    │
│  (APIs, DBs,  │    │  (Connectors│    │  (Entity      │    │  (Neo4j  │
│   Files, Logs)│    │   Parsers)  │    │   Resolution, │    │   Bulk   │
│               │    │             │    │   Validation)  │    │   Import)│
└──────────────┘    └────────────┘    └───────────────┘    └──────────┘

Key design decisions:

  • Idempotent writes — use MERGE (upsert) instead of CREATE, so re-running an ingestion job never duplicates data.
  • Batch sizing — roughly 5,000-10,000 nodes per transaction for Neo4j to balance throughput against lock contention.
  • Node-then-edge ordering — create nodes first, relationships second, so a relationship never references a not-yet-created node.
  • Error handling — dead-letter queue for records that fail validation, rather than dropping or crashing on them.
// Batch upsert entities
UNWIND $services AS service
MERGE (s:Service {service_id: service.id})
SET s.name = service.name,
    s.criticality = service.criticality,
    s.updated_at = datetime()

// Batch upsert relationships
UNWIND $dependencies AS dep
MATCH (source:Service {service_id: dep.source_id})
MATCH (target:Service {service_id: dep.target_id})
MERGE (source)-[r:DEPENDS_ON]->(target)
SET r.type = dep.dependency_type,
    r.criticality = dep.criticality,
    r.updated_at = datetime()

38.2 Streaming Ingestion

For dynamic knowledge graphs (e.g., AIOps or real-time operational graphs):

  • Kafka → Graph connector — a custom consumer writes events to the graph as they arrive.
  • Change Data Capture (CDC) — a tool like Debezium captures upstream database changes and transforms them into graph mutations.
  • Event-driven updates — service-mesh or platform events (deploys, scaling actions) auto-update the graph in near-real time.
  • Conflict resolution — timestamp-based last-write-wins is the common default for concurrent updates to the same node/edge; use vector clocks or explicit merge logic where last-write-wins would lose meaningful information.

38.3 Pipeline for 100K+ Entities With Complex Relationships

  1. Source connectors — API scrapers, CDC from operational databases, log parsers, one connector per source system.
  2. Staging layer — a message bus (e.g., Kafka) with topics partitioned per source type.
  3. Entity resolution service — dedupe by canonical ID first, then fuzzy match (string + embedding similarity) for name-based entities, with a manual review queue for low-confidence matches.
  4. Graph writer service — consumes from the bus, batches transactions, uses MERGE for idempotency, writes nodes then relationships in separate passes.
  5. Validation & quality — orphan detection (nodes with no relationships), cycle detection for dependency graphs where cycles shouldn’t exist, completeness checks (required properties present), and stale-entity detection (not updated in N days).

Performance optimizations: indices on every frequently-matched property; UNWIND-batched MERGE instead of one statement per record; parallel ingestion via partitioned topics; incremental updates that process only changed records rather than a full re-import each run.

Section 38 — Interview Q&A

Q1: How would you build a graph ingestion pipeline that handles 100K entities with complex relationships reliably? Source connectors feed a partitioned message bus; an entity resolution service dedupes by canonical ID first and falls back to fuzzy (string + embedding) matching with a human review queue for low-confidence matches; a graph writer service consumes the bus and performs idempotent, batched UNWIND-MERGE writes — nodes in one pass, relationships in a second pass so relationships never reference not-yet-created nodes. A validation stage runs orphan detection, cycle detection (where cycles are invalid for the domain), completeness checks, and stale-entity detection. Incremental (delta-only) updates keep steady-state ingestion fast; a full re-import is reserved for recovery scenarios.

Q2: Why is idempotency (MERGE over CREATE) non-negotiable in a graph ingestion pipeline? Any real pipeline will re-process records — retries after a transient failure, replayed messages from an at-least-once queue, or a manual backfill. CREATE on a re-run duplicates nodes/edges, silently corrupting the graph (double-counted dependencies, inflated fan-out in traversal queries). MERGE makes the same input produce the same graph state regardless of how many times it’s applied, which is a prerequisite for safe retries and safe replay.

Q3: How do you handle real-time graph updates without conflicting with batch ingestion running concurrently? Partition responsibility clearly — streaming updates own recent, fine-grained mutations (a new relationship discovered, a status change) while batch runs own periodic full reconciliation and cleanup (removing stale entities, correcting drift). Use timestamp-based last-write-wins as the default conflict resolution for the same node/edge property, but make batch writes explicitly check updated_at before overwriting so a batch job can’t silently clobber a more-recent streaming update.