Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:1jehuang Jcode Telemetry Dual Write

From Leeroopedia
Knowledge Sources
Domains Telemetry, Data_Pipeline
Last Updated 2026-10-18 00:00 GMT

Overview

Data ingestion pattern that writes each event to both a queryable relational store (D1) and a high-throughput analytics store (Analytics Engine) simultaneously, enabling both ad-hoc SQL queries and time-series aggregation.

Description

Dual-write ingestion is a pattern where every incoming event is written to two storage systems in a single pipeline: a relational database for structured querying and an analytics engine for high-cardinality time-series aggregation. The relational write enables dashboard queries (daily active users, token usage by model, spend summaries), while the analytics write enables real-time aggregation and trending.

The problem solved is storage system specialization: no single storage backend is optimal for both ad-hoc SQL queries and high-throughput time-series ingestion. A relational database (D1) handles complex joins and aggregations but has limited write throughput and storage size. An analytics engine (Cloudflare Workers Analytics Engine) handles high-throughput writes and time-series aggregation but cannot do arbitrary SQL joins.

The design includes retention management: the relational store has a soft size limit and runs emergency and scheduled pruning to reclaim space. The analytics store has its own retention independent of the relational store.

Usage

Use this principle when a telemetry or event ingestion system needs both queryable storage for dashboards and high-throughput storage for time-series analysis. The trigger is: "events arrive from distributed clients and need both SQL-queryable and time-series-aggregated views." It is appropriate for: product analytics, usage tracking, and spend dashboards.

Theoretical Basis

The dual-write pipeline has three stages:

  1. Validate: The incoming event body is validated against known event types, required fields (telemetry_id, event, version, os, arch), and type-specific payloads.
  2. Relational write: The event is inserted into the D1 events table and a detail table (e.g., session_details, turn_details) with foreign-key relationship. Daily activity is also updated.
  3. Analytics write: The event is written to the Analytics Engine firehose with a flattened schema for time-series aggregation.

Pseudo-code logic:

# Abstract algorithm (NOT real implementation)
def ingest_event(env, body):
    # 1. Validate
    if not is_known_event(body.event):
        return error(400)
    if not has_required_fields(body):
        return error(400)

    # 2. Relational write (D1)
    env.DB.execute(
        "INSERT INTO events (telemetry_id, event, version, ts) VALUES (?, ?, ?, ?)",
        body.telemetry_id, body.event, body.version, now()
    )
    env.DB.execute(
        "INSERT INTO session_details (event_id, model, tokens) VALUES (?, ?, ?)",
        ...
    )
    update_daily_active(body.telemetry_id)

    # 3. Analytics write (firehose)
    env.FIREHOSE.put({
        "blob": encode_firehose_schema(body),
        "indexes": [body.telemetry_id, body.event],
    })

    return ok(200)

Retention management runs on a schedule:

  • Scheduled prune: Nightly cron deletes high-volume event rows older than the retention window.
  • Emergency prune: When D1 size exceeds the soft limit, retention windows are halved and rows are deleted with a rate-limit per isolate to avoid overwhelming D1.

Related Pages

Implemented By

Uses Heuristics

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment