Migrate an Apache Kafka® cluster to Aiven with MirrorMaker 2
Migrate an existing Apache Kafka® cluster to Aiven for Apache Kafka® using Aiven for Apache Kafka® MirrorMaker 2. The source can be a self-hosted, on-premises, or third-party managed cluster.
MirrorMaker 2 replicates topic data while producers and consumers keep using the source cluster. After replication catches up, migrate the schemas, ACLs, and connectors you need. Then switch your applications to the target Aiven service.
The procedures use the Aiven Console. You can also manage replication flows using the Aiven CLI, the Aiven API, or Terraform.
MirrorMaker 2 replicates data asynchronously. Plan a cutover period for the final steps. During the cutover, stop writes to the source cluster and synchronize consumer group offsets. Verify the migration before you decommission the source cluster.
Prerequisites
Before you begin, you need the following:
-
An Aiven project
-
Connection details for the source cluster, including bootstrap servers, security protocol, and credentials or SSL certificates
-
A MirrorMaker 2 user with the following permissions:
- On the source cluster:
READaccess to the topics to replicate andDESCRIBEaccess to the consumer groups to migrate - On the target cluster:
WRITEaccess to the replicated topics - On both clusters:
READ,WRITE,CREATE, andDESCRIBEaccess to the MirrorMaker 2 internal topics
For more information, see Permissions and internal topics.
- On the source cluster:
-
Network access from MirrorMaker 2 to the source cluster and from your applications to the target service, including required firewall rules or private connectivity, with enough bandwidth to sync your data
-
Kafka clients compatible with the Apache Kafka version of the target service
-
A list of the topics, consumer groups, schemas, ACLs, and Kafka Connect connectors to migrate
-
A cutover plan that covers which producers, consumers, and connectors to switch, and how to roll back if needed
Define the source cluster endpoint
Create an external Apache Kafka integration endpoint so that MirrorMaker 2 can connect to
the source cluster.
Follow
Integrate an external Apache Kafka® cluster
and use a descriptive name, such as source-cluster.
Aiven does not test connectivity when you create an external Apache Kafka endpoint. Ensure the firewall of the source cluster allows traffic from MirrorMaker 2.
To allow only specific addresses, set up static IP addresses for the MirrorMaker 2 service and allow them on the source cluster firewall. Static IP addresses incur an additional charge.
If you run MirrorMaker 2 standalone, define the source cluster in the mm2.properties
file instead.
Create the target services
Create an Aiven for Apache Kafka service as the target cluster and a MirrorMaker 2 service to replicate the data.
-
Create an Aiven for Apache Kafka service as the target cluster, for example
target-kafka. -
Apply the custom broker parameters that your applications depend on, such as message size limits or retention defaults. Aiven manages some broker parameters, so first review the available advanced parameters. To apply a parameter:
- In the target service, click Service settings.
- In Advanced configuration, click Configure.
- Click Add configuration options.
- Search for the parameter.
For example, search for
kafka.message_max_bytesto set the maximum message size. - Set the value and click Save configuration.
ImportantYou cannot reset advanced parameters to their defaults or remove them after you add them. Most changes restart the service nodes one at a time, so apply them before you create the replication flow.
For more information, see Manage Apache Kafka parameters.
-
Optional: Enable the Apache Kafka REST API to browse topic data in the Aiven Console. If your applications use a schema registry, enable it as well. For more information, see Enable schema registry and REST proxy.
-
Optional: If your clients use SASL, enable SASL authentication.
-
Optional: Enable automatic topic creation if your applications rely on it.
-
Create a MirrorMaker 2 service and integrate it with the target Aiven for Apache Kafka service. Enter a cluster alias that identifies the target cluster, such as
kafka-target. You cannot change the alias after you create the integration.
Create a replication flow
Add the source cluster to MirrorMaker 2 and create a replication flow to the target cluster.
-
In the MirrorMaker 2 service, click Integrations.
-
Under External integrations, click Cluster for replication.
-
Click the source endpoint, such as
source-cluster, and click Continue. -
Enter a cluster alias, such as
kafka-source, and click Enable. -
Click Replication flows > Create replication flow.
-
Configure the replication flow:
- Source cluster: The source cluster alias, such as
kafka-source. - Target cluster: The target cluster alias, such as
kafka-target. - Topics: Click All topics, or enter Java regular expressions for the topics
to replicate.
For example,
customer\..*matches topics that start withcustomer.. For more patterns, see Include or exclude topics in a replication flow. - Topic blacklist: The topics to exclude.
Click Internal topics, then explicitly exclude
_schemasand any custom schema registry storage topic. Migrate schemas using the import procedure instead of replicating registry storage topics. - Replication policy class: The naming of replicated topics:
org.apache.kafka.connect.mirror.DefaultReplicationPolicyprefixes replicated topics with the source cluster alias. For example,ordersbecomeskafka-source.orders.org.apache.kafka.connect.mirror.IdentityReplicationPolicykeeps the original topic names.
- Sync group offsets: Turn on to synchronize consumer group offsets. Required to resume consumers from their replicated positions.
- Sync interval in seconds: How often MirrorMaker 2 synchronizes offsets.
- Offset syncs topic location: The source or target cluster, depending on your permissions and configuration.
- Exactly-once message delivery enabled: Turn on to use exactly-once delivery. Recommended for new replications.
- Emit heartbeats enabled: Turn on to emit heartbeats to the target cluster.
- Enable: Starts the replication when you create the flow.
- Source cluster: The source cluster alias, such as
-
Click Create.
-
In the target service, click Manage stream > Topics and confirm that the topics appear with the expected names.
After you enable the flow, MirrorMaker 2 checks the source cluster at regular intervals. It replicates new topics that match your patterns. For more options, see Set up an Apache Kafka® MirrorMaker 2 replication flow.
MirrorMaker 2 copies topic configurations from the source topics to the target topics at
regular intervals.
By default, it does not copy min.insync.replicas, and it never copies
unclean.leader.election.enable.
It sets the replication factor of a target topic only when it creates the topic.
To preserve target-specific settings, see
Topic configurations on the target cluster.
Test with a sample topic
Before you migrate production topics, test the setup with a sample topic.
Create a topic on the source cluster, such as pizza-orders, and stream data to it with
the
Aiven fake data producer.
Include the topic in the replication flow and confirm that the data arrives on the
target cluster.
Stop the producer when you finish testing.
Monitor replication
Replication lag is the delay between the source and target clusters. Monitor it to find out when the target has caught up with the source.
-
Create a service integration that sends the MirrorMaker 2 metrics to a monitoring system. For more information, see Monitor replication execution.
-
Track the following metrics:
kafka_mirrormaker_summary.replication_lag: The lag for each replication flow, tagged withreplication-flow. For Datadog, see Configure Apache Kafka® MirrorMaker 2 metrics sent to Datadog.jmx.kafka.connect.mirror.record_count: The number of replicated records. A flat line means that no new records arrive. It does not show that the lag is0.
A replication lag of 0 shows that replication has caught up.
It does not confirm that schemas, ACLs, connectors, or consumer group offsets are in
place.
MirrorMaker 2 does not offer synchronous replication. Consumers can receive duplicate or out-of-order records, so make sure your applications handle both.
Migrate schemas
If your applications use a schema registry, migrate the schemas to the target service before you switch clients. To preserve schema IDs and versions, see Migrate schemas to Karapace. Confirm that the schema subjects, IDs, and versions are available on the target service.
Migrate ACLs
MirrorMaker 2 does not replicate Apache Kafka ACLs. Extract the ACL definitions from the source cluster and recreate them on the target service, together with the application credentials. Confirm that each application has the permissions it needs for its topics. For more information, see Manage access control lists.
Migrate Kafka Connect connectors
MirrorMaker 2 replicates topic data but does not migrate Apache Kafka Connect connector configurations or runtime state. If your source environment uses Kafka Connect:
- Identify the connectors to migrate from the source environment.
- Ensure that your target Aiven for Apache Kafka® Connect service supports the connector plugins. For more information, see Available Apache Kafka® Connect connectors.
- Prepare the target connector configurations, including compatible plugins, credentials, and connections to external systems. Don't create the connectors yet. Creating a connector starts its tasks, which can duplicate writes while the source connectors are still running.
- Plan how to migrate offsets or runtime state from the source connectors, so that they don't process records twice.
- During cutover, stop the source connectors and migrate their offsets or runtime state. Then create the target connectors.
Switch applications to Aiven
Switch your producers and consumers during a planned cutover period.
To let consumer groups resume from their replicated positions, turn on Sync group offsets in the replication flow before the cutover. MirrorMaker 2 translates committed source offsets and writes them to the target. It does this only while the consumer groups have no active consumers on the target.
Offset synchronization depends on the following:
- Sync interval: The default
kafka_mirrormaker.sync_group_offsets_interval_secondsis60seconds. - Checkpoints: MirrorMaker 2 emits offset checkpoints by default.
If you turned them off with
kafka_mirrormaker.emit_checkpoints_enabled, turn them back on. - Offset syncs topic location: For more information, see Offset sync topic location.
To switch the applications:
-
Confirm that the topics, schemas, ACLs, and application credentials are available on the target service.
-
Stop the producers and source connectors that write to the source cluster.
-
Let the source consumers process the remaining records and commit their offsets, then stop them.
-
Confirm that the replication lag is
0and the replication flow has no errors. -
Keep MirrorMaker 2 running and wait for at least one sync interval.
-
Confirm that the migrated consumer groups have committed offsets on the target before you start the target consumers:
kafka-consumer-groups.sh \--bootstrap-server TARGET_BOOTSTRAP_SERVERS \--command-config CLIENT_PROPERTIES_FILE \--describe \--group CONSUMER_GROUPReplace the following:
TARGET_BOOTSTRAP_SERVERS: The bootstrap servers of the target service.CLIENT_PROPERTIES_FILE: The path to a client properties file with the connection settings of the target service.CONSUMER_GROUP: The consumer group to inspect.
Compare the topics, partitions, and committed offsets with the expected positions. Offsets on the target can differ from the source because MirrorMaker 2 translates them between clusters. If a group has no committed offsets on the target, do not start it there. First review its synchronization status in Offset sync status analysis.
-
Update your producers and consumers to use the target service, including the following:
- Bootstrap servers and authentication details
- Topic names, if the replication policy adds a prefix. To keep the original names,
select
org.apache.kafka.connect.mirror.IdentityReplicationPolicybefore you start replication. Do not change the policy during cutover. - Schema registry endpoints, if applicable
For connection details and examples, see Connect to Aiven for Apache Kafka.
-
Start the producers, consumers, and migrated connectors on the target service.
-
Produce and consume test records to confirm that the applications work and the consumers resume at the intended positions.
Test the offset behavior with a non-production consumer group before the cutover.
Verify and complete the migration
After you switch the applications, confirm the following:
- Producers and consumers write and read records on the target cluster.
- Consumer groups resume at the expected positions.
- Migrated connectors and other integrations work as expected.
- Applications no longer depend on the source cluster.
Keep the source cluster available until you finish verification and no longer need it for rollback. Then turn off the replication flow and decommission the source cluster.
Related pages