Video in progress
What is the high cardinality problem in metrics, and how does it arise?
Every unique combination of tags creates a separate time series. If you put an unbounded value like userId, a full URL or an error message into a tag, the number of series climbs into the millions, Prometheus runs out of memory, and the application itself uses a lot of memory too.
📖 Answer
In Prometheus, orders_created_total{channel="web"} and orders_created_total{channel="mobile"} are two different series. Each has its own index and samples in memory.
Now somebody wrote this:
registry.counter("orders.created", "userId", userId).increment();
100 thousand users means 100 thousand series. If you multiply several such tags (userId × status × productId), the number explodes. The results:
- Prometheus RAM grows and it dies with an OOM, or queries slow down;
- inside the application, the
MeterRegistryholds every Meter in memory too, so the heap grows; - in managed systems (Datadog, Grafana Cloud) the bill depends on the number of series.
Another hidden source is the URI. An endpoint with a path variable is fine when written as the template /orders/{id}. But when unknown URLs return 404, or there is a hand-written filter that loses the template, every ID becomes its own uri.
The rule: tag values should come from a known, small set: status, method, region, error type (the class name, not the message). If you need a specific user or order, that is a job for logs and traces.
As a safeguard, you can limit it in Micrometer with MeterFilter.maximumAllowableTags(...).
Sign up to mark studied questions and save your progress.