Skip to main content

Implementing Performance Tuning

1. Optimizing Scrape Interval

Configuration Values Description
scrape_interval 15s How frequently Prometheus scrapes targets. Default is 15 seconds.
scrape_timeout 10s Timeout for individual scrape requests. Default is 10 seconds.

Example: Configuring scrape interval

scrape_configs:
  - job_name: "prometheus"
    scrape_interval: 30s
    scrape_timeout: 15s
    static_configs:
      - targets: [localhost:9090]
Note: Lower scrape intervals increase Prometheus' resource usage (CPU, memory) and can impact target performance. Consider increasing the interval for less critical jobs or reducing it only for high-priority metrics.

2. Configuring Evaluation Interval

Configuration Option Default Value Description
evaluation_interval 1m How often to evaluate rules and scrape targets.

Example: Setting Evaluation Interval

global:
  evaluation_interval: 30s
Note: Setting a shorter evaluation interval increases the load on the Prometheus server.

3. Tuning Storage Retention

Configuration Values Description
storage.tsdb.retention.time 15d How long to retain samples in storage. Default is 15 days.
storage.tsdb.retention.size 50GB Maximum size of the data directory before old data is deleted.

Example: Setting retention policy

storage:
  tsdb:
    retention:
      time: 30d
      size: 100GB
Note: Setting storage.tsdb.retention.time to 0s disables automatic deletion of old data.
Warning: Setting storage.tsdb.retention.size too low may cause data loss during high ingestion rates.
Retention Mode Description Use Case
time-based Deletes data older than a specified time. Compliance or long-term monitoring with fixed retention needs.
size-based Deletes oldest data when storage exceeds a limit. Resource-constrained environments where disk space is limited.

4. Implementing Recording Rules

Rule Description Example
record Specifies the name of the new time series to be created record: http_requests_total
expr Expression to evaluate and store as a new metric expr: rate(http_requests_total[5m])
labels Optional labels to add to the new metric labels: {job="api-server"}

Example: Recording Rule for HTTP Request Rate

groups:
- name: http_rules
  rules:
  - record: job:http_requests:rate5m
    expr: rate(http_requests_total[5m])
    labels:
      job: api-server
Note: Recording rules are useful for pre-aggregating data to reduce query load and improve performance.

5. Optimizing Label Cardinality

Label Description Impact Best Practice
job Identifies the job that scraped the metric Low cardinality, stable Use descriptive but consistent names
instance Identifies the instance that scraped the metric Medium cardinality, changes with deployment Limit to necessary instances
environment Environment where the metric originates Low to medium cardinality Use predefined values (e.g., prod, staging)
version Version of the application or service Medium cardinality, can grow over time Limit to major versions or stable releases
endpoint HTTP endpoint being scraped High cardinality if unique per request Avoid high-cardinality labels like URLs or IDs
http_requests_total{endpoint="/api/users/12345"} 100

Example: Optimized Label Usage

http_requests_total{endpoint="/api/users", status="200"} 100
Note: High label cardinality can lead to increased memory usage and reduced query performance.

Label Cardinality Optimization Tips

  • Avoid using unique identifiers (e.g., user IDs, session IDs) as labels
  • Use predefined values for environment and version labels
  • Limit the number of unique label combinations
  • Monitor and alert on high cardinality metrics

6. Configuring Memory Limits

Configuration Option Description Default Value Notes
storage.tsdb.max-block-duration Maximum duration a block can last before being cut 2h Blocks are cut based on time or size limits
storage.tsdb.min-block-duration Minimum duration a block can last 2h Used to enforce minimum retention policies
storage.tsdb.retention.time How long to keep data in the database 15d Can be overridden per scrape job
storage.tsdb.head-chunks Number of chunks to keep in memory 1024 Higher values increase memory usage but reduce disk I/O
storage.tsdb.chunk-size Size of chunks in bytes 2048 Smaller chunks can improve query performance

Example: Setting Memory Limits

storage:
  tsdb:
    max-block-duration: 3h
    min-block-duration: 1h
    retention:
      time: 30d
    head-chunks: 2048
    chunk-size: 4096
Note: Adjusting these values can significantly impact performance and memory usage. Monitor system metrics after changes.

7. Using WAL Compression

Prometheus uses the Write-Ahead Log (WAL) to ensure data durability and consistency. WAL compression can significantly reduce disk I/O and storage usage, especially in high-cardinality environments.

Configuration Option Values Description
--storage.tsdb.wal-compression true / false Enables or disables WAL compression. Default is true.

Example: Enabling WAL Compression

prometheus --storage.tsdb.wal-compression=true
Note: WAL compression is available starting in Prometheus v2.30.0 and is enabled by default in newer versions.
Warning: Enabling WAL compression may increase CPU usage slightly due to compression/decompression overhead.
Compression Algorithm Details Performance Impact
Snappy Default compression algorithm used by Prometheus WAL. Moderate CPU overhead; high compression ratio.
Zstd Alternative compression algorithm (requires Prometheus v2.35+). Higher compression ratio, moderate CPU overhead.

Example: Using Zstd Compression (v2.35+)

storage:
  tsdb:
    wal-compression: true
    wal-compression-level: 3

8. Implementing Query Concurrency

Property Description Default Value Notes
query.max-concurrency Maximum number of concurrent queries that can be executed 10 Controls the level of parallelism in query execution
query.timeout Maximum time a query can take before being cancelled 2m Prevents long-running queries from blocking resources

Example: Configuring Query Concurrency

query:
  max-concurrency: 15
  timeout: 3m
Note: Increasing query.max-concurrency can improve performance for complex queries but may lead to resource contention.
Warning: Setting query.timeout too low may cause valid queries to be prematurely cancelled.

9. Tuning Compaction Settings

Setting Description Default Value Notes
compaction.min-compactible Minimum number of files to compact 10 Adjust based on storage and performance needs
compaction.max-compactible Maximum number of files to compact 100 Prevents excessive compaction overhead
compaction.block-size Size of blocks in bytes 4096 Higher values may reduce I/O overhead
compaction.max-file-size Maximum file size before compaction 128MB Can be adjusted to balance storage and performance

Example: Compaction Configuration

compaction:
  min-compactible: 15
  max-compactible: 80
  block-size: 8192
  max-file-size: 256MB
Note: Compaction settings should be tuned based on the specific workload and storage characteristics of your Prometheus instance.

10. Optimizing Remote Write

Configuration Description Default Value Notes
remote_write.url URL of the remote write endpoint None Required field
remote_write.name Name of the remote write configuration None Used for identifying the config in logs
remote_write.basic_auth Basic authentication credentials None Supports username/password
remote_write.tls_config Transport Layer Security configuration None Enables secure communication
remote_write.write_retries Number of retries on write failure 10 Set to 0 to disable retries
remote_write.queue_config Queue configuration for remote writes Default queue settings Controls batching and retry behavior

Example: Basic Remote Write Configuration

remote_write:
  - url: https://prometheus.example.com/api/v1/write
    name: "remote"
    basic_auth:
      username: "user"
      password: "pass"

Example: Optimized Queue Configuration

remote_write:
  - url: https://prometheus.example.com/api/v1/write
    queue_config:
      capacity: 10000
      max_shards: 20
      min_shard_size: 1000
      max_samples_per_send: 1000
      batch_send_deadline: 5s
Note: Adjust capacity, max_shards, and batch_send_deadline based on your system's memory and network capacity to avoid overloading the remote endpoint.
Warning: Setting write_retries too high can lead to excessive load on the remote system. Monitor and adjust accordingly.

11. Using Chunk Encoding

Encoding Type Description Notes
chunked Encodes data in chunks of variable size Used for streaming responses
gzip Compresses data using gzip algorithm Reduces bandwidth usage
br Compresses data using Brotli algorithm More efficient than gzip, newer browsers support it

Example: Configuring chunked encoding in Prometheus

remote_write:
  - url: http://prometheus-remote-write:9090/api/v1/write
    queue_config:
      chunk_size: 1048576
      chunk_interval: 1m
Note: Chunk encoding can significantly reduce memory usage when handling large datasets.

12. Monitoring Resource Usage

Resource Metric Name Description Unit
CPU Usage prometheus_engine_cpu_seconds_total Total CPU time used by the Prometheus engine seconds
Memory Usage prometheus_engine_memory_bytes Memory usage of the Prometheus engine in bytes bytes
Storage Usage prometheus_storage_blocks_bytes Total storage used by blocks in the Prometheus storage bytes
Query Duration prometheus_engine_query_duration_seconds Duration of query execution in seconds seconds

Example: Querying CPU Usage


prometheus_engine_cpu_seconds_total

Example: Querying Memory Usage


prometheus_engine_memory_bytes

Was this article helpful?