🎉 75% of content is free forever — Unlock Premium from $10/mo →
CW
NEWSLIVESearch All Content
đŸ’ŧ Servicesâ„šī¸ Aboutâœ‰ī¸ ContactView Pricing Plansfrom $10

Multi-Datacenter Kafka: MirrorMaker 2, Replication

Apache KafkaMulti-Datacenter⭐ Premium

Multi-Datacenter Kafka: MirrorMaker 2, Replication

Difficulty: Staff | Asked at: Netflix, Uber, LinkedIn, AWS

â„šī¸Interview Context

Multi-datacenter Kafka is a complex topic. Interviewers expect you to understand replication strategies, failover mechanisms, and the trade-offs between consistency and availability.

The Question

How do you replicate Kafka topics across datacenters? Explain MirrorMaker 2 architecture and how it handles failover. What are the trade-offs between active-active and active-passive setups?

Multi-DC Architecture Patterns

Choosing a topology starts from the recovery objectives you are contractually bound to, not from a preference for a particular tool. Recovery point objective says how much data you can afford to lose, and it therefore sets the replication mode: asynchronous replication leaves seconds to minutes of exposure because the remote copy trails, while a synchronous write across both sites removes that exposure and adds a full round trip to every produce request. Recovery time objective says how quickly traffic must move, and it therefore decides how much of the stack is pre-deployed and warm on the standby. Active-passive keeps cost low and the runbook short because only one site accepts writes, at the price of a failover window measured in minutes. Active-active keeps both sites hot and shrinks that window to seconds, but it hands the conflict problem to your application, because two sites can now accept writes for the same key before replication reconciles them.

Active-Passive (Cold Standby)

Active-Passive ArchitecturePrimary DCBroker 1 │ │ Broker 2 │ │ Broker 3↑ ↑ ↑MirrorMaker 2ReplicationDR DCBroker 4 │ │ Broker 5 │ │ Broker 6Passive: Ready for failover

Active-Active

Active-Active ArchitectureActive-Active ArchitDC 1┌─────────────┐ ┌──Broker 1Broker 2Broker 3└─────────────┘ └──↑ ↑ └────────────────â”ŧ──┌───────────┴───────MirrorMaker 2←──────┐└───────────â”Ŧ───────BidirectionalReplicationDC 2┌─────────────┐ ┌──Broker 4Broker 5Broker 6└─────────────┘ └──↑ ↑ └────────────────â”ŧ──┌───────────┴───────MirrorMaker 2───────→└───────────────────Both DCs active, bid

Pattern Comparison

PatternRPORTOComplexityUse Case
Active-PassiveMinutesMinutes-HoursLowDR, compliance
Active-ActiveSecondsSecondsHighGlobal availability
Multi-ActiveSub-secondSecondsVery HighUltra-low latency

â„šī¸Pattern Selection

  • Active-Passive: When you need DR for compliance but don't need immediate failover
  • Active-Active: When you need global availability and can handle conflict resolution
  • Multi-Active: When you need sub-second failover and have the infrastructure budget

MirrorMaker 2 Architecture

MirrorMaker 2 is a Kafka Connect cluster whose source connectors read from one cluster and whose sink connectors write to another, and that heritage explains most of its behaviour. It replicates the topics you select by pattern, preserves keys and partitioning within each topic, and can mirror consumer group offsets so a group resuming on the target cluster continues where it left off instead of restarting from the earliest offset. Checkpoint and heartbeat topics record what has been transferred, and replication lag is measured against them. Topic naming is decided by the replication policy: the default prefixes remote topics with the source cluster alias, which avoids collisions in active-active but forces every consumer to know the scheme, while an identity policy keeps names identical and is safe only when replication never runs in both directions. Exclusions matter as much as inclusions, because mirroring internal topics back and forth creates replication loops.

How MM2 Works

MirrorMaker 2 ArchitectureSource ClusterTopics: orders, users, eventsConsumerMirrorMaker 2Source Connector- Reads from source topics- Preserves partitioning- Handles schema translationSink Connector- Writes to target topics- Remaps topic names- Translates consumer groupsProducerTarget ClusterTopics: DC1.orders, DC1.users, DC1.events

MM2 Configuration

# mm2.properties
clusters = source, target
source.bootstrap.servers = kafka-source:9092
target.bootstrap.servers = kafka-target:9092

# Replication configuration
source->target.enabled = true
target->source.enabled = false  # One-way replication

# Topic naming
replication.policy.class = org.apache.kafka.connect.mirror.IdentityReplicationPolicy
# Or custom: com.example.CustomReplicationPolicy

# Topic pattern to replicate
topics = orders.*, users.*, events.*

# Exclude topics
topics.exclude = .*\\.internal, __consumer_offsets

# Sync group offsets
sync.group.offsets.enabled = true
sync.group.offsets.interval.seconds = 60

# Consumer group translation
groups = my-consumer-group, another-group

# Checkpointing
checkpoints.topic = mm2-checkpoints
heartbeats.topic = mm2-heartbeats

MM2 Topic Naming

python
# Default topic naming with IdentityReplicationPolicy
# Source topic: orders
# Target topic: orders (same name)

# With DefaultReplicationPolicy
# Source topic: orders
# Target topic: source.cluster.orders

class CustomReplicationPolicy:
    """
    Custom topic naming for cross-DC replication.
    """
    
    def __init__(self, prefix='dc2'):
        self.prefix = prefix
    
    def sourceTopic(self, target_topic):
        """Map target topic back to source"""
        if target_topic.startswith(self.prefix + '.'):
            return target_topic[len(self.prefix) + 1:]
        return None
    
    def targetTopic(self, source_topic):
        """Map source topic to target"""
        return f"{self.prefix}.{source_topic}"

# Usage
policy = CustomReplicationPolicy(prefix='dc2')
print(policy.targetTopic('orders'))  # dc2.orders
print(policy.sourceTopic('dc2.orders'))  # orders

MM2 Checkpointing

python
class CheckpointManager:
    """
    Manage MM2 checkpoints for offset tracking.
    """
    
    def __init__(self):
        self.checkpoints = {}
    
    def record_checkpoint(self, source_cluster, topic, partition, offset):
        """
        Record checkpoint for replicated offset.
        
        Checkpoints are stored in mm2-checkpoints topic.
        Used for exactly-once replication.
        """
        key = f"{source_cluster}.{topic}.{partition}"
        
        self.checkpoints[key] = {
            'source_cluster': source_cluster,
            'topic': topic,
            'partition': partition,
            'offset': offset,
            'timestamp': int(time.time() * 1000)
        }
    
    def get_checkpoint(self, source_cluster, topic, partition):
        """Get last checkpoint for partition"""
        key = f"{source_cluster}.{topic}.{partition}"
        return self.checkpoints.get(key)
    
    def calculate_lag(self, source_cluster, topic, partition, current_offset):
        """
        Calculate replication lag.
        
        Lag = Current Source Offset - Checkpointed Offset
        """
        checkpoint = self.get_checkpoint(source_cluster, topic, partition)
        
        if checkpoint is None:
            return current_offset  # Full replication needed
        
        return current_offset - checkpoint['offset']

âš ī¸MM2 Limitation

MM2 uses at-least-once delivery. Messages may be duplicated during failover. For exactly-once replication, you need custom solutions or Confluent Replicator.

Failover Strategies

Failover is a sequence rather than a switch, and each step has a cost you can estimate in advance: detect the failure, confirm it is not a transient blip, decide to move, stop or drain producers to the old primary, wait for replication to reach acceptable lag, redirect consumers, update DNS or the load balancer, and then verify the new primary is actually serving traffic. Sum those parts and you have your recovery time, which is what the calculation below estimates. Two decisions deserve rehearsal rather than automation: how many consecutive failed health checks constitute a real outage, because too few triggers a failover during a network partition that resolves itself, and when to roll back, because a half-completed failover with diverging writes is worse than the original incident. Keep a manual override in every automated controller for exactly that reason.

Failover Decision Matrix

python
def evaluate_failover_conditions(dc1_metrics, dc2_metrics):
    """
    Evaluate conditions for failover.
    
    Failover triggers:
    1. DC completely unreachable
    2. Broker cluster down
    3. Replication lag too high
    4. Manual failover for maintenance
    """
    failover_needed = False
    reasons = []
    
    # Check DC reachability
    if not dc1_metrics.get('reachable', True):
        failover_needed = True
        reasons.append("DC1 unreachable")
    
    # Check broker health
    if dc1_metrics.get('active_brokers', 0) == 0:
        failover_needed = True
        reasons.append("No active brokers in DC1")
    
    # Check replication lag
    if dc1_metrics.get('replication_lag_seconds', 0) > 300:
        failover_needed = True
        reasons.append(f"Replication lag: {dc1_metrics['replication_lag_seconds']}s")
    
    # Check partition availability
    if dc1_metrics.get('offline_partitions', 0) > 0:
        failover_needed = True
        reasons.append(f"Offline partitions: {dc1_metrics['offline_partitions']}")
    
    return {
        'failover_needed': failover_needed,
        'reasons': reasons,
        'recommended_action': 'FAILOVER' if failover_needed else 'MONITOR'
    }

Automated Failover

python
class FailoverController:
    """
    Automated failover controller for multi-DC Kafka.
    """
    
    def __init__(self, primary_dc, dr_dc):
        self.primary = primary_dc
        self.dr = dr_dc
        self.failover_in_progress = False
    
    def monitor_and_failover(self):
        """
        Monitor primary DC and failover if needed.
        """
        while True:
            # Check primary DC health
            health = self.check_dc_health(self.primary)
            
            if not health['healthy']:
                log.warning(f"Primary DC unhealthy: {health['issues']}")
                
                # Wait for confirmation period
                time.sleep(30)
                
                # Re-check
                health = self.check_dc_health(self.primary)
                
                if not health['healthy']:
                    self.execute_failover()
            
            time.sleep(10)
    
    def execute_failover(self):
        """
        Execute failover to DR DC.
        
        Steps:
        1. Stop producers to primary
        2. Wait for replication to catch up
        3. Redirect consumers to DR
        4. Update DNS/load balancers
        5. Verify DR is serving traffic
        """
        if self.failover_in_progress:
            return
        
        self.failover_in_progress = True
        
        try:
            # Step 1: Stop producers
            self.stop_producers(self.primary)
            
            # Step 2: Wait for replication
            self.wait_for_replication_catchup()
            
            # Step 3: Redirect consumers
            self.redirect_consumers(self.dr)
            
            # Step 4: Update DNS
            self.update_dns(self.dr)
            
            # Step 5: Verify
            if self.verify_dr_healthy():
                log.info("Failover completed successfully")
            else:
                log.error("Failover verification failed")
                
        except Exception as e:
            log.error(f"Failover failed: {e}")
            # Attempt rollback
            self.rollback_failover()
        
        finally:
            self.failover_in_progress = False
    
    def check_dc_health(self, dc):
        """Check datacenter health"""
        metrics = {
            'reachable': self.ping_dc(dc),
            'active_brokers': self.count_active_brokers(dc),
            'offline_partitions': self.get_offline_partitions(dc),
            'replication_lag': self.get_replication_lag(dc)
        }
        
        healthy = (
            metrics['reachable'] and
            metrics['active_brokers'] > 0 and
            metrics['offline_partitions'] == 0 and
            metrics['replication_lag'] < 60
        )
        
        return {'healthy': healthy, 'metrics': metrics}

Failover Timing

python
def estimate_failover_time(
    health_check_interval_sec: int,
    failure_threshold: int,
    decision_delay_sec: int,
    dns_propagation_sec: int
) -> dict:
    """
    Estimate total failover time.
    """
    detection_time = health_check_interval_sec * failure_threshold
    decision_time = decision_delay_sec
    execution_time = dns_propagation_sec
    
    total_time = detection_time + decision_time + execution_time
    
    return {
        'detection_time_sec': detection_time,
        'decision_time_sec': decision_time,
        'execution_time_sec': execution_time,
        'total_failover_time_sec': total_time,
        'total_failover_time_min': total_time / 60
    }

# Example
failover_estimate = estimate_failover_time(
    health_check_interval_sec=10,
    failure_threshold=3,
    decision_delay_sec=30,
    dns_propagation_sec=60
)
print(f"Total failover time: {failover_estimate['total_failover_time_min']:.1f} minutes")
# Total failover time: 2.0 minutes

â„šī¸Failover Best Practice

Test failover regularly. Document the process. Use runbooks for manual failover. Automate where possible but keep manual override capability.

Latency Considerations

Cross-site replication latency is round trip time plus transfer time plus queueing, and only the first term is fixed by geography. A 50 millisecond path with a 16 kilobyte batch on a gigabit link is dominated by the round trip, which is why lowering latency there means batching less aggressively rather than buying more bandwidth, while a congested link makes transfer time dominate and the opposite advice applies. Measure the two components separately: if replication lag grows while round trip time stays flat, the bottleneck is throughput or consumer performance on the receiving side rather than the network. Latency also bounds your recovery point, because the lag between clusters is exactly how much data you lose if the primary disappears at this instant, so quoting an objective without a measured lag figure is guesswork.

Replication Latency

python
def calculate_replication_latency(
    network_latency_ms: float,
    batch_size_bytes: int,
    throughput_mbps: float
) -> dict:
    """
    Calculate replication latency components.
    """
    # Network transfer time
    transfer_time_ms = (batch_size_bytes * 8) / (throughput_mbps * 1000000) * 1000
    
    # Total replication latency
    total_latency_ms = network_latency_ms + transfer_time_ms
    
    return {
        'network_latency_ms': network_latency_ms,
        'transfer_time_ms': transfer_time_ms,
        'total_latency_ms': total_latency_ms,
        'rpo_seconds': total_latency_ms / 1000
    }

# Example
latency = calculate_replication_latency(
    network_latency_ms=50,  # 50ms cross-DC
    batch_size_bytes=16384,  # 16KB batch
    throughput_mbps=1000  # 1 Gbps
)
print(f"Replication latency: {latency['total_latency_ms']:.1f} ms")
print(f"RPO: {latency['rpo_seconds']:.2f} seconds")

Latency vs Throughput Trade-off

python
def optimize_batch_for_latency(
    target_latency_ms: float,
    network_rtt_ms: float,
    throughput_mbps: float
) -> int:
    """
    Calculate optimal batch size for target latency.
    """
    # Available time for batch transfer
    available_time_ms = target_latency_ms - network_rtt_ms
    
    if available_time_ms <= 0:
        return 0  # Cannot achieve target latency
    
    # Calculate batch size
    batch_size_bytes = throughput_mbps * 1000000 * available_time_ms / 8 / 1000
    
    return int(batch_size_bytes)

# Example
optimal_batch = optimize_batch_for_latency(
    target_latency_ms=100,
    network_rtt_ms=50,
    throughput_mbps=1000
)
print(f"Optimal batch size: {optimal_batch} bytes")

âš ī¸Latency Warning

Cross-DC replication always adds latency. For active-active setups, ensure your application can handle eventual consistency. Consider conflict resolution strategies for simultaneous writes to the same key.

Conflict Resolution

python
class ConflictResolver:
    """
    Resolve conflicts in active-active replication.
    """
    
    def __init__(self, strategy='last-write-wins'):
        self.strategy = strategy
    
    def resolve(self, key, dc1_value, dc2_value, dc1_timestamp, dc2_timestamp):
        """
        Resolve conflict between two DCs.
        
        Strategies:
        - last-write-wins: Most recent timestamp wins
        - source-priority: Primary DC always wins
        - merge: Custom merge logic
        - custom: Application-specific resolution
        """
        if self.strategy == 'last-write-wins':
            return dc1_value if dc1_timestamp > dc2_timestamp else dc2_value
        
        elif self.strategy == 'source-priority':
            return dc1_value  # Primary DC always wins
        
        elif self.strategy == 'merge':
            return self.merge_values(dc1_value, dc2_value)
        
        elif self.strategy == 'custom':
            return self.custom_resolve(key, dc1_value, dc2_value)
        
        else:
            raise ValueError(f"Unknown strategy: {self.strategy}")
    
    def merge_values(self, dc1_value, dc2_value):
        """Merge two values (application-specific)"""
        # Example: merge user profiles
        merged = dc1_value.copy()
        
        for field, value in dc2_value.items():
            if field not in merged:
                merged[field] = value
            elif merged[field] != value:
                # Conflict - use merge logic
                merged[field] = self.merge_field(merged[field], value)
        
        return merged

â„šī¸Key Insight

Multi-datacenter Kafka is about trade-offs:

  • Consistency vs Availability: Active-active provides availability but eventual consistency
  • Latency vs Durability: Synchronous replication is durable but slow
  • Complexity vs Features: More complex setups provide better failover

Selecting and rehearsing a topology

Start with data classification rather than with architecture diagrams. Split the topics you carry into those that must not lose a single record, those where a few seconds of loss is tolerable, and those that can be rebuilt from a source system, then replicate only the first two and exclude the rest to conserve bandwidth. Choose conflict resolution per topic as well: last-write-wins is acceptable for session state and dangerous for balances, and a topic where simultaneous writes from two sites cannot be merged has no business being active-active at all. Finally, rehearse. Schedule a failover into the maintenance calendar, run it against realistic load, measure the actual recovery time against the objective you promised the business, and write down what broke. Teams that have never failed over discover during an incident that their DNS time to live is an hour, their runbook assumes one engineer in one timezone, and their standby cluster has never taken a produce request.

Common Mistakes

Quoting an RPO without measuring actual replication lag under load

Fix: Derive the recovery point from lag measured between clusters during peak traffic, not from the nominal network latency

Replicating every topic including internal and derived ones

Fix: Classify topics first and replicate only those that cannot be rebuilt, excluding internal topics to avoid loops and wasted bandwidth

Fully automating failover with no manual override

Fix: Require consecutive failed health checks and keep a tested manual kill switch, because a half-completed failover is worse than the outage

Key Takeaways

  • Recovery point objective sets replication mode while recovery time objective sets how warm the standby must be, and the two pull in opposite directions
  • MirrorMaker 2 is Kafka Connect under the hood: source and sink connectors, checkpoint and heartbeat topics, and at-least-once delivery
  • Topic naming through the replication policy decides whether active-active is safe, and consumer group mirroring is what makes resume points portable
  • Failover time is detection plus decision plus execution, so every term should be measured and rehearsed rather than assumed
🔒

Premium Content

Multi-Datacenter Kafka: MirrorMaker 2, Replication

You've previewed the first section. Unlock this full lesson and 900+ advanced tutorials with a Premium plan.

đŸŽ¯End-to-end Projects
đŸ’ŧInterview Prep
📜Certificates
🤝Community Access

Already a member? Log in

CW

ChatWhole Learn Editorial Team

Written by experienced data scientists, software engineers, and educators. All content is reviewed by industry professionals to ensure accuracy and relevance.

Last reviewed: 2026-10-17

—
☆☆☆☆☆
0 ratings

Rate & Feedback

Advertisement