TechBuzzPro

Loki Retention: Does It Need an Active Compactor?

· 4 min read · Apps & Browsers

In this article
  1. How Loki Retention Works
  2. The Compactor's Role in Storage
  3. What Happens Without an Active Compactor?
  4. How to Configure the Compactor
  5. Common Issues and Misconceptions
  6. Cost and Performance Trade-offs
  7. Alternatives to the Compactor
  8. Frequently Asked Questions

No, Loki retention does not require an active compactor to cut storage, but running one is strongly recommended for efficient storage management. The compactor is the component responsible for merging and deduplicating index and chunk files, and while Loki can function without it, retention enforcement and storage optimization depend on it.

How Loki Retention Works

Loki stores log data in two main parts: chunks (compressed log data) and indexes (metadata pointing to chunks). Retention is configured via the retention_enabled flag and retention periods for logs and metadata. When retention is enabled, Loki periodically deletes expired data based on these periods. However, the actual deletion of chunk files from object storage is handled by the compactor, which scans for expired chunks and removes them. Without the compactor, expired data remains on disk, consuming storage indefinitely.

The Compactor's Role in Storage

The compactor performs several tasks: it merges small index tables into larger ones, deduplicates index entries, and deletes expired chunks. It also runs a periodic task to enforce retention by removing chunks older than the retention period. This process is essential for reclaiming storage space. In a typical Loki deployment, the compactor runs as a single instance (singleton) and is responsible for all retention-related deletions. If the compactor is not running, retention settings are ignored, and storage grows without bound.

What Happens Without an Active Compactor?

If you disable or fail to run the compactor, Loki will still ingest and query logs, but it will not delete expired data. This leads to unbounded storage growth, increased costs, and potentially degraded query performance as indexes grow. Some operators run Loki without a compactor in development or short-lived environments, but this is not recommended for production. Even if you manually delete old chunks from object storage, the index will still reference them, causing query errors. The compactor ensures the index and chunks stay consistent.

How to Configure the Compactor

To enable the compactor, you must set the compactor configuration block in your Loki config. Key settings include:

  • working_directory: local directory for temporary files.
  • shared_store: the object store type (e.g., s3, gcs, azure).
  • compaction_interval: how often the compactor runs (default 10m).
  • retention_enabled: must be true for retention to work.
  • retention_period: how long to keep logs (e.g., 30d).

You also need to configure the schema and storage to support retention. For example, in the schema_config, set store to a TSDB or boltdb-shipper type, as these support retention. The compactor runs as a separate target; you can run it as a single binary with -target=compactor or as part of the read/write deployment.

Common Issues and Misconceptions

A common misconception is that setting retention_enabled alone is enough. In reality, without the compactor running, no deletion occurs. Another issue is misconfigured retention periods: if the retention period is shorter than the time taken to compact, data may be deleted prematurely or not at all. Also, the compactor must have access to the same object store as the ingesters and queriers. If the compactor fails or is down, retention pauses, and storage grows. Monitoring compactor metrics like compactor_compaction_runs_total and compactor_compaction_failures_total helps catch problems early.

Cost and Performance Trade-offs

Running the compactor adds some CPU and memory overhead, but it is generally small compared to the cost of unlimited storage. Without compaction, you pay for storage that could be freed. Compaction also improves query performance by reducing the number of index tables and chunks to scan. In multi-tenant environments, the compactor handles all tenants, so it must be sized appropriately. For very high log volumes, consider running the compactor on a dedicated node or increasing its parallelism settings.

Alternatives to the Compactor

If you cannot run the compactor for some reason, you could manually delete old chunks from object storage and then rebuild the index, but this is error-prone and not recommended. Another alternative is to use Loki's retention with a separate cleanup job that calls the delete_requests API, but this still requires the compactor to process the requests. In essence, the compactor is the only supported way to enforce retention and reclaim storage in Loki.

Frequently Asked Questions

Can I run Loki without the compactor?

Yes, you can run Loki without the compactor, but retention will not work, and storage will grow indefinitely. It is only suitable for testing or very short-lived environments.

Does the compactor delete data immediately?

No, the compactor runs on an interval (default 10 minutes) and processes deletion requests in batches. There is a delay between when data expires and when it is actually removed.

What happens if the compactor is down?

If the compactor is down, retention pauses. Chunks and indexes are not deleted, and storage grows. Once it resumes, it will catch up on pending deletions.

Do I need to configure retention for each tenant?

Yes, you can set per-tenant retention limits using the limits_config overrides, allowing different retention periods for different tenants.

Does the compactor affect query performance?

Generally, the compactor improves query performance by reducing the number of files to scan. However, during compaction, there may be a slight increase in resource usage, but this is usually negligible.