Why Kafka: A Log Instead of a Report

Batch Report or Stream: How Long Until You Have an Answer

18 min

06:40. How many antennas are silent right now

November 2, 2184, Vault-9. It's an orbital archive: the station's antennas pick up signals from old Earth, and you're the archivist who makes sense of them. For six months, you've been running the night intake belt. Once a day, it collects everything that arrived, runs the calculation through the night, and puts a report on the table by breakfast. Three days ago, an antenna picked up the carrier, a signal that never ends. A once-a-day batch can't handle that. The station had a mothballed comms level: the dispatch room, where the live feed was once distributed. At 04:40, you brought it back online and took over as duty officer.

At 06:40, you bring the station's curator the morning report, the same daily report the night belt has been producing for six months. The belt closed the day at 00:00, the run took 6 hours 12 minutes, and the report was printed at 06:12.

The curator looks at the page and asks, “How many antennas are silent right now?”

The report says one. The intake board behind him says seven out of twelve.

Both numbers are correct. The report and the board measure the same thing: how many antennas didn't send their intake to the night belt. The report covers the previous day; the board covers the last hour. The report is accurate as of 23:59 yesterday. But the question is about “right now.” The newest number in the report is already six hours forty-one minutes old, and the oldest is thirty hours forty minutes old. As you talk, the report keeps aging: the next one won't arrive until 06:12 tomorrow.

The feed, the continuous signal itself, isn't the issue here: it comes in steadily from all twelve antennas. Kafka won't appear anywhere in this lesson either. First, we need to understand when it's needed at all. One number answers that question: how old is the answer you're bringing to the curator?

QUERY, the station's AI mentor with distinctly catlike habits, opens one eye.

QUERY: Good report. For yesterday.

The duty officer, seen from behind, holds a glowing daily report with one row highlighted. In front of them is a board of twelve holographic slots: seven are dark and five are lit. Behind it, belts of amber capsules keep moving without stopping; QUERY, the archive's cat interface, watches the board.
The report is accurate for yesterday; the board reflects the last hour. The question was about right now.
The obvious question is: why not just run the calculation more often? We'll answer it with numbers. The cell first recalculates the curator's report, so check that its result matches what you read above. Then it prints a table showing the same answer-age calculation for five schedules and for a stream. With a stream, each event is processed as soon as it arrives instead of waiting for a schedule. In this example, each run has 40 seconds of startup cost.
python · kafka

Answer age

The number we've just been calculating has a name we'll use throughout the course: answer age. It's the time from when an event happens until that event first appears in a published answer.

The table measures age when the report is printed, at 06:12, so it shows 30 h 12 min. By the time the curator sees it at 06:40, the age has reached 30 h 40 min.

The table shows two rules:

  • on average, an event waits half the period plus the run before publication; that's the “typical age” column;
  • the unluckiest event waits the full period plus the run; that's the “worst-case age” column. It happens just after a run has collected its data, so it waits through the entire next period and then the entire run.

The second concept in this lesson is answer shelf life: how long an answer remains useful to the person making a decision from it. Compare that limit with the worst-case age, not the average. The person acting on the answer can't choose which event will be the unlucky one. For the curator, the answer shelf life is one hour because the board looks at the last hour. The reading room, where you'll start sending the feed today and where the station uses it to answer questions, has an answer shelf life of only 30 seconds.

Look at the worst-case age column: none of the schedules fits within 30 seconds. Even a run every 40 seconds produces a worst-case age of 1 minute 20 seconds.

Worst-case answer age for five schedules and a stream, compared with the reading room's 30 s answer shelf life. The top three bars extend beyond the axis and are cut off; only the stream remains within the limit.

Why not run even more often: the wall

You can shorten the period further. But every run has a fixed part: work it must do no matter how little data has arrived. It has to start the program, find where to resume reading, and record how far it got. That work doesn't shrink with the period. You pay for it on every run, which is what the “starts” column in the table shows.

With one run per day, starts cost 40 seconds. With one run per minute, they cost 16 hours per day, so the machine spends two-thirds of the day doing nothing but starting runs. With one run every 40 seconds, starts consume all 24 hours. That's the wall: the period can't get any shorter because starts alone already occupy the machine full-time.

This gives us the main conclusion of the lesson. To keep worst-case age within 30 seconds, the period can be no longer than 30 seconds minus the run time. But startup alone takes 40 seconds. No schedule can meet the requirement.

A stream has a cost too: it's more expensive to keep running and harder to repair. Frequent scheduled runs are therefore a reasonable choice while they can still meet the requirement. Here they can't. A stream has no period, so its answer age is simply the time required to process one event: 1.2 seconds.

How much time per day the machine spends just starting runs: the “every 40 seconds” bar reaches the far right edge, meaning starts consume the entire day. That's the wall. The worst-case answer age on that schedule is still 1 min 20 s.

Before the exercise: how long the machine is busy

In the table, the budget counted only starts: the number of runs multiplied by the cost of one start. The exercise below uses a stricter budget. It counts machine occupancy: the number of runs multiplied by the duration of the entire run, including startup and the work itself.

Work through this example on paper. The requirement is a worst-case age of no more than two hours, each run takes 15 minutes, and the budget is half a day of machine time:

  • worst-case age = period + run ≤ 7,200 s, so period ≤ 7,200 − 900 = 6,300 s;
  • occupancy = (86,400 / period) × 900 ≤ 43,200, so period ≥ 1,800 s;
  • the period can't be shorter than the run itself—900 s; this bound is weaker than the previous one.

Any period from 1,800 through 6,300 seconds works. The least frequent is 6,300: fewer runs cost less.

Practice: write the code
Write max_period(requirement_s, run_s, budget_s): the least frequent run period, in whole seconds, that meets both the requirement and the budget—or None if no period works. The model has three lines:
  • worst-case answer age = period + run duration, and it must be no greater than requirement_s;
  • machine occupancy per day = (86,400 / period) × run duration, and it must be no greater than budget_s;
  • the period can't be shorter than the run duration.
The requirement is met only when the worst-case age fits: the age of the unluckiest event, not the average.
python · kafka

What's real in this lesson, and what's simplified for learning

Kafka hasn't appeared yet. We calculate answer age with simple arithmetic over a ready-made list of intake events, which is fine because answer age doesn't depend on a particular tool.

The teaching constants are 40 seconds to start a run, 6 h 12 min for the daily run, and 1.2 seconds to process one event in a stream. These aren't measurements from a live system. In your own system, you'd need to measure them because they determine where the wall falls.

The method itself is real: typical answer age is half the period plus the run, worst-case age is the full period plus the run, and the time spent on starts grows with the number of runs until it consumes the machine's entire day, potentially before the answer-age requirement can be met.

Interview question

How this comes up in interviews

A common question is: “You already have a nightly ETL—why do you need Kafka?” Answering “Kafka is faster and more scalable” misses the question. The interviewer is asking about the requirement, while that answer jumps straight to a technology.

A good answer starts with a number. State the answer shelf life from the perspective of the person acting on the result, then use arithmetic to show why a schedule can't meet it: with any schedule, worst-case age is at least the period plus the run, and in our example startup alone takes 40 seconds.

A follow-up question checks whether you understand where the approach breaks down: “Where's your wall?” You should be able to name the fixed part of each run and explain that frequent scheduled runs are a reasonable choice as long as the period stays above a few minutes. Push it lower, and the machine ends up constantly starting new runs. Streaming isn't the only way around the wall. Micro-batch engines such as Spark Structured Streaming keep the process running, pay the startup cost once, and take batches every few seconds.

Industry terms: answer age is end-to-end latency or freshness, answer shelf life is a latency SLO or freshness , and the fixed part of a run is per-run overhead.

Check yourself
The reading room has an answer shelf life of 30 seconds. A colleague suggests running every 20 seconds: “20 is less than 30, so we're fine.” But each run takes 40 seconds. What's wrong?

What's next: the log

You can't make the daily report fresh enough. Keep shortening the schedule and eventually you hit the wall. You need a different form of data, one you can start reading at any time and from any position. That form is a log.

The idea is simple. New events are appended only at the end, like entries in a watch log, and each gets an offset in sequence. Reading an entry doesn't remove it: one reader can start from the morning, another can catch up from noon, and a third can reread yesterday's entries without getting in anyone else's way.

Kafka is a system that maintains logs like these and makes them available to many systems at once. The next lesson covers what Kafka is, what parts it consists of, and where it's used.

Key takeaways
scheduleworst-case agestarts per day
once a day30 h 12 min40 s
once an hour64 min16 min
every 40 seconds1 min 20 s24 h — the wall
stream1.2 sno period

Answer age is the time from an event until it appears in a published answer. Answer shelf life is how long that answer remains useful to the person acting on it, and you compare it with worst-case age. The reading room's shelf life is 30 seconds. No schedule can meet it because you hit the wall first.

QUERY: The curator asked about “right now.” A report can only tell you what was true earlier. For “right now,” you need a log.