Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Welcome

How to cite this book

If you use this book in your work, please cite it as:

APA: Chaudhri, A. (2026). Real-Time Supply Chain Routing with Neo4j, Snowflake Postgres and Confluent Kafka. Self-published. https://realtime-supply-chain.github.io/

BibTeX:

@online{chaudhri2026supplychain,
  author  = {Chaudhri, Akmal},
  title   = {Real-Time Supply Chain Routing with Neo4j, Snowflake Postgres and Confluent Kafka},
  year    = {2026},
  url     = {https://realtime-supply-chain.github.io/},
  urldate = {2026-09-13}
}

Cover

License

Copyright © 2026 Akmal Chaudhri. All rights reserved.

Publication Information

First published: September 2026

The latest version of this book, together with updates, errata and additional resources, is available at:

realtime-supply-chain.github.io

Book License

Real-Time Supply Chain Routing with Neo4j, Snowflake Postgres and Confluent Kafka is licensed under the Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License (CC BY-NC-ND 4.0).

You are free to copy and redistribute this book in any medium or format under the following conditions:

  • Attribution - You must give appropriate credit, provide a link to the license and indicate if changes were made.
  • NonCommercial - You may not use the material for commercial purposes.
  • NoDerivatives - If you remix, transform or build upon the material, you may not distribute the modified material.

The full license text is available at:

creativecommons.org/licenses/by-nc-nd/4.0

Code License

Unless otherwise stated, all code samples, notebooks, scripts and source files accompanying this book are licensed under the Apache License 2.0.

You are free to use, modify and redistribute this code, including for commercial purposes, subject to the terms of the Apache License 2.0.

The full license text is available at:

apache.org/licenses/LICENSE-2.0

This distinction means that the book’s written content is protected under the Creative Commons license, while the accompanying code remains freely available for use, modification and integration into your own projects.

Trademarks

Product names, company names and logos mentioned in this book may be trademarks or registered trademarks of their respective owners.

Their inclusion is for identification and educational purposes only and does not imply any affiliation with, sponsorship by or endorsement from the respective trademark holders. All trademarks remain the property of their respective owners.

Disclaimer

The information in this book, including all code samples, scripts and notebooks, is provided “as is” without warranty of any kind, express or implied.

The author makes no representations or warranties regarding the accuracy, completeness, reliability or suitability of the information contained herein for any particular purpose.

Examples are provided solely to illustrate technical concepts, patterns and software architectures.

Readers are responsible for independently validating all code, configurations and recommendations before using them in production environments.

To the fullest extent permitted by law, the author shall not be liable for any direct, indirect, incidental, special, consequential or other damages arising from the use of or inability to use, the information, code or techniques described in this book.

About the Author

Akmal Chaudhri is a technical leader, educator and author with extensive experience in databases, AI and developer relations. He specializes in technical writing, developer education and community building, helping engineers and organizations understand and adopt complex technologies through clear, practical and engaging content. He is a frequent international speaker, a published author and a contributor to industry discussions on data platforms, AI and software development.

Today, Akmal works in developer education at Neo4j, where he focuses on technical content, workshops and community initiatives. While his professional role has evolved, this book represents an independent exploration of various data platforms.

Based in the United Kingdom, Akmal continues to work at the intersection of databases, AI and developer tooling, helping developers build modern data-driven applications.

For book updates, code samples and additional resources, visit the Book website.

To connect professionally or follow his latest work, visit LinkedIn.

Real-Time Supply Chain Routing with Neo4j, Snowflake Postgres and Confluent Kafka

This book builds a real-time supply chain operations dashboard using three database systems:

  1. Neo4j Aura for the supply chain graph.
  2. Snowflake Postgres for live shipment records.
  3. Snowflake for historical analytics.

A simulated supply chain generates shipment events that flow through a Confluent Cloud Kafka topic in real time. Warehouse disruptions are injected automatically, triggering live rerouting along the cheapest available route in the Neo4j graph. Two Streamlit dashboards show live operations and analytics.

How the code is organized

All runnable code lives in the code/ directory.

Notebooks are numbered to match the chapters – 02_neo4j_supply_chain.ipynb corresponds to Chapter 2, and so on. The two Streamlit apps, app.py and analytics_app.py, and their requirements.txt live alongside the notebooks. Chapter 7 gives the order in which to run everything.

The shipment data are synthetic, generated by the simulator in Chapter 5, so the patterns in the analytics illustrate the architecture rather than real supply chain behavior.

Chapter 1: Architecture and Setup

What we’re building

Real-time supply chain routing sits at the intersection of three distinct data problems:

  1. Graph problem – which route connects a supplier to a retailer through active warehouses, and what’s the best alternative when one goes offline?
  2. Operational problem – what’s the current status of each shipment and which ones have been rerouted?
  3. Analytical problem – which warehouses are causing the most delays, which suppliers have the highest delay rates and where are SLA breaches concentrated?

Each of those problems has a natural home in a different kind of database. That’s the core idea behind this book. We’re not going to use one database and make it do everything. We’re going to use three and let each one do what it does best.

The system we’ll build is a real-time supply chain dashboard with 20 suppliers, 12 warehouses, 10 distribution centers and 30 retailers. A simulator generates shipment events that flow through a Confluent Cloud Kafka topic. Warehouse disruptions are injected automatically and the Neo4j graph finds alternative routes in real time. Two Streamlit dashboards show live operations and historical analytics.

The three-system architecture

The architecture has three layers, each serving a different purpose.

Neo4j Aura is the graph layer. It holds the supply chain network – suppliers, warehouses, distribution centers and retailers connected by shipping relationships. Aura answers the questions that a relational database handles with difficulty:

  • What’s the cheapest path from a supplier to a retailer through active warehouses?
  • When a warehouse goes offline, which alternative route avoids it?

The variable-length path queries and inline filtering that Cypher provides make disruption routing fast and clean.

Snowflake Postgres is the operational layer. It’s a fully managed Postgres database, hosted inside Snowflake, that accepts the high-frequency shipment writes from our Kafka consumer. Every event consumed from the shipment-events topic is written here in real time. Snowflake Postgres handles this with standard psycopg2 connectivity. The shipments and disruption_events tables give the live operations dashboard everything it needs.

Snowflake is the analytics layer. Shipment data flow from Snowflake Postgres into a shipment_history table using an incremental sync pattern. We run aggregations here – delay trends over time, warehouse health scores, supplier delay rates and SLA breach analysis. The analytics Streamlit app syncs and queries this layer on every refresh.

The relationship between these systems is as follows:

  • Aura is where the structure lives – the supply chain graph that neither Snowflake Postgres nor Snowflake knows anything about.
  • Snowflake Postgres is where data are written, in real time, by the Kafka consumer.
  • Snowflake is where we analyze the history that Snowflake Postgres accumulates.

Each system has a clear role and none of the roles overlap.

What Aura adds

A natural question is: what does Aura add? We already have a Postgres database. Can’t we just store supply chain relationships there too? We could store them there, but querying them would be difficult. Finding the cheapest path from a supplier to a retailer that avoids a disrupted warehouse requires traversing a graph – following edges from node to node, filtering out inactive nodes mid-path. In SQL this means recursive CTEs with exclusion logic, which become unwieldy fast. In Cypher, Neo4j’s query language, it’s a concise pattern match with an inline filter:

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 supply chain is a graph. Suppliers ship to warehouses, warehouses ship to distribution centers, distribution centers ship to retailers. The connectivity between them is the point. Storing that as rows and columns in a relational table is possible but goes against the natural structure of the data.

What you’ll need

To follow along you’ll need accounts and access to these services:

  1. Neo4j Aura – the free tier is sufficient. Create an account at Get Started for Free and note your URI, username and password.
  2. Snowflake – a free trial account gives us access to both Snowflake Postgres and the Snowflake warehouse. Create an account at Try Snowflake for Free. Create a Snowflake Postgres instance before running the notebooks. Chapter 4 creates the database, schema and warehouse for you.
  3. Confluent Cloud – a free trial account is sufficient. Create an account at Get Started with Confluent and create a cluster with a topic named shipment-events.
  4. Python – we’ll use Python 3.12 throughout. The notebooks run in a local Jupyter environment. A virtual environment is highly recommended. For example:
python3 -m venv ~/supply-chain-env
source ~/supply-chain-env/bin/activate

All dependencies are installed using %pip install cells at the top of each notebook, with pinned versions for stability. The Streamlit apps install from a pinned requirements.txt.

Environment variables

Credentials are passed to notebooks and the Streamlit applications using environment variables. Set these before starting Jupyter:

# Neo4j Aura
export NEO4J_URI="neo4j+s://xxxx.databases.neo4j.io"
export NEO4J_USERNAME="your-username"
export NEO4J_PASSWORD="your-password"

# Snowflake Postgres
export SNOWFLAKE_PG_HOST="your-instance.eu-west-2.aws.postgres.snowflake.app"
export SNOWFLAKE_PG_USER="snowflake_admin"
export SNOWFLAKE_PG_PASSWORD="your-password"
export SNOWFLAKE_PG_DBNAME="postgres"

# Snowflake warehouse
export SNOWFLAKE_ACCOUNT="your-account-identifier"
export SNOWFLAKE_USER="your-username"
export SNOWFLAKE_PASSWORD="your-password"
export SNOWFLAKE_DATABASE="SUPPLY_CHAIN"
export SNOWFLAKE_SCHEMA="PUBLIC"
export SNOWFLAKE_WAREHOUSE="SUPPLY_CHAIN_WH"

# Confluent Cloud
export CONFLUENT_BOOTSTRAP_SERVERS="your-cluster.confluent.cloud:9092"
export CONFLUENT_API_KEY="your-api-key"
export CONFLUENT_API_SECRET="your-api-secret"

Chapter overview

Here’s what each chapter covers:

Chapter 2: The Supply Chain Graph walks through building the supply chain network in Neo4j Aura – 20 suppliers, 12 warehouses, 10 distribution centers and 30 retailers connected by shipping relationships. We’ll cover the Cypher queries that make routing and disruption handling efficient, including variable-length path queries ordered by shipping cost.

Chapter 3: The Operational Layer sets up Snowflake Postgres as the operational store for live shipment records. We’ll cover the table design, indexes and the disruption_events table that records every warehouse disruption and restore event.

Chapter 4: The Analytics Layer sets up the Snowflake warehouse as the analytics layer for historical shipment data. We’ll cover the shipment_history table, the incremental sync pattern that keeps it current and how the analytics app uses it on every refresh.

Chapter 5: The Simulator writes a Kafka producer and consumer that generate synthetic shipment events and write them to both Neo4j and Snowflake Postgres in real time. Warehouse disruptions fire randomly, triggering live rerouting through the Neo4j graph.

Chapter 6: The Streamlit Apps covers the two Streamlit dashboards – the live operations dashboard (app.py) showing current shipment status, active disruptions and rerouted shipments and the analytics dashboard (analytics_app.py) showing status breakdown, delay trends, warehouse health, shipment flow, supplier performance and SLA breaches. There is also an optional analytics notebook for deeper one-off analysis.

Chapter 7: Bringing It All Together explains how the three systems work as a unit, covers the order of operations for running the full stack end-to-end and suggests directions for taking the project further.

Let’s start by building the supply chain graph.

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: S000 to S019
  • 12 warehouses: W000 to W011
  • 10 distribution centers: DC000 to DC009
  • 30 retailers: R000 to R029

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.

Chapter 3: The Operational Layer

What we need from the operational layer

The supply chain graph in Chapter 2 gives us the structure – the nodes and relationships that define how suppliers connect to retailers through warehouses and distribution centers. What it doesn’t give us is a place to write live shipment events as they arrive from Kafka.

We need an operational store that can:

  • Accept high-frequency writes from the Kafka consumer in real time.
  • Record the current status of every shipment.
  • Flag rerouted shipments and store the alternative warehouse used.
  • Log every warehouse disruption and restore event with a timestamp.
  • Serve the live operations Streamlit dashboard with fast reads.

Snowflake Postgres is that store. It’s a fully managed Postgres database hosted inside Snowflake that accepts connections from standard psycopg2 clients. The connection pattern is identical to any other managed Postgres service.

Connecting from Python

The connection pattern is standard psycopg2. SSL is required for all Snowflake Postgres connections:

conn = psycopg2.connect(
    host     = SNOWFLAKE_PG_HOST,
    user     = SNOWFLAKE_PG_USER,
    password = SNOWFLAKE_PG_PASSWORD,
    dbname   = SNOWFLAKE_PG_DBNAME,
    sslmode  = "require",
    port     = 5432
)
conn.autocommit = True

autocommit = True is set for DDL statements. The simulator uses the same connection pattern and relies on autocommit for the high-frequency event writes.

Role setup

We create a dedicated application role with the minimum permissions needed to read and write both tables. No superuser privileges are granted. The DO block skips creation cleanly if the role already exists, making the cell safe to re-run. The password below is a placeholder, so change it before using the role anywhere real:

DO $$
BEGIN
    IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = 'supply_chain_app') THEN
        CREATE ROLE supply_chain_app LOGIN PASSWORD 'change-me-in-production';
    END IF;
END
$$;

GRANT USAGE ON SCHEMA public TO supply_chain_app;

The role can’t read or write any table yet. We grant table permissions once the tables exist.

This role is an optional least-privilege example. For simplicity, the notebooks, simulator and apps in this book connect with the admin user from the environment variables in Chapter 1. The role suits the read-only dashboards. It isn’t enough for the simulator, which also needs TRUNCATE on both tables and USAGE on the disruption_events_id_seq sequence.

The schema

Two tables hold all the operational data.

shipments is the primary table. Every event consumed from the shipment-events Kafka topic is written here. If a shipment is rerouted, the rerouted flag is set to true and the alternative warehouse is recorded in alt_warehouse_id:

CREATE TABLE shipments (
    shipment_id      TEXT PRIMARY KEY,
    supplier_id      TEXT        NOT NULL,
    warehouse_id     TEXT        NOT NULL,
    dist_center_id   TEXT        NOT NULL,
    retailer_id      TEXT        NOT NULL,
    status           TEXT        NOT NULL,
    delay_minutes    INTEGER     NOT NULL DEFAULT 0,
    rerouted         BOOLEAN     NOT NULL DEFAULT FALSE,
    alt_warehouse_id TEXT,
    recorded_at      TIMESTAMPTZ NOT NULL DEFAULT NOW()
)

disruption_events records every warehouse disruption and restore event. The simulator writes a row each time a warehouse goes offline and again when it comes back online. The live operations dashboard reads this table to determine the current phase and display the disruption banner:

CREATE TABLE disruption_events (
    id            SERIAL PRIMARY KEY,
    warehouse_id  TEXT        NOT NULL,
    event_type    TEXT        NOT NULL,
    recorded_at   TIMESTAMPTZ NOT NULL DEFAULT NOW()
)

The event_type column holds either disrupted or restored. The most recent row tells the dashboard which phase the system is currently in.

Both tables are created using a loop to keep the notebook concise:

tables = {
    "shipments":         "CREATE TABLE shipments ...",
    "disruption_events": "CREATE TABLE disruption_events ...",
}

for name, ddl in tables.items():
    cursor.execute(f"DROP TABLE IF EXISTS {name}")
    cursor.execute(ddl)
    print(f"Table {name} created.")

The loop drops each table before creating it, so the notebook is safe to re-run from the start during development. This deletes any shipment data already stored, so only run it against a database you’re happy to reset.

Granting table permissions

With both tables in place, we grant the application role read and write access to each of them:

GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE shipments TO supply_chain_app;
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLE disruption_events TO supply_chain_app;

ALTER DEFAULT PRIVILEGES IN SCHEMA public
GRANT SELECT, INSERT, UPDATE, DELETE ON TABLES TO supply_chain_app;

The ALTER DEFAULT PRIVILEGES statement ensures any future tables in the schema are also accessible to the role without an additional grant.

Indexes

We create indexes on the columns most commonly used in dashboard queries:

CREATE INDEX IF NOT EXISTS idx_shipments_status      ON shipments (status);
CREATE INDEX IF NOT EXISTS idx_shipments_warehouse   ON shipments (warehouse_id);
CREATE INDEX IF NOT EXISTS idx_shipments_recorded_at ON shipments (recorded_at DESC);
CREATE INDEX IF NOT EXISTS idx_shipments_rerouted    ON shipments (rerouted);

The status index speeds up the per-status counts shown in the metric tiles. The warehouse_id index speeds up the delayed-by-warehouse bar chart. The recorded_at index speeds up the incremental sync used by the analytics layer in Chapter 4. The rerouted index speeds up the rerouted shipments table.

Verifying the result

After creating both tables and indexes, we confirm the column definitions and index names match expectations:

Table: shipments
  Column               Type                     Nullable   Default
  ----------------------------------------------------------------------
  shipment_id          text                     NO
  supplier_id          text                     NO
  warehouse_id         text                     NO
  dist_center_id       text                     NO
  retailer_id          text                     NO
  status               text                     NO
  delay_minutes        integer                  NO         0
  rerouted             boolean                  NO         false
  alt_warehouse_id     text                     YES
  recorded_at          timestamp with time zone NO         now()

Table: disruption_events
  Column               Type                     Nullable   Default
  ----------------------------------------------------------------------
  id                   integer                  NO         nextval('disruption_events_id_seq'::regclass)
  warehouse_id         text                     NO
  event_type           text                     NO
  recorded_at          timestamp with time zone NO         now()

Indexes (shipments):
  idx_shipments_recorded_at
  idx_shipments_rerouted
  idx_shipments_status
  idx_shipments_warehouse
  shipments_pkey

What the simulator writes

The simulator writes to both tables on every lifecycle event. For shipment events, it uses ON CONFLICT DO UPDATE to handle rerouted shipments correctly – if a shipment arrives that was initially routed through a warehouse that then went offline, the update overwrites the routing fields rather than creating a duplicate row:

INSERT INTO shipments (...)
VALUES (...)
ON CONFLICT (shipment_id) DO UPDATE
    SET rerouted         = EXCLUDED.rerouted,
        alt_warehouse_id = EXCLUDED.alt_warehouse_id,
        status           = EXCLUDED.status

For disruption events, it’s a plain insert each time a warehouse changes state:

INSERT INTO disruption_events (warehouse_id, event_type)
VALUES (%s, %s)

The recorded_at timestamp is set by the database using DEFAULT NOW(), which ensures the timestamp reflects when the event landed in Postgres rather than when it was generated by the simulator.

Gotchas

ON CONFLICT DO UPDATE not DO NOTHING. Using DO NOTHING for shipment inserts means rerouted events are silently dropped when the original shipment row already exists. The rerouted flag and alternative warehouse ID never get written. Always use DO UPDATE and explicitly set the rerouting columns.

disruption_events grant must be explicit. The ALTER DEFAULT PRIVILEGES statement only covers tables created after it runs. Since disruption_events is created before the default privileges are set, it needs its own explicit GRANT statement. Relying on default privileges alone leaves the table inaccessible to the application role.

Chapter 4: The Analytics Layer

What the analytics layer does

Snowflake Postgres in Chapter 3 gives us a fast operational store for live shipment events. But it’s optimized for writes and point reads, not for the heavier aggregation queries that answer questions like “which warehouses are causing the most delays over time?” or “how has the average delay changed across the last hour?”

For those questions we need an analytics layer – somewhere to accumulate historical data and run aggregations without affecting the operational store. The Snowflake warehouse is that layer.

The analytics layer needs to:

  • Store a full history of shipment events as they accumulate over a run.
  • Stay current with new events arriving from Snowflake Postgres.
  • Serve the analytics Streamlit dashboard with the six charts it displays on every refresh.
  • Handle heavier aggregation queries efficiently without impacting live operations.

Connecting to Snowflake

We use the snowflake-connector-python library to connect to the Snowflake warehouse. The account identifier is the part of your Snowflake URL before .snowflakecomputing.com. For example, if your URL is abc123.eu-west-1.snowflakecomputing.com then your account is abc123.eu-west-1:

sf = snowflake.connector.connect(
    account  = SNOWFLAKE_ACCOUNT,
    user     = SNOWFLAKE_USER,
    password = SNOWFLAKE_PASSWORD,
)
sf_cursor = sf.cursor()

The connection doesn’t select a database, schema or warehouse. We create and select those next.

Database and schema

We create the database and schema that hold the historical shipment data. IF NOT EXISTS makes the cell safe to re-run. The names come from the SNOWFLAKE_DATABASE and SNOWFLAKE_SCHEMA environment variables (SUPPLY_CHAIN and PUBLIC in our setup):

sf_cursor.execute(f"CREATE DATABASE IF NOT EXISTS {SNOWFLAKE_DATABASE}")
sf_cursor.execute(f"USE DATABASE {SNOWFLAKE_DATABASE}")
sf_cursor.execute(f"CREATE SCHEMA IF NOT EXISTS {SNOWFLAKE_SCHEMA}")
sf_cursor.execute(f"USE SCHEMA {SNOWFLAKE_SCHEMA}")

The compute warehouse

The Snowflake compute warehouse runs the analytics queries. We use an X-SMALL warehouse with auto-suspend and auto-resume to keep costs low. It spins up when a query arrives and suspends automatically after 60 seconds of inactivity. The notebook takes the name from the SNOWFLAKE_WAREHOUSE environment variable (SUPPLY_CHAIN_WH in our setup) and substitutes it into the SQL:

CREATE WAREHOUSE IF NOT EXISTS SUPPLY_CHAIN_WH
WITH WAREHOUSE_SIZE      = 'X-SMALL'
     AUTO_SUSPEND        = 60
     AUTO_RESUME         = TRUE
     INITIALLY_SUSPENDED = TRUE;

USE WAREHOUSE SUPPLY_CHAIN_WH;

INITIALLY_SUSPENDED = TRUE means the warehouse doesn’t consume credits until the first query runs.

The shipment history table

The shipment_history table mirrors the shipments table in Snowflake Postgres but is designed for analytics rather than transactions. It has no primary key constraint since it’s append-only and adds a synced_at column recording when each row was pulled from Snowflake Postgres:

CREATE TABLE shipment_history (
    shipment_id      TEXT,
    supplier_id      TEXT,
    warehouse_id     TEXT,
    dist_center_id   TEXT,
    retailer_id      TEXT,
    status           TEXT,
    delay_minutes    INTEGER,
    rerouted         BOOLEAN,
    alt_warehouse_id TEXT,
    recorded_at      TIMESTAMP_TZ,
    synced_at        TIMESTAMP_TZ
)

The table is dropped and recreated on each notebook run, so the notebook is safe to re-run from the start during development. This deletes any history already stored, so only run it against a database you’re happy to reset. In the analytics Streamlit app the table persists across refreshes and the incremental sync keeps it current.

The incremental sync pattern

Rather than writing directly to the Snowflake warehouse from the simulator, we use an incremental sync pattern. The analytics app reads new rows from the shipments table in Snowflake Postgres and appends them to shipment_history in the Snowflake warehouse.

The sync tracks a watermark – the MAX(recorded_at) already in shipment_history. On each sync call, it pulls only rows newer than that watermark and appends them. A small helper, get_pg_conn(), opens a psycopg2 connection to Snowflake Postgres using the same pattern as Chapter 3. The sync closes that connection in a finally block as soon as the rows are read, so it’s released even if the query fails:

def get_last_synced_at(sf_cursor):
    sf_cursor.execute("SELECT MAX(recorded_at) FROM shipment_history")
    result = sf_cursor.fetchone()[0]
    return result if result else datetime(1970, 1, 1, tzinfo=timezone.utc)

def sync_to_warehouse(sf_cursor):
    last_synced_at = get_last_synced_at(sf_cursor)
    synced_at      = datetime.now(timezone.utc)

    pg_conn = get_pg_conn()
    try:
        with pg_conn.cursor() as pg_cursor:
            pg_cursor.execute("""
                SELECT
                    shipment_id, supplier_id, warehouse_id, dist_center_id,
                    retailer_id, status, delay_minutes, rerouted,
                    alt_warehouse_id, recorded_at
                FROM shipments
                WHERE recorded_at > %s
                ORDER BY recorded_at
            """, (last_synced_at,))
            rows = pg_cursor.fetchall()
    finally:
        pg_conn.close()

    if not rows:
        return 0

    # batch insert into shipment_history (see below)
    ...
    return total

This pattern is the same one used by the analytics Streamlit app on every 30-second refresh. Running it in the notebook here confirms the connections and the watermark query work before the Kafka layer is running. The insert step only runs once there are rows to sync.

The first run will find no rows since the simulator hasn’t started yet. That’s expected.

Batch inserts

The sync function inserts rows in batches of 500 using executemany with %s placeholders. Any value with an isoformat method, such as a timestamp, is converted to a string first:

BATCH_SIZE = 500
placeholders = ", ".join(["%s"] * 11)
total = 0
for i in range(0, len(rows), BATCH_SIZE):
    batch = rows[i:i + BATCH_SIZE]
    values_list = [
        tuple(str(v) if hasattr(v, "isoformat") else v for v in (*row, synced_at))
        for row in batch
    ]
    sf_cursor.executemany(
        f"INSERT INTO shipment_history VALUES ({placeholders})",
        values_list,
    )
    total += len(batch)

There are 11 placeholders because each row has the 10 columns read from Snowflake Postgres plus the synced_at value.

Verifying the result

After creating the table and running the initial sync, we confirm the column definitions match expectations and check the row count:

Table: shipment_history
  Column               Type
  ----------------------------------------
  SHIPMENT_ID          TEXT
  SUPPLIER_ID          TEXT
  WAREHOUSE_ID         TEXT
  DIST_CENTER_ID       TEXT
  RETAILER_ID          TEXT
  STATUS               TEXT
  DELAY_MINUTES        NUMBER
  REROUTED             BOOLEAN
  ALT_WAREHOUSE_ID     TEXT
  RECORDED_AT          TIMESTAMP_TZ
  SYNCED_AT            TIMESTAMP_TZ

Row count: 0

Snowflake reports INTEGER columns as NUMBER, which is why DELAY_MINUTES appears with that type. Zero rows is correct at this stage – the simulator hasn’t run yet.

If you rerun the notebook after Chapter 5, the sync picks up the shipments the simulator wrote. The sync reports how many rows it copied and the row count matches the number of shipments in Snowflake Postgres.

Gotchas

mogrify doesn’t exist on the Snowflake connector. mogrify is a psycopg2 method that returns a complete SQL string with the values inserted. The Snowflake cursor has no such method, so code that calls it fails as soon as there are rows to insert. Use executemany with %s placeholders instead. This is the pattern used in the analytics notebook and the Streamlit app.

Timestamp binding. Convert datetime objects to strings before inserting into Snowflake. The connector doesn’t always handle timezone-aware datetime objects correctly. Wrapping with str(v) if hasattr(v, 'isoformat') else v ensures safe binding for all timestamp columns.

CREATE TABLE IF NOT EXISTS in the analytics app. The Chapter 4 notebook drops and recreates shipment_history on each run. The analytics Streamlit app doesn’t – it creates the table only if it doesn’t already exist, then appends to it incrementally. Don’t run this notebook while the app is running, as its DROP TABLE will delete data the app has already accumulated. The simulator in Chapter 5 truncates the table at startup, so you don’t need to rerun this notebook between simulator runs. The Chapter 6 notebook uses its own scratch table and can run alongside the app.

Chapter 5: The Simulator

What the simulator does

The simulator generates synthetic shipment events, publishes them to a Confluent Cloud Kafka topic and consumes them back in real time. For each consumed event, it writes the shipment to both Neo4j and Snowflake Postgres – and if the shipment passes through a warehouse that’s currently offline, it reroutes it through an alternative warehouse found via the Neo4j graph.

The simulator has four responsibilities:

  1. Publish synthetic shipment events to the shipment-events Kafka topic.
  2. Consume those events from the same topic.
  3. Inject warehouse disruptions at random intervals and restore them after a random duration.
  4. Write every event to Neo4j and Snowflake Postgres, with rerouting applied where needed.

It runs directly in a Jupyter notebook cell. The loop stops on keyboard interrupt, after N_EVENTS events when that’s a positive number or automatically after MAX_RUNTIME_SECONDS.

The Neo4j graph (Chapter 2), the Snowflake Postgres tables (Chapter 3) and the Snowflake warehouse table (Chapter 4) must be in place before you start.

Starting clean

Every run starts by resetting the live state in all three systems, so you never have to clear anything by hand between runs:

  • Snowflake Postgres – the shipments and disruption_events tables are truncated.
  • Neo4j – all Shipment nodes are deleted in batches of 1000 and the active flag is removed from every warehouse. The supply chain graph itself is left alone.
  • Snowflake warehouse – the shipment_history table is truncated, so the analytics layer starts empty too.
with neo4j_driver.session() as session:
    session.run("MATCH (w:Warehouse) REMOVE w.active")
    deleted = 0
    while True:
        n = session.run("""
            MATCH (sh:Shipment)
            WITH sh LIMIT 1000
            DETACH DELETE sh
            RETURN count(*) AS n
        """).single()["n"]
        if n == 0:
            break
        deleted += n

The cell prints a summary of what it cleared. This is the output from a second run, after the previous run had left 1,975 shipments behind:

Postgres table shipments truncated.
Postgres table disruption_events truncated.
Neo4j: removed 1,975 Shipment nodes and cleared all active flags.
Snowflake warehouse: shipment_history truncated (if it exists).

This deletes all shipment data from the previous run, so stop the Streamlit apps before restarting the simulator.

The Kafka Producer and Consumer

The simulator uses confluent-kafka for both producing and consuming events. The producer publishes one event per iteration of the loop. The consumer reads one event back before writing to the databases. This tight produce-consume loop ensures that what gets written to Neo4j and Snowflake Postgres is exactly what landed in Kafka – the same event, with the same timestamp.

Both producer and consumer authenticate with SASL/SSL:

producer = Producer({
    "bootstrap.servers": CONFLUENT_BOOTSTRAP_SERVERS,
    "security.protocol": "SASL_SSL",
    "sasl.mechanisms":   "PLAIN",
    "sasl.username":     CONFLUENT_API_KEY,
    "sasl.password":     CONFLUENT_API_SECRET,
})

The consumer uses a fixed group.id and auto.offset.reset = "earliest", so on its first run it reads from the beginning of the topic. On later runs it resumes from the last committed offset.

Generating shipment events

Each shipment event is generated from a seeded random number generator, so the mix of suppliers, warehouses and statuses is broadly repeatable between runs. The shipment IDs are random UUIDs and the exact sequence can shift if the consumer poll returns nothing for an iteration, so two runs won’t be identical:

rng  = np.random.default_rng(RANDOM_SEED)

def make_shipment_event():
    status = str(rng.choice(STATUSES, p=STATUS_WEIGHTS))
    return {
        "shipment_id":    str(uuid.uuid4()),
        "supplier_id":    str(rng.choice(SUPPLIER_IDS)),
        "warehouse_id":   str(rng.choice(WAREHOUSE_IDS)),
        "dist_center_id": str(rng.choice(DIST_CENTER_IDS)),
        "retailer_id":    str(rng.choice(RETAILER_IDS)),
        "status":         status,
        "timestamp":      time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
        "delay_minutes":  int(rng.integers(15, 240)) if status == "delayed" else 0,
    }

Status weights are [0.25, 0.45, 0.15, 0.15] for departed, in_transit, delayed and delivered. Roughly 15% of shipments are delayed, which produces enough delay data for the analytics charts without overwhelming the status breakdown. Delays are assigned at random, independent of disruptions, warehouses and suppliers. Disruptions affect routing, not delay, so any differences you see between warehouses or suppliers in the analytics are noise in the synthetic data.

The events are synthetic: the supplier, warehouse, distribution center and retailer are chosen independently, so an event doesn’t always follow a real path through the graph. That’s fine for the demonstration, because the routing logic only needs to know which warehouse an event used.

The disruption lifecycle

Warehouse disruptions fire randomly during the simulation. Two configuration parameters control the timing:

  • DISRUPTION_INTERVAL_MIN / DISRUPTION_INTERVAL_MAX – how many events pass between disruptions.
  • DISRUPTION_DURATION_MIN / DISRUPTION_DURATION_MAX – how long each disruption lasts in events.

Both the gap between disruptions and the duration of each disruption are drawn randomly from these ranges using the seeded rng. Disruptions look unpredictable but the overall pattern is broadly repeatable.

The first disruption always hits W003, which is the warehouse used in the Chapter 2 graph verification test. Subsequent disruptions target a randomly chosen warehouse drawn after each restore. Because the choice is random, the same warehouse can be hit more than once in a row.

The lifecycle has three phases:

normal -> disrupted -> restored -> disrupted -> restored -> ...

Transitioning to disrupted sets the warehouse’s active property to false in Neo4j and writes a disrupted row to disruption_events in Snowflake Postgres. Transitioning to restored removes the active property and writes a restored row. If you stop the simulator during a disruption, the finally block restores the offline warehouse and logs the restore, so the graph and the dashboard banner aren’t left in a disrupted state.

Rerouting

When the simulator is in the disrupted phase and a consumed event routes through the disrupted warehouse, it calls find_alternative_route() to ask Neo4j for a path that avoids it:

if phase == "disrupted" and consumed["warehouse_id"] == current_disruption:
    alt_warehouse_id = find_alternative_route(
        session,
        consumed["supplier_id"],
        consumed["retailer_id"],
    )

The rerouting query is the one from Chapter 2: a variable-length path with a WHERE NONE() filter, ordered by total shipping cost. Every supplier-to-retailer path has exactly three hops, so hop count can’t rank the alternatives and cost has to:

def find_alternative_route(session, supplier_id, retailer_id):
    result = session.run("""
        MATCH (s:Supplier {id: $supplier_id}),
              (r:Retailer {id: $retailer_id})
        MATCH path = (s)-[:SHIPS_TO*1..6]->(r)
        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) | n.id] AS node_ids
    """,
        supplier_id = supplier_id,
        retailer_id = retailer_id,
    )
    record = result.single()
    if record is None:
        return None
    # The warehouse is always the second node in the path
    return record["node_ids"][1]

If an alternative is found, alt_warehouse_id is passed to both write_to_neo4j and write_to_postgres, which record the rerouting in both systems. If no alternative exists, the function returns None and the shipment is written without being rerouted.

Writing to Neo4j and Snowflake Postgres

Each consumed event is written to both systems before the loop advances.

Neo4j – a MERGE on shipment_id creates or updates the Shipment node and its four relationships. If the shipment was rerouted, the VIA_WAREHOUSE relationship points to the alternative warehouse rather than the original:

MERGE (sh:Shipment {shipment_id: $shipment_id})
SET sh.status        = $status,
    sh.delay_minutes = $delay_minutes,
    sh.timestamp     = $timestamp,
    sh.rerouted      = $rerouted

Neo4j writes use write_to_neo4j_with_retry(), which opens a fresh session per attempt and retries up to twice on connection failure. This handles the session timeouts that Aura enforces on long-running connections.

Snowflake Postgres – an INSERT ... ON CONFLICT DO UPDATE makes the write an idempotent upsert. Every event has a fresh UUID, so a conflict only happens if Kafka redelivers a message. When it does, the update overwrites the routing fields instead of failing with a duplicate key error:

INSERT INTO shipments (...)
ON CONFLICT (shipment_id) DO UPDATE
    SET rerouted         = EXCLUDED.rerouted,
        alt_warehouse_id = EXCLUDED.alt_warehouse_id,
        status           = EXCLUDED.status

The display

The simulator refreshes its output every PRINT_EVERY events, showing the current phase, the latest event and the running status breakdown:

Events processed : 1000
Phase            : Restored -- W004 back online
Rerouted         : 16
Refresh every    : 10

Latest event:
  -> 38988ea7... S008 -> W001 -> DC007 -> R001 [departed]

Last reroute:
  9a95b4b5... W004 -> W002

Status breakdown:
  departed     : 254
  in_transit   : 433
  delayed      : 166
  delivered    : 147
Total events processed : 1000
Total rerouted         : 16

These outputs come from a run with N_EVENTS set to 1000, so the loop stops by itself. The two total lines come from the finally block, which also runs when you interrupt the loop.

The !! prefix marks delayed events. The -> prefix marks normal events.

Runtime limit

The simulator stops automatically after MAX_RUNTIME_SECONDS even when N_EVENTS is set to -1 for continuous operation:

start_time = time.time()
while (N_EVENTS == -1 or counter < N_EVENTS) and \
      (time.time() - start_time < MAX_RUNTIME_SECONDS):
    ...

This ensures the simulator doesn’t run indefinitely if left unattended.

Verifying the result

Two verify cells at the end of the notebook check that both systems agree. The first connects to Neo4j and counts Shipment nodes by status:

Neo4j -- Shipment nodes by status:
  departed     : 254
  in_transit   : 433
  delayed      : 166
  delivered    : 147

  Total         : 1000
  Rerouted      : 16

The second connects to Snowflake Postgres and prints the same summary for the shipments table, followed by every row of disruption_events:

Snowflake Postgres -- shipments table by status:
  departed     : 254
  in_transit   : 433
  delayed      : 166
  delivered    : 147

  Total         : 1000
  Rerouted      : 16

Snowflake Postgres -- disruption_events:
  Warehouse    Event        Timestamp
  W003         disrupted    2026-10-17 18:02:22.552081+00:00
  W003         restored     2026-10-17 18:03:27.962843+00:00
  W003         disrupted    2026-10-17 18:05:14.694457+00:00
  W003         restored     2026-10-17 18:05:53.010754+00:00
  W004         disrupted    2026-10-17 18:07:55.396563+00:00
  W004         restored     2026-10-17 18:08:47.601405+00:00

The totals and rerouted counts match across the two systems and match the simulator’s final totals. This run produced six disruption rows. A longer run produces more.

Gotchas

Long-lived Neo4j Aura sessions time out. Aura closes idle connections after a period of inactivity, throwing SessionExpired. The fix is to open a fresh session per operation rather than holding one open for the duration of the loop. All Neo4j calls in the simulator use short-lived with neo4j_driver.session() as session: blocks and the main shipment write uses write_to_neo4j_with_retry() to handle dropped connections automatically.

Very few rerouted shipments. If the disruption window is too short relative to the event rate and the number of warehouses, very few shipments will pass through the disrupted warehouse during the disruption. Increase DISRUPTION_DURATION_MAX or decrease DISRUPTION_INTERVAL_MIN to produce more rerouting events. Some shipments also have no alternative route, in which case they’re written without being rerouted.

ON CONFLICT DO NOTHING would discard routing changes. If Kafka redelivers a message, DO NOTHING keeps the existing row and silently drops the new routing fields. Use DO UPDATE and set the rerouting columns explicitly. Note that the update doesn’t change recorded_at, so the Chapter 4 sync, which pulls only rows newer than its watermark, won’t copy the changed row to the warehouse.

Rerun the Kafka setup after stopping the loop. The loop’s finally block closes the consumer and restores any warehouse that is still offline. To run the loop again, rerun the cell that creates the producer and consumer first.

Tables are truncated, not dropped, at simulator startup. The shipments and disruption_events tables are owned by the Chapter 3 notebook, and shipment_history by the Chapter 4 notebook. The simulator truncates them at startup rather than dropping and recreating them. Re-running the Chapter 3 or Chapter 4 notebook after the simulator has run will drop and recreate the tables, clearing all data.

Stop the apps before restarting the simulator. The startup reset empties the tables the dashboards read from. If the analytics app is syncing at that moment, it can leave a half-reset state. Stop both apps, restart the simulator, then start the apps again.

Chapter 6: The Streamlit Apps

Two dashboards

The system has two Streamlit dashboards. The live operations dashboard (app.py, port 8501) shows current shipment status, active disruptions, rerouted shipments and a disruption events log. The analytics dashboard (analytics_app.py, port 8502) shows status breakdown, delay trends, warehouse health, shipment flow, supplier performance and SLA breaches, updated every 30 seconds from the Snowflake warehouse.

Install the app dependencies from requirements.txt first with pip install -r requirements.txt, then run the apps alongside the simulator. Stop both apps before restarting the simulator, because its startup reset empties the tables they read from:

streamlit run app.py
streamlit run analytics_app.py --server.port 8502

The live operations dashboard

Auto-refresh

The live operations dashboard refreshes every 10 seconds. streamlit-autorefresh handles this with a single call near the top of the script:

st_autorefresh(interval=REFRESH_SECONDS * 1000)

Every 10 seconds the entire script re-runs from top to bottom, re-querying Snowflake Postgres for the latest shipment and disruption data.

Connections

All queries read from Snowflake Postgres using psycopg2. Each query opens its own connection and closes it as soon as the query finishes, using contextlib.closing. (A psycopg2 connection used in a plain with block ends the transaction but stays open.) @st.cache_data caches each result for just under the refresh interval, which avoids hammering the database on every rerun but still fetches fresh data on each refresh:

CACHE_TTL_SECONDS = REFRESH_SECONDS - 1

@st.cache_data(ttl=CACHE_TTL_SECONDS)
def fetch_status_counts():
    with closing(get_conn()) as conn:
        with conn.cursor() as cur:
            cur.execute("""
                SELECT status, COUNT(*) AS count
                FROM shipments
                GROUP BY status
            """)
            rows = cur.fetchall()
    return {row[0]: row[1] for row in rows}

The disruption banner

The most prominent element on the dashboard is the disruption banner – a full-width colored box at the top of the page. It shows green for normal operations, red when a warehouse is offline and amber immediately after a restore. After a configurable grace period (RESTORED_GRACE_SECONDS, defaulting to 60 seconds), the banner returns to green even if the last event in disruption_events was a restore.

The current phase is determined by the most recent row in disruption_events:

cur.execute("""
    SELECT event_type, warehouse_id, recorded_at
    FROM disruption_events
    ORDER BY recorded_at DESC
    LIMIT 1
""")

If the last event was disrupted, the banner is red. If it was restored and fewer than RESTORED_GRACE_SECONDS have elapsed, the banner is amber. Otherwise it’s green.

Status metric tiles

Four metric tiles below the banner show shipment counts for each status – departed, in_transit, delayed and delivered. Each tile uses the same color palette as the analytics dashboard’s doughnut chart for consistency across both apps.

Delayed shipments by warehouse

A vertical bar chart shows delayed shipment counts per warehouse, using a Reds colorscale. Warehouses with more delayed shipments appear with a deeper red fill, making the worst-performing warehouses immediately visible.

Recent rerouted shipments

A table of up to ten rows shows the most recent rerouted shipments with the original warehouse, the alternative warehouse and the timestamp. This table is the most direct way to see the disruption routing in action.

Disruption events log

A second table of up to ten rows at the bottom of the page shows the most recent rows from disruption_events – every warehouse disruption and restore, with timestamps. This gives an at-a-glance history of disruption activity during the current simulator run.

The analytics dashboard

Auto-refresh and sync

The analytics dashboard refreshes every 30 seconds. On each refresh it syncs new rows from shipments in Snowflake Postgres into shipment_history in the Snowflake warehouse using the incremental watermark pattern from Chapter 4:

sf_cur.execute("SELECT MAX(recorded_at) FROM shipment_history")
last_synced_at = sf_cur.fetchone()[0] or datetime(1970, 1, 1, tzinfo=timezone.utc)

Only rows newer than the watermark are pulled and appended. The first refresh after a long simulator run may sync thousands of rows; subsequent refreshes sync only the handful of rows that arrived in the last 30 seconds.

The table is created if it’s missing, with CREATE TABLE IF NOT EXISTS, so a dropped table doesn’t stop the app. The database, schema and compute warehouse from Chapter 4 must already exist:

sf_cur.execute("""
    CREATE TABLE IF NOT EXISTS shipment_history (...)
""")

Connections

The analytics dashboard connects to two systems:

  1. Snowflake Postgres – for the incremental sync source.
  2. Snowflake warehouse – for all analytics queries.

The Snowflake connection is opened at the start of each refresh and closed in a finally block once the queries finish, so it’s released even if a query fails. The Postgres connection is opened only for the sync. It’s closed in a finally block as soon as the new rows have been read, before they’re inserted into the warehouse:

pg_conn = get_pg_conn()
try:
    with pg_conn.cursor() as pg_cur:
        pg_cur.execute(...)
        rows = pg_cur.fetchall()
finally:
    pg_conn.close()

The Snowflake warehouse compute is an X-SMALL instance with auto-suspend, so it only consumes credits while queries are running.

Status breakdown

The first chart is a doughnut using go.Pie with hole=0.4. The same STATUS_COLORS dictionary is used in both apps for consistency (the notebook calls it COLOR_MAP):

STATUS_COLORS = {
    "departed":   "#4C9BE8",
    "in_transit": "#2A9D8F",
    "delayed":    "#E63946",
    "delivered":  "#57CC99",
}

A dual-axis line chart shows delayed shipment count per minute on the left axis and average delay duration in minutes on the right axis. The dashed line for average delay makes it easy to distinguish the two series without relying on color alone. The first and last points cover partial minutes, so the count often looks lower at both ends of the line.

Warehouse health heatmap

A go.Heatmap shows each warehouse’s health score – the percentage of its shipments that were not delayed – on a red-to-orange-to-green colorscale. The simulator assigns delays at random, independent of disruptions, so differences between warehouses are noise in the synthetic data. The scale is fixed at 0 to 100 so colors always mean the same thing, which means every warehouse lands in the green range with this synthetic data. With real data the heatmap would be a quick way to spot unhealthy warehouses.

Shipment flow sankey

A go.Sankey diagram shows the 50 largest flows of shipments from suppliers to warehouses. The left nodes are suppliers (blue) and the right nodes are warehouses (teal). The width of each link is proportional to the number of shipments between that supplier and warehouse. The chart uses each shipment’s original warehouse, so rerouted shipments are counted against the warehouse that was disrupted. Because the simulator picks suppliers and warehouses independently, the flows between any supplier and warehouse are similar in size, so the top 50 are close to an arbitrary selection and the chart shows a dense web.

Top suppliers and SLA breach

Two horizontal bar charts sit side by side at the bottom of the dashboard. The first ranks the ten suppliers with the highest delay rate using a Reds colorscale. The second ranks the ten warehouses with the highest SLA breach rate – the percentage of shipments where delay_minutes exceeded the threshold – using an Oranges colorscale. Both charts exclude nodes with fewer than three shipments to avoid misleading results from small samples. Because the simulator assigns delays at random, the rankings show how the dashboard works rather than real patterns.

The optional analytics notebook

The chapter’s notebook, 06_analytics.ipynb, is a one-time, static version of the analytics. It loads every row from shipments in Snowflake Postgres into a fresh scratch table in the Snowflake warehouse, then runs five analytical queries and charts them:

  1. Status breakdown.
  2. Delay trends over time.
  3. Rerouted shipments by alternative warehouse, with average delay for rerouted versus normal shipments.
  4. Top suppliers by delay rate.
  5. SLA breach rate, overall and by warehouse.

The SLA queries use a threshold set once in the configuration cell and passed in as a bind parameter:

SLA_THRESHOLD_MINUTES = 60

sf_cursor.execute("""
    SELECT
        warehouse_id,
        COUNT(*) AS total,
        SUM(CASE WHEN delay_minutes > %s THEN 1 ELSE 0 END) AS breaches
    FROM shipment_history_nb
    GROUP BY warehouse_id
""", (SLA_THRESHOLD_MINUTES,))

Running the notebook against the 1,000 shipments from the Chapter 5 run produces output like this:

Status breakdown:
    status  count  pct
in_transit    433 43.3
  departed    254 25.4
   delayed    166 16.6
 delivered    147 14.7

SLA threshold  : 60 minutes
Total          : 1,000
Breaches       : 133
Breach rate    : 13.3%

The status counts match the Chapter 5 verification output.

Unlike the dashboard, the notebook does a full load rather than an incremental sync. It writes to its own scratch table, shipment_history_nb, and drops that table in its teardown, so it can run alongside the Streamlit apps without touching the shipment_history table the analytics app uses. Stop the simulator before running it so the data don’t change while it loads.

Gotchas

use_container_width deprecated. Streamlit deprecated use_container_width in favor of width='stretch' in recent versions. Use st.plotly_chart(fig, width='stretch') rather than use_container_width=True.

streamlit-autorefresh silent argument removed. The silent keyword argument was removed from st_autorefresh in recent versions. Use st_autorefresh(interval=N) with no additional arguments.

@st.cache_data and live data. Setting ttl too high on cached queries means the dashboard shows stale data. Set ttl slightly below the refresh interval so that each auto-refresh fetches fresh data from the database.

Snowflake NUMBER columns arrive as Decimal. The Snowflake connector returns values such as rates rounded to one decimal place as Python Decimal objects. pandas stores them as object, so Plotly treats a column passed to color as categories and ignores color_continuous_scale. The bars come out in a pastel rainbow instead of a Reds or Oranges gradient. Cast the column with .astype(float) after building the DataFrame.

Charts fade during a refresh. Streamlit dims elements while the script reruns, and the analytics script syncs and runs six queries on every refresh. A screenshot taken mid-refresh shows faded charts, so wait for the page to settle first.

Keep one browser tab open on the analytics dashboard. Every open session runs its own sync. Two tabs read the same watermark and insert the same rows, which duplicates data in shipment_history. If that happens, restart the simulator, which resets the table, and keep a single tab open.

Snowflake warehouse auto-suspend. The X-SMALL compute warehouse suspends after 60 seconds of inactivity. The first query after a suspension has a cold-start delay of a few seconds while the warehouse resumes. This is normal and expected – the analytics dashboard shows a spinner while the sync and queries run.

mogrify doesn’t exist on the Snowflake connector. mogrify is a psycopg2 method. The Snowflake cursor has no such method, so use executemany with %s placeholders for batch inserts into shipment_history, as in Chapter 4.

Chapter 7: Bringing It All Together

The full system

With all six chapters complete, the system looks like this:

  • Neo4j Aura. Holds the supply chain graph – 20 suppliers, 12 warehouses, 10 distribution centers and 30 retailers connected by shipping relationships, loaded once in Chapter 2 and updated in real time as shipments arrive. It answers graph questions: what’s the cheapest path from a supplier to a retailer and which warehouse should we use when one goes offline.

  • Snowflake Postgres. Holds the live operational data – every shipment event consumed from Kafka, written in real time by the simulator. It answers OLTP questions: how many shipments are currently delayed, which shipments have been rerouted, when did W003 go offline and when did it come back.

  • Snowflake. Holds the analytical history – shipment data synced from Snowflake Postgres into shipment_history and aggregated for trend analysis. It answers analytical questions: which suppliers have the highest delay rates, which warehouses are breaching SLA thresholds, how has delay volume changed over the last hour.

None of these systems knows about the others. Aura knows nothing about Snowflake Postgres shipment records. Snowflake Postgres and Snowflake know nothing about the graph topology. The intelligence sits in the application layer. The simulator is the only component that talks to the graph: it asks Aura for a route and records the outcome in Snowflake Postgres. The analytics app moves data from Snowflake Postgres into Snowflake, and each dashboard reads from a single system.

Running the system

Once the three systems are set up, running the demo is one short sequence.

First time only – run each notebook once, in order:

  1. 02_neo4j_supply_chain.ipynb – Builds the supply chain graph in Aura.
  2. 03_snowflake_postgres.ipynb – Creates the operational tables in Snowflake Postgres.
  3. 04_snowflake_warehouse.ipynb – Creates the Snowflake database, schema, warehouse and shipment_history table.

Every run:

  1. Stop the Streamlit apps if they’re running.
  2. Start the simulator, 05_simulator.ipynb. It resets the live state in all three systems before it begins, so you never clear anything by hand.
  3. In a terminal: streamlit run app.py.
  4. In a second terminal: streamlit run analytics_app.py --server.port 8502.
  5. Let the simulator run for as long as you like, up to the one-hour limit. The notebook is set to stop after 1,000 events (N_EVENTS = 1000). Change it to -1 for a longer run.
  6. Stop the simulator with the stop button. It restores any warehouse that is still offline.

Give the simulator a few minutes before looking at the analytics dashboard. The charts fill in as data accumulate, and a run of 15 to 20 minutes covers several disruptions.

06_analytics.ipynb is optional. It gives a static snapshot of the same data and uses its own scratch table, so you can run it at any time, ideally with the simulator stopped.

The one rule to remember is to stop the apps before restarting the simulator, because the startup reset empties the tables they read from.

What each system does that the others can’t

It’s worth noting why three systems are better than one for this use case.

Why not just Aura? Neo4j Aura is purpose-built for graph traversals – exactly what the disruption routing requires. Finding an alternative path that avoids an inactive warehouse mid-route is a natural graph query. In Cypher it’s a variable-length path with a WHERE NONE() filter. In SQL it would require a recursive CTE with exclusion logic that grows in complexity with each additional hop. The graph structure also makes it easy to answer questions about connectivity – such as which suppliers can reach which retailers – that would require expensive self-joins in a relational database.

Why not just Snowflake Postgres? Snowflake Postgres is purpose-built for operational data – high-frequency writes from the Kafka consumer, transactional consistency and low-latency reads for the live operations dashboard. It handles thousands of shipment inserts per session exactly as a production Postgres instance would. Standard psycopg2 connectivity, ON CONFLICT DO UPDATE for idempotent upserts and TIMESTAMPTZ for accurate event timing all work exactly as expected.

Why not just Snowflake? Snowflake is excellent for large-scale analytics and historical queries. Aggregating delay trends by minute, computing warehouse health scores across thousands of shipments and ranking suppliers by delay rate are all queries that benefit from Snowflake’s columnar storage and parallel execution. Running these queries against the operational Snowflake Postgres table could add load to the live system and slow down the simulator’s writes.

The three-system architecture isn’t complexity for its own sake. Each system does what it’s genuinely good at and the results are combined at the application layer.

Going further

The system as built is a working demo. Here are some directions for making it more realistic.

Real supply chain topology. The supply chain graph is generated synthetically with a fixed random seed. A natural extension is to load a real supplier-warehouse-retailer network from a CSV or an ERP system export. The MERGE-based loading pattern in Chapter 2 is already idempotent, so replacing the synthetic data with real data requires only changing the source.

Multiple disruptions simultaneously. The simulator disrupts one warehouse at a time. In reality, multiple warehouses can go offline at once – due to weather events, regional power outages or carrier failures. Extending the disruption lifecycle to track a set of disrupted warehouses rather than a single one would make the routing more challenging and the analytics more interesting.

Kafka Streams processing. The simulator currently consumes events one at a time in a tight loop. A natural production extension is to add a Kafka Streams application between the producer and the consumer that enriches events in real time – adding routing decisions, delay predictions or SLA breach flags before they land in Snowflake Postgres. This decouples the enrichment logic from the consumer and scales independently.

Real delay prediction. Delays are currently assigned at random: each shipment has a 15% chance of being delayed, with a uniform 15 to 239 minute duration, independent of suppliers, warehouses and disruptions. A more realistic model would use a machine learning model trained on historical delay data to predict delay duration based on supplier, warehouse, route and time of day, and could link delays to disruptions. The delay_minutes column is already in the schema – replacing the random draw with a model prediction requires only changing the event generation function.

Continuous replication. The incremental sync from Snowflake Postgres to shipment_history is handled in application code. Snowflake Postgres can also mirror tables into Snowflake continuously, which would remove the sync code from the analytics app and keep the warehouse copy current without any application involvement. Check the Snowflake documentation for the current availability of mirroring.

Streamlit Cloud deployment. Both Streamlit apps run locally in this demo. Deploying them to Streamlit Cloud would make the dashboards accessible from any browser without running a local server. You’ll need to commit the requirements.txt, add the credentials to Streamlit’s secrets management and make sure the hosted app can reach your Snowflake Postgres instance over the network.

Free Books

The SingleStore Cookbook: Recipes for Multi-Model, Machine Learning and AI Data Engineering

Read Online ↗

A hands-on cookbook covering SingleStore’s multi-model capabilities, from time series and geospatial data through vector search, machine learning pipelines and AI-powered applications. The recipes draw on first-hand experience building applications with the platform and are organized into four parts:

  1. Multi-Model
  2. Streaming and Big Data Pipelines
  3. Machine Learning
  4. AI and Agentic Frameworks

Seven Vector Databases in Seven Days

Read Online ↗

A practical guide that takes one vector database per day and pairs each with a use case chosen to showcase that database’s strengths. Databases covered:

  1. PostgreSQL and pgvector - Semantic job search
  2. MongoDB Atlas - Recipe finder
  3. Pinecone - E-commerce search
  4. Weaviate - Research paper discovery
  5. Neo4j - Fraud detection
  6. Snowflake - Customer support analytics
  7. Databricks - RAG over internal documents

Each chapter is self-contained, comes with a Jupyter notebook and gives an assessment of when you’d look elsewhere.


Generative AI: A Manager’s Guide

Read Online ↗

A practical guide for managers, directors and executives who need to make decisions about AI in their organizations, not the engineers building it, but the people responsible for making it work well. The book uses a single central metaphor, the Digital Intern, to frame what AI is genuinely good at, where it falls short and what managing it actually requires. It covers governance, risk, board-level accountability, business case building and the organizational change of moving from pilot to embedded capability.


Seven Ways to Do Vector Search in Python

Read Online ↗

A practitioner’s guide that benchmarks seven Python libraries against the same dataset, measuring recall and latency consistently so you can compare like-for-like. Libraries covered:

  1. FAISS
  2. Voyager
  3. Scikit-learn NearestNeighbors
  4. PyNNDescent
  5. USearch
  6. Chroma
  7. LanceDB

Each chapter covers one library, explains what it’s genuinely good at and when you’d reach for something else.


Real-Time Vehicle Tracking with Neo4j, Databricks Lakebase and OpenStreetMap

Read Online ↗

A fleet operations demo that puts ten simulated vehicles onto real road networks loaded from OpenStreetMap. The architecture:

  • Neo4j Aura holds the road network graph
  • Databricks Lakebase stores live vehicle positions
  • Databricks Lakehouse handles historical analytics

Two Streamlit dashboards display live positions and trend data. The primary demo uses the London Borough of Merton, with additional configurations for San Francisco and Singapore.


Real-Time Supply Chain Routing with Neo4j, Snowflake Postgres and Confluent Kafka

Read Online ↗

A supply chain routing demo that combines graph-based route optimization with real-time event streaming. The architecture:

  • Neo4j Aura holds the supplier and logistics network graph
  • Snowflake Postgres stores product and inventory data
  • Confluent Kafka streams supply chain events in real time

Route queries traverse the graph to find optimal paths between suppliers, warehouses and distribution centers under live capacity and disruption constraints.


Neo4j Beyond the Graph

Read Online ↗

A hands-on guide to Neo4j’s multi-model capabilities, going beyond graph traversal to explore seven native features of the platform. Each chapter pairs one capability with a use case chosen to showcase it:

  1. Native Graph - London Underground routing
  2. Full-Text Search - Research paper discovery
  3. Geospatial - Air quality along the Pyrenees corridor
  4. Temporal - S&P 500 stock price analysis
  5. Document-Style Properties - Library inventory management
  6. Vector Search - Fashion image similarity
  7. Graph + Vector - Fraud ring detection

Every chapter comes with a Jupyter notebook and a Streamlit application. All examples run on Neo4j Aura’s free tier.


Seven Ways to Use Jev

Read Online ↗

A practical guide to TypeSafe AI’s structured decision model, exploring seven domains where a single Choice call – structured state in, typed decision out, calibrated confidence alongside – replaces brittle rules-based logic. Each chapter pairs one domain with a hands-on Jupyter notebook and examines what the confidence score reveals about the decision:

  1. Transit Routing - Northern Line branch triage using a NetworkX graph
  2. Customer Support Triage - Ticket routing by team and priority
  3. Financial Fraud Detection - Flag or pass with a three-tier confidence response
  4. Content Moderation - Approve, flag or remove user-generated content
  5. Supply Chain Disruption - Alternative supplier selection under live constraints
  6. Hiring Screening - CV screening against a structured job description
  7. Medical Symptom Triage - Care pathway assignment with a conservative bias

A recurring theme across all seven chapters: the confidence score is more informative than the decision itself for uncertain cases and the threshold between automatic routing and human review is always a business decision, not a technical one.


Lessons Learned Putting Jev Inside Neo4j

Read Online ↗

A hands-on account of building a Neo4j function and batch procedure in Java that ask a model to make a decision about a node and store the answer back in the graph. The book follows one running example, 500 synthetic transactions and each chapter adds the code that the previous chapter’s problem called for. The lessons:

  1. Why in the Database - Where a decision belongs and what the finished function returns
  2. A First Working Function - Converting Neo4j’s types on the way in and out
  3. Failures as Data - Returning errors as values so one bad node can’t stop a query
  4. Testing Without a Network - Seams for the network and the clock, with 68 fast tests
  5. Keeping the Key Safe - Sending a credential only to the host the parser reports
  6. Many at Once - A bounded, ordered pool and a measured concurrency sweep
  7. Running It Locally - Swapping in a small local model and seeing how the judgment changes

Every chapter with code has its own folder in the repository and each Cypher script also comes as a notebook, so you can use whichever you prefer. The results are reported as measured, including the ones we can’t explain!