Kafka Topics
Declarative Management
Data Streaming
Kafka Configuration
Programming Guides

How to declaratively manage Kafka 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 distributed event streaming platform capable of handling trillions of events a day. One of the core components of Kafka is a "topic," a category/feed name to which records are stored and published. Traditionally, managing Kafka topics involves either manually creating them using the Kafka command line tools or programmatically via Kafka AdminClient API. However, declarative management of Kafka topics can provide more advantages, such as improved consistency, version control, and easier integration with CI/CD pipelines.

Declarative Management of Kafka Topics

Declarative management involves defining the desired state of a system in configuration files or scripts, rather than executing commands to change the state. This approach can be applied to manage Kafka topics, where the topic configurations are specified in files, and a tool or process ensures that the Kafka cluster's state matches the desired state described in these files.

Tools and Methods

Several tools and frameworks aid in the declarative management of Kafka topics. One prominent method is using Kubernetes in combination with the Strimzi operator, and another method involves using Terraform.

  1. Strimzi and Kubernetes:

Strimzi provides a way to run Kafka cluster in Kubernetes in a cloud-native fashion. It simplifies the process of managing Kafka topics declaratively with Kubernetes Custom Resource Definitions (CRDs). Below is a step-by-step guide to manage Kafka topics using Strimzi:

  • Step 1: Install Strimzi to your Kubernetes cluster.
  • Step 2: Create a KafkaTopic resource. Here’s an example YAML configuration:
yaml
1    apiVersion: kafka.strimzi.io/v1beta2
2    kind: KafkaTopic
3    metadata:
4      name: my-topic
5      labels:
6        strimzi.io/cluster: my-kafka-cluster
7    spec:
8      partitions: 3
9      replicas: 2
10      config:
11        retention.ms: 7200000
12        segment.bytes: 1073741824
  • Step 3: Apply this configuration using kubectl:
bash
    kubectl apply -f my-topic.yaml
  • Step 4: Strimzi ensures that the Kafka cluster state matches the declared configuration.
  1. Terraform:

Terraform by HashiCorp allows you to describe the infrastructure as code. Terraform can manage Kafka topics as well, using Kafka provider plugins. Here is a basic example:

  • Step 1: Define the provider and resource in Terraform configuration:
hcl
1    provider "kafka" {
2      bootstrap_servers = ["localhost:9092"]
3    }
4
5    resource "kafka_topic" "my_topic" {
6      name               = "my-topic"
7      partitions         = 3
8      replication_factor = 2
9      config = {
10        "retention.ms" = "7200000"
11        "segment.bytes" = "1073741824"
12      }
13    }
  • Step 2: Apply the configuration:
bash
    terraform apply

Benefits of Declarative Management

Here are some key benefits of managing Kafka topics declaratively:

BenefitDescription
ConsistencyEnsures the Kafka cluster configuration matches the desired state.
Version ControlTopic configurations can be versioned along with other codes.
AutomationIntegrates easily with CI/CD pipelines for automatic updates.
Audit and SecurityChanges are trackable and can be reviewed before application.
Disaster RecoveryEasy to restore configurations from declarative files in crisis.

Best Practices

When managing Kafka topics declaratively, consider the following best practices:

  • Version Control: Store all Kafka topic configuration files in a version-controlled repository.
  • Validation: Use validation tools or scripts to check configurations for errors before applying them.
  • Monitoring: Always monitor the changes applied and their impact on the Kafka cluster.
  • Security: Define access controls and permissions for managing configuration files and applying changes to Kafka clusters.

Conclusion

Declaratively managing Kafka topics not only simplifies the administration tasks but also enhances the automation, consistency, and reliability of the Kafka infrastructure. Whether using Kubernetes with Strimzi or managing resources via Terraform, adopting declarative management aligns Kafka operations with modern DevOps practices and infrastructure as code paradigms.


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.