Skip to content
CodeBrewerz logoCodeBrewerz
System Design

Observability: Cardinality Is the Bill, and Sampling Decides What You Can Debug

Metrics, logs and traces answer three different questions and cost money in three different ways. The label somebody adds without thinking is what turns a monitoring system into an outage of its own.

Published 8 min readBy Piyush Jain
  • System Design
  • Observability
  • Monitoring
  • Distributed Systems

Every system in this series has a failure mode that only appears at scale, and this is the one that decides whether you can see any of the others.

Observability is usually introduced as three pillars, which makes them sound like three views of the same thing. They are not. They answer different questions, they have different cost curves, and the mistake that dominates every real deployment is using one for a job that belongs to another.

Metrics answer “is it happening, and how much” over time — cheap per data point, aggregated, and priced by the number of distinct series. Logs answer “what exactly happened in this one case” — expensive per byte, high detail, priced by volume and by indexing. Traces answer “where did the time go across services” — a causally linked set of spans, priced by how many you keep.

The pillar you reach for first is almost always metrics, and that is where the money goes missing.

The label that ends the budget

Figure 1

One metric, and the labels somebody wanted on it

http_request_duration_seconds—
Active series—
Memory, one server—
Samples per day at 15 s—

—

Every label multiplies with every other label, so the series count is a product and not a sum. The first four labels are ordinary and produce a number a time-series database is happy with. Adding one unbounded label — a user id, a URL with an id in it, a request id — multiplies by its cardinality and takes the metric from thousands of series to millions, which is the single most common way a monitoring bill and a monitoring outage are created.

A metric with four labels — service, method, status, route — produces a number a time-series database does not notice. Now turn on user_id.

The number becomes millions, because labels multiply. Everyone’s intuition here is additive: adding a label with 250,000 values feels like adding 250,000 things. It is not; it multiplies every existing combination by 250,000, and a metric that was 144,000 series becomes 36 billion.

That is not a bill. It is an outage. The metrics server runs out of memory and dies, and it does so during whatever incident caused the traffic pattern that pushed it over — so you lose your monitoring at precisely the moment you need it. This is the single most common self-inflicted observability failure, and it always arrives as a well-meaning pull request.

The rule that follows: a label must have a small, bounded, known set of values. User ids, request ids, email addresses, full URLs, error messages, and anything containing a timestamp are not labels. They are log fields, where high cardinality is free because nothing is being aggregated across it. If you want to slice by user, that is a query over logs or traces, not a dimension on a counter.

Two practical protections. Template your routes before they become labels — /orders/{id}, never /orders/98213 — because a raw path label is an unbounded label with a friendly name. And put a cardinality limit in the collector so a bad deploy is a dropped metric rather than a dead server.

Sampling decides what you can debug

Traces are where you find why a request was slow across a dozen services, and you cannot afford to keep them all. So you sample, and the way you sample determines what you are able to investigate later.

Figure 2

A million requests, four hundred of them broken

1,000,000 requests · 0.04% fail—
failed requests, and which of them you can still inspect
traces stored, all outcomes
Errors captured—
Traces stored—
Buffering required—

—

Strategy
Sample rate for successes
Head sampling decides before the request runs, so it keeps the same fraction of failures as of everything else — at 1% you retain four of the four hundred traces you actually needed. Tail sampling decides after, when the outcome is known, so it can keep every error and a thin slice of successes: far fewer traces stored and far more of the ones worth reading. The cost is that every span must be buffered until the request completes.

Head sampling makes the decision at the first span, before the request has done anything. It is cheap and stateless, and it is blind to outcome by construction — at 1% you keep 1% of your failures, which for four hundred failures in a million requests means four traces. Lowering the rate to control cost lowers your chance of having the one trace an incident needs, and you discover that during the incident.

Tail sampling waits until the request finishes and then decides with the outcome in hand: keep every error, every request over a latency threshold, and a thin slice of successes for comparison. Switch the figure and watch both numbers improve at once — every failure retained, and a fraction of the storage.

The cost is architectural rather than financial. Every span must be buffered until the trace completes, which means a stateful collector holding partial traces in memory, with all the capacity planning that implies. It is worth it, and it is not free, and the reason head sampling persists is that it requires nothing.

Whatever you sample, propagate context everywhere. A trace that stops at a service boundary because someone did not forward the headers is worse than no trace, because it points confidently at the wrong place. And put the trace id in your log lines: the join between “this request was slow” and “here is what it printed” is the single highest-value thing in an observability stack, and it costs one field.

What to do with the volume

The remaining problem is that all three pillars grow with traffic and your budget does not.

Metrics are compressed by time and by dimension. Raw resolution is only useful for a short window — a few weeks at 15 seconds, then downsample to a minute, then to an hour, and keep the long-range series for capacity planning. Delete the dimensions before the data: an hourly series with three labels is worth keeping for a year, a fifteen-second series with eight labels is not.

Logs are compressed by deciding what is worth indexing. Storing everything and indexing nothing is cheap and slow to search; indexing everything is fast and ruinous. The usual shape is a small set of indexed fields — service, level, trace id, request id — with the body searchable but not indexed. Structured logs are what make this possible, which is the real argument for them: not tidiness, but that a JSON field can be indexed selectively and a sentence cannot.

Traces are covered above. The one addition is that trace retention can be much shorter than metric retention, because a trace is for debugging something that is happening now, while a metric is for noticing that something changed over a quarter.

And the tier nobody plans: alerting must not depend on the expensive path. If your pager fires from a query over indexed logs, then a logging outage is a silent outage. Alert on metrics, which are cheap, aggregated and independent, and use logs and traces to investigate after the page.

The parts nobody mentions

Alert on symptoms, not causes. Page on “checkout error rate is above 2%”, not on “CPU is high”. CPU being high is frequently fine, and the alert that fires on a cause you guessed in advance will miss every cause you did not. The four signals worth alerting on — latency, traffic, errors, saturation — are properties of the service, not of the machine.

Every alert needs a runbook and an owner. An alert that fires at 3 a.m. and tells nobody what to do is a notification that trains people to ignore notifications. If you cannot write the first three steps, the alert is not ready.

Dashboards rot faster than code. A dashboard built during an incident encodes the understanding of that day, and nothing updates it when the architecture changes. Delete the ones nobody opened this quarter.

The observability stack is production. It needs its own capacity planning, its own on-call and its own monitoring, and it must not share failure domains with the thing it watches — a metrics system in the same cluster as your application tells you nothing about the cluster outage.

Instrument the business, not just the process. Request rate does not tell you that orders stopped completing while returning 200s. One metric per critical business outcome is worth a hundred technical ones.

The short version

Metrics, logs and traces answer different questions with different cost curves. Use each for its own job, and join them with a trace id in the log line.

Labels multiply, so cardinality is the bill and occasionally the outage: a user id on a counter turns thousands of series into billions and kills the metrics server during the incident you needed it for. Labels must be bounded and known — template your routes, cap cardinality in the collector, and put high-cardinality facts in logs where nothing aggregates over them.

Sample traces at the tail rather than the head, because a head-sampled 1% keeps 1% of your failures and none of the specific one you are looking for. The price is a stateful collector buffering spans, and it is worth paying.

Then downsample metrics by age, index a small set of log fields rather than all of them, keep traces for days rather than months, and make sure the pager fires from the cheap independent path. An observability stack that shares a failure domain with the system it watches is a system that goes blind exactly when it matters.

Next step

Want This Built, Not Just Explained?

CodeBrewerz builds the systems these posts take apart: web, mobile, cloud and the infrastructure underneath. Tell us what you are building.

Start a conversation