Skip to content

Block 2 — RisingWave: The In-Memory Streaming MV Store

Duration: 30 min


What RisingWave Is Best At

RisingWave is the write-complexity end of the trade-off Block 1 laid out. It maintains each materialized view incrementally as events arrive: a streaming operator graph does the aggregation work up front, on the write path, and keeps the result sitting in memory. By the time you query, there is nothing left to compute — a read is a trivial keyed lookup of an already-computed result, so its latency floor is very low and, crucially, flat with history size. A view over a billion ingested rows reads just as fast as one over a thousand.

That is the opposite of TimescaleDB's raw GROUP BY or Athena, which defer all the aggregation to read time and pay it — growing with data volume — on every query. Use RisingWave when the aggregation is known in advance and you want it always current: latest-value-per-key, rolling windowed averages, the metrics behind a live dashboard.

Keep the two axes separate

  • Freshness (now − MAX(ts)) is driven by the ingestion path — how fast a new event reaches the view. RisingWave's freshness risk is checkpoint/ingestion lag, never a slow read.
  • Query latency is the read cost. RisingWave serves from in-memory MV state, so its floor is very low and flat regardless of how much history exists.

The Materialized Views

Below is RisingWave's own DDL — the sources it consumes and the views it maintains. The views are the point: each one names an aggregation that is kept continuously up to date, so the read side never recomputes it.

View source — ddl-cloud.sql

Open in GitHub

-- RisingWave DDL — cloud EKS instance
--
-- Applied automatically by the post-install/post-upgrade Job in
-- helm/cloud-analytics/templates/risingwave-ddl-job.yaml (mirrors the pattern
-- in helm/edge-stack/templates/risingwave-ddl-job.yaml) — the Job reads the
-- __MSK_BOOTSTRAP__/__MSK_USER__/__MSK_PASS__ placeholders below from the
-- in-cluster `msk-credentials` Secret, substitutes them itself, then pipes
-- the result through psql. No manual sed/psql step (see #159).
--
-- #207: `CREATE ... IF NOT EXISTS` alone only guards against a re-run erroring
-- on an already-existing NAME — it does NOT re-apply a changed DEFINITION, so
-- on a slot where these objects were already created by an earlier version of
-- this file, a `helm upgrade` carrying updated SQL silently no-ops forever.
-- Confirmed live on ws-slot90: `sensors_raw_telemetry` still had its pre-#195
-- column list (no `deployment_id`) and `mv_sensor_fleet_latest` still read
-- straight off the unscoped sources, despite both having been fixed on `main`
-- for hours. Every object below is now DROPped (reverse dependency order)
-- immediately before being recreated, so a definition/schema change here
-- always reaches an already-provisioned slot on its next upgrade, not just a
-- fresh one.
--
-- To re-apply by hand for debugging, port-forward the frontend service and
-- substitute the same placeholders yourself:
--   kubectl port-forward -n cloud-analytics svc/risingwave-cloud-frontend 4567:4567
--
-- #253 — SHARED instance, filtered by deployment_id (not per-slot anymore):
--   cloud-analytics used to be deployed once PER SLOT, each filtered down to
--   its own slot's rows (the `sim_this_slot` / `telemetry_this_slot` views,
--   WHERE-filtered on a per-slot placeholder substituted from
--   `.Values.deploymentId`). That hit a hard EKS capacity ceiling — every slot
--   added a full RisingWave+TimescaleDB+dashboard stack. cloud-analytics is
--   now ONE shared release for every slot: the two views below no longer
--   filter anything, they only CARRY `deployment_id` through (sim rows derive
--   it from the `^ws-slot[0-9]+` prefix of `site_id`; device rows already
--   have it as a column, stamped by the IoT rule — see
--   amplify/custom/participant-stack.ts). Every MV downstream promotes
--   `deployment_id` into its SELECT list and GROUP BY, so one shared instance
--   still reports separate numbers per slot — the dashboard/API layer
--   (cloud-dashboard, frontend) filters reads to the caller's slot via
--   `?did=`.
--
--   scan.startup.mode stays 'latest' (NOT 'earliest'): 'earliest' would replay
--   the entire shared-topic history (observed ~7.5M rows) on every source
--   restart — the exact startup-backfill memory spike that caused the compute
--   OOM fixed in PR #191.

-- ─────────────────────────────────────────────────────────────────────────────
-- #207: drop every object this file (re)creates, in reverse dependency order,
-- before recreating any of them below.
--
-- The pg_sleep after DROP SUBSCRIPTION is load-bearing, confirmed live: a
-- subscription's teardown (its underlying streaming job) is asynchronous —
-- DROP SUBSCRIPTION returns success immediately, but a subsequent DROP
-- MATERIALIZED VIEW on the relation it read from can still fail with "table
-- used by 1 other objects" for a few seconds afterward, until that teardown
-- actually completes. Nothing else here tears down a streaming job on drop,
-- so no further delay is needed.
-- ─────────────────────────────────────────────────────────────────────────────
DROP SUBSCRIPTION IF EXISTS dashboard_freshness_sub;
SELECT pg_sleep(5);
DROP MATERIALIZED VIEW IF EXISTS mv_fleet_1min_avg;
DROP MATERIALIZED VIEW IF EXISTS mv_sensor_fleet_latest;
DROP MATERIALIZED VIEW IF EXISTS mv_device_hop_latency;
DROP VIEW IF EXISTS telemetry_all_slots;
DROP VIEW IF EXISTS sim_all_slots;
DROP SOURCE IF EXISTS sensors_raw_telemetry;
DROP SOURCE IF EXISTS sensors_raw_cloud;

-- ─────────────────────────────────────────────────────────────────────────────
-- Source: raw sensor telemetry from MSK (sim site — normalised sensor schema)
-- ─────────────────────────────────────────────────────────────────────────────
CREATE SOURCE IF NOT EXISTS sensors_raw_cloud (
    sensor    VARCHAR,
    site_id   VARCHAR,
    value     DOUBLE,
    unit      VARCHAR,
    ts_ms     BIGINT
)
WITH (
    connector                        = 'kafka',
    topic                            = 'sensors.raw.sim',
    properties.bootstrap.server      = '__MSK_BOOTSTRAP__',
    properties.security.protocol     = 'SASL_SSL',
    properties.sasl.mechanism        = 'SCRAM-SHA-512',
    properties.sasl.username         = '__MSK_USER__',
    properties.sasl.password         = '__MSK_PASS__',
    scan.startup.mode                = 'latest'
)
FORMAT PLAIN ENCODE JSON;

-- ─────────────────────────────────────────────────────────────────────────────
-- Source: flat IoT node telemetry (cpu/mem/disk/net from EC2 instances via IoT Core)
-- ─────────────────────────────────────────────────────────────────────────────
CREATE SOURCE IF NOT EXISTS sensors_raw_telemetry (
    thing_name         VARCHAR,
    deployment_id      VARCHAR,
    cpu_pct            DOUBLE,
    mem_used_pct       DOUBLE,
    disk_used_pct      DOUBLE,
    net_io_bytes_sent  BIGINT,
    net_io_bytes_recv  BIGINT,
    message_timestamp  BIGINT,
    ingest_ts          BIGINT
)
WITH (
    connector                        = 'kafka',
    topic                            = 'raw.telemetry',
    properties.bootstrap.server      = '__MSK_BOOTSTRAP__',
    properties.security.protocol     = 'SASL_SSL',
    properties.sasl.mechanism        = 'SCRAM-SHA-512',
    properties.sasl.username         = '__MSK_USER__',
    properties.sasl.password         = '__MSK_PASS__',
    scan.startup.mode                = 'latest'
)
FORMAT PLAIN ENCODE JSON;

-- ─────────────────────────────────────────────────────────────────────────────
-- #253: fleet-wide views (plain CREATE VIEW — no extra streaming state or
-- memory footprint) that carry `deployment_id` through from each source,
-- WITHOUT filtering to any one slot — the shared instance serves every slot
-- from these same two sources.
--   - sim rows: derive deployment_id from the `^ws-slot[0-9]+` prefix of
--     site_id (SITE_ID env on the sensor-sim EC2 is `${deploymentId}-sim`).
--   - device rows: deployment_id is already a column (the IoT→MSK rule
--     stamps `'<deploymentId>' AS deployment_id`).
-- The MVs below select from these views, never from the raw sources directly,
-- so deployment_id derivation is enforced in exactly one place.
-- ─────────────────────────────────────────────────────────────────────────────
-- deployment_id is the `^ws-slot[0-9]+` prefix of site_id. Use regexp_match, NOT
-- the SQL-standard `substring(x FROM pattern)` POSIX form: RisingWave does not
-- support the pattern form of substring — it reads the second argument as an
-- integer start position and fails at runtime with `str_parse('^ws-slot...')` /
-- "integer invalid digit found in string", which broke the whole DDL hook job.
-- CI never catches this because the DDL is only ever applied against a live
-- RisingWave, not in CI. regexp_match returns a varchar[]; element [1] is the
-- whole match (no capture group needed).
CREATE VIEW IF NOT EXISTS sim_all_slots AS
SELECT sensor, site_id, value, unit, ts_ms,
       (regexp_match(site_id, '^ws-slot[0-9]+'))[1] AS deployment_id
FROM sensors_raw_cloud;

CREATE VIEW IF NOT EXISTS telemetry_all_slots AS
SELECT thing_name, deployment_id, cpu_pct, mem_used_pct, disk_used_pct,
       net_io_bytes_sent, net_io_bytes_recv, message_timestamp, ingest_ts
FROM sensors_raw_telemetry;

-- ─────────────────────────────────────────────────────────────────────────────
-- Materialized view: latest reading per sensor per site across the entire fleet.
-- Unions industrial sensor data (sensors_raw_cloud) with IoT node telemetry
-- (sensors_raw_telemetry unpivoted into per-metric rows). deployment_id is
-- carried into the SELECT list and GROUP BY (#253) so one shared MV still
-- reports a distinct row per (sensor, site_id) *per slot*.
-- Uses array_agg ORDER BY to pick the value with the highest ts_ms.
-- ─────────────────────────────────────────────────────────────────────────────
CREATE MATERIALIZED VIEW IF NOT EXISTS mv_sensor_fleet_latest AS
SELECT
    sensor,
    site_id,
    deployment_id,
    (array_agg(value ORDER BY ts_ms DESC))[1]  AS value,
    (array_agg(unit  ORDER BY ts_ms DESC))[1]  AS unit,
    MAX(ts_ms)                                  AS ts_ms
FROM (
    SELECT sensor, site_id, value, unit, ts_ms, deployment_id FROM sim_all_slots
    UNION ALL
    SELECT 'cpu_pct'           AS sensor, thing_name AS site_id, cpu_pct                    AS value, 'percent' AS unit, ingest_ts AS ts_ms, deployment_id FROM telemetry_all_slots
    UNION ALL
    SELECT 'mem_used_pct'      AS sensor, thing_name AS site_id, mem_used_pct               AS value, 'percent' AS unit, ingest_ts AS ts_ms, deployment_id FROM telemetry_all_slots
    UNION ALL
    SELECT 'disk_used_pct'     AS sensor, thing_name AS site_id, disk_used_pct              AS value, 'percent' AS unit, ingest_ts AS ts_ms, deployment_id FROM telemetry_all_slots
    UNION ALL
    SELECT 'net_io_bytes_sent' AS sensor, thing_name AS site_id, net_io_bytes_sent::DOUBLE  AS value, 'bytes'   AS unit, ingest_ts AS ts_ms, deployment_id FROM telemetry_all_slots
    UNION ALL
    SELECT 'net_io_bytes_recv' AS sensor, thing_name AS site_id, net_io_bytes_recv::DOUBLE  AS value, 'bytes'   AS unit, ingest_ts AS ts_ms, deployment_id FROM telemetry_all_slots
) combined
GROUP BY sensor, site_id, deployment_id;

-- ─────────────────────────────────────────────────────────────────────────────
-- Materialized view: 1-minute tumbling window averages per sensor per site.
-- Buckets by (ts_ms / 60000) integer division — no watermark required.
-- deployment_id carried into SELECT + GROUP BY (#253).
-- ─────────────────────────────────────────────────────────────────────────────
CREATE MATERIALIZED VIEW IF NOT EXISTS mv_fleet_1min_avg AS
SELECT
    sensor,
    site_id,
    deployment_id,
    AVG(value)                                            AS avg_value,
    MIN(value)                                            AS min_value,
    MAX(value)                                            AS max_value,
    COUNT(*)                                              AS sample_count,
    to_timestamp((ts_ms / 60000) * 60)                   AS window_start,
    to_timestamp((ts_ms / 60000) * 60 + 60)              AS window_end
FROM (
    SELECT sensor, site_id, value, ts_ms, deployment_id FROM sim_all_slots
    UNION ALL
    SELECT 'cpu_pct'           AS sensor, thing_name AS site_id, cpu_pct                   AS value, ingest_ts AS ts_ms, deployment_id FROM telemetry_all_slots
    UNION ALL
    SELECT 'mem_used_pct'      AS sensor, thing_name AS site_id, mem_used_pct              AS value, ingest_ts AS ts_ms, deployment_id FROM telemetry_all_slots
    UNION ALL
    SELECT 'disk_used_pct'     AS sensor, thing_name AS site_id, disk_used_pct             AS value, ingest_ts AS ts_ms, deployment_id FROM telemetry_all_slots
    UNION ALL
    SELECT 'net_io_bytes_sent' AS sensor, thing_name AS site_id, net_io_bytes_sent::DOUBLE AS value, ingest_ts AS ts_ms, deployment_id FROM telemetry_all_slots
    UNION ALL
    SELECT 'net_io_bytes_recv' AS sensor, thing_name AS site_id, net_io_bytes_recv::DOUBLE AS value, ingest_ts AS ts_ms, deployment_id FROM telemetry_all_slots
) combined
GROUP BY sensor, site_id, deployment_id, (ts_ms / 60000);

-- ─────────────────────────────────────────────────────────────────────────────
-- Materialized view: device→ingest hop latency (#245). ingest_ts is stamped by
-- the IoT Rule on arrival; message_timestamp is stamped by the device
-- immediately before publish (see job-scripts/telemetry-v*.sh). The delta is
-- the device-side MQTT publish + network hop -- upstream of any data store,
-- and currently the dominant segment of the sensor→dashboard budget (#246).
-- Bounded to a recent window (temporal filter, mirrors the TSDB 15-min
-- freshness window in cloud-dashboard/src/lib/freshness-queries.ts) so the
-- number reflects current behaviour rather than a lifetime average, and so
-- this MV's state stays bounded. Grouped by deployment_id (#253) so the
-- shared instance reports one hop-latency number per slot, not one fleet-wide
-- average across every slot's devices. Degrades to NULL (not an error) when no
-- row in a slot's window carries message_timestamp -- e.g. older payloads or a
-- sim-only slot with no device telemetry.
-- ─────────────────────────────────────────────────────────────────────────────
CREATE MATERIALIZED VIEW IF NOT EXISTS mv_device_hop_latency AS
SELECT
    deployment_id,
    AVG(ingest_ts - message_timestamp) AS avg_device_hop_ms,
    COUNT(*)                            AS sample_count
FROM telemetry_all_slots
WHERE message_timestamp IS NOT NULL
  AND to_timestamp(ingest_ts / 1000) > now() - INTERVAL '15' MINUTE
GROUP BY deployment_id;

-- ─────────────────────────────────────────────────────────────────────────────
-- Subscription: change feed on mv_sensor_fleet_latest for the cloud dashboard's
-- push path (#160). A server-side consumer (cloud-dashboard's
-- /api/stream/risingwave route) declares a SUBSCRIPTION CURSOR against this
-- and relays each change event to the browser over SSE — no client polling.
-- Every slot shares this one subscription; each connected browser's route
-- re-aggregates mv_sensor_fleet_latest filtered to its own `?did=` on every
-- change event, so a change to slot A's rows doesn't push stale-looking data
-- to slot B's viewers, it just wakes them to re-read their own, unaffected rows.
-- Retention window bounds how far a newly-connected cursor can look back.
-- ─────────────────────────────────────────────────────────────────────────────
CREATE SUBSCRIPTION IF NOT EXISTS dashboard_freshness_sub
FROM mv_sensor_fleet_latest
WITH (retention = '1D');
Object Kind Purpose
sensors_raw_cloud Source Industrial sensor readings from MSK topic sensors.raw.sim
sensors_raw_telemetry Source EC2 node telemetry (cpu/mem/disk/net) from MSK topic raw.telemetry
mv_sensor_fleet_latest Materialized view Latest reading per sensor per site, across both sources — grouped by deployment_id too, since one shared instance now serves every slot
mv_fleet_1min_avg Materialized view 1-minute tumbling-window averages per sensor per site, also grouped by deployment_id
dashboard_freshness_sub Subscription Change feed on mv_sensor_fleet_latest — the push path the dashboard consumes (Block 6)

One shared instance, filtered by deployment_id

RisingWave here is one shared instance for the whole workshop, not one per slot — it reads the same shared MSK topics every slot publishes to, and every view/MV below carries a deployment_id column instead of being scoped to a single slot. The queries in this block filter on deployment_id = 'ws-slot00' so you only see your own devices; the dashboard (Block 6) does the same filtering via its ?did= param.

Each source declares the shape of a Kafka/MSK stream; each CREATE MATERIALIZED VIEW compiles into an operator graph that updates on every arriving row. The cost of the aggregation is paid there, incrementally — not on your query.

RisingWave streaming-SQL constraints (v2.8.x)

These shape how the views above are written — worth knowing before you author your own:

  • MAX_BY(val, ts) is not available — use (array_agg(val ORDER BY ts DESC))[1].
  • TUMBLE(src, proctime(), ...) is only valid for tables with declared watermarks — use integer epoch-bucketing for sources without watermarks.
  • The Kafka topic must exist before CREATE SOURCE — wildcard topics are not supported.

Connect and Verify

RisingWave speaks the PostgreSQL wire protocol on port 4567, so any psql client works:

kubectl port-forward -n cloud-analytics svc/risingwave-cloud-frontend 4567:4567 > /tmp/rw-cloud-pf.log 2>&1 &
RW_PF_PID=$!
until grep -q "Forwarding from" /tmp/rw-cloud-pf.log 2>/dev/null; do sleep 1; done
psql -h localhost -p 4567 -U root -d dev -c "SHOW MATERIALIZED VIEWS;"
kill "$RW_PF_PID" 2>/dev/null || true

Or leave the port-forward running in another terminal and connect interactively with psql -h localhost -p 4567 -U root -d dev.


Query the Views

The EC2 nodes you provisioned in earlier sessions have been publishing cpu/mem/disk/net node telemetry to the raw.telemetry topic all along, so that data is already flowing through these views right now — the industrial sensor sim (sensors_raw_cloud) comes online later, once your edge nodes are running (Session 5). The shared instance holds every slot's rows, so filter on deployment_id to see only your own devices:

-- Poll the raw telemetry as it lands from your EC2 instances. Unlike the sim
-- source, this one carries deployment_id directly (the IoT rule stamps it —
-- see amplify/custom/participant-stack.ts), so you can filter to your own slot.
SELECT thing_name,
       round(cpu_pct::numeric, 1)       AS cpu_pct,
       round(mem_used_pct::numeric, 1)  AS mem_pct,
       round(disk_used_pct::numeric, 1) AS disk_pct
FROM sensors_raw_telemetry
WHERE deployment_id = 'ws-slot00'
LIMIT 10;

-- Latest reading per metric per node — your slot only. The telemetry columns
-- are unpivoted into per-metric rows (sensor = cpu_pct, mem_used_pct, …;
-- site_id = the thing_name), unioned with the sim site (offline until Session 5).
SELECT sensor, site_id, round(value::numeric, 2) AS value, unit
FROM mv_sensor_fleet_latest
WHERE deployment_id = 'ws-slot00'
ORDER BY site_id, sensor;

-- 1-minute bucket averages — your slot only
SELECT sensor, site_id, round(avg_value::numeric, 2) AS avg_v,
       sample_count, window_start
FROM mv_fleet_1min_avg
WHERE deployment_id = 'ws-slot00'
ORDER BY site_id, sensor, window_start DESC;

Query the views and observe sub-100 ms response times — and notice they stay that fast whether the fleet has been running for a minute or a month.

Why is the MV always fast?

RisingWave already did the aggregation on the write path. Each new row updates the view's state in memory through its operator graph — nothing is recomputed from scratch at query time. So the read is a single keyed lookup into an already-materialized result: read cost is independent of fleet size and history depth. This is the payoff for paying aggregation complexity at ingest time.


The Subscription Behind the Push Path

Because the view is always current, you don't have to poll it — RisingWave can push each change to you. dashboard_freshness_sub is a subscription: a change feed over the same pg wire protocol, no Kafka topic and no extra broker involved.

DECLARE dashboard_freshness_cur SUBSCRIPTION CURSOR FOR dashboard_freshness_sub;
FETCH 10 FROM dashboard_freshness_cur;
CLOSE dashboard_freshness_cur;

Each FETCH returns the rows of mv_sensor_fleet_latest that changed since the last fetch. The dashboard's server-side route holds one long-lived connection doing exactly this in a loop and forwards each change to the browser over SSE — the live-push tier you compare against the others in Block 6.


Reference