A fan-out problem
3 writers and 4 readers meet at the log: 7 connections where direct pipes would need 12.
One log in the middle
Kafka's names for the roles: a producer appends records, a consumer reads them, and a broker is a server that stores the log on its disk. Brokers together form a cluster. A named log is a topic, such as orders or page_views, and a company runs hundreds of them.
What kind of system that makes it
- Holds the current state of each row
- An update overwrites the old value in place
- Answers a question about now: what is order 1042's status?
- Each reader runs a query, a fresh question each time
- Holds every event in the order it happened
- A new event is appended and nothing is overwritten
- Answers a question about history: what happened to order 1042, step by step?
- Each reader walks forward through the records from a position it chooses
- ▸A durable store: records are written to disk and kept for days even if nobody reads them.
- ▸An ordered store: records stay in the order they were appended within one partition, which is a slice of a topic covered 2 sections from here.
- ▸A shared store: any number of readers read the same records, each at its own pace, and none of them changes the log.
The path an order takes
The checkout service writes the order to its database for the storefront and to Kafka for everyone else.
What the writing side relies on
What the reading side relies on
A producer or consumer is given the addresses of a few brokers and learns the rest of the cluster from them, so it names a topic and never needs to know which broker holds it.
Where the data goes next
| System | What it holds | Who asks it |
|---|---|---|
| Checkout database | The current state of each order | The storefront, on every page load |
| orders topic | Every order event, in order, for 7 days | Fraud scorer, search index, loaders, alerting |
| Data warehouse | Every order ever, as tables | Analysts and the nightly report |
Nothing in this section ran a query against Kafka. Readers walk the log forward; questions about totals and joins are asked of the systems the readers fill.
A topic is several logs
| Partition | Offset 0 | Offset 1 | Offset 2 | Offset 3 |
|---|---|---|---|---|
| 0 | cart-7: placed | cart-7: paid | cart-2: placed | |
| 1 | cart-4: placed | cart-4: paid | cart-4: shipped | cart-9: placed |
| 2 | cart-1: placed |
Grows at the end, and only there
- ▸Writes: the broker writes to the end of a file in sequence, the fastest pattern a disk has.
- ▸Reads: a reader asks for records from a position onward and receives one contiguous run of bytes.
- ▸Many readers: 10 readers of the same partition read the same bytes, which the operating system keeps in memory after the first read.
- ▸Bookkeeping: the broker keeps no per-record state about who has read what, because reading changes nothing.
An offset is a record's address
orders partition 1 offset 2 is cart-4: shipped for a reader on the fraud team and for a reader on the warehouse team. Both can say they have read through offset 2 of partition 1 and mean the same thing, and a reader that stops there tonight and asks for offset 3 tomorrow receives cart-9: placed, the record that followed.
Order holds inside a partition
4 parts the producer sets, 2 the broker adds
| Part | Set by | Holds | There for |
|---|---|---|---|
| Key | Producer | Bytes, or empty | Picking the partition, so records about one thing stay together and in order |
| Value | Producer | Bytes | The event itself |
| Timestamp | Producer, by default | Milliseconds since 1970 | When it happened, and finding records by time |
| Headers | Producer | Pairs of a name and bytes | Facts about the record that are not the event |
| Partition | Broker | A number | Where the record landed |
| Offset | Broker | A number | The record's position in that partition |
The key
The value
The timestamp
Headers
- ▸The address comes from 2 sides: the producer names the topic and its key picks the partition, then the broker assigns the offset.
- ▸The content is what the producer sets, the key and value along with the timestamp and headers, and the broker stores it as bytes it doesn't parse.
- ▸The broker reads a key only on one kind of topic a later lesson covers; on the topics here it stores the key untouched.
- ▸A reader receives the 3 parts of the address with the 4 of content and can quote the address back to anyone to name the record.
- Key each record by the thing whose order matters: one cart, one customer, one device
- Put the event itself in the value and keep transport facts out of it
- Keep headers small: a source name, a trace id, a content type
- Key by a field that changes during the story, such as order status
- Bury routing facts inside the value, where a filter must parse it to find them
- Send one form of key from one service and another form from a second for the same entity
A read is a copy, and the log is the original
- A worker takes a message and the message is gone
- Each message has one reader
- A second system needs a second copy of the message
- Re-reading means the producer sends it again
- A reader copies a record and the record stays at its offset
- Each record has as many readers as show up
- A second system reads the same record at the same offset
- Re-reading means the reader moves its position back
Each reader keeps its own position
| Clock | Log, partition 0 | Scorer position | Loader position | Audit position |
|---|---|---|---|---|
| 09:00 | offsets 0 to 3 | 4 | 2 | not started |
| 09:30 | offsets 0 to 6 | 7 | 2 | not started |
| 10:00 | offsets 0 to 6 | 7 | 7 | 0 |
| 10:05 | offsets 0 to 8 | 9 | 7 | 9 |
Starting positions
What many readers buy you
> A concert ticketing site opens sales for a stadium show at 10:00. 40,000 fans hit the checkout in 3 minutes, and every reservation, payment and release must reach the seat map, the fraud checker, the email service and the finance warehouse, while the checkout service itself has to answer each fan in under a second.
Apache Kafka is an open-source distributed event streaming platform used by thousands of companies for high-performance data pipelines, streaming analytics, data integration, and mission-critical applications.
kafka.apache.org
- An Append-Only Log
- Main Concepts and TerminologyPersistence
- Topics and Partitions
- Main Concepts and TerminologyAdding and removing topics
- Offsets Mark a Position
- Consumer PositionReads
- What a Record Carries
- RecordRecord Header
- Reading Does Not Delete
- DeletesThe Consumer
Apache Kafka, Kafka, and the Kafka logo are either registered trademarks or trademarks of The Apache Software Foundation in the United States and other countries.
One append-only log that every system writes to once and every reader reads at its own pace
- Category
- Kafka
- Difficulty
- beginner
- Duration
- 20 minutes
- Challenges
- 6 hands-on challenges
Topics covered: What Kafka Is For, Where Kafka Fits, The Partitioned Log, What a Record Carries, Reading Does Not Delete
Lesson Sections
- What Kafka Is For
A fan-out problem A company's data starts as events: a customer places an order, a page loads, a payment clears, a sensor reports a temperature. Each event is produced by one system and wanted by several. A sale the order service records is wanted by 4 other systems: the warehouse that packs it, the email service that confirms it, the fraud model that scores it, and the analytics tables that count it each night. Connecting each system that produces events to each system that wants them works for
- Where Kafka Fits
The path an order takes Trace one order at an online store. A customer pays, and the checkout service writes the order to its own database, which keeps the current state the storefront needs: the order exists, it is paid, it hasn't shipped. In the same request the checkout service produces a record to the orders topic. From that moment the order is in 2 places, and the 2 places serve different readers. The database serves the storefront's next page load. The topic serves everyone else. Kafka sit
- The Partitioned Log (concepts: Offsets)
A topic is several logs A topic is split into partitions, and each partition is its own log: an ordered sequence of records that grows at its end. The orders topic created with 3 partitions is 3 such sequences, numbered 0 through 2. When a producer sends a record, the broker appends it to the end of exactly one partition, and that is where the record lives from then on. Which partition is decided by the record's key, which the next section covers; this section is about what happens once it lands
- What a Record Carries (concepts: The append-only log)
4 parts the producer sets, 2 the broker adds A record is more than its payload. The producer fills in a key and a value, plus a timestamp and a list of headers, and the broker adds the partition the record landed in and the offset it got. The table lays out all 6, and the rest of the section walks through why each one is there. Here is the record from the last section, offset 2 of partition 1, written out as a reader would print it. The key and value are bytes on the wire; the printout shows the
- Reading Does Not Delete (concepts: The append-only log)
A read is a copy, and the log is the original A reader asks a broker for records from an offset, the broker sends it a copy of the bytes, and the log is as it was. Reading changes nothing on the broker: the record is at the same offset, with the same bytes, for the next reader and for the same reader an hour later. The broker removes records on its own schedule, by age, 7 days by default on a new topic, and whether a record has been read 0 times or 50 times plays no part in that decision. Each r