TL;DR
Storing and searching text logs is usually heavy and expensive. Loki turns this upside down. By refusing to index the actual text, Loki acts more like a distributed grep command than a traditional search engine. We also explore the paradox of the 10-minute Compactor and the 2-hour "Death Row."
Before Loki existed, if a company wanted to aggregate its logs, it usually turned to Elasticsearch. Elasticsearch is brilliant, but it is a Full-Text Search EngineA system that indexes every single word within a document (like Google does with websites), allowing instant keyword searches, but requiring a lot of storage and memory..
When you send a 100-word log line to Elasticsearch, it breaks apart every single word and adds each one to a giant index, so you can search for any random word instantly. This is very fast for the user, but that index can grow as large as the logs themselves, it needs plenty of RAM and fast disks to stay quick, and it gets expensive to scale.
Loki was built with a totally different philosophy: Logs should be cheap.
1. The Envelope vs. The Letter
Loki refuses to index your text.
When your application sends a log to Loki, Loki only looks at the labels attached to it (the metadata). If we use an analogy, Loki only reads the address on the outside of the Envelope (e.g., {job="django", environment="production"}).
It takes the actual Letter (your text log), compresses it into a file called a "Chunk," and drops it into storage.
When you search for the word "timeout" in Grafana, Loki uses its tiny, lightweight index to find the exact "Django Production" chunk files. Then, it decompresses them on the fly and brute-force scans the text, much like running a standard Linux grepA famous command-line utility used in Linux to search plain text for lines that match a pattern. It relies on brute-force scanning rather than a pre-built index. command (spread across many workers in a large setup). Because it only indexes the envelope, Loki's index is typically a tiny fraction of the total data size.
The trade-off: searching is brute force, so a query that matches lots of chunks (a broad label selector over a long time range) is slower than in Elasticsearch. The trick is to narrow down with labels first, then search the text.
2. From Local Disks to Infinite S3
Because Loki stores these compressed chunks as simple objects, it is flexible about where they live. If you are running a small setup, you can tell Loki's configuration to use your local hard drive:
common:
path_prefix: /var/lib/loki
storage:
filesystem:
chunks_directory: /var/lib/loki/chunks
rules_directory: /var/lib/loki/rules
But as you grow, local disks fill up. At production scale, you can swap filesystem for s3 and point Loki at an Amazon S3A cloud object storage service designed for massive amounts of unstructured data (like zip files, images, or backups). Significantly cheaper than the disks attached to active servers. bucket:
common:
storage:
s3:
bucketnames: my-loki-logs
region: us-east-1
You'll also need AWS credentials (ideally an IAM role) and object_store: s3 in your schema_config. Because S3 object storage is effectively unlimited and cheap, you no longer have to worry about a "Disk Full" crash taking down your logs.
3. The Compactor and "Death Row"
Whether you store logs on a local disk or in S3, you need to tell Loki to delete old logs, or storage grows forever (on S3 you could use a bucket lifecycle rule instead). How Loki actually deletes these files is a fascinating paradox.
Retention is handled by a background worker called the CompactorA Loki background component that keeps the index tidy and, when retention is enabled, marks and deletes logs older than the retention period., and it's off by default. You have to switch it on explicitly:
compactor:
working_directory: /var/lib/loki/compactor
compaction_interval: 10m
retention_enabled: true
retention_delete_delay: 2h
delete_request_store: filesystem # or s3, matching your storage
limits_config:
retention_period: 30d
Forget retention_enabled: true, and Loki will happily keep every log forever, no matter what retention_period says.
Once it's on, the Compactor wakes up every 10 minutes and scans for logs older than 30 days.
But it doesn't delete them.
Instead, it removes them from the search index (so users can't see them anymore) and writes the chunk IDs into a marker file in its working directory. This is the Death Row Marker.
Why the delay? Why not just delete the file instantly? Because Loki cachesA temporary, ultra-fast storage layer. Systems keep frequently requested data in a cache so they don't have to fetch it from slower storage over and over again. its index to make those brute-force queries fast. If Loki deleted a chunk instantly, but a query was still running with an old, cached index that pointed to that chunk, the query would fail with a "chunk not found" error.
Loki waits an extra 2 hours (the retention_delete_delay) to give all the caches in the system time to refresh and realize the log is gone before it finally, permanently deletes the chunks.
Up Next in the Series:
In our final post, we look at Promtail, the silent mail carrier that sits on your application server, and how you can watch it make its network calls in real time.
Part 5: Inside Promtail's Brain