A Topic Is a Log: Segments and Retention
17:40. One topic deleted everything older than six hours; another deleted nothing
This morning, when you brought the level back online at 04:40, you switched twelve antennas to receive mode and set six-hour on the signal_raw topic: storage is tight on the comms level. You also configured the log to roll more often. On disk, a partition's log is stored as a sequence of segments rather than one file. You configured Kafka to start a new segment every fifteen minutes or every 900 megabytes, whichever comes first, so old data can be removed more precisely. You brought the topic up in a hurry, with three partitions.
It's 17:40 now. The reading room needs to replay the feed from 11:00 because its reports have a gap between 11:00 and 12:30. You position a consumer at the required offset in partition 0, with the setting that says “don't skip ahead silently,” and get an error instead of data: offset 456,000 is gone. The earliest available offset is 504,000, from 11:40. Eleven o'clock was six hours and forty minutes ago, so those frames crossed the six-hour boundary forty minutes ago and have already been deleted.
You check the neighboring signal_ack topic, where the dispatch room writes responses to requests. It has the same six-hour retention. There you find messages that are thirteen hours old.
One topic deleted everything older than six hours, so the request for 11:00 can no longer be fulfilled. The neighboring topic deleted nothing. Both followed the rule.
QUERY: Six hours means “not before six hours.” Nobody promised “exactly.”

Key numberFeed rate
48 frames per second — that's the normal feed rate: twelve antennas, 4 frames per second from each one.
A frame averages 450 KB, which adds up to 21.6 MB per second, 2,880 frames per minute, 172,800 per hour, and 4,147,200 per day. You'll see these numbers again later in the course.
In this lesson, we'll focus on partition 0. Each partition has its own offsets, and the source_id key sends five of the twelve antennas to partition 0, so it receives 20 frames per second, or 9 MB/s. The next lesson explains why the key distributes antennas unevenly.
TermsThree more terms
| term | what it means |
|---|---|
| message and frame | the same data, named for two different contexts: in Kafka mechanics, we say “message”; in watch scenes, a message from the feed is called a frame |
| broker | a machine in the Kafka cluster that stores partitions on its disk; RabbitMQ and similar systems are “queue brokers” |
| downstream system | an external system that uses the result: the reading room and its neighbors. A consumer is a program; a downstream system is the system that uses the program's output |
You'll find the standard English terms used in interviews in the callout at the end of the lesson.
signal_raw topic and what retention cleanup did to those segments. Second, what a consumer requesting the feed from 11:00 sees under two different settings. Third, the sparse signal_ack topic with the same retention. Pay attention to the “time” column in the first segment.Why “six hours” means “at least six hours”
A partition log isn't one file. It's a sequence of segments, each stored as a separate file on disk. Kafka writes only to the last one, the active segment. When that segment reaches its size or time limit, Kafka closes it and opens a new one.
Kafka can't delete one message from the middle of a file. So works on whole segments, using three rules:
- Kafka deletes only a whole segment—never individual messages from inside it;
- the decision is based on the newest message in the segment: the segment can be deleted only when even that message is older than the retention period;
- the active segment isn't touched while it still contains a fresh message.
Those rules explain both symptoms. In signal_raw, a segment didn't stay open for fifteen minutes. It closed after 100 seconds because 900 MB fills up faster at 9 MB/s. On a busy stream, size rolls the segments first. Because the segments are short, the cutoff sits close to the six-hour boundary: 252 segments were deleted, and the oldest surviving message is exactly six hours old.
In signal_ack, there are only four responses per hour, while the default segment rolls once every seven days. Thirteen hours isn't long enough for it to roll even once. The segment is still active and contains a fresh message, so Kafka leaves it alone. That's why thirteen hours of data can remain under six-hour retention. If writing stopped and the whole segment eventually became old, the broker would open a new empty segment and delete the old one—that's the bottom row in the diagram below.
The offset of the oldest message that can still be read is the partition's log start offset. Each partition has its own log start offset, just as each has its own offsets. After cleanup, partition 0's log start offset moved forward from 0 to 504,000.
What a lagging consumer sees
The consumer asked for 11:00, but those messages are already gone. What happens next depends on a consumer setting, not on the broker:
- with
auto_offset_reset='earliest', the consumer silently jumps to the partition's log start offset, leaving an 11:00–11:40 gap without any error: data is missing, but nothing reports the loss; - with
'latest'—the default in both kafka-python and the Java client—it jumps to the end and skips everything that was still stored in between. Again, silently; - with
'none', it gets an error on the first read: the requested offset is gone, and the client tells you instead of skipping ahead.
Chapter 3 covers how to replay a window from the past and when this setting actually takes effect.
How to enter the log by time
The broker keeps each segment's time boundaries in memory, so it can identify the right segment without scanning the log. Beside every segment are two small index files: one maps time to an offset, and the other maps an offset to a position in the file. Asking for “from 11:00” is therefore almost as cheap as reading from the end. Kafka doesn't scan the log sequentially in either case, so replaying yesterday's data is a normal operation rather than an emergency.
SEGMENT_MS (and, if you want, RETENTION_MS) for signal_ack so that after cleanup at 17:40, the oldest available message is no older than seven hours and no newer than six hours. Responses are written once every fifteen minutes, from 04:40 through 17:25, into one partition.
The function oldest_age(segment_ms, retention_ms) is already connected to the editor; its code is hidden. It creates the topic, writes 52 responses, runs cleanup at 17:40, and returns the age of the oldest surviving response in milliseconds—or None if none survive. The bottom lines of the starter code call the function and print the age. You'll see that output when you click “Check.” The constants MIN and HOUR (one minute and one hour in milliseconds) are already defined.What's real in this lesson, and what's simplified for learning
In the course's Kafka simulation, a segment is a counter plus a pair of boundaries stored in the browser tab's memory, not a file on disk. Its size is calculated using the course's 450 KB frame size. The cell doesn't store the frames themselves; it counts them and creates only the ones you ask to read. On a real cluster, the feed would have written about a terabyte over those thirteen hours.
In the sandbox, you trigger cleanup yourself by calling cluster.apply_retention(...). A real broker runs cleanup automatically in the background on its own schedule. In , the boundary therefore moves when cleanup reaches it, rather than the moment you happen to look—another reason “six hours” means “at least six hours.”
The rules themselves are real and carry over to a production cluster without qualification: offsets belong to partitions, deletion happens by whole segment, the newest message determines whether a segment can be deleted, the active segment isn't touched, the partition's log start offset moves forward, and a consumer setting determines whether a lagging consumer sees a gap or an error.
Interview question
How this comes up in interviews
“We set .ms to 6 hours, but the data is still there 13 hours later. Why?” A good answer should mention three things: the log segment, the active segment, and the fact that deletion is decided using the newest message in the segment. You can also mention that cleanup runs on a schedule rather than exactly when the retention period expires.
“What happens if a consumer group's committed offset points to data that has already been deleted?” Start with “it depends on auto.offset.reset”: earliest gives you a silent gap, latest silently skips ahead, and none produces an error. One more detail worth knowing: the client default is latest.
“How does Kafka find a message by time?” It uses two indexes beside each segment: the time index returns an offset, and the offset index returns the corresponding position in the file. That's why offsetsForTimes doesn't scan the log. Terms you'll hear: log, retention.ms, segment.ms, segment.bytes, log start offset.
Key takeaways
| topic | segments | deleted | oldest message | |
|---|---|---|---|---|
signal_raw, partition 0 | size-based, 100 s | 6 h | 252 segments, log start offset 0 → 504,000 | 6 h 00 min |
signal_ack | default, one active segment | 6 h | nothing | 13 h 00 min |
Retention works on whole segments and decides based on the newest message in each segment. The active segment isn't touched while it contains a fresh message, so “six hours” means “at least six hours.” A consumer setting determines whether a lagging consumer sees a gap or an error. Reading from a point in time is cheap because each segment has small index files beside it.
QUERY: You told Kafka to cut the log every fifteen minutes. The feed cut it every hundred seconds—all by itself.