Skip to content

    System Design

    Analytics Pipelines: Batch, Stream, Lambda and Kappa

    Compare batch, stream, Lambda and Kappa analytics pipelines: how each recomputes results, where logic is duplicated, and how to pick one for a dashboard.

    By Published 9 min read

    Share

    Focused lessonadvancedBackend System Design

    Before this: Stream Processing Basics

    What you'll learn: By the end of this lesson you will be able to describe a batch pipeline, a stream pipeline, the Lambda architecture and the Kappa architecture, explain why Lambda's duplicated logic is its main cost, state what a Kappa replay needs from the log, and choose a pipeline for a ShopStream analytics dashboard.

    A restaurant counts its takings twice. At closing the owner adds up every receipt for the exact figure. During the evening a waiter keeps a quick, sometimes wrong tally on a notepad. The notepad answers "how are we doing right now?" and the receipts answer "what was the truth?". The architectures below differ in how they answer both with the least duplicated work.

    The stream processing lesson taught windows and late events inside one stream job, and the data warehouses and lakes lesson taught the batch side. This lesson is about the architecture that joins them.

    Verified on 2026-12-01 against Nathan Marz's Lambda article, Jay Kreps's article on the Lambda architecture, the Apache Flink 2.3 execution-mode page and the Apache Kafka 4.2 documentation. The only code on this page is a small Node script, which was run.

    The core idea

    Every analytics number is a function of an append-only log of events. The architectures differ in how often, and with how many programs, that function is applied. A batch job applies it to everything at intervals. A stream job applies it to each event as it arrives. Lambda runs both and merges them. Kappa runs only the stream job and re-runs it from the start of the log when the logic changes.

    Three illustrations run through the lesson:

    • Minimal example: six order events, summed per store by one rule, then by two copies of it.
    • Realistic example: a ShopStream store dashboard of revenue per store.
    • Failure example: a refund bug fixed by a replay that the log can no longer serve.

    Batch and stream, side by side

    A batch pipeline reads a bounded dataset and writes a result: simple and easy to re-run, but as old as its last run. A stream pipeline updates results continuously: fresh, but it must handle state, ordering and late events.

    Batch Stream
    Input Bounded set, such as one day of files Unbounded flow of events
    Result freshness As old as the last run Seconds, in principle
    Fixing a bug Re-run over the data Needs a replay, or the old results stay wrong
    Hard parts Scheduling, backfills, big recomputes State, time, late events, exactly-once effects

    Lambda: two paths and a merge

    Marz described a design with a batch layer that stores the master dataset as "an append-only set of immutable facts" and precomputes views over all of it, a speed layer that covers only the recent window the batch layer has not yet processed, and a serving step where a query reads the batch view and the real-time view and merges them. The batch results are a few hours old; the real-time results are temporary, because "everything the realtime layer computes is eventually overridden by the batch layer".

    Lambda architecture: one log, two paths, a merge at query timeEvent log to Batch job; Event log to Stream job; Batch job to Batch view; Stream job to Speed view; Batch view to Query layer (old part); Speed view to Query layer (recent part)Event log: immutable eventsEvent logimmutable eventsBatch job: all historyBatch joball historyStream job: recent eventsStream jobrecent eventsBatch view: exact, hours oldBatch viewexact, hours oldSpeed view: fresh, temporarySpeed viewfresh, temporaryQuery layer: merges bothQuery layermerges bothold partrecent part

    Figure 1. Lambda. How to read this: the same events feed two programs. The query layer adds the batch view for everything before a cutoff to the speed view for everything after it. When the batch job catches up, its numbers replace the speed view's.

    The cost is that the transformation logic exists twice, in two systems. Kreps put it this way: maintaining code "that needs to produce the same result in two complex distributed systems" is as painful as it sounds, and he did not think the problem was fixable. He called the operational complexity of running two systems "universally agreed on by everyone doing it".

    Kappa: one path and a replay

    Kreps proposed the alternative later named Kappa. Keep the full input in a log that supports several readers, with retention long enough for your reprocessing window. To change the logic, start a second instance of the job from the beginning of the retained log, writing to a new output table. When it catches up, switch readers to the new table, then stop the old job and delete the old table. Keeping both tables briefly allows an instant revert.

    Kappa architecture: one job, replayed into a new tableEvent log to Job v1; Event log to Job v2 (from offset 0); Job v1 to Table v1; Job v2 to Table v2; Table v1 to Readers (until cutover); Table v2 to Readers (after cutover)Event log: long retentionEvent loglong retentionJob v1: liveJob v1liveJob v2: replays from the startJob v2replays from thestartTable v1Table v1Table v2Table v2Readers: switch when v2 catches upReadersswitch when v2catches upfrom offset 0until cutoverafter cutover

    Figure 2. Kappa. How to read this: only one program exists at a time per version. Reprocessing is a second run of the same job, not a different batch system.

    sequenceDiagram accTitle: Replaying a stream job into a new table and switching over accDescr: The live job v1 serves readers from table v1. A fixed job v2 starts at the beginning of the retained log and writes table v2. When v2 has caught up to the end of the log, readers switch to table v2. Job v1 and table v1 are removed after a safety period. participant R as Readers participant J1 as Job v1 participant L as Event log participant J2 as Job v2 R->>J1: read table v1 J2->>L: read from offset 0 J2->>J2: writes table v2 Note over J2,L: v2 reaches the end of the log R->>J2: switch to table v2 Note over J1: stop v1 after a safety period

    Figure 3. A Kappa cutover. How to read this: readers are never moved until the new table has caught up, which is also what lets you compare both tables first.

    What the log must provide

    Kafka's documentation says time- or size-based retention discards "old log data", while log compaction guarantees Kafka retains "at least the last known value for each message key". A compacted topic is a snapshot of final values, not the full history, so it cannot replay a sum from first principles; a replay needs long retention or an archive of the raw events. Kafka's Streams reset tool resets input offsets (by default to the beginning) and deletes internal topics, but does not reset output topics or local state, and all instances must be stopped, so a side-by-side replay is a different procedure.

    Closing the gap: one engine for both

    Duplication shrinks when one engine runs the same program over both kinds of input. Flink offers STREAMING, BATCH and AUTOMATIC modes; BATCH "can only be used" for bounded jobs, and a bounded job produces "the same final results regardless of the configured execution mode". Only intermediate output differs: streaming emits incremental updates, batch one final result.

    Illustration 1: the minimal example

    Six illustrative events (amounts in cents) are summed per store. The rule is paid minus refunds. The Lambda paths use a batch cutoff at offset 3. The script was run with Node 26.

    const applyEvent = (view, e) => ({
      ...view,
      [e.store]: (view[e.store] ?? 0) + (e.type === "refund" ? -e.cents : e.cents),
    });
    // batchView = log.filter((e) => e.offset < 3).reduce(applyEvent, {})
    // speedSame = recent.reduce(applyEvent, {}); speedDrifted ignores refunds

    Running the full script printed:

    full recompute          {"s1":1100,"s2":800}
    lambda, same rule       {"s1":1100,"s2":800}
    lambda, drifted speed   {"s1":1100,"s2":900}
    kappa, v1 (buggy)       v1 {"s1":1300,"s2":900}
    kappa, after replay     v2 {"s1":1100,"s2":800}

    With one shared rule, Lambda matches a full recompute. When the speed layer's copy forgets refunds, store s2 reads 900 instead of 800 until the batch run catches up. In the Kappa lines the buggy job's table is wrong for the whole history, and a replay into a new table corrects all of it.

    Choosing: a decision flow

    flowchart TD accTitle: Choosing an analytics pipeline accDescr: If results may be hours old, a batch pipeline is enough. If freshness matters and the log can be retained long enough to replay, use a stream job with Kappa-style replay. If the log cannot hold the history, keep a batch path for correctness and a stream path for freshness, which is Lambda, ideally on one engine. S["Need an analytics number"] --> Q1{"Can it be hours old?"} Q1 -->|yes| B["Batch only"] Q1 -->|no| Q2{"Can the log be kept long enough
    to replay all history?"} Q2 -->|yes| K["Stream job, Kappa-style replay"] Q2 -->|no| L["Lambda: batch for truth,
    stream for freshness, one engine if possible"]

    Figure 4. A first-pass choice. How to read this: ask about freshness first, then about the log. Lambda is the exit for when neither a stale answer nor an unreplayable log is acceptable.

    Trade-offs

    Design Gain Cost Wrong when
    Batch only Simplest, easy re-runs Stale answers Users need current numbers
    Stream only, no replay Fresh A logic bug stays in history Numbers feed money decisions
    Lambda Fresh and exact; works if the log is short Two programs, two systems, drift Two teams cannot keep them equal
    Kappa One program, one framework Needs long retention and temporary double storage The log cannot hold the history

    Build it

    This lesson has no lab. The script in Illustration 1 is a teaching model, not a repository lab, and has no golden file.

    In practice: a ShopStream store dashboard

    This is a realistic scenario, not a case history. It is a composite on the ShopStream reference project, and every number in it is an illustrative assumption.

    This is the realistic example.

    The situation

    ShopStream (500,000 orders a day at full scale, which is about 5.8 per second on average, illustrative) shows each store manager a dashboard of revenue per store per hour. Today it reads a warehouse table loaded overnight, so managers see yesterday. They want the last hour, and finance still needs the exact day.

    Starting architecture

    Starting architecture: nightly warehouse load onlyOrders database to Nightly load; Nightly load to Warehouse; Warehouse to DashboardOrders databaseOrdersdatabaseNightly load: ETL jobNightly loadETL jobWarehouse: revenue per storeWarehouserevenue per storeDashboard: yesterday's numbersDashboardyesterday'snumbers

    Options considered

    Option Gain Cost Wrong when
    A. Lambda: keep the nightly job, add a stream job Exact day plus fresh hour Refund and cancel rules written twice Rules change often
    B. Kappa: one stream job over the order-event log, with the warehouse fed from the same events One rule set; replays fix bugs Retention must cover the replay window Events are not complete or kept

    The decision

    Option B. The revenue rule (paid minus refunds, minus cancelled orders) lives in one stream job over the order events. The log keeps 90 days (illustrative); older history comes from the warehouse, which is loaded from the same events for finance. A fix is shipped as a new job version that replays into a new table and is switched over after a comparison.

    Rollout and verification

    Run the stream job beside the nightly load for two weeks, compare each store-hour with the warehouse and explain every difference, then switch the dashboard and keep the old table for a week.

    The 2 a.m. failure

    A store manager reports negative revenue. The job ignored a new refund event type added by the payments team. The fix is ready, but a replay needs 120 days of events and the log holds 90.

    First checks: the log's retention against the oldest event the replay needs; which event types the job's rule covers; whether the warehouse has the missing days. Mitigation: replay the retained 90 days, and rebuild older days from the warehouse. Follow-up: a contract test that fails when a new event type has no rule.

    Architect's notes

    • Decide the replay window first. It sets retention, storage cost and how far back a bug can be fixed.
    • Put the rule in one place. If two copies exist, test them against one fixture.

    Common mistakes

    • Assuming a compacted topic can replay history.
    • Cutting over before the new job has caught up.
    • Running Lambda "for safety" when one engine and a long log would do.

    Key takeaways

    • Analytics numbers are functions of an immutable event log; architectures differ in how often and how many times the function runs.
    • Lambda pairs a batch layer for exactness with a speed layer for freshness and merges at query time; its cost is the same logic in two systems.
    • Kappa uses one job and replays the retained log into a new table to change logic.
    • Compaction keeps the last value per key, not the history, so replays need retention or an archive.

    Practice exercises

    Exercise 1 (30 min). Extend the script's log with a cancelled-order event, then update the Lambda copies and observe which one breaks.

    Exercise 2 (1 hour). For three ShopStream metrics (orders per minute, daily revenue, monthly store ranking), choose batch, Lambda or Kappa and state the replay window.

    Official documentation

    Ask about this article

    Get answers grounded in this post. AI-generated — based on this article, and may be imperfect.

    Scaled AI Weekly

    Enjoyed this? Get more like it every Monday.

    Real architecture decisions, LLMOps patterns that survive production, and engineering leadership advice — from 12+ years of building at enterprise scale. Free. No spam. Unsubscribe anytime.

    Get new articles by email

    Free. We email a confirmation link first, and you can unsubscribe from any email.

    Join engineers building production AI systems

    Comments