Replicating messages from one Kafka topic to another kafka topic
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
Apache Kafka is a distributed streaming platform capable of handling trillions of events a day. One common requirement when working with Kafka is the ability to replicate messages from one topic to another. This can serve various purposes such as data aggregation, stream processing, or simply as a mechanism for data backup. In this article, we delve into the details of how to perform this message replication effectively using Kafka native tools and other third-party tools.
Kafka's MirrorMaker
One of the primary tools provided by Kafka for replicating data between topics is MirrorMaker. Kafka MirrorMaker copies data from the source Kafka cluster to a target cluster, but it can also be used within the same cluster for topic-to-topic replication. Here’s how you can use MirrorMaker to replicate messages from one Kafka topic to another.
Setting up MirrorMaker
MirrorMaker requires setting up consumer configurations for the source cluster and producer configurations for the destination cluster. It works by consuming messages from the source topic and then producing those messages to the destination topic. Below is an example configuration for MirrorMaker:
- Source consumer configuration:
- Destination producer configuration:
- Running MirrorMaker:
It's important to note that --whitelist specifies the topic patterns from the source cluster that you want to replicate.
Advanced Tools and Techniques
Kafka Connect
An alternative to MirrorMaker is Kafka Connect, which is a tool designed for scalable and reliable streaming data between Apache Kafka and other data systems. Kafka Connect can be used for more complex data pipelines than MirrorMaker because it supports custom transformations and connectors for numerous external systems.
Example Configuration
- Kafka Connect Source Connector:
Stream Processing Frameworks
Frameworks like Kafka Streams and Apache Flink can also be used for more complex topic-to-topic data replication needs. These frameworks allow complex transformations, aggregations, or joins before writing data to a new topic.
Example with Kafka Streams
Summary Table
| Feature | MirrorMaker | Kafka Connect | Kafka Streams |
| Purpose | Basic replication | Complex pipelines, transformation | Advanced stream processing |
| Ease of Use | Simple to set up | Requires configuration & possibly custom connectors | Requires writing code |
| Performance | Moderate | High (with proper tuning) | High (depends on processing complexity) |
| Customizability | Low | High | Very High |
Conclusion
Replicating messages from one Kafka topic to another can be achieved using MirrorMaker, Kafka Connect, or stream processing frameworks depending on the requirements like whether transformations are needed, and the level of control required over the replication process. Each tool offers different levels of power and flexibility to handle various use cases efficiently. For straightforward replication, MirrorMaker might suffice, but for more complex scenarios involving transformations and enhancements, Kafka Connect or a streaming framework would be more appropriate.
Related reading
- Replication factor 3 larger than available brokers 1 when starting the kafka
- ReplicationFactor vs replicas in kafka
- Reproduce RabbitMQ network partition scenario
- Request messages between two timestamps from Kafka
- Replication between Mysql and Postgres for data warehousing
- replication between SQL Server and MySQL server
- Rereading message from Kafka topic by refusing acknowledgement
- Reset consumer offset in kafka 0.10

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.