Prompt
VictoriaMetrics query latency is inconsistent for us
Latest observation
Inconsistent query latency in VictoriaMetrics is usually caused by one of a few buckets: query shape, storage/cache behavior, compactions, or resource contention. Here’s a practical way to narrow it down.
First: what kind of inconsistency?
If you can, check whether slow queries correlate with:
- time range (e.g. 6h fast, 30d slow)
- cardinality (many series / label combinations)
- query type (
rate(),sum by(), regex matchers, joins, subqueries) - load spikes (ingest spikes, background merges, backups, remote read)
- specific nodes in a cluster
Common causes
1) High-cardinality or expensive selectors
These often cause “same query, different latency” depending on cache state or the number of matching series. Examples:
- regex matchers like
=~".*foo.*" - broad selectors with few label constraints
- large
sum by (...)over many series - joins /
group_left/group_right - subqueries with large step/range
Mitigation
- Add more selective labels.
- Avoid regex where exact match or prefix match is possible.
- Pre-aggregate high-volume metrics at ingest or recording rules.
2) Cache effects
VictoriaMetrics performance can vary a lot if some queries hit cache and others miss. Common caches:
- index/series cache
- result cache
- OS page cache
Symptoms
- First query is slow, subsequent identical queries are fast.
- Latency varies after restarts or memory pressure.
Mitigation
- Ensure enough RAM for cache and active working set.
- Avoid frequent restarts.
- Check whether queries are repeated and can benefit from caching.
- If using Grafana, identical dashboards may still vary if templated variables change query text.
3) Large time ranges or long retention scans
Long lookback ranges can make queries depend heavily on disk access and chunk scans.
Symptoms
- 30d/90d queries much slower than 1h/6h.
- Slowdowns increase when querying older data.
Mitigation
- Use recording rules / downsampling patterns.
- Reduce time range when possible.
- Precompute common aggregates.
4) Background compaction / merge pressure
VM can experience latency spikes when background work competes for CPU, memory, or disk I/O.
Symptoms
- Periodic spikes.
- Slower queries during heavy ingestion or retention activity.
- Disk utilization or IO wait rises.
Mitigation
- Check CPU, memory, disk IOPS, and IO wait.
- Make sure the storage disk is fast enough.
- Separate ingest-heavy and query-heavy workloads if needed.
- Review compaction-related metrics and logs.
5) Disk bottlenecks or noisy neighbors
If the storage backend is slow, query latency can swing significantly.
Symptoms
- Latency spikes align with high disk await / read latency.
- Better on warm cache, worse on cold cache.
- VM node shares host resources with other workloads.
Mitigation
- Use local SSD/NVMe if possible.
- Avoid oversubscribed shared storage.
- Check host-level metrics, not just app metrics.
6) Too many concurrent queries
Queries can queue or contend for CPU/memory.
Symptoms
- Some requests are fast, others wait.
- Latency grows during dashboard refresh storms.
- Grafana panels all fire at once.
Mitigation
- Lower dashboard refresh frequency.
- Reduce panel count / query fanout.
- Tune concurrency limits if applicable.
- Add alerting/recording rules for expensive panels.
7) Cluster topology / load balancing issues
In a clustered setup, inconsistent query latency may happen if:
- requests hit unevenly loaded vmselect nodes
- vmstorage nodes are imbalanced
- some nodes are “hot”
- cross-zone/remote latency varies
Mitigation
- Check per-node CPU, RAM, disk, and request rate.
- Ensure load balancer distributes queries evenly.
- Look for hot shards or imbalanced partitions.
What to collect next
If you want to troubleshoot quickly, gather:
-
A slow query and a fast query
- exact PromQL
- time range
- dashboard/panel if applicable
-
VictoriaMetrics version
-
Deployment mode
- single-node or cluster
-
Resource metrics during slow periods
- CPU, memory, disk latency/IOPS, network
-
VictoriaMetrics logs/metrics
- query duration histograms
- cache hit rates
- compaction/merge activity
- queue/concurrency indicators
Quick tests
Try these to isolate the cause:
- Run the same query twice back-to-back.
- If the second is much faster → caching/cold data issue.
- Compare short vs long range.
- If only long ranges are slow → scan/retention issue.
- Simplify the query step by step.
- Remove regex, joins, and extra grouping.
- Run during low load vs peak load.
- If only slow under load → resource contention.
If you share details, I can help pinpoint it
Send me:
- the query (or a sanitized version),
- single-node vs cluster,
- data volume / retention,
- whether latency improves on повторed runs,
- and any relevant metrics/log snippets.
Then I can suggest likely root cause and concrete tuning steps.
Brands