Sling Academy
Home/DevOps/Apache Kafka: A Practical Cheat Sheet (Updated)

Apache Kafka: A Practical Cheat Sheet (Updated)

Last updated: January 30, 2024

Apache Kafka is a distributed streaming platform that has established itself as a critical component for building real-time, fault-tolerant, and scalable messaging systems. With Kafka, developers can publish, subscribe to, store, and process streams of records in a fault-tolerant manner. As Kafka continues to evolve, staying up-to-date with its features is essential for developers and data engineers. In this cheat sheet, we will cover key commands, configurations, and concepts needed to work efficiently with Apache Kafka these days.

Introduction to Kafka Basics

Before diving into the practical commands, let’s ground our knowledge with some Kafka basics:

  • Producer: An entity that publishes data to Kafka topics.
  • Consumer: An entity that subscribes to topics and processes the feed of published records.
  • Broker: A Kafka server that stores data and serves clients.
  • Topic: A category or feed name to which records are published.
  • Partition: Topics are split into partitions, which are ordered logs for a subset of the data.

Setting Up Kafka

Starting with the installation of Kafka, we’ll need to download the binaries, extract them, and start the Kafka environment.

wget https://archive.apache.org/dist/kafka/3.2.0/kafka_2.13-3.2.0.tgz
tar -xzf kafka_2.13-3.2.0.tgz
cd kafka_2.13-3.2.0
bin/zookeeper-server-start.sh config/zookeeper.properties &
bin/kafka-server-start.sh config/server.properties &

Working with Topics

Topics in Kafka are the core around which the rest of Kafka is built. Let’s go through some essential topic operations:

Creating a Topic

bin/kafka-topics.sh --create --partitions 3 --replication-factor 1 --topic my_topic --bootstrap-server localhost:9092

Listing Topics

bin/kafka-topics.sh --list --bootstrap-server localhost:9092

Describing a Topic

bin/kafka-topics.sh --describe --topic my_topic --bootstrap-server localhost:9092

Deleting a Topic

bin/kafka-topics.sh --delete --topic my_topic --bootstrap-server localhost:9092

Producing and Consuming Messages

Producing Messages to a Topic

echo "Hello, Kafka!" | bin/kafka-console-producer.sh --broker-list localhost:9092 --topic my_topic

Consuming Messages from a Topic

bin/kafka-console-consumer.sh --bootstrap-server localhost:9092 --topic my_topic --from-beginning

Advanced Kafka Operations

As we get more familiar with Kafka, we might need to perform more advanced operations like configuring partitions and consumer groups or conducting administrative tasks such as increasing replicas for topics.

Modifying Topic Partitions

bin/kafka-topics.sh --alter --zookeeper localhost:2181 --topic my_topic --partitions 4

Consumer Groups

Consumers in Kafka are typically part of a consumer group. Let’s see how to manage these groups:

Listing Consumer Groups

bin/kafka-consumer-groups.sh --list --bootstrap-server localhost:9092

Describing Consumer Groups

bin/kafka-consumer-groups.sh --describe --group my_group --bootstrap-server localhost:9092

Resetting Consumer Group Offsets

bin/kafka-consumer-groups.sh --bootstrap-server localhost:9092 --group my_group --reset-offsets --to-earliest --execute --topic my_topic

Monitoring and Managing Kafka

Monitoring Kafka is critical for understanding the performance and the health of the cluster. Apache Kafka provides JMX metrics out of the box, and you may choose to monitor these with tools like JConsole or integrate them into monitoring solutions such as Prometheus.

Conclusion

In this guide, we’ve explored key concepts, configurations, and commands that are essential for effective Kafka operations. From setting up a Kafka cluster to advanced consumer group management, these practical tips should serve as a rapid reference for your Kafka-related tasks. With the power of Kafka at your fingertips, you can build and maintain robust streaming applications well into the future.

Next Article: Understanding Topics and Partitions in Apache Kafka (with Examples)

Previous Article: How to completely remove Kafka from your computer

Series: Apache Kafka Tutorials

DevOps

You May Also Like

  • NGINX underscores_in_headers: Explained with examples
  • How to use Jenkins CI with private GitHub repositories
  • Terraform: Understanding State and State Files (with Examples)
  • SHA1, SHA256, and SHA512 in Terraform: A Practical Guide
  • CSRF Protection in Jenkins: An In-depth Guide (with examples)
  • Terraform: How to Merge 2 Maps
  • Terraform: How to extract filename/extension from a path
  • JSON encoding/decoding in Terraform: Explained with examples
  • Sorting Lists in Terraform: A Practical Guide
  • Terraform: How to trigger a Lambda function on resource creation
  • How to use Terraform templates
  • Understanding terraform_remote_state data source: Explained with examples
  • Jenkins Authorization: A Practical Guide (with examples)
  • Solving Jenkins Pipeline NotSerializableException: groovy.json.internal.LazyMap
  • Understanding Artifacts in Jenkins: A Practical Guide (with examples)
  • Using Jenkins with AWS EC2 and S3: A Practical Guide
  • Terraform: 3 Ways to Remove Duplicates from a List
  • Terraform: How to convert a number to a string and vice versa
  • Using bcrypt() and md5() functions in Terraform