Real-time Analytics Pipeline

Bookshop's real-time pipeline: MySQL, Debezium (CDC), Kafka, a stream processor, a serving store, a dashboard. The processor can be a small consumer, Flink…

At a badminton match, every point shows up on the scoreboard at once, because someone watches the court and adds it right then. The official match report comes out only afterwards. If the scorekeeper is unsure whether he already counted the last point, and counts it again, the score is wrong in front of everyone. A real-time analytics pipeline is that scorekeeper: it carries each event in the app all the way to a number on a dashboard within seconds, not the next morning. This lesson walks the pipeline, and how to keep the scorekeeper from counting twice.

From checkout to dashboard

The owner's question is different this time: not "best seller per week" but "how many books sold today, right now?". The path joins up the earlier lessons:

Stage Its job At Bookshop
Source the app writes orders MySQL, table order_items
CDC reads every change from the binlog Debezium 3.7 (the CDC lesson)
Broker keeps events in order Kafka topic bookshop.bookshop.order_items (the Kafka lesson)
Stream processor counts continuously a small consumer, or Flink, Kafka Streams, RisingWave, Materialize
Serving store where the dashboard asks Postgres; for big data ClickHouse, Druid, Pinot
Dashboard shows the number Metabase, Superset, Grafana

In the Lab, 22 events flow down this path: 19 order lines (op = c, insert), then Saturday's three changes from the CDC lesson: duplicate item 9 deleted at 09:00 (d), item 16's price fixed (u), and Siti's item 20 arriving at 10:15:30 (c).

Stream processor: who does the counting

A stream processor is a program that runs all the time, reads events as they arrive and updates its result. The options people use as of October 2026:

-- RisingWave or Materialize: order_items here is a table fed by CDC
CREATE MATERIALIZED VIEW daily_sold AS
SELECT CAST(created_at AS DATE) AS order_date, SUM(qty) AS books
FROM order_items
GROUP BY CAST(created_at AS DATE);

In plain PostgreSQL the same materialized view changes only when you run REFRESH MATERIALIZED VIEW. In a streaming database, inserts, updates and deletes from CDC change the numbers right away.

The simplest option, and the one the Lab models: a small consumer (Python with kafka-python from the Kafka lesson, say) that writes each event to a Postgres table, with the dashboard running a SELECT.

Windows by order time

A stream never ends, so "add everything up" is not possible. Windowing means grouping events by time ranges (every 5 minutes, every hour, every day) and summing per range. "Today" is a tumbling 1-day window from the Streaming vs Batch lesson: fixed slices, each order lands in exactly one day. What decides the day is the event time, the order's created_at, not the time the event reached the consumer. The dashboard after 22 events, from SQLite in the Lab:

order_date books
2026-09-28 3
2026-10-01 3
2026-10-09 1
2026-10-10 1

(8 other days left out; 3 October does not appear because nothing sold.) Before Saturday, 1 October read 5: duplicate item 9 was counted. Saturday's 09:00 delete corrects the 1 October window, not Saturday's, because the row belongs to a 1 October order. A stream from CDC is not just additions: updates and deletes change old windows.

The other windows from that lesson still apply. A sliding window of the last 24 hours at Saturday 10:16 holds 2 books (Friday's order 14 at 10:30 is still in). And late events are still a problem: in the Streaming vs Batch lesson, the week of 28 September came out as 12 books with no watermark wait and 13 with a 12-hour wait.

Serving store: where the dashboard asks

At-least-once and idempotent sinks

The Lab's consumer commits its offset every 5 events. After the 22nd event (offset 21), the last commit covers offsets 0 to 19, so the committed offset is 20, where the next consumer starts. If the consumer dies here, its replacement reads offsets 20 and 21 again. This is at-least-once from the Kafka lesson: an event may arrive twice, none is lost. What decides the result is the sink, the way the consumer writes to the serving store.

A sink that adds:

INSERT INTO daily_sold (order_date, books) VALUES ('2026-10-10', 1)
ON CONFLICT (order_date) DO UPDATE SET books = books + excluded.books;

A sink that writes one row per item (an upsert), with the dashboard summing:

INSERT INTO item_live (item_id, order_date, qty) VALUES (20, '2026-10-10', 1)
ON CONFLICT (item_id) DO UPDATE SET order_date = excluded.order_date, qty = excluded.qty;
-- a d event: DELETE FROM item_live WHERE item_id = 9;

SELECT order_date, SUM(qty) AS books FROM item_live GROUP BY order_date ORDER BY order_date;

On Saturday at 10:15:30 Siti buys 1 This Earth of Mankind (order 15), and its event sits at offset 21. The consumer has saved it to the dashboard's table, then dies before it can commit. Guess first: once offsets 20 and 21 are read again, does "Sold today" say 1 or 2? It depends on the sink: the adding sink says 2 books, the upsert says 1. Only the idempotent one is right.

Exactly-once means each event takes effect exactly once. Flink achieves it inside itself with checkpoints: the state and the Kafka offsets are saved together, and after a crash both are restored together. Kafka Streams has processing.guarantee=exactly_once_v2, but that covers writes to Kafka itself. As soon as results are written to another database, rows that went in after the last checkpoint can be written again. So all the way to the dashboard, exactly-once needs a transactional sink or an idempotent one. The second is far simpler.

Cost, and whether Bookshop needs this

Honestly: not yet. Bookshop has 1 to 3 orders a day. The path above means Kafka, Kafka Connect with Debezium and a processor, all running 24 hours, monitored, upgraded and paid for every month, for a number that changes three times a day. The owner's question can be answered by one query on a read replica (a copy of the database used only for reading), run by the dashboard every few minutes:

SELECT COALESCE(SUM(oi.qty), 0) AS sold_today
FROM order_items oi
JOIN orders o ON o.id = oi.order_id
WHERE o.created_at >= CURDATE();

Real time pays off when a decision must be made within seconds: holding a suspicious payment, locking stock in a flash sale, sounding an alarm when checkouts fail in a row. Or when querying the database directly has become too heavy because the data is big.

⚠️ Dashboards that miscount, or get built too early

TL;DR: Bookshop's real-time pipeline: MySQL, Debezium (CDC), Kafka, a stream processor, a serving store, a dashboard. The processor can be a small consumer, Flink 2.3 (state + checkpoints), Kafka Streams (a library in Kafka 4.3), or the streaming databases RisingWave 3.1 and Materialize, which update a CREATE MATERIALIZED VIEW on every event. The serving store is Postgres for small data, ClickHouse, Druid or Pinot for big data. "Today" is a tumbling 1-day window by order time (event time), and a d event corrects its old window (1 October from 5 to 3). Kafka gives at-least-once, so an event can arrive twice: the adding sink says 2, the idempotent upsert by item_id stays at 1. Flink's exactly-once holds inside Flink; reaching the database needs an idempotent or transactional sink. For 1 to 3 orders a day, a query on a read replica every few minutes is enough.

⚡ From checkout to dashboard

Meaning: The path: MySQL, Debezium (CDC), Kafka, a stream processor, a serving store, then the dashboard. Each event becomes a number on the dashboard within seconds, not the next morning.

⚡ A live materialized view

Write:

CREATE MATERIALIZED VIEW daily_sold AS
SELECT CAST(created_at AS DATE) AS order_date, SUM(qty) AS books
FROM order_items
GROUP BY CAST(created_at AS DATE);

Meaning: In a streaming database such as RisingWave or Materialize, the result is updated on every event. In plain PostgreSQL, this view only changes after REFRESH MATERIALIZED VIEW.

⚡ Windows by order time

Meaning: Windowing groups events by time ranges so they can be summed. "Today" is a tumbling 1-day window by the order's created_at. Saturday's delete of item 9 corrects the 1 October window, from 5 to 3.

⚡ An idempotent sink

Guess first: Siti's order event has been saved, then the consumer dies before it can commit. Once it is read again, does "Sold today" say 1 or 2?

It depends on the sink. The adding sink says 2 books, the upsert by item_id stays at 1. Only the idempotent one is right.

Meaning: Kafka gives at-least-once, so write to the serving store by a unique key, don't add.

What you will learn