AlgoViz

Kappa architecture

medium 27 min

Kappa architecture uses one stream job and one log that keeps its events.

Video lesson · Chapter 1 of 5

One ticket machine can rebuild a scoreboard

0:00 / 3:54

Kappa architecture in 75 seconds

1:15Reel coming soon

Theory

Read or listen first. About 2 min.

What it is

Kappa architecture is a simpler answer to the problem that Lambda architecture solves. All events go into a log that keeps them in order. One stream job reads the log and maintains the results, called views. Questions are answered from the views.

There is no batch layer. If the logic was wrong or has to change, the events are still in the log. A new version of the same job starts at the beginning, reads everything again, and builds new views. Live processing and reprocessing use the same code.

Why it exists

In Lambda each rule exists twice, in two systems that work differently. Keeping them equal costs a team more than the machines do. Kappa asks: if the stream job can be exact, and the log can be read again, what is the batch layer for?

The answer depends on two numbers: how far back the log goes, and how long reading it all takes. If both are acceptable, one road is enough.

A short history

  1. 2011

    Nathan Marz describes a batch layer that corrects a realtime layer. The combination becomes known as Lambda architecture.

  2. 2 July 2014

    Jay Kreps publishes Questioning the Lambda Architecture on O'Reilly Radar. The article proposes to keep the log, start a second instance of the stream job from the beginning into a new table, and switch. It suggests the name Kappa, with some doubt that the idea deserves a Greek letter.

  3. 2017

    Kafka 0.11 adds transactions and exactly-once processing from topic to topic, which makes one exact stream job easier to build.

Words to know

Log
A list of events that is only appended to and keeps its order.
Offset
The position of an event in a partition of the log.
Retention
How long the log keeps events.
Replay
Reading old events again.
View
A result built from the log, such as a table of counts.
Catch up
To reach the newest event while more keep coming.
Consumer group
A named reader whose position is remembered. A new name has no position yet.
Side effect
Something the job does outside its views, such as sending an email.

How it works

Step through the idea, or press Play.

Step 1 of 3

Append to the log. In order, never changed, kept long enough

Understand it

Start here

The parade film

Lee and Noor count what passes in the town parade. Lee also films it. The film turns out to be more useful than the count.

Count again from the film
Count the carts3

The same film, counted twice with two rules. Without the film there would be nothing to count again.

The idea

One path

Log, stream job, view

Events are appended to a Kafka log that keeps history. Version 1 of a Flink stream job consumes it and updates table 1 in Cassandra. The app reads that table.
Compare this with the two roads of Lambda. Nothing here has to be merged.
  • The log is the truth

    All events are there. Views can be thrown away.

  • Stream only

    One way of processing, for new and old events.

  • Replay is reprocessing

    Start at the first offset and build the view again.

  • One code base

    Each rule is written, tested and released once.

The log

What the log must offer

PropertyMeaningWhy Kappa needs it
Append onlyEvents are added, never changedA replay sees what happened
OrderedOffsets count up inside a partitionEvents of one key keep their order
ReplayableReading does not remove eventsSeveral jobs can read at their own pace
DurableStored and replicatedIt is the source of truth
PartitionedSplit over machinesA replay can run in parallel
RetentionHow long events are keptSets how far back you can go
Details: log-based queues. A database can feed such a log with change data capture.

Two readers of one log

Each reader has its own position. The log does not care who is ahead.

What keeping it costs

10,000/s×1 KB×86,400×30×3≈77.8 TB10{,}000/\text{s} \times 1\text{ KB} \times 86{,}400 \times 30 \times 3 \approx 77.8 \text{ TB}
Assumed: average rate, 30 days, three copies. Kafka's tiered storage can move older parts to a remote store. A copy as files in object storage also works.

Replay

Reprocessing in five steps

Two versions side by side

During a replay the same Kafka log feeds two Flink jobs. Version 1 is still running and writes table 1, which the app reads. Version 2 reads old events from the start, is catching up and writes table 2, which nobody reads yet.
Nobody reads table 2 until it is complete and has been compared.
  1. 1

    Deploy the new version

    The fixed or extended logic, as a second job with its own state and output table.

  2. 2

    Start at the beginning

    A new consumer group, from the first offset that is needed.

  3. 3

    Rebuild the view

    All old events go through the new code.

  4. 4

    Catch up

    The job reaches the newest events. Compare both tables on keys you know.

  5. 5

    Switch

    Point readers to the new table. Later stop the old job and delete its table.

The switch, in time

When to reprocess

ReasonExample
Bug fixRefunds were counted as sales
New featureA count per country is wanted, also for the past
Schema changeThe view needs another shape
Better algorithmA new model scores old events again

Numbers

How long does it take?

Catch-up time

T=backlogrreplay−rlive=25.92×109200,000−10,000≈136,400 s≈38 hT = \frac{\text{backlog}}{r_{replay} - r_{live}} = \frac{25.92 \times 10^9}{200{,}000 - 10{,}000} \approx 136{,}400\text{ s} \approx 38\text{ h}
30 days at 10,000 a second, replayed at 200,000 a second. With less than 10,000 spare a second it would take more than 30 days.

Ways to make a replay faster

WayLimit
More parallel tasksOne per partition is the usual maximum for reading
Bigger writes to the sinkThe database must take many times the normal load
Read old data from filesFiles can be read in parallel without partitions
Replay only what changedOne event type, or the last 30 days
Start from a snapshotOnly if the older state is right

History

Older than the log

If the log keeps 30 days and the view needs a year, the job reads files first. It stops at a known offset of each partition and continues in the log from there, so nothing is missing and nothing is counted twice.

Files first, then the log

The same transformation code runs over both. Only the reading differs.

Correctness

What can go wrong

ProblemWhyRemedy
Emails are sent againA replay repeats side effectsViews only in the job, a separate sender that remembers ids
Other numbers each timeThe job asks another service, whose data changedPut that data into the log as events and join by time
Windows are empty or wrongWindows use the machine's clockUse event time
Doubled countsResults were added instead of setUpsert by key, keep the offset with the result
Old events cannot be readThe format changedVersioned schemas, readers that understand old ones
Order is lostEvents of one user have different keysKey by the user

Compare

Kappa and Lambda

AspectLambdaKappa
RoadsBatch and streamStream only
CodeTwo, same logicOne
ReprocessingRun the batch jobReplay the log
OperationsMoreLess, except during a replay
Reprocessing speedUsually faster, batch reads in bulkLimited by partitions, state and sink
StorageMaster data and viewsLog and views, two sets during a replay

Kappa when

  • Simplicity matters most
  • The team runs streams well
  • The replay time is acceptable
  • One code base is wanted

Lambda, or a batch backfill, when

  • Reprocessing must be quick
  • History is in petabytes
  • Batch processing is already strong
  • The logic needs all data at once, such as training a model

Is Kappa enough?

The last box is common today: engines such as Flink and Beam can run one program over files and over topics.

When to use it

Where it fits

  • Event sourcing

    State is kept as a list of changes, so views can be rebuilt at any time.

  • Log analytics

    Application logs counted and searched as they come.

  • Click streams

    What users do, grouped into sessions.

  • Sensor data

    Readings from devices, with new rules applied to old readings.

Practice

Good habits

Do

  • Choose the retention on purpose
  • Use a new consumer group per version
  • Make processing idempotent
  • Watch the progress of a replay
  • Plan for a parallel replay

Do not

  • Delete events that you may need
  • Guess the replay time
  • Forget the state of the old job
  • Skip testing a replay
  • Depend on outside data that changes

Summary

The whole topic in one minute.

Kappa keeps one log and one stream job. The log is the source of truth, and views are copies that can be built again. To change the logic, deploy a second version that reads from the start into a new table, wait until it has caught up, compare, switch readers, and remove the old one.

It removes the second code base of Lambda. The limits are the retention of the log, the time a replay takes, double resources while it runs, and side effects that must not be repeated.

  • One log, one stream job, views that can be rebuilt.
  • Reprocessing is a second job, a second table, then a switch.
  • The log reaches back only as far as its retention. Kafka's default is 7 days.
  • Catch-up time is the backlog divided by the spare rate.
  • Side effects and changing outside data make a replay unsafe.
  • If replay is too slow, run the same code over files.

Sources