Apache Kafka
Consumer Groups
Kafka Topics
Distributed Systems
Message Brokering

understanding kafka, consumer groups, and topics

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

Apache Kafka is a popular distributed streaming platform that allows for high-throughput, scalable, fault-tolerant handling of real-time data feeds. It’s widely utilized in a variety of applications, primarily for building real-time streaming data pipelines and applications that adapt to data streams. Here, we’ll delve into the fundamental concepts of Kafka, focusing on topics, consumer groups, and their respective roles and functionalities.

What is Kafka?

At its core, Kafka is built as a distributed system designed for stream processing. Essentially, it enables businesses to process and analyze data as it arrives, which is crucial in situations where responsiveness and real-time analytics are critical — such as in monitoring user activity on websites, financial trading, or connected devices in the IoT environment.

Kafka Topics

In Kafka, a topic is a category or feed name to which records are stored and published. All Kafka records are organized into topics. Producers write data to topics and consumers read from topics.

Characteristics of Kafka Topics:

  • Partitioned: Each topic is split into partitions. This allows the topic to be scaled horizontally by distributing partitions across multiple brokers in the cluster.
  • Log-Based: Each partition is essentially a log (an ordered, immutable sequence of records that is continually appended).
  • Replicated: Partitions can be replicated across multiple Kafka brokers to ensure fault tolerance.

Here’s how you might create a new topic in Kafka:

bash
kafka-topics.sh --create --topic my-topic --bootstrap-server localhost:9092 --replication-factor 1 --partitions 4

This command creates a new topic named my-topic with one replication factor and four partitions.

Kafka Brokers

Kafka brokers are servers that store data and serve clients. A Kafka cluster is composed of multiple brokers to ensure load balancing and fault tolerance. Each broker may have zero or more partitions per topic.

Kafka Producer

Producers are applications or processes that send records to Kafka topics. The producer is responsible for choosing which record to assign to which partition within the topic. This can be done in a round-robin fashion for load balancing or directed to a specific partition based on some key.

Kafka Consumer and Consumer Groups

A consumer fetches data from Kafka topics and processes it. Consumers label themselves with a consumer group name, and each record published to a topic is delivered to one consumer instance within each subscribing consumer group. Consumer groups provide the ability to have multiple consumers that are cooperating and share a workload.

Consumer Group Characteristics:

  • Load Balancing: Within a consumer group, each consumer is assigned one or more partitions from the topics they subscribe to, ensuring that the consumption workload is balanced among consumers.
  • Fault Tolerance: If a consumer fails, its partitions will be automatically reassigned to other consumers in the same group.

When a consumer in a group has processed data received from Kafka, it commits the offsets of records. This offset commit tells Kafka that the consumer group has successfully received and processed all prior records returned by the broker.

Here’s an example of starting a consumer from the command line:

bash
kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic my-topic --group my-group

This command starts a Kafka consumer that subscribes to the my-topic topic as part of the my-group consumer group.

Summary Table

ConceptDescription
TopicA category or feed name where records are stored. Topics are partitioned, log-based, and replicated.
BrokerA server that stores Kafka data and services clients. Part of a Kafka cluster.
ProducerApplications that publish records to topics.
ConsumerApplications that read records from topics. Part of a consumer group for load balancing and fault tolerance.
PartitionA division within a topic that allows it to be parallelized across brokers.

Conclusion

Apache Kafka’s architecture—the concepts of topics, partitions, consumer groups, and brokers—enables highly scalable and fault-tolerant real-time data processing. Understanding these core components is crucial for effectively leveraging Kafka in data streaming and processing scenarios. By mastering these concepts, developers and data engineers can design more resilient and scalable systems tailored to their specific data needs.


Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

System Design practice on Codemia

Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.

Practice system design

All Rights Reserved.