Project 06 · Streaming + batch

Unified Streaming and Batch Pipeline

Web events arrive through Kafka and mobile app events through Amazon Kinesis. A Flink job deduplicates and sessionizes both streams in real time and writes events and sessions to Apache Iceberg, so product dashboards run a few seconds behind users. Every night a Spark job recomputes the previous day from the raw events, including the ones that arrived late, and atomically replaces those partitions of the same tables. One set of tables, one definition of a session, and two engines that check each other.

  • Apache Kafka
  • Amazon Kinesis
  • Apache Flink
  • Apache Iceberg
  • Apache Spark
Sample data Tuesday, Oct 6, 2026 · reconciliation over the last 14 days Download data (CSV)

Events per hour

Oct 6, by source

Purchase funnel

Sessions reaching each step, Oct 6

Stream vs batch difference

Session counts per day; the nightly batch is the reference

How late events arrive

Delay from event time to arrival, log scale

Sessions by channel

Oct 6

Top pages

Oct 6, web and app combined

Jobs

State at 02:30 on Oct 7

How it works

The stream is fast, the batch is complete, and both write the same tables.

  1. 01 · Collect

    Web and mobile SDKs

    Page views, searches, cart changes and purchases, each with a client event id and timestamp.

  2. 02 · Ingest

    Kafka · Kinesis

    Web events land in Kafka. The mobile backend already runs on AWS, so app events arrive through Kinesis.

  3. 03 · Stream

    Apache Flink

    Reads both streams, drops duplicates by event id, groups events into sessions with a 30-minute gap and commits every 30 s.

  4. 04 · Store

    Apache Iceberg

    One set of tables for both engines: events, sessions and funnel steps, partitioned by day.

  5. 05 · Recompute

    Apache Spark, nightly

    Rebuilds yesterday from raw events, late arrivals included, and replaces just those partitions.

  6. 06 · Reconcile

    Spark SQL

    Compares the streaming and batch results per day and alerts above 0.1%.

  • One definition of a session. Both engines use the same rules: dedupe on event_id, a 30-minute inactivity gap and the same session id, so on-time data gives the same answer in Flink and Spark.
  • Late data is expected. Flink's watermark waits 5 minutes. Anything later is still stored in the raw events table within seconds, and the nightly recompute adds it to sessions.
  • Replace, don't append. The batch job uses Iceberg's dynamic partition overwrite, so a recomputed day replaces the old one in a single commit. Readers see either the old day or the new one, never a mix.
  • The engines check each other. A daily job compares counts by day and platform, using Iceberg time travel to read the streaming result from before the overwrite.

Data model

Both engines write these Iceberg tables. The CSV download is the hourly rollup for the last 14 days.

clickstream.eventsIceberg · appended by Flink · days(event_ts)
event_idstring
Client-generated id; duplicates are dropped
anonymous_id / user_idstring
Device id, and the account id once known
platform / sourcestring
web or ios/android; kafka or kinesis
event_typestring
page_view, search, add_to_cart, purchase and others
pagestring
Route template, for example /p/{product}
event_ts / ingest_tstimestamp
When it happened and when it arrived
clickstream.sessionsIceberg · Flink writes, Spark replaces nightly
session_idstring
md5 of anonymous_id and the session start
anonymous_idstring
Device
platform / channelstring
Where the session happened and how it started
started_at / ended_attimestamp
First event, and last event plus the 30-minute gap
events / page_viewsint
Activity in the session
convertedboolean
True when the session has a purchase
events_hourlyRollup · the CSV download
hour_starttimestamp
Hour, US Central
web_events / mobile_eventsbigint
Events by source
sessions / purchasesbigint
Sessions started and purchases made
late_eventsbigint
Events that arrived after the watermark

Key code

The Flink sessionization job, the nightly Spark recompute, and the query that reconciles them.

Flink SQL: both sources unioned, deduplicated by event_id, and grouped into sessions with a 30-minute gap.

CREATE CATALOG lake WITH (
  'type' = 'iceberg',
  'catalog-impl' = 'org.apache.iceberg.aws.glue.GlueCatalog',
  'io-impl' = 'org.apache.iceberg.aws.s3.S3FileIO',
  'warehouse' = 's3://clickstream-lake/warehouse'
);

CREATE TEMPORARY TABLE web_events (
  event_id STRING, anonymous_id STRING, user_id STRING, event_type STRING, page STRING,
  event_ts TIMESTAMP_LTZ(3),
  WATERMARK FOR event_ts AS event_ts - INTERVAL '5' MINUTE
) WITH (
  'connector' = 'kafka',
  'topic' = 'web.events',
  'properties.bootstrap.servers' = 'kafka:9092',
  'properties.group.id' = 'flink-sessionize',
  'scan.startup.mode' = 'group-offsets',
  'properties.auto.offset.reset' = 'latest',
  'format' = 'json'
);

CREATE TEMPORARY TABLE mobile_events (
  event_id STRING, anonymous_id STRING, user_id STRING, event_type STRING, page STRING, os STRING,
  event_ts TIMESTAMP_LTZ(3),
  WATERMARK FOR event_ts AS event_ts - INTERVAL '5' MINUTE
) WITH (
  'connector' = 'kinesis',
  'stream.arn' = 'arn:aws:kinesis:us-east-1:111122223333:stream/mobile-events',
  'aws.region' = 'us-east-1',
  'source.init.position' = 'LATEST',
  'format' = 'json'
);

-- keep the first copy of each event (state expires after an hour)
SET 'table.exec.state.ttl' = '1 h';

CREATE TEMPORARY VIEW all_events AS
SELECT * FROM (
  SELECT *, ROW_NUMBER() OVER (PARTITION BY event_id ORDER BY proc_time) AS rn
  FROM (
    SELECT event_id, anonymous_id, user_id, event_type, page, 'web' AS platform, event_ts, PROCTIME() AS proc_time FROM web_events
    UNION ALL
    SELECT event_id, anonymous_id, user_id, event_type, page, os AS platform, event_ts, PROCTIME() AS proc_time FROM mobile_events
  )
) WHERE rn = 1;

INSERT INTO lake.clickstream.sessions
SELECT
  MD5(CONCAT(anonymous_id, '|', DATE_FORMAT(window_start, 'yyyy-MM-dd HH:mm:ss.SSS'))) AS session_id,
  anonymous_id,
  FIRST_VALUE(platform)                                AS platform,
  window_start                                         AS started_at,
  window_end                                           AS ended_at,
  COUNT(*)                                             AS events,
  COUNT(*) FILTER (WHERE event_type = 'page_view')     AS page_views,
  MAX(CASE WHEN event_type = 'purchase' THEN 1 ELSE 0 END) = 1 AS converted
FROM TABLE(
  SESSION(TABLE all_events PARTITION BY anonymous_id, DESCRIPTOR(event_ts), INTERVAL '30' MINUTES)
)
GROUP BY window_start, window_end, anonymous_id;

Engineering notes

The Sep 30 upgrade

Flink was upgraded and restarted from a savepoint. Nine minutes of mobile events were processed after their sessions had closed, so the streaming count for that day was 0.14% low until the nightly recompute corrected it.

Late, not lost

About 0.8% of events arrive after the 5-minute watermark, mostly from mobile apps that were offline. They are in the raw events table within seconds and in sessions after the nightly run.

Seconds behind users

Product dashboards read sessions about 4 seconds behind real time at p95, instead of waiting for a nightly job.

Cheap to reprocess

Recomputing one day takes about 38 minutes. A month-long backfill runs as one job per day, in parallel, and each day lands as its own commit.

About the data. Every number on this page is sample data from a seeded generator written for this project. It is shaped to behave like a real workload, with daily peaks, late events and the odd incident, but it does not come from any employer or client. Download it as events_hourly.csv or read data.json.