Kafka
Topic Configuration
Properties File
Data Streaming
IT Administration

kafka topic configuration using properties file

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 widely-used stream-processing software platform developed by the Apache Software Foundation, written in Scala and Java. A core element within Kafka is the concept of topics, which are essentially categories or feeds to which records are published.

Topics in Kafka, like most other parts of its configuration, can be controlled through various settings. These settings can be specified in a properties file, allowing for a consistent and reproducible setup. Below, we explore the essential properties for Kafka topic configuration and provide examples for practical application.

Understanding Kafka Topic Properties

The configuration of a Kafka topic can significantly impact its performance and behavior. The key properties affecting topic behavior in Kafka are:

  1. Topic Name:
    • It's a unique string that identifies the topic.
  2. Partitions:
    • A topic can be split into multiple partitions. This allows for parallel processing and increases the scalability of the system. Partitions play a crucial role in how data is distributed across the cluster.
  3. Replication Factor:
    • This specifies the number of copies of a topic's partitions across multiple brokers for fault tolerance. Higher replication factors increase data availability.
  4. Retention Policy:
    • Determines how long data is retained before it is eligible for deletion.
  5. Cleanup Policy:
    • Configures whether to delete or compact old records. The delete policy will remove old records after a retention time, while the compact policy retains at least the latest value for each key.

Example of a Kafka Properties File for Topic Configuration

Kafka's topic-specific configuration can be set within a dedicated .properties file. Below is a basic example of how to configure a Kafka topic using a properties file:

properties
1# File: topic-config.properties
2
3# Basic Configuration
4topic.name=exampleTopic
5num.partitions=3
6replication.factor=2
7
8# Retention Policy
9retention.bytes=1073741824 # 1GB
10retention.ms=1680000 # 1 week
11
12# Cleanup Policy
13cleanup.policy=compact

This configuration describes a topic named exampleTopic that has 3 partitions and a replication factor of 2. It has a cleanup policy set to compact, and it retains a maximum of 1GB of logs or one week's worth of logs, whichever comes first.

Applying Properties File to Kafka Topic

After outlining the configuration in a properties file, you must use Kafka's CLI tools to apply these settings. For example, you can create a topic with this configuration using the Kafka command-line interface:

bash
$ kafka-topics.sh --create --bootstrap-server localhost:9092 --config-file topic-config.properties

Key Properties and Their Default Values

For reference, here is a summarizing table of commonly used topic properties and their default values:

PropertyDefaultDescription
num.partitions1Number of partitions for the topic.
replication.factor1Number of replicated copies of a topic. Used for fault tolerance.
retention.bytes-1Maximum size of log before old logs are deleted. (-1 is unlimited)
retention.ms604800000Time in milliseconds to keep logs. (7 days by default)
cleanup.policydeletePolicy to handle deletion of old logs. Options: delete, compact.

Conclusion

Configuring Kafka topics via a properties file offers a flexible and efficient way to manage topic behaviors based on specific needs. It is vital to understand each property and its impact on the system to optimize the performance and reliability of your Kafka deployment. Remember, while the default values provide a good starting point, fine-tuning specific configurations can tremendously benefit Kafka's operation in production environments.


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.