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

Kafka Metrics: Lag, Throughput, Consumer Group Health

Apache KafkaMonitoring⭐ Premium

Kafka Metrics: Lag, Throughput, Consumer Group Health

Difficulty: Senior | Asked at: Netflix, Uber, LinkedIn, Airbnb

â„šī¸Interview Context

Monitoring is critical for production Kafka operations. Interviewers expect you to know key metrics, alerting thresholds, and how to diagnose common issues using metrics.

The Question

What are the critical Kafka metrics to monitor? How do you calculate consumer lag? What metrics indicate consumer group health issues?

Critical Metrics Overview

Architecture Diagram
Kafka Metrics Categories:
1. Broker Metrics - Cluster health
2. Producer Metrics - Write performance
3. Consumer Metrics - Read performance
4. Consumer Group Metrics - Group health
5. Request Metrics - Protocol performance
6. Log Metrics - Storage health

A useful monitoring setup separates signals by the question they answer rather than by the component they come from. Throughput metrics tell you whether data is moving, latency metrics tell you whether it is moving on time, error metrics tell you whether what moved is valid, and saturation metrics tell you how close you are to the point where everything degrades at once. Kafka publishes these through JMX, so the numbers you alert on are only as fresh and as complete as the exporter scraping them. Choose a small set, perhaps fifteen series across the whole cluster, and resist graphing everything the broker publishes, because a dashboard nobody can read during an incident is worse than having no dashboard. Every alert you configure should map back to one of those questions and to a runbook action somebody can take.

Consumer Lag Metrics

Lag is the difference between the newest offset in a partition and the offset the group last committed, and it is the single number that says whether consumers are keeping up. Absolute lag is easy to alert on but misleading across topics of different sizes: a hundred thousand messages is nothing on a topic holding ten billion and catastrophic on one holding a million. Time-based lag, obtained by dividing lag by the observed consumption rate, is the version worth paging on, because it translates directly into staleness your downstream systems will feel. Read lag per partition rather than per topic as well, since one hot partition hides behind an average while its own consumer falls hours behind.

Lag Calculation

python
from kafka import KafkaConsumer, TopicPartition

def calculate_consumer_lag(consumer, topic):
    """
    Calculate consumer lag for all partitions.
    
    Lag = Latest Offset - Committed Offset
    
    A positive lag means consumer is behind.
    """
    lag_report = {}
    
    partitions = consumer.partitions(topic)
    
    for partition in partitions:
        tp = TopicPartition(topic, partition)
        
        # Get latest offset (end of partition)
        consumer.seek_to_end(tp)
        latest_offset = consumer.position(tp)
        
        # Get committed offset
        committed = consumer.committed(tp)
        
        # Calculate lag
        lag = latest_offset - (committed or 0)
        
        lag_report[partition] = {
            'latest_offset': latest_offset,
            'committed_offset': committed or 0,
            'lag': lag,
            'lag_percentage': (lag / latest_offset * 100) if latest_offset > 0 else 0
        }
    
    return lag_report

# Example usage
consumer = KafkaConsumer(
    'orders',
    group_id='my-group',
    bootstrap_servers='localhost:9092'
)

lag = calculate_consumer_lag(consumer, 'orders')
for partition, info in lag.items():
    print(f"Partition {partition}: lag={info['lag']} ({info['lag_percentage']:.1f}%)")

Lag Metrics

python
# Key lag metrics
lag_metrics = {
    # Absolute lag
    'records-lag-max': 'Maximum lag across all partitions',
    'records-lag': 'Lag per partition',
    
    # Rate-based lag
    'lag-rate': 'Rate of lag increase/decrease',
    'consumption-rate': 'Messages consumed per second',
    'production-rate': 'Messages produced per second',
    
    # Time-based lag
    'estimated-lag-time': 'Estimated time to catch up',
    'lag-time': 'Time since last consumed message'
}

def estimate_lag_time(lag_messages, consumption_rate_per_sec):
    """
    Estimate time to consume lag.
    
    Returns estimated seconds to catch up.
    """
    if consumption_rate_per_sec == 0:
        return float('inf')
    
    return lag_messages / consumption_rate_per_sec

# Example
lag_seconds = estimate_lag_time(
    lag_messages=10000,
    consumption_rate_per_sec=1000
)
print(f"Estimated time to catch up: {lag_seconds:.1f} seconds")

âš ī¸Lag Alert Thresholds

  • Warning: lag > 10,000 messages OR lag > 1% of total messages
  • Critical: lag > 100,000 messages OR lag > 10% of total messages
  • Emergency: lag increasing for > 5 minutes continuously

Throughput Metrics

Throughput is measured at three points, and the gaps between them are where problems hide: what producers send, what brokers accept, and what consumers consume. A cluster where bytes in and bytes out both look healthy but consumption rate has dropped is not suffering a broker issue at all, so compare the rates rather than reading any one of them in isolation. Batch size and fetch size are what separate a configuration problem from a capacity problem, since tiny batches under low latency mean the producer never waited and tiny batches under high latency mean the path is saturated. Also remember these are averages over the scrape interval, so a workload that alternates between bursts and stalls can look perfectly smooth while latency seesaws.

Producer Throughput

python
# Producer throughput metrics
producer_throughput = {
    'record-send-rate': 'Messages sent per second',
    'record-send-total': 'Total messages sent',
    'throughput': 'Bytes sent per second',
    'batch-size-avg': 'Average batch size',
    'batch-size-max': 'Maximum batch size',
    
    # Request metrics
    'request-rate': 'Requests per second',
    'request-latency-avg': 'Average request latency',
    'request-latency-max': 'Maximum request latency',
    
    # Error metrics
    'record-error-rate': 'Error rate',
    'record-error-total': 'Total errors',
    'retry-rate': 'Retry rate'
}

def calculate_producer_throughput(
    messages_sent: int,
    time_window_seconds: int,
    avg_message_size: int
) -> dict:
    """
    Calculate producer throughput metrics.
    """
    messages_per_sec = messages_sent / time_window_seconds
    bytes_per_sec = messages_per_sec * avg_message_size
    mb_per_sec = bytes_per_sec / (1024 * 1024)
    
    return {
        'messages_per_sec': messages_per_sec,
        'bytes_per_sec': bytes_per_sec,
        'mb_per_sec': mb_per_sec,
        'messages_per_min': messages_per_sec * 60
    }

# Example
throughput = calculate_producer_throughput(
    messages_sent=100000,
    time_window_seconds=60,
    avg_message_size=500
)
print(f"Throughput: {throughput['mb_per_sec']:.2f} MB/s")
print(f"Message rate: {throughput['messages_per_sec']:.0f} msgs/sec")

Consumer Throughput

python
# Consumer throughput metrics
consumer_throughput = {
    'records-consumed-rate': 'Messages consumed per second',
    'records-consumed-total': 'Total messages consumed',
    'bytes-consumed-rate': 'Bytes consumed per second',
    
    # Poll metrics
    'poll-rate': 'Poll calls per second',
    'poll-total': 'Total poll calls',
    
    # Processing metrics
    'process-rate': 'Messages processed per second',
    'process-total': 'Total messages processed',
    
    # Fetch metrics
    'fetch-rate': 'Fetch requests per second',
    'fetch-latency-avg': 'Average fetch latency',
    'fetch-size-avg': 'Average fetch size'
}

def calculate_consumer_throughput(
    messages_consumed: int,
    time_window_seconds: int,
    avg_message_size: int
) -> dict:
    """
    Calculate consumer throughput metrics.
    """
    messages_per_sec = messages_consumed / time_window_seconds
    bytes_per_sec = messages_per_sec * avg_message_size
    mb_per_sec = bytes_per_sec / (1024 * 1024)
    
    return {
        'messages_per_sec': messages_per_sec,
        'bytes_per_sec': bytes_per_sec,
        'mb_per_sec': mb_per_sec,
        'messages_per_min': messages_per_sec * 60
    }

# Example
throughput = calculate_consumer_throughput(
    messages_consumed=95000,
    time_window_seconds=60,
    avg_message_size=500
)
print(f"Consumer throughput: {throughput['mb_per_sec']:.2f} MB/s")
print(f"Message rate: {throughput['messages_per_sec']:.0f} msgs/sec")

Consumer Group Health

A healthy group is boring: it sits in stable, holds a fixed number of members, and rebalances only when topology changes. The metrics that break that picture are rebalance rate, rebalance latency, active member count and offset commit latency. A group flapping between stable and preparing rebalance is usually hiding a slow consumer that exceeds max.poll.interval.ms, or a session timeout that is too tight for the workload, and the lag that follows is a symptom rather than the cause. Compare member count against partition count too, because a group with more members than partitions still pays a rebalance on every change while gaining no parallelism, and commit latency that climbs steadily often points at the coordinator broker rather than at the consumers themselves.

Group State Metrics

python
# Consumer group health metrics
group_health = {
    # State
    'group-state': 'Consumer group state (Stable, Rebalancing, Empty)',
    'active-members': 'Number of active consumers',
    'assigned-partitions': 'Partitions assigned per consumer',
    
    # Rebalance metrics
    'rebalance-rate-per-hour': 'Rebalances per hour',
    'rebalance-total': 'Total rebalances',
    'rebalance-latency-avg': 'Average rebalance time',
    
    # Offset metrics
    'offset-commit-rate': 'Offset commits per second',
    'offset-commit-latency': 'Average commit latency',
    'offset-lag': 'Consumer offset lag'
}

def assess_group_health(consumer_group_metrics):
    """
    Assess consumer group health.
    
    Returns health score and recommendations.
    """
    score = 100
    issues = []
    
    # Check rebalance rate
    if consumer_group_metrics.get('rebalance-rate-per-hour', 0) > 1:
        score -= 20
        issues.append("High rebalance rate detected")
    
    # Check active members
    if consumer_group_metrics.get('active-members', 0) == 0:
        score -= 50
        issues.append("No active consumers in group")
    
    # Check lag
    max_lag = consumer_group_metrics.get('records-lag-max', 0)
    if max_lag > 100000:
        score -= 30
        issues.append(f"High consumer lag: {max_lag}")
    
    # Check rebalance latency
    if consumer_group_metrics.get('rebalance-latency-avg', 0) > 30000:
        score -= 15
        issues.append("Slow rebalancing detected")
    
    health_status = 'healthy' if score > 80 else 'degraded' if score > 50 else 'critical'
    
    return {
        'health_score': score,
        'status': health_status,
        'issues': issues,
        'recommendations': generate_recommendations(issues)
    }

def generate_recommendations(issues):
    """Generate recommendations based on issues"""
    recommendations = []
    
    for issue in issues:
        if 'rebalance' in issue.lower():
            recommendations.append("Consider static group membership or cooperative rebalancing")
        elif 'lag' in issue.lower():
            recommendations.append("Add more consumers or optimize processing logic")
        elif 'no active' in issue.lower():
            recommendations.append("Check consumer health and restart if needed")
    
    return recommendations

Rebalance Detection

python
class RebalanceMonitor:
    """
    Monitor and alert on consumer group rebalances.
    """
    
    def __init__(self):
        self.rebalance_counts = {}
        self.last_rebalance_time = {}
    
    def record_rebalance(self, group_id, timestamp):
        """Record a rebalance event"""
        if group_id not in self.rebalance_counts:
            self.rebalance_counts[group_id] = 0
        
        self.rebalance_counts[group_id] += 1
        self.last_rebalance_time[group_id] = timestamp
    
    def get_rebalance_rate(self, group_id, window_seconds=3600):
        """
        Calculate rebalance rate per hour.
        """
        if group_id not in self.last_rebalance_time:
            return 0
        
        current_time = time.time()
        window_start = current_time - window_seconds
        
        # Count rebalances in window
        # In production, use a time-series database
        return self.rebalance_counts.get(group_id, 0)
    
    def check_health(self, group_id):
        """
        Check if rebalance rate is healthy.
        """
        rate = self.get_rebalance_rate(group_id)
        
        if rate > 10:
            return {'status': 'critical', 'rate': rate}
        elif rate > 5:
            return {'status': 'warning', 'rate': rate}
        else:
            return {'status': 'healthy', 'rate': rate}

â„šī¸Rebalance Health

Healthy systems should have < 1 rebalance per hour under normal operation. Frequent rebalances indicate:

  1. Consumers processing too slowly (max.poll.interval.ms exceeded)
  2. Session timeouts too short
  3. Network connectivity issues
  4. Consumer crashes

Broker Metrics

Broker health reduces to three questions: is there exactly one controller, are all partitions fully replicated, and are requests being served quickly. Offline partitions are always critical, because that data is unavailable until a broker returns. Under-replicated partitions are the early warning, since they mean the cluster is one failure away from reduced durability even though reads and writes still succeed. ISR shrink and expand rates tell you whether that condition is transient or steady, and request latency broken down by api name tells you which operation is suffering. Beyond those, watch disk growth against retention expectations and the throughput of the busiest broker, because a cluster fails at its narrowest resource long before the average node looks busy.

Cluster Health Metrics

python
# Broker health metrics
broker_health = {
    # Basic health
    'active-controller': 'Is this broker the controller?',
    'offline-partitions': 'Number of offline partitions',
    'under-replicated-partitions': 'Partitions below replication factor',
    
    # ISR metrics
    'isr-shrink-rate': 'ISR shrink rate per second',
    'isr-expand-rate': 'ISR expand rate per second',
    'isr-size': 'Current ISR size',
    
    # Request metrics
    'request-rate': 'Request rate per second',
    'request-latency-avg': 'Average request latency',
    'request-latency-max': 'Maximum request latency',
    
    # Log metrics
    'log-size': 'Total log size',
    'log-size-rate': 'Log growth rate',
    'log-flush-rate': 'Log flush rate',
    
    # Network metrics
    'network-io-rate': 'Network I/O rate',
    'bytes-in-rate': 'Bytes received per second',
    'bytes-out-rate': 'Bytes sent per second'
}

def assess_broker_health(broker_metrics):
    """
    Assess broker health.
    """
    issues = []
    score = 100
    
    # Check controller status
    if broker_metrics.get('active-controller', 0) == 0:
        # Not a controller, skip controller-specific checks
        pass
    elif broker_metrics.get('active-controller', 0) != 1:
        issues.append("Multiple controllers detected")
        score -= 30
    
    # Check offline partitions
    if broker_metrics.get('offline-partitions', 0) > 0:
        issues.append(f"Offline partitions: {broker_metrics['offline-partitions']}")
        score -= 40
    
    # Check under-replicated partitions
    if broker_metrics.get('under-replicated-partitions', 0) > 0:
        issues.append(f"Under-replicated partitions: {broker_metrics['under-replicated-partitions']}")
        score -= 20
    
    # Check ISR shrink rate
    if broker_metrics.get('isr-shrink-rate', 0) > 0.1:
        issues.append("High ISR shrink rate")
        score -= 15
    
    # Check request latency
    if broker_metrics.get('request-latency-avg', 0) > 100:
        issues.append("High request latency")
        score -= 10
    
    health_status = 'healthy' if score > 80 else 'degraded' if score > 50 else 'critical'
    
    return {
        'health_score': score,
        'status': health_status,
        'issues': issues
    }

JMX Metrics Collection

python
import jmx_fetch

class KafkaMetricsCollector:
    """
    Collect Kafka metrics via JMX.
    """
    
    def __init__(self, jmx_url):
        self.jmx_url = jmx_url
    
    def collect_broker_metrics(self):
        """Collect broker metrics via JMX"""
        metrics = {
            'kafka.server:type=BrokerTopicMetrics': [
                'MessagesInPerSec',
                'BytesInPerSec',
                'BytesOutPerSec',
                'FailedProduceRequestsPerSec',
                'FailedFetchRequestsPerSec'
            ],
            'kafka.server:type=ReplicaManager': [
                'UnderReplicatedPartitions',
                'IsrShrinksPerSec',
                'IsrExpandsPerSec',
                'PartitionCount',
                'LeaderCount'
            ],
            'kafka.controller:type=KafkaController': [
                'ActiveControllerCount',
                'OfflinePartitionsCount',
                'PreferredReplicaImbalancesPerSec'
            ],
            'kafka.network:type=RequestMetrics': [
                'RequestsPerSec',
                'TotalTimeMs',
                'RequestQueueSize'
            ]
        }
        
        return self._collect_metrics(metrics)
    
    def collect_consumer_metrics(self):
        """Collect consumer metrics via JMX"""
        metrics = {
            'kafka.consumer:type=consumer-fetch-manager-metrics': [
                'records-lag-max',
                'records-consumed-rate',
                'bytes-consumed-rate',
                'fetch-rate',
                'fetch-latency-avg'
            ],
            'kafka.consumer:type=consumer-coordinator-metrics': [
                'rebalance-rate-per-hour',
                'last-rebalance-seconds-ago'
            ]
        }
        
        return self._collect_metrics(metrics)

Alerting Thresholds

Thresholds are where monitoring either earns trust or loses it. Static absolute numbers are a starting point, but every alert should carry a rate of change or a duration as well, so lag crossing ten thousand and holding for five minutes pages while a momentary spike does not. Alert on absence as well: a broker that stops publishing metrics, a group reporting no active members, or a scrape target that disappears are failures that produce silence rather than noise, and silence is exactly what a naive dashboard mistakes for health. Keep severity honest by reserving critical for conditions that deserve someone being woken, and make sure every paged condition has a documented first command to run, because an alert nobody knows how to act on is only a notification.

Alert Configuration

python
# Alert thresholds
alert_thresholds = {
    # Consumer lag
    'consumer-lag-warning': {
        'metric': 'records-lag-max',
        'threshold': 10000,
        'window': '5m',
        'severity': 'warning'
    },
    'consumer-lag-critical': {
        'metric': 'records-lag-max',
        'threshold': 100000,
        'window': '5m',
        'severity': 'critical'
    },
    
    # Throughput drop
    'throughput-drop': {
        'metric': 'records-consumed-rate',
        'condition': 'decrease > 50%',
        'window': '10m',
        'severity': 'warning'
    },
    
    # Error rate
    'error-rate': {
        'metric': 'record-error-rate',
        'threshold': 0.01,  # 1%
        'window': '5m',
        'severity': 'warning'
    },
    
    # Rebalance rate
    'rebalance-rate': {
        'metric': 'rebalance-rate-per-hour',
        'threshold': 5,
        'window': '1h',
        'severity': 'warning'
    },
    
    # Broker offline
    'broker-offline': {
        'metric': 'offline-partitions',
        'threshold': 0,
        'window': '1m',
        'severity': 'critical'
    },
    
    # ISR shrink
    'isr-shrink': {
        'metric': 'isr-shrink-rate',
        'threshold': 0.1,
        'window': '5m',
        'severity': 'warning'
    }
}

Dashboard Metrics

python
# Grafana dashboard metrics
dashboard_metrics = {
    'Overview': [
        'cluster-healthy',
        'active-controller',
        'offline-partitions',
        'under-replicated-partitions'
    ],
    'Throughput': [
        'messages-in-rate',
        'bytes-in-rate',
        'bytes-out-rate',
        'request-rate'
    ],
    'Consumer Health': [
        'records-lag-max',
        'consumption-rate',
        'rebalance-rate',
        'active-members'
    ],
    'Latency': [
        'request-latency-avg',
        'request-latency-p99',
        'fetch-latency-avg',
        'commit-latency-avg'
    ],
    'Storage': [
        'log-size',
        'log-size-rate',
        'log-flush-rate'
    ]
}

â„šī¸Monitoring Stack

Recommended monitoring stack:

  1. Metrics Collection: JMX Exporter or Kafka Exporter
  2. Time Series DB: Prometheus or InfluxDB
  3. Visualization: Grafana
  4. Alerting: Alertmanager or PagerDuty
  5. Logging: ELK Stack or Loki

Lag Monitoring Formula

python
def comprehensive_lag_analysis(consumer, topic):
    """
    Comprehensive consumer lag analysis.
    """
    analysis = {
        'total_lag': 0,
        'total_messages': 0,
        'partitions': {},
        'estimated_catch_up_time': 0,
        'health_score': 100
    }
    
    for partition in consumer.partitions(topic):
        tp = TopicPartition(topic, partition)
        
        consumer.seek_to_end(tp)
        latest = consumer.position(tp)
        committed = consumer.committed(tp) or 0
        
        lag = latest - committed
        analysis['total_lag'] += lag
        analysis['total_messages'] += latest
        
        analysis['partitions'][partition] = {
            'latest': latest,
            'committed': committed,
            'lag': lag,
            'lag_percentage': (lag / latest * 100) if latest > 0 else 0
        }
    
    # Calculate overall metrics
    if analysis['total_messages'] > 0:
        analysis['lag_percentage'] = (
            analysis['total_lag'] / analysis['total_messages'] * 100
        )
    
    # Estimate catch-up time
    # Assume 1000 msgs/sec consumption rate
    consumption_rate = 1000
    analysis['estimated_catch_up_time'] = analysis['total_lag'] / consumption_rate
    
    # Calculate health score
    if analysis['lag_percentage'] > 10:
        analysis['health_score'] = 0
    elif analysis['lag_percentage'] > 1:
        analysis['health_score'] = 50
    else:
        analysis['health_score'] = 100
    
    return analysis

âš ī¸Key Insight

Consumer lag is the most important metric. It directly indicates if your consumers are keeping up with production. Set up alerts for both absolute lag (10k messages) and relative lag (1% of total).

Diagnosing common failures from the numbers

Start from the shape of the failure rather than from the metric list. If producers are slow while brokers look quiet, compare batch size against request latency: small batches with high latency point at the network path or a saturated broker, whereas small batches with low latency mean the producer is simply not waiting long enough to fill them. If consumers lag while fetch latency stays low, the bottleneck is processing, so look at poll rate and time spent inside your application instead of at the cluster. If lag rises alongside ISR shrinks, the cluster is the problem and consumers are merely unable to fetch from replicas that keep leaving. If exactly one partition lags, inspect key skew before changing consumer count, because additional consumers will never fix a single hot partition. If rebalance rate climbs without any deployment, check heartbeat and session settings and whether one member repeatedly exceeds its poll interval. Every one of these chains uses numbers you already collect; the skill is reading them in combination rather than in isolation.

Common Mistakes

Paging on a fixed absolute lag number for every topic regardless of its size

Fix: Alert on time-to-catch-up by dividing lag by the observed consumption rate, with absolute lag kept as a secondary signal

Alerting only on thresholds and never on missing metrics

Fix: Add absence alerts for stale scrapes, zero active members and offline partitions, since those failures show up as silence

Adding consumers whenever a single partition falls behind

Fix: Check producer key skew and per-partition lag first, because extra consumers cannot rebalance one hot partition

Key Takeaways

  • Read metrics in combination: throughput, latency, errors and saturation together identify which side of the system is failing
  • Lag measured in time-to-catch-up is more meaningful than raw record counts across topics of different sizes
  • Group health is stable state, steady member count and near-zero rebalance rate; flapping points at poll or session configuration
  • Offline partitions are critical, under-replicated partitions are the early warning, and ISR rates say whether either is transient
🔒

Premium Content

Kafka Metrics: Lag, Throughput, Consumer Group Health

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