Course contentsShow
← System Design
Lesson 1193 of 1,91930. Observability: MetricsFree lesson

Aggregation Functions

Using sum, avg, max, min, and quantile aggregations appropriately for different metric types.

Aggregation Functions

You'll learn how to choose the right aggregation function—sum, avg, max, min, or quantile—based on your metric type and what you're actually measuring.

When you collect metrics, raw data points are just the beginning. Aggregation functions transform these individual measurements into meaningful insights. But choosing the wrong function can mislead you completely.

Matching Functions to Metric Types

Counters want sum() or rate(). If you're counting HTTP requests across five servers, you add them up. Using avg() on a counter makes no sense—the "average number of requests" per server tells you nothing about total system load.

Gauges are flexible. CPU usage across replicas? Use avg() to understand typical load, max() to find hotspots, or min() to detect idle instances. Temperature, memory, queue depth—all benefit from different aggregations depending on your question.

Histograms and Summaries demand quantile() for latency analysis. The p99 latency (99th percentile) shows you tail behavior that avg() completely hides. One slow request in 100 won't budge the average, but it is the p99.

Common Pitfalls

Using avg() for latency is the classic mistake—you'll miss critical outliers. A system with 99% of requests at 10ms and 1% at 5 seconds shows an "average" of ~60ms, which describes nobody's actual experience.

Using max() on counters that reset (like process restarts) gives you useless data. Always apply rate() first, then aggregate.

The Analogy

Think of a restaurant chain: sum() tells you total revenue, avg() shows typical per-location sales, max() finds your star performer, and quantile() reveals whether 90% of customers wait under 10 minutes—even if a few wait an hour.

Key Takeaway: Match your aggregation function to both your metric type and your diagnostic question—counters sum, gauges vary by need, and latencies demand quantiles to catch tail behavior.