What Is a Topic in Kafka?
A beginner-friendly guide to the one idea every Kafka user must understand first — how Kafka organises the flood of data flowing through it into named, manageable streams.
Imagine a large newsstand with hundreds of different magazines on its shelves. Sports news sits in one rack, cooking magazines in another, technology journals in a third. Nobody dumps every magazine into one giant pile, because then finding anything would be impossible. Instead, everything is organised into clearly labelled sections, so a shopper who wants cooking tips can walk straight to that rack and ignore everything else. Apache Kafka faces exactly the same organisational challenge with the data flowing through it, and its answer to that challenge is called a topic. In this guide we will build up the whole picture slowly, defining every new word the first time it appears.
1Why Kafka Needs a Way to Organise Data
Before defining a topic precisely, it helps to see the mess it prevents.
A flood of different kinds of data
A single company might use Kafka to move many different kinds of information at once: customer orders, payment confirmations, delivery updates, website clicks, and system error alerts, all flowing through the same Kafka cluster at the same time. If all of this data landed in one shared, unlabelled pile, any program trying to read it would have to sift through everything just to find the handful of events it actually cared about.
This is not just inconvenient, it is also unsafe. A program that only needs to read payment confirmations should not need to see customer support messages, and giving it access to everything would break the principle of giving each program only the access it truly needs.
Picture a post office with no sorting system at all, where every letter for every person in the city is thrown into one enormous bin. Finding your own mail would take hours. Post offices instead sort mail by address, so each person only has to look through what is actually theirs. Kafka needs the same kind of sorting for the data flowing through it.
The answer: named categories
Kafka solves this by letting data be organised into named categories from the very start. Whoever sends data decides which category it belongs to, and whoever wants to read data simply asks for the category they care about. This category is called a topic, and it is one of the very first ideas anyone learning Kafka needs to understand.
2So, What Exactly Is a Topic?
A precise definition, then the plain-English version.
A short definition
A topic in Kafka is a named stream of related events, similar to a category, a folder, or a channel, into which producers write data and from which consumers read it. A producer is any program that sends data into Kafka, and a consumer is any program that reads data out of it. Every single piece of data written to Kafka must belong to exactly one topic, chosen by whoever writes it.
In everyday terms, a topic is simply a label with a name, such as “orders” or “payments”, that groups together everything related to that subject. Anything published under the “orders” topic is treated as part of one continuous stream of order-related events, kept separate from everything happening in other topics.
Topics are not files, and not folders
It is tempting to think of a topic as a file that gets appended to, and that mental picture is a useful starting point, but a topic behaves a little differently. A topic can be split internally, spread across multiple machines, and read by many independent programs at the same time, none of which is normally true of an ordinary file. We will explore that internal structure shortly.
Topic names are chosen by whoever creates the topic, not assigned automatically by Kafka. Teams typically agree on naming conventions, such as using lowercase words separated by dots or hyphens, so topic names stay consistent and easy to understand across a whole organisation.
3What Actually Lives Inside a Topic
Understanding the contents of a topic makes everything else about Kafka easier to follow.
Event (Record)
A single fact stored in a topic, such as “order 4521 was placed at 10:03”. Each event usually has a key, a value, and a timestamp.
Key
An optional label attached to an event, often used to group related events together, such as a customer number.
Value
The actual content of the event, such as the details of an order. Kafka treats this as raw bytes and does not enforce a particular format.
Timestamp
The moment the event was created or written, letting consumers understand when something happened.
A topic is a sequence, not a single blob
A topic does not store one giant lump of data. It stores an ordered sequence of individual events, one after another, in the order they were written. New events are always added to the end of this sequence, never inserted in the middle and never edited once written. This append-only behaviour is a defining feature of how Kafka stores data.
Think of a topic as a very long receipt roll at a shop till. Every new sale is printed at the bottom of the roll, one after another, and nothing already printed can be erased or rewritten. Anyone can read back through the roll to see exactly what happened and in what order.
4Partitions: How a Topic Is Actually Built
A topic looks simple from the outside, but it hides a clever structure inside.
Splitting a topic into pieces
A single topic can grow far too large and busy for one machine to handle comfortably. To solve this, Kafka splits every topic into smaller pieces called partitions. Each partition is its own ordered, append-only sequence, and a topic is really just a named collection of one or more of these partitions working together.
Partitions can live on different machines, called brokers, which lets a topic’s workload be spread across an entire cluster instead of resting on a single computer. A topic named “orders” might have three partitions, each holding roughly a third of all order events, with all three working in parallel.
Picture a supermarket with several checkout lanes instead of just one. Each lane is a partition. Customers are served in order within their own lane, but the lanes operate side by side, so the store can serve far more shoppers overall than it could with a single, very long queue.
flowchart LR
T
T --> P0[Partition 0]
T --> P1[Partition 1]
T --> P2[Partition 2]
Order is guaranteed only within a partition
This is one of the most important facts about topics for a beginner to learn early. Kafka guarantees that events stay in the exact order they were written, but only inside a single partition. There is no guarantee about the order of events across different partitions of the same topic. If two events absolutely must stay in order relative to each other, they need to share the same key, since Kafka uses the key to decide which partition an event goes to, ensuring they land in the same partition together.
Many beginners assume a topic guarantees strict order across everything written to it. In reality, order is only guaranteed within each individual partition, not across the whole topic.
5Offsets: Keeping Track of Position
Every event inside a partition needs a way to be located precisely.
A running position number
Every event within a partition is given a number called an offset, starting at zero and increasing by one for each new event. This offset never changes for a given event and is never reused, even if older events are eventually removed. It works much like a page number in a book, letting anyone say precisely “start reading from position 500” or “I have already read up to position 750”.
Why offsets matter so much
Offsets are what let a consumer remember exactly where it stopped reading. If a consumer goes offline for an hour and then comes back, it can simply resume from the offset it last processed, rather than starting over or missing data. Offsets also let a consumer intentionally rewind and reread older events, which is extremely useful for fixing mistakes, testing new features against real historical data, or building an entirely new system from a topic’s existing history.
An offset is like a bookmark placed inside a long book. You can always find your exact place again, hand the bookmark to a friend so they can start from where you left off, or move it backward to reread a chapter you want to revisit.
flowchart LR
O0["Offset 0"] --> O1["Offset 1"] --> O2["Offset 2"] --> O3["Offset 3"] --> O4["Offset 4"]
6Producers: Writing Into a Topic
A closer look at how data actually enters a topic.
Choosing where each event goes
When a producer sends an event, it must specify which topic the event belongs to. If the topic has more than one partition, the producer, or Kafka acting on its behalf, must also decide which specific partition receives that event. If the event has a key, Kafka runs the key through a formula so the same key always lands in the same partition, keeping related events together and in order. If there is no key, Kafka spreads events across the available partitions in a balanced way.
Think of sorting parcels by postcode at a delivery depot. Every parcel for the same postcode always ends up in the same delivery van, keeping them together, while parcels with different postcodes are naturally spread across the fleet of vans.
A producer does not need to know its readers
A producer’s job ends the moment its event is safely written to a topic. It does not need to know who will read that event, how many readers there are, or when they will read it. This separation between writing and reading is one of the reasons Kafka-based systems can grow and change so easily over time.
7Consumers: Reading From a Topic
How programs actually pull events back out of a topic.
Subscribing to a topic
A consumer reads events by subscribing to one or more topics it cares about. Once subscribed, it can request events starting from a particular offset, often simply continuing from wherever it last left off. Multiple, completely unrelated consumers can read from the exact same topic independently, each keeping track of its own position, without disturbing one another in any way.
Consumer groups and shared work
Consumers are often organised into consumer groups, where the partitions of a topic are divided among the group’s members so the reading work is shared. If a topic has six partitions and a group has three consumers, each consumer typically handles two partitions. Different consumer groups are independent of each other, and each group receives its own complete view of the topic, at its own pace.
flowchart TD
T[Topic: orders]
T --> C1[Payment Service]
T --> C2[Warehouse System]
T --> C3[Analytics Pipeline]
Unlike some traditional messaging systems, reading an event from a Kafka topic does not remove it. The event stays in the topic, available to be read again by this consumer or any other, until it is eventually removed according to the topic’s retention settings.
8Retention: How Long a Topic Keeps Its Data
A topic does not hold onto everything forever by default, and that is a choice you control.
Time-based and size-based retention
Every topic has a retention setting that decides how long events are kept before being removed. This is commonly measured in time, such as keeping events for seven days, or in total size, such as keeping up to a certain number of gigabytes per partition, after which the oldest events are discarded to make room for new ones. Teams choose retention based on how long downstream systems might realistically need to catch up or replay data.
Log compaction: keeping only the latest per key
Some topics do not need their full history at all, only the most recent value for each distinct key. For these cases, Kafka offers a special retention mode called log compaction, where older events are removed once a newer event with the same key has been written, keeping only the latest state for every key. This is especially useful for topics that represent the current state of something, such as the latest address on file for each customer.
Ordinary retention is like a shop keeping receipts for exactly thirty days before shredding them. Log compaction is more like a filing cabinet that only ever keeps the single most recent form for each customer, quietly throwing away any older version once a newer one arrives.
| Retention style | What it keeps | Good fit for |
|---|---|---|
| Time-based | Everything, until it reaches a certain age | Event history, auditing, replay scenarios |
| Size-based | Everything, until the topic reaches a certain size | High-volume topics with a fixed storage budget |
| Log compaction | Only the latest event per key | Current-state data, such as latest customer details |
9Replication: Protecting a Topic’s Data
A topic’s partitions are far too important to trust to a single machine.
Copies across multiple brokers
Each partition of a topic can be copied onto several brokers, a setting called the replication factor. A replication factor of three, a common production choice, means every event is stored on three different machines. If one broker fails, the data remains safe and available on the other copies.
Leaders and followers
For every partition, one copy is designated the leader, and the rest are followers that continuously copy whatever the leader receives. All reads and writes for that partition go through the leader. If the leader’s broker fails, one of the up-to-date followers is promoted to become the new leader, and producers and consumers automatically discover and adjust to the change, usually within moments.
Think of an important contract kept as photocopies in three separate offices. One office is the official copy people visit, while the other two quietly stay up to date. If the official office is unavailable, one of the others instantly becomes the official copy, and nobody loses the contract.
A topic created with a replication factor of one has no safety net at all. That may be fine for learning on a laptop, but in production it turns any single broker failure into permanent data loss for that partition.
10Choosing the Number of Partitions
One of the most important decisions made when a topic is created.
More partitions, more parallel work
The number of partitions a topic has directly limits how much of its work can happen in parallel. More partitions generally allow more consumers within a single group to work simultaneously, and can allow more producers to write at once without competing for the same resources. A topic with only one partition can never be read in parallel by more than one active consumer within a group, no matter how many consumers that group has.
The cost of too many partitions
More partitions are not automatically better. Each partition adds a small amount of overhead to every broker that hosts it, and having far more partitions than a cluster actually needs can slow down recovery after a failure and add unnecessary strain to the brokers managing them. Choosing the right number of partitions is a balance between expected parallel workload and practical overhead.
Example: Sizing a New Topic
A team expecting to eventually run twelve parallel consumers for a busy “clickstream” topic might choose twelve partitions from the start, or slightly more to allow future growth, rather than starting with just one and hitting a ceiling later.
It is easier to plan generously for partitions early than to change them later, since adding partitions to an existing topic can disturb how keys map to partitions, potentially breaking the ordering guarantee that some events were relying on.
11Topic Configuration Options
A topic is not just a name; it comes with settings that shape its behaviour.
Partitions
How many partitions the topic is split into, set when the topic is created and adjustable later, though usually only by increasing it.
Replication Factor
How many copies of each partition are kept across the cluster, protecting the topic against broker failures.
Retention Time or Size
How long, or how much data, the topic keeps before older events are removed.
Cleanup Policy
Whether the topic uses ordinary time or size based deletion, log compaction, or a combination of both.
Minimum In-Sync Replicas
How many replicas must confirm a write before it is considered successful, controlling the balance between safety and speed.
Most of these settings can be adjusted after a topic is created, letting teams start with sensible defaults and refine them as real usage patterns become clearer.
12Naming and Organising Topics Well
A small amount of discipline here saves a lot of confusion later.
Why naming conventions matter
As a company adopts Kafka more widely, the number of topics tends to grow quickly, often into the hundreds. Without a clear naming convention, it becomes hard to tell what a topic contains just by looking at its name, who owns it, or which team is responsible for it. Many organisations settle on a structured naming pattern, such as combining the domain, the kind of event, and a version number.
Example Naming Pattern
A pattern like “domain.event-name.version”, giving names such as “orders.order-placed.v1” or “payments.payment-confirmed.v1”, makes a topic’s purpose and owner easy to guess at a glance, even in a cluster with hundreds of topics.
One topic per distinct kind of event
A well-organised Kafka deployment generally favours many small, focused topics over a few large, mixed ones. A topic that mixes several unrelated kinds of events forces every consumer to filter through irrelevant data and makes access control much harder to manage cleanly.
Problem
Creating one giant, generically named topic, such as “events”, and sending every kind of data into it.
Why It’s Harmful
Consumers must filter out irrelevant data, permissions become impossible to manage cleanly, and retention cannot be tuned for the different needs of different kinds of data.
Correct Approach
Create focused, clearly named topics, one for each distinct kind of event, following an agreed naming convention.
13Real-World Examples of Topics
Seeing a few concrete examples makes the concept click.
An E-commerce Order Topic
A topic named “orders” might hold every order placed on a website, read independently by the payment service, the warehouse system, and the analytics team, each processing the same stream in their own way.
A Ride-Sharing Location Topic
A ride-sharing app might use a topic to carry a constant stream of driver location updates, read by the rider’s live map, the fare calculation service, and a safety monitoring system.
A Banking Transactions Topic
A bank might maintain a highly replicated, carefully retained topic for transaction events, feeding fraud detection systems, balance updates, and regulatory reporting from a single trusted stream.
A Compacted Customer Profile Topic
A topic using log compaction might store the current profile details for every customer, keyed by customer ID, always reflecting only the latest known information for each person.
A Log Aggregation Topic
Many servers across a company might all publish their application logs into a shared topic, from which monitoring tools, search systems, and long-term archives each read what they need.
14Best Practices and Common Mistakes
Lessons that save teams from painful surprises later on.
Best practices
Plan partitions with future growth in mind
Estimate realistic future traffic and choose a partition count that gives room to grow, without going wildly beyond current needs.
Set a sensible replication factor
Use a replication factor of at least three in production, paired with a suitable minimum in-sync replicas setting for important data.
Choose keys carefully
Pick a key that keeps related events together while spreading load evenly, avoiding a single overly popular key that overloads one partition.
Agree on event formats early
Define and document the structure of each topic’s events so producers and consumers stay compatible as the system evolves.
Follow a consistent naming convention
Adopt a clear topic naming pattern across the whole organisation before the number of topics grows too large to manage informally.
Mistakes beginners often make
Forgetting that ordering is only guaranteed within a single partition can lead to subtle bugs when events that must stay in sequence end up scattered across different partitions.
Setting retention too long for the available disk space can fill up brokers, while setting it too short can cause slower consumers to miss data that has already been removed.
Cramming unrelated kinds of events into one topic to avoid “creating too many topics” usually causes more problems than it solves, from messy permissions to mismatched retention needs.
15Frequently Asked Questions
Not quite. A traditional queue usually removes a message once it has been read, and typically delivers each message to only one reader. A Kafka topic keeps its events for a chosen retention period and can be read independently by many different consumers.
Yes. A topic can have as few as one partition, which is simple and guarantees strict overall ordering, but it also limits how much parallel reading and writing that topic can support.
A single cluster can comfortably host many thousands of topics, though the practical limit depends on the cluster’s size, hardware, and total partition count across all topics combined.
Yes, partitions can be increased later, but this can affect how keys map to partitions, so it is generally safer to plan a reasonable partition count from the start rather than relying on frequent changes.
No. Reading an event from a topic does not remove it. The event remains available to be read again, by the same consumer or a different one, until it is eventually removed according to the topic’s retention rules.
A topic is the named category that consumers subscribe to and producers write to. A partition is one of the internal, ordered pieces that a topic is physically split into, allowing the topic’s data to be spread across a cluster.
Kafka itself does not enforce a structure on the data inside a topic. It is up to the producers and consumers, often guided by an agreed schema or format, to decide and maintain a consistent structure for that topic’s events.
16Summary and Key Takeaways
A topic is the fundamental way Kafka organises the flow of data passing through it, giving every kind of event a clear, named home. Internally, a topic is built from one or more partitions, each an ordered, append-only sequence of events identified by increasing offsets. Producers write events into a topic, often guided by a key that decides the partition, while consumers subscribe to read those events independently, at their own pace, without removing them. Settings like replication, retention, and cleanup policy shape how safely and how long a topic’s data is kept, and thoughtful choices around partition count and naming pay off enormously as a Kafka deployment grows. Understanding topics deeply is the single most important step toward understanding Kafka as a whole.
Key Takeaways
- A topic is a named stream of events — the basic way Kafka organises all the data flowing through it.
- Topics are built from partitions — ordered, append-only sequences that let a topic scale across many machines.
- Order holds only within a partition — related events need a shared key to stay in guaranteed order.
- Offsets mark position — letting consumers resume, rewind, and replay precisely where they need to.
- Producers and consumers are decoupled — writers and readers never need to know about each other directly.
- Retention and replication protect and shape data — controlling how long events last and how safely they are stored.
- Good naming and sizing choices matter — clear topic names and sensible partition counts keep a growing Kafka deployment manageable.