Skip to content
Bronze 8 min read · Kafka 2 practice problems ↓

Kafka in plain words

Most streams in this track read from Kafka, so it pays to know what Kafka actually is. It is not a queue that hands out messages and forgets them: it is a log that keeps every message, in order, for as long as you tell it to. This lesson explains topics, partitions, offsets, brokers and retention, and why that design lets many readers share one stream.

Step 4 · Structured Streaming · Bronze · Kafka basics, lesson 1 of 2

Read first: Sources and sinks (skip if you know it)

In plain words first

Think of a shop's CCTV recorder. It records everything, all the time, with a timestamp on every second. The security guard watches it live. The manager rewinds to yesterday afternoon. The insurance company asks for last Tuesday. Nobody's viewing deletes anything; the recorder only overwrites footage when it is older than, say, a week.

Kafka is that recorder for events. Applications write events into it, and Kafka keeps them in order. Any number of readers can watch live, or rewind to any point that is still kept. Reading never removes anything: each reader just remembers how far it has watched.

A log, not a queue

In a classic queue, a message is delivered to one worker and then deleted. Kafka works differently. It appends each message to the end of a log, and the message stays there until its retention time runs out, whether or not anyone has read it. That one choice explains most of Kafka:

Many readers

The billing service, the fraud model and your Spark job all read the same messages, each at its own pace.

Rewind

A reader can go back to an older position and read again. That is what makes Kafka a replayablereplayable: A source that can give back exactly the same data again from a saved position, so a failed micro-batch can be run again. Learn more → sourcesource: Where a stream reads from: a Kafka topic, a folder of files, a Delta table. Learn more → for Spark.

Slow readers are fine

A reader that falls behind just reads older messages later. Producersproducer: An application that writes events to a topic. Learn more → never wait for it.

Topics, partitions and offsets

A topic is a named stream, such as orders. A topic is split into partitionspartition: In Kafka, one ordered slice of a topic, read in order. In Spark, one chunk of a DataFrame's rows, processed as one task. Learn more →, and each partition is its own log. Inside a partition, every message gets the next number, its offset: 0, 1, 2, and so on, never reused.

Write · producer

A producer sends an order for customer c-17. Kafka picks a partition from the message key (c-17), say partition 1, and appends the message at offset 842.

Order · per partition

Every later order for c-17 goes to partition 1 too, at offsets 843, 844 and on. Inside partition 1 the order is fixed. Across partitions there is no order at all: offset 842 in partition 1 says nothing about offset 842 in partition 2.

Read · consumersconsumer: An application that reads events from a topic and keeps track of how far it has read. Learn more →

A reader keeps one number per partition: "I have read partition 0 up to 1,203, partition 1 up to 842, partition 2 up to 977." To continue after a restart, it asks for the next offset in each.

Expire · retention

After the retention time (7 days by default), Kafka deletes the oldest messages of each partition, a whole file at a time. Offsets keep counting up; the early ones just no longer exist.

Partitions are how Kafka scales. Each partition can live on a different server, and each one can be read by a different worker, so a topic with 12 partitions can be written and read 12 ways at once. The price is ordering: Kafka orders messages only within a partition. Ordering and out-of-order events shows how keys put the messages that need order into the same partition.

What is in a message

PartWhat it holdsExample
KeyOptional. Decides the partition; messages with the same key stay in orderc-17
ValueThe event itself, as bytes: JSON, AvroAvro: A compact binary format whose messages need a schema to be read; common on Kafka. Learn more → or Protobuf{"order_id": "o-9", "amount": 42.5}
TimestampWhen the producer created it (or when Kafka stored it, if the topic is set that way)2026-11-28 10:15:02
HeadersOptional key-value pairs, like the labels on an envelopesource=mobile-app

Kafka does not look inside the value. To Kafka it is just bytes, which is why Spark gives you key and value as binary columns that you cast and parse yourself.

Brokers and replicas

A Kafka cluster is a group of servers called brokers. Each partition is stored on several brokers: one copy is the leader, which takes all the writes, and the others are followers that copy it. If the leader's broker dies, a follower that is fully caught up takes over, so no data is lost. How many copies to keep, and how many must confirm a write, is a production question covered in Kafka in production.

Retention decides how far back you can replay

Retention is set per topic, by time (retention.ms, 7 days by default) or by size (retention.bytes, unlimited by default). It is also your replay window. If a Spark job is down for longer than the retention, the messages it still needed are gone, and the query stops with a data-loss error (failOnDataLoss). Some topics instead keep the latest message per key forever, called compaction; that suits "current state" topics such as customer profiles.

Tip: land the raw messages in a bronze Delta table as soon as you read them. Kafka retention then only needs to cover an outage, and your full history lives in cheap storage that you can replay any time.

How Spark fits in

  1. Producers

    apps, CDC tools

  2. Kafka topic

    partitions, offsets

  3. Spark query

    offsets in the checkpointcheckpoint: In streaming, the folder where Spark records which input it has processed and its state, so a restarted query continues where it stopped. Learn more →

  4. Delta table

    bronze

Spark reads a topic like any other reader: for each micro-batchmicro-batch: One small round of a stream: Spark takes the data that arrived since the last round, processes it like a little batch job, and saves its progress. Learn more → it records which offsets it will read in every partition, reads them, and marks the batch done. Because the messages stay in Kafka, a failed batch can be read again from the same offsets. The next lesson, Consumer groups and offsets, shows how ordinary Kafka readers share the work, and why Spark keeps track of offsets differently.

Common mistakes

  • Treating Kafka like a queue that empties — Messages stay until retention ends. Reading does not remove them; each reader keeps its own position.
  • Expecting order across a whole topic — Order holds only within a partition. Use a key so related messages share a partition.
  • Keeping retention shorter than your worst outage — A job that is down longer than retention loses data. Size retention for recovery, and land raw data in Delta.
  • Putting large files inside messages — Messages are meant to be small (up to about 1 MB by default). Store the file elsewhere and send its path.

Key takeaways

  • Kafka is a log: messages stay until retention ends, and reading never removes them.
  • A topic is split into partitions; each partition is an ordered log numbered by offsets.
  • Order holds only within a partition, and the key decides the partition.
  • Retention is your replay window, so size it for your longest outage.

Interview prep

THE QUESTION

"What is the difference between Kafka and a traditional message queue?"

A queue delivers each message to one consumer and deletes it once acknowledged. Kafka appends messages to a partitioned log and keeps them for the retention period whether or not they were read. Each consumer group tracks its own offsets, so many independent readers can read the same data, at their own speed, and rewind to replay. Order is guaranteed only within a partition, and partitions are the unit of scaling. Mention that Kafka 4 added share groups for queue-like consumption when you need it.

Avoid: "Kafka is a message queue" without saying that messages stay after they are read, that order holds only within a partition, or that retention is the replay window.

Follow-ups · answer out loud, then open

Follow-up"Why does Kafka split a topic into partitions?"
Partitions are the unit of scale. Each partition is an ordered log that can live on a different broker and be read by a different consumer, so a topic with 12 partitions can be written and read 12 ways in parallel. The cost is ordering: Kafka guarantees order only within a partition, so messages that must stay in order share a key and therefore a partition.
Scenario"A downstream job was down for longer than the topic's retention. What happened, and how do you prevent it next time?"
Kafka deleted the oldest messages on schedule whether or not they were read, so the job's saved offsets now point at data that no longer exists. Spark stops with a data-loss error if failOnDataLoss is on, which it should be. Recover from the raw copy if one exists, otherwise accept the gap and document it. To prevent it, size retention for the longest realistic outage, alert on lag well before retention runs out, and land raw messages in a bronze Delta table so history does not depend on Kafka retention.
Trap"Offsets are global across the topic, so offset 500 is older than offset 900."
Offsets are per partition. Offset 500 in partition 0 and offset 900 in partition 3 say nothing about each other. Comparing order across partitions needs a timestamp or a sequence number inside the event.

Check yourself

3 questions

A consumer reads a message from a Kafka topic. What happens to the message?

Show the answer

It stays until the topic's retention removes it. Kafka keeps messages for the retention period; each reader only remembers its own position.

What does an offset identify?

Show the answer

A message's position within one partition. Offsets number the messages in each partition: 0, 1, 2 and so on.

Your Spark job was down for 9 days and the topic keeps 7 days. What happens on restart?

Show the answer

Messages it needed are gone; with failOnDataLoss on, the query stops. Retention does not wait for readers. The safe default stops the query so someone notices the gap.

Practise this

Interview problems on streaming, graded on hidden tests

Keep going

Up next · Kafka basics · lesson 2 of 2 · 7 min read
Consumer groups and offsets
How a group shares partitions, rebalances, committed offsets and lag, and why Spark keeps its offsets in the checkpoint instead.

Related lessons

Previous: Checkpoints and state stores

Primary sources: Kafka introduction · Kafka design · Topic configs (retention.ms, cleanup.policy) · Spark Kafka integration