Skip to content

Lambda, kappa and reprocessing history

The lambda architecture, as Jay Kreps describes it, feeds an immutable sequence of records into a batch system and a stream processing system in parallel. The transformation logic is implemented twice, once in each system, and the results are stitched together at query time. As the idea is usually explained, it is that a batch over all the data is complete and can be rerun when the logic changes, while a stream gives a recent answer quickly, and that the two together cover each other’s weakness. Kreps’s objection is its cost: keeping code that must give the same result in two complex distributed systems is as painful as it sounds.

The kappa alternative keeps one path. There is a stream job, and the log underneath it keeps the history. When the logic changes, a second instance of the improved job is started from the beginning of the retained data and writes to a new output. When it has caught up, the application is switched to read from the new output. Reprocessing happens only when the code changes and the results really have to be recomputed, and the recomputation is the same code, on the same framework, over the same input. Kafka’s documentation says that events are not deleted when they are read, and that they can be read as often as needed, which is what makes the replay possible.

Mental model. Lambda has two kitchens that must cook the same dish and a waiter who combines the plates. Kappa has one kitchen, and when the recipe changes it cooks the whole history again in a second pot, and swaps the pots when the new one is as fresh as the old.

Lambda Kappa
Code paths Two, in two systems, to keep in agreement One job, run in two versions during a change
How history is recomputed A batch job over stored data A replay of the retained log
What it needs A batch system and a stream system A log that keeps enough history
Where it hurts Keeping the two results the same Retention, and the capacity to replay

A replay is a consumer that starts from offset 0 while the topic keeps receiving new messages, so it is a consumer-lag problem, the quantity of the first lesson. The lab’s consumer-group scenario is the right size to reason about.

A partition is read by one consumer of the group at a time. Add consumers beyond the number of partitions and see that they sit idle.

Topic and group
Partitions
4

Changing the number of partitions starts the topic again, because it changes where every key goes.

16 per second
0%

The other messages carry any of 64 keys with equal chance.

Consumers
Consumers
4
5 per second
5 s

A consumer that crashes loses what it processed since the last commit: the new owner starts from there.

Time controls
Time: 10 s

One second of model time passes each half second while the lab runs. Step and Skip move time on by hand.

What is happening

Producers send 16 a second and the 4 consumers at work can process 20, so the lag does not grow overall.

Consumer lag
00 on Partition 0, the furthest behind
Messages processed
160160 different messages, 0 read again
Consumers at work
4 of 4each partition is read by one consumer of the group
Rebalances
0each pauses the whole group for 2 s
Consumer lag over time
035110 seconds

Total lag Furthest behind

Partitions of the topic, after 10 s
Partition numberRead byLog endRead up toCommittedLagShare of messages
Partition 0Consumer 1393939023%
Partition 1Consumer 2484848027%
Partition 2Consumer 3414141027%
Partition 3Consumer 4323232023%

Offsets count messages from 0: "Read up to" is the offset the consumer reads next, and "Committed" is where a new owner would start.

Last messages processed

Within a partition the offsets only go up. Between partitions there is no order at all.

  1. Partition 2, offset 38
  2. Partition 2, offset 39
  3. Partition 2, offset 40
  4. Partition 3, offset 27
  5. Partition 3, offset 28
  6. Partition 3, offset 29
  7. Partition 3, offset 30
  8. Partition 3, offset 31

Illustrative values, not benchmarks of any real product.

Model notes

Assumptions

  • The lab advances one second at a time. A consumer receives up to its rate each second and finishes each message in the same second; processing time, the network and the broker itself are not modelled.
  • A message goes to the partition its key hashes to: FNV-1a of the key text mod the number of partitions. There are 64 keys, and the hot key is key-0. The hash is a choice of the lab, not what any particular broker uses.

Simplifications

  • Partitions are shared out in consecutive ranges, and a change of the group pauses all consumers for two seconds, as an eager rebalance does. A crashed consumer loses what it processed since the last commit; the commit interval is a setting.
  • Exactly-once processing, transactions, replicated brokers, retention, message size and a consumer that is slow rather than dead are not modelled. The numbers are floors and illustrations, not measurements of a product.

Parameter values are illustrative. They are not benchmarks of any real product.

The lab opens with 4 partitions and 4 consumers, producers send 16 messages a second and the 4 consumers at work can process 20, so the lag does not grow. The lab has no way to replay from the start, so the rest is arithmetic, not a measurement. Suppose the log holds the last hour, and the corrected job starts from its beginning:

  • The hour holds 3,600 × 16 = 57,600 messages.
  • The 4 consumers take 4 × 5 = 20 a second, and 16 a second are still arriving, so the backlog shrinks by 4 a second.
  • 57,600 ÷ 4 = 14,400 seconds, which is 240 minutes, or 4 hours.

The spare capacity is what the replay runs on. With 8 partitions and 8 consumers, the capacity is 8 × 5 = 40 a second, the backlog shrinks by 24 a second, and the same hour takes 57,600 ÷ 24 = 2,400 seconds, which is 40 minutes. Both numbers are floors from the model, with no network and no uneven messages, and a hot key as in the first lesson would slow either of them. The lesson to take is that the time to reprocess is set by the volume of history and by the capacity left over after the live traffic, and that a topic created with room for more partitions has room for a faster replay.

Check your understanding: reprocessing

This quiz needs JavaScript to check your answers.

Question 1 of 3

A topic receives 16 messages a second, and its log keeps the last hour. A corrected version of the job replays that hour from the start, with 4 consumers that can each process 5 messages a second, while new messages keep arriving.

How many minutes does the new job take to catch up with the live stream?

Enter a number and choose its unit.

minutes
Question 2 of 3

A team keeps a batch job and a stream job that must compute the same figures, and they keep drifting apart.

What does the kappa approach change?

Choose one answer.

Question 3 of 3

A topic keeps its events for 7 days. A bug in a job is found after 10 days.

Can a replay through a fixed version rebuild all 10 days?

Choose one answer.

What happens if the log keeps only a week?

Section titled “What happens if the log keeps only a week?”

Retention is a setting per topic: the log keeps events for the period you choose and then discards the old ones. A kappa-style replay can go back only as far as the log does. If the topic keeps 7 days and the bug is found after 10, a replay rebuilds the last 7 days and the first 3 have to come from somewhere else, such as an archive of the same events in cheap storage, read by a batch job. That is a reason to keep a batch path in some systems, and it is a choice of what to keep and for how long, not a verdict between the two architectures. Longer retention costs storage, and a replay costs the capacity of the consumers. The question to answer before the bug is how far back you may ever need to recompute.

  • Lambda runs a batch path and a stream path over the same records, with the logic written twice and the results combined at query time. Kreps’s objection is the cost of keeping the two in agreement.
  • Kappa keeps one job and replays the retained log through a new version of it when the logic changes, then switches the application to the new output once it has caught up.
  • A replay is a lag problem. For an hour of 57,600 messages at 16 a second, with consumers that take 20 a second, it takes 240 minutes. With 8 partitions and 8 consumers taking 40 a second it takes 40 minutes.
  • Reprocessing time is the history divided by the capacity left over after the live traffic.
  • A log that keeps 7 days cannot rebuild 10. How far back you may need to recompute is a decision about retention, and a batch path over an archive can still be the right way to go further.

Next: recap and quiz.

Your progress

Built by GhaziAlibi.

© ShardCraft. All rights reserved.