Loki — Fundamentals
What is Loki?
Loki is a log aggregation system built by Grafana Labs — designed as the logging equivalent of Prometheus. It collects logs from your applications and infrastructure, stores them cost-efficiently, and lets you query them with LogQL, a language with the same feel as PromQL.
Loki is part of the PLG stack: Promtail (log collector on every node) → Loki (log storage and query engine) → Grafana (visualisation and alerting). All three integrate natively — metrics and logs are queried side by side in the same Grafana dashboard.
Why Loki is different from Elasticsearch: the fundamental difference is the indexing strategy. Elasticsearch indexes the full text of every log — every word becomes searchable, which is fast for arbitrary searches but produces a large index (20-30% of raw log volume) that's expensive to operate. Loki indexes only labels (metadata: namespace, app, pod, environment) — never the log content itself. Log content is stored as compressed text chunks in cheap object storage (S3, GCS, Azure Blob).
| Loki | Elasticsearch (ELK) |
|---|
|---|---|---|
| What is indexed | Labels only (namespace, app, pod) | Full text of every log message |
|---|---|---|
| Storage cost | Very low — compressed chunks in S3/GCS | High — Elasticsearch index + shard storage |
| Query speed | Fast for label-based, slower for full-text scan | Fast for any text search |
| Setup complexity | Simple — 3 components, one Helm chart | Complex — ES cluster sizing, JVM tuning |
| Integration | Native Grafana data source | Kibana (separate UI) |
| Best for | K8s logs with consistent labels | Full-text search, unknown log patterns |
Architecture — Promtail, Loki, Grafana
| Component | What it does |
|---|
|---|---|
| Promtail | Agent running on every node — reads container logs from `/var/log/pods/`, attaches K8s labels (pod, namespace, app), sends to Loki |
|---|---|
| Loki | The log aggregation server — receives logs, indexes only labels, stores compressed chunks in object storage |
| Grafana | Queries and visualises logs via the Explore tab plus LogQL — dashboards combine Prometheus metrics and Loki logs |
| Ruler | Evaluates LogQL rules and sends alerts — the logs equivalent of Prometheus's Alertmanager |
LogQL — Querying Logs
Every LogQL query starts with a log stream selector in curly braces (selects logs using the label index — fast), followed by optional pipeline stages that filter or parse the content.
| Use case | LogQL query |
|---|
|---|---|
| All errors in a namespace | `{namespace="production"} \ | = "ERROR"` | ||
|---|---|---|---|---|
| Specific pod's logs | {pod="payment-api-7d8f9-xyz"} | |||
| JSON field filter | `{app="api"} \ | json \ | status_code >= 500` | |
| Error rate per app | `sum by(app)(rate({ns="prod"} \ | = "ERROR" [5m]))` | ||
| Slowest requests | `{app="api"} \ | json \ | unwrap duration \ | p99 by (endpoint) [5m]` |
Using Loki in Grafana: go to Explore, select Loki as the data source, and paste a LogQL query — the result shows matching log lines in a timeline, and switching to Metrics view shows the rate chart. Dashboard panels can combine a Prometheus error-rate metric with the corresponding Loki log lines for the same service side by side, giving correlation without switching tools.
Promtail — Log Collection
Alerting from Logs
Loki's Ruler evaluates LogQL expressions on a schedule and fires alerts, the same way Prometheus evaluates PromQL — alerting on ERROR rate, specific exception messages, or any log pattern, without needing to instrument the application to emit a corresponding metric.
Interview Questions
What is Loki and how does it differ from ELK?
Loki is a log aggregation system by Grafana Labs, designed to be cost-effective and Kubernetes-native. The key architectural difference from ELK is that Loki does not index the content of log messages at all — it only indexes labels (metadata like namespace, app, and pod), and stores the actual log text as compressed chunks in cheap object storage like S3. Elasticsearch, by contrast, indexes the full text of every log line, making it fast for arbitrary free-text search but producing a much larger, more expensive index — commonly 20-30% of raw log volume. This makes Loki dramatically cheaper to operate at scale, and it integrates natively into Grafana alongside Prometheus metrics, letting you correlate a metric spike with the exact log lines from the same time window in one dashboard. The tradeoff: Loki's label-based indexing means queries need consistent, well-chosen labels to be fast, and full-text search across log content (not filtered by label first) is genuinely slower than Elasticsearch's purpose-built full-text index — Loki is the right choice for Kubernetes environments with consistent labeling conventions, while ELK remains stronger for genuinely unpredictable, full-text-search-heavy log analysis.

