Chapter 2: The Supply Chain Graph
What we need from the graph
The supply chain graph is the foundation of the routing system. Without it, we can’t find paths from suppliers to retailers, identify alternative routes when a warehouse goes offline or understand the connectivity of the network. Everything the simulator and the Streamlit application do around routing depends on having an accurate, connected graph of the supply chain.
We specifically need:
- Every supplier, warehouse, distribution center and retailer as a node.
- Every shipping relationship as a directed edge.
- A way to find paths from any supplier to any retailer through active warehouses.
- A way to exclude disrupted warehouses from routing without changing the graph structure.
The Aura graph model
The supply chain maps naturally to the property graph model. Here’s what we’re storing:
(:Supplier {id, name, region, capacity, reliability})
-[:SHIPS_TO {cost, distance, capacity}]->
(:Warehouse {id, name, region, capacity, active})
-[:SHIPS_TO {cost, distance, capacity}]->
(:DistributionCenter {id, name, region})
-[:SHIPS_TO {cost, distance, capacity}]->
(:Retailer {id, name, region, demand})
Each node label represents a distinct tier of the supply chain. Each SHIPS_TO relationship carries a shipping cost in dollars, a distance in kilometers and a maximum number of units per shipment.
The active property on Warehouse nodes is the key to disruption routing – setting it to false marks a warehouse as offline without deleting it from the graph. The property isn’t set when the graph is loaded. It appears only when a warehouse is disrupted and the routing query treats a missing value as active. The routing query filters out inactive warehouses mid-path using a WHERE NONE() clause.
Shipment events from the Kafka consumer are stored as Shipment nodes with relationships back to the supplier, warehouse, distribution center and retailer involved:
(:Supplier)-[:HAS_SHIPMENT]->(:Shipment)-[:VIA_WAREHOUSE]->(:Warehouse)
(:Shipment)-[:VIA_DIST_CENTER]->(:DistributionCenter)
(:Shipment)-[:DESTINED_FOR]->(:Retailer)
Building the network
The supply chain is generated synthetically. Node IDs follow a fixed pattern and a fixed random seed makes the rest of the data, including the routes, identical in every notebook and in the simulator. The node counts are:
- 20 suppliers:
S000toS019 - 12 warehouses:
W000toW011 - 10 distribution centers:
DC000toDC009 - 30 retailers:
R000toR029
Routes are generated in three passes for each tier. Every source node gets at least two outbound routes, every target node gets at least two inbound routes and any remaining routes are added at random up to a target count. Here the first two passes already exceed the target counts, so the random fill isn’t needed. The result is 61 supplier-to-warehouse routes, 38 warehouse-to-distribution-center routes and 73 distribution-center-to-retailer routes, 172 in total.
The minimum-connectivity rule makes it likely that an alternative path exists when a warehouse goes offline, but it doesn’t guarantee one for every supplier-retailer pair.
Loading into Aura
We load nodes and relationships in separate passes using MERGE to make the operation idempotent. Running the notebook twice or more won’t create duplicate nodes.
Nodes. Each tier is loaded with MERGE on id. We don’t create uniqueness constraints, only indexes (see below):
UNWIND $rows AS row
MERGE (n:Supplier {id: row.id})
SET n += row
The same pattern applies for Warehouse, DistributionCenter and Retailer nodes. In the notebook a single function takes the label as a parameter and each node receives every property in its row. Warehouses don’t get an active property at load time. It’s added only when a warehouse is disrupted.
Relationships. Each shipping connection becomes a SHIPS_TO relationship carrying cost, distance and capacity properties:
UNWIND $rows AS row
MATCH (src:Supplier {id: row.source})
MATCH (tgt:Warehouse {id: row.target})
MERGE (src)-[r:SHIPS_TO]->(tgt)
SET r.cost = row.cost,
r.distance = row.distance,
r.capacity = row.capacity
The same pattern is used for each tier with the labels changed. Each tier is loaded in a single pass rather than in batches.
Clearing before reload
We clear the database before each run to keep the notebook idempotent. A clear_database() function drops every constraint, drops every non-lookup index and then deletes all nodes and relationships in batches of 1000. The first two steps are short loops over SHOW CONSTRAINTS and SHOW INDEXES that issue a DROP for each result. The last step, the batched delete, is the statement below:
MATCH (n) WITH n LIMIT 1000
DETACH DELETE n
RETURN count(*) AS deleted
The notebook repeats this statement until deleted is 0. This wipes the whole database, including constraints and indexes, so only run it against an instance you’re happy to wipe. We recreate the indexes we need after loading.
Indexes
After loading the nodes and relationships, we create indexes on id for all four node labels and an index on Shipment.status for the queries used by the analytics notebook:
CREATE INDEX supplier_id IF NOT EXISTS FOR (n:Supplier) ON (n.id)
CREATE INDEX warehouse_id IF NOT EXISTS FOR (n:Warehouse) ON (n.id)
CREATE INDEX distributioncenter_id IF NOT EXISTS FOR (n:DistributionCenter) ON (n.id)
CREATE INDEX retailer_id IF NOT EXISTS FOR (n:Retailer) ON (n.id)
CREATE INDEX shipment_status IF NOT EXISTS FOR (n:Shipment) ON (n.status)
Routing queries
With the graph in place, two routing queries drive the rest of the system.
Every path from a supplier to a retailer in this graph has exactly three hops, so fewest hops can’t separate the candidates. Both queries therefore match every path up to a hop limit of 6 (a safety cap, since the deepest possible path has three hops) and pick the one with the lowest total shipping cost.
Normal routing – finding the cheapest path from a supplier to a retailer:
MATCH path = (s:Supplier {id: $supplier_id})-[:SHIPS_TO*1..6]->(r:Retailer {id: $retailer_id})
WITH path, reduce(total = 0.0, rel IN relationships(path) | total + rel.cost) AS total_cost
ORDER BY total_cost ASC
LIMIT 1
RETURN [n IN nodes(path) WHERE n:Warehouse | n.id][0] AS warehouse_id
Disruption routing – finding the cheapest path that avoids the disrupted warehouse:
MATCH path = (s:Supplier {id: $supplier_id})-[:SHIPS_TO*1..6]->(r:Retailer {id: $retailer_id})
WHERE NONE(n IN nodes(path) WHERE n:Warehouse AND coalesce(n.active, true) = false)
WITH path, reduce(total = 0.0, rel IN relationships(path) | total + rel.cost) AS total_cost
ORDER BY total_cost ASC
LIMIT 1
RETURN [n IN nodes(path) WHERE n:Warehouse | n.id][0] AS warehouse_id
The two queries are identical apart from the WHERE NONE() clause, which checks every node in the path and rejects any path that passes through an inactive warehouse. The normal query has no filter, so it ignores the active property.
Disruption and restore
Marking a warehouse as offline is a single SET operation:
MATCH (w:Warehouse {id: $warehouse_id})
SET w.active = false
Restoring it removes the property, which the routing query treats as active:
MATCH (w:Warehouse {id: $warehouse_id})
REMOVE w.active
The graph structure doesn’t change. Only the active property changes and the routing query filters accordingly on the next call.
Verifying the result
After loading, we verify the node and relationship counts, find the cheapest path for a known supplier-retailer pair and then disrupt the warehouse on that path to confirm the routing query finds an alternative:
Node counts:
DistributionCenter : 10
Retailer : 30
Supplier : 20
Warehouse : 12
Total SHIPS_TO routes : 172
Cheapest path: S000 -> R001
Hops : 3
Total cost : $365.90
Total dist. : 3995 km
Route:
Supplier S000
Warehouse W003
DistributionCenter DC009
Retailer R001
Alternative path avoiding W003: S000 -> R001
Hops : 3
Total cost : $423.37
Total dist. : 1764 km
Route:
Supplier S000
Warehouse W005
DistributionCenter DC009
Retailer R001
Disrupting W003 pushes the route through W005 and adds $57.47 to the cost.
Gotchas
shortestPath() and disruption filters don’t mix, and hops can’t rank routes here. shortestPath() finds a fewest-hop path first and applies a WHERE NONE() filter afterwards. If that single path goes through the disrupted warehouse, the filter can discard it without considering other valid paths, so the query can return no rows. Every supplier-to-retailer path in this graph also has exactly three hops, so hop count can’t choose between routes and shortestPath() would return an arbitrary one. We use a variable-length [:SHIPS_TO*1..6] match ordered by total shipping cost for both queries. Leaving the WHERE NONE() filter out ignores the active flag and can return paths through the disrupted warehouse.
NONE() and three-valued logic. The WHERE NONE() clause must use coalesce(n.active, true) = false rather than n.active = false. If active is missing from a node (for example, on a Warehouse that has never been disrupted, which is the normal state of every warehouse after loading), n.active = false evaluates to null rather than false. NONE() then returns null instead of true and WHERE rejects the path incorrectly. Wrapping with coalesce() treats a missing value as active, so paths through healthy warehouses are kept.
Fixed random seed. The supply chain topology is generated with RANDOM_SEED = 42. Every notebook and the simulator use the same seed, which keeps the routes and node properties identical across the system. Node IDs follow a fixed pattern (S000, W000 and so on) and don’t depend on the seed. Changing the seed will produce a different graph and break the cross-notebook consistency.