DownloadFiles icon
Queries icon
Log icon
If icon
UploadFiles icon
Set icon
Fail icon
Schedule icon

Dynamic Event Sessionization and Inactivity Compaction with DuckDB

Ingest event streams, stitch active sessions across runs, derive bounce attribution, and compact lakehouse marts in DuckDB with quality invariant gates.

Categories
Data

Clickstream telemetry arrives with irregular intervals, out-of-order timestamps, and duplicate retries. Fixed tumbling windows split visits across batch boundaries, distorting duration and bounce metrics.

This blueprint implements a demo-scale in-engine event sessionization and compaction pipeline using DuckDB. It ingests micro-batch event streams, hydrates cumulative marts and active buffers across runs via Namespace Files, resolves inactivity timeouts, stitches continuous visits, and gates publication on quality invariants before writing to storage and the KV store.

How it works

  1. Dual State Hydration: DownloadFiles loads cumulative sessions (/lakehouse/sessions_mart.parquet) and active buffers (/lakehouse/state/active_sessions.parquet) from Namespace Files.
  2. Demo Staging: When input URIs are empty, stage_demo_events generates demo Parquet streams adapting between bootstrap and incremental arrivals.
  3. Sessionization & Compaction: sessionize_and_compact_stream executes DuckDB window functions to drop retries, derive monotonic watermarks, filter late arrivals, detect session boundaries via gaps-and-islands, and partition visits into sealed and active states.
  4. Reconciliation Invariants: Validates raw accounting, row conservation, PK uniqueness, temporal disjointness, inactivity gaps, active isolation, user integrity, null checks, conflict prevention, discard tolerance, and bounce attribution. Structural anomalies halt promotion and retain staging files for inspection.
  5. Scorecard & Gating: log_session_scorecard outputs the scorecard, and sessionization_gate (If) publishes Parquet to Namespace Files (UploadFiles) and writes metadata to the KV store (kv.Set) on pass, or halts via Fail on violation.

What you get

  • In-engine sessionization in DuckDB without external streaming clusters.
  • Dual-dataset hydration via Namespace Files preventing visit fragmentation.
  • Cross-run replay deduplication and event ID conflict detection.
  • Monotonic watermark tracking preventing premature sealing during empty batches.
  • Monotonic sealed session accumulation with zero active-state leakage.
  • Row-conservation, temporal, and bounce reconciliation gates.
  • Mart publishing to Namespace Files paired with KV store discovery.

Who it's for

  • Analytics engineers transforming raw clickstream into sessionized data marts.
  • Growth teams tracking conversion attribution and user journeys.
  • Data engineers building micro-batch lakehouse compaction pipelines.

Why orchestrate this with Kestra

  • Embedded Compute: Runs DuckDB in the Kestra worker with zero external database dependencies.
  • Stateful Hydration: Hydrates active buffers and historical marts across runs using Namespace Files.
  • Circuit Breaker Gates: Flow controls (If and Fail) isolate anomalies before bad metrics reach dashboards.
  • Auditable Metadata: Emits key session metrics directly into the KV store.

Prerequisites

  • Kestra with the DuckDB plugin (plugin-jdbc-duckdb).

Secrets

  • None required.

Quick start

  1. Run Clean Sessionization: Execute with defaults. The demo is a two-run walkthrough: Run 1 bootstraps initial state (2 sealed sessions, 4 active visits); Run 2 hydrates both files and expands the mart to 3 sealed sessions. To reset, delete the two files in Namespace Files.
  2. Verify Gate Failure: Execute with inject_corrupt_event: true. The pipeline catches unmapped records and aborts via Fail.
  3. Custom Event Streams: Supply storage URIs in inputs. Pass events_stream_uri alone for initial raw ingestion, or supply all three URIs for incremental runs.
  4. Activate the Trigger: Enable the hourly Schedule trigger in the Triggers tab. Shipped disabled; in production, configure inputs: events_stream_uri to ingest landing storage.

How to extend

  • Dead-Letter Quarantine: Route records failing null or conflict checks to a quarantine Parquet table and gate on an error threshold rather than halting.
  • Automated Scheduling: Enable the hourly Schedule trigger or attach S3/GCS triggers for event-driven compaction.
  • Warehouse Sync: Add Snowflake or BigQuery tasks to sync the compacted mart.
  • Alerts: Wire Slack or PagerDuty notification tasks to sessionization_gate when invariants fail.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.