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.
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:
- Topic Name:
- It's a unique string that identifies the topic.
- 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.
- 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.
- Retention Policy:
- Determines how long data is retained before it is eligible for deletion.
- Cleanup Policy:
- Configures whether to delete or compact old records. The
deletepolicy will remove old records after a retention time, while thecompactpolicy 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:
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:
Key Properties and Their Default Values
For reference, here is a summarizing table of commonly used topic properties and their default values:
| Property | Default | Description |
| num.partitions | 1 | Number of partitions for the topic. |
| replication.factor | 1 | Number of replicated copies of a topic. Used for fault tolerance. |
| retention.bytes | -1 | Maximum size of log before old logs are deleted. (-1 is unlimited) |
| retention.ms | 604800000 | Time in milliseconds to keep logs. (7 days by default) |
| cleanup.policy | delete | Policy 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
- Kafka topic creation best-practice
- Kafka topic creation Timed out waiting for a node assignment
- Kafka Topic creation upon specific broker
- Kafka topic has partitions with leader=-1 (Kafka Leader Election), while node is up and running
- Kafka topic is getting reappeared after 10 sec of deletion
- Kafka Topic Message Versioning
- Kafka topic no longer exists after restart
- Kafka topic partition and Spark executor mapping

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack 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.