Part 2: How Prometheus Actually Uses RAM and Disk
Observability Series - Part 3 of 5

The "Query of Death" and PromQL Defenses

• 7 min read • Observability
Prometheus PromQL OOM Performance

TL;DR

Prometheus is built for speed, but that speed comes with a vulnerability: the Out-Of-Memory (OOM) crash. In this post, we look at how a single heavy PromQL query can take down your monitoring server, why PromQL refuses to do Cartesian Products, the guardrails Prometheus puts in your way, and the "Flight Recorder" that helps you catch the culprit when all else fails.

In our last post, we explored how Prometheus relies on your server's RAM for its speed. Because its "brain" lives in memory, Prometheus is very fast. But it also means it is exposed to the Linux Grim Reaper: the OOM KillerOut Of Memory Killer. A safety mechanism inside Linux. If the server runs out of RAM, it picks a process (usually the one using the most memory) and forces it to shut down so the whole server doesn't freeze..

If Prometheus tries to load more data into RAM than your server physically has, the Linux operating system steps in to protect itself and kills the Prometheus process.

How does this happen? Usually, it's caused by someone running a "Query of Death."

1. The Labelset Identity Crisis

To understand a bad query, you have to understand how Prometheus identifies data. Prometheus doesn't use SQL tables. Instead, every series is identified by its metric name plus its LabelsetThe set of key-value pairs (like job="django", status="500") attached to a metric. Together with the metric name, it uniquely identifies a time series.. The labels aren't descriptions of the data; they are the data's identity. Change one label value and you have created a brand-new series.

This is where the identity crisis begins. Add a label like user_id or request_id to a metric, and every new user creates a new series. A metric that used to have 50 series can quietly grow to 500,000. This is called high cardinalityWhen a label has a huge number of possible values (like user IDs), creating a huge number of separate series. The number one cause of Prometheus memory problems., and it is the root cause of most Prometheus memory problems.

If you want to see the API errors for your Django app, you write a query with a label matcherThe curly braces {} in a PromQL query, used to filter data down to a specific subset (like filtering by server name, environment, or HTTP status).:

http_requests_total{job="django", status="500"}

Prometheus looks at its index, grabs the handful of series that match those labels, and graphs them. It takes milliseconds and barely touches RAM.

Now imagine someone opens Grafana, sets the time range to "Last 30 days", and types this:

count({__name__=~".+"})

That regex matches every metric name in the database. You just told Prometheus: "Find every single series from every application, server, and environment, load every sample from the last 30 days, and count them." Multiply hundreds of thousands of series by 30 days of 15-second samples, and RAM spikes fast.

What about an empty selector, like a bare {}? Prometheus refuses to run it at all, and returns an error: vector selector must contain at least one non-empty matcher. The same goes for matchers that match everything, like {job=~".*"}. That's the first of several built-in tripwires.

2. The Cartesian Product Trap (That PromQL Won't Let You Fall Into)

Another way people try to hurt Prometheus is by doing math between two metrics. Let's say you want to divide your network errors by your network traffic to get an error percentage:

rate(network_errors_total[5m]) / rate(network_traffic_total[5m])

In a SQL database, joining two big tables without a matching rule produces a Cartesian ProductIn databases, the result of joining two tables without a matching rule: every possible combination of rows from both tables.: every row of table A paired with every row of table B. With 10,000 servers on each side, that's 100,000,000 combinations.

PromQL doesn't let that happen. By default, it only pairs series whose labels match exactly (same instance, same job, and so on). If the labels don't match, you don't get a hundred million results. You get no results at all, which is a confusing silent bug, but not a crash.

And if you loosen the matching with on() so that many series on both sides line up with each other, Prometheus stops you with an error: many-to-many matching not allowed: matching labels must be unique on one side. Try forcing a Cartesian Product yourself in a test environment. It's a good way to see this guardrail in action.

The real danger of joins isn't the combinations. It's that both sides have to be fully loaded into memory first. Two high-cardinality metrics over a 30-day range can still be very expensive, even though the join itself is safe.

3. The Guardrails: Prometheus's Built-in Defenses

Prometheus ships with a few command-line flags that act as circuit breakers for heavy queries:

  • --query.max-samples (default: 50,000,000): The most important one. If a single query would load more samples into memory than this, Prometheus aborts it with "query processing would load too many samples into memory". Lower it if your server has limited RAM.
  • --query.timeout (default: 2m): Any query running longer than this is cancelled.
  • --query.max-concurrency (default: 20): How many queries can run at the same time. Extra queries wait in line instead of all piling into RAM together.

These won't save you from every bad query, but tuning max-samples to fit your server's memory is the single best defense against the Query of Death.

4. The Flight Recorder (queries.active)

If your Prometheus server does crash, how do you know what caused it? The process died mid-query, so it never got the chance to log an error.

To solve this, Prometheus has a built-in black boxA system that records critical data continuously, so that if the system crashes, engineers can see exactly what happened right before the crash. called the Active Query Tracker. When Prometheus receives a query, before it even starts running it, it writes the query text into a small file in its data directory called queries.active.

When the query finishes, its entry is cleared. But if the query triggers the OOM Killer and takes Prometheus down, that entry stays frozen in the file. The next time Prometheus starts, it reads queries.active and prints the unfinished queries in its startup log, so you can see exactly which query was running when it died.

One limitation: the file records only the query text and when it started, not who ran it. To trace a query back to a specific dashboard or user, match the query text against your Grafana dashboards. Or, if you want a permanent record of every query, enable query logging with the query_log_file setting in prometheus.yml.

Up Next in the Series:

In the next post, we shift away from metrics and look at text. We'll explore Loki, why logging has historically been so expensive, and how Loki's architecture solves the cost problem.

Part 4: Loki's Secret: Why It's So Cheap to Run