How do Loki and Elasticsearch store logs differently?

Elasticsearch does most of its work when a log arrives. Each event becomes a JSON document. Analyzers tokenize text fields into terms, and those terms go into an inverted index that maps each term to the documents containing it. The index is split into shards, and each shard is a set of immutable segments that are periodically merged. Elasticsearch also writes doc values, a columnar copy of field data that supports sorting and aggregations. This costs CPU, memory, and disk at ingest. The benefit is that a later search for a term is mostly an index lookup.

Loki takes the opposite approach. A log stream is identified by its label set, such as {app="api", env="prod"}. Loki indexes only those labels. It does not index the log line content. Lines from the same stream are batched, compressed into chunks, and usually written to object storage. Ingest is cheap because there is little to index. The trade-off is that filtering on content means decompressing and scanning chunks at query time.

           WRITE PATH                         READ PATH
Elasticsearch:
  log -> analyze -> tokenize ->        query -> inverted index lookup
         inverted index + doc values          -> matching docs
         (shards / segments)

Loki:
  log -> match labels -> append        query -> label index -> select
         to stream chunk -> compress          chunks -> fetch from object
         -> object storage                    storage -> decompress + grep

The core thesis: Elasticsearch pays the indexing cost at write time, and Loki defers that cost to query time.

ELASTICSEARCH  (indexing cost paid at write time)

 WRITE  +-------+   +------------+   +-------------------------------+
        | log   |-->| analyze +  |-->| shards -> immutable segments  |
        | (doc) |   | tokenize   |   |  inverted index (term->docs)  |
        +-------+   +------------+   |  doc values (columnar fields) |
                                     +-------------------------------+
                                                     ^
 READ   +-------+   +------------+                   |
        | query |-->| term lookup|-------------------+
        +-------+   +------------+---> matching docs

LOKI  (scanning cost deferred to query time)

 WRITE  +-------+   +------------+   +------------+   +---------------+
        | log + |-->| label set  |-->| append to  |-->| compressed    |
        | labels|   | = stream   |   | chunk      |   | chunks in     |
        +-------+   +-----+------+   +------------+   | object storage|
                          |                           +---------------+
                          v                                   ^
                    +------------+                            |
                    | label index|                            |
                    | labels only|                            |
                    +------------+                            |
                          ^                                   |
 READ   +-------+         |          +---------------------+  |
        | query |---------+--------->| fetch chunks,       |--+
        +-------+  select streams    | decompress + filter |
                                     +---------------------+

Elasticsearch builds an inverted index and doc values at ingest so reads are index lookups, while Loki indexes only labels at ingest and does content filtering at query time by fetching and decompressing chunks from object storage.

How does each system work end to end?

The storage models above sit inside two quite different cluster architectures. Knowing which component does what tells you where latency, memory pressure, and failures will show up.

Elasticsearch. A log shipper or application sends documents to the cluster, usually in batches through the bulk API. A node receiving the request acts as the coordinating node. It optionally runs an ingest pipeline to parse or enrich the document, then routes the document to a primary shard. The primary indexes it and forwards it to its replica shards. New documents sit in an in-memory buffer and a transaction log. A periodic refresh turns that buffer into a new searchable segment, which is why Elasticsearch search is described as near real-time rather than immediate. Background merges combine small segments into larger ones.

At query time, the coordinating node fans the request out to one copy of every relevant shard. Each shard runs the query against its own segments and returns its top hits or partial aggregation results. The coordinating node merges those partial results into the final response. This scatter-gather step is why shard count matters so much. Every extra shard adds work to every query that touches its index.

Loki. Loki runs as a set of components. They can be deployed as one process or split into separate services.

  • Distributor. It receives pushed log entries, validates them against limits, and hashes each stream's label set to decide which ingesters own it. It then sends the entries to several ingesters for replication.
  • Ingester. It holds open chunks for each active stream in memory, protected by a write-ahead log. It also serves queries for recent data that has not been flushed yet. When a chunk fills up or ages out, the ingester compresses it and flushes it to object storage along with index entries.
  • Querier. It resolves the stream selector against the index, fetches matching chunks from object storage and recent data from ingesters, then runs line filters, parsers, and aggregations.
  • Query frontend. It splits a query by time, shards it, queues it, and caches results. Its job is to spread the work across queriers.
  • Compactor. It compacts index files and applies retention by deleting expired data.

The practical difference is where state lives. Elasticsearch data nodes hold the index on local disk, so losing a node means recovering shards from replicas. In Loki, long-term data lives in object storage. Ingesters are the main stateful component, and they only hold the recent, unflushed window.

Why does label cardinality matter so much in Loki?

Because Loki's only index is the label set, every distinct combination of label values becomes its own stream, with its own chunks in the ingester and its own entries in the index. Cardinality multiplies across labels: 50 services × 3 environments is 150 streams, but adding a user_id label with 100,000 active users can create millions. Each stream fills slowly, so chunks get flushed small and underfilled. Object storage then holds a huge number of tiny files, ingester memory grows with the count of open streams, and queries spend their time listing and fetching chunks instead of scanning log data.

Compare two label sets:

Bad Good
{service="checkout", env="prod", pod_uid="7f3c…", request_id="a91e…", user_id="48213"} {service="checkout", env="prod", cluster="eu-1", level="error"}

The good set uses values that are bounded and stable over time. Keep request_id and user_id in the log line, where a line filter or parser can match them at query time. Alternatively, attach them as structured metadata, which is stored with each entry but does not define a stream.

The common migration trap is to map every Elasticsearch indexed field to a Loki label so existing dashboards keep working. That approach recreates field indexing in a system that was not designed for it.

Fix label design early. Changing relabeling rules only affects new writes. Chunks already stored keep their original streams until retention removes them, so queries that span the change must handle both label shapes.

{service="checkout", env="prod", user_id="48213"}

{service="checkout", env="prod", level="error"} |= "a91e7c2d"

{service="checkout", env="prod"} | json | user_id="48213"

{service="checkout", env="prod"} | request_id="a91e7c2d"

sum by (user_id) (
  count_over_time({service="checkout", env="prod", level="error"} | json [5m])
)

LogQL queries that select streams with bounded labels and match high-cardinality values like user_id and request_id with line filters, the json parser or structured metadata instead of stream labels.

How do you query logs with LogQL compared to Elasticsearch Query DSL?

Every LogQL query starts with a stream selector such as {app="checkout", env="prod"}. The selector picks streams through the label index, so it determines how much data gets fetched. Line filters come next: |= "timeout", != "healthcheck", and |~ "5\\d\\d" for regex. These are cheap substring and regex scans over raw lines. Parser stages (| json, | logfmt, | regexp, | pattern) extract fields at query time. You can then filter on the extracted fields, for example | status >= 500. Metric queries wrap all of this in a range: sum by (pod) (rate({app="checkout"} |= "timeout" [5m])) or count_over_time(...). If you already write PromQL, the aggregation and range syntax will look familiar.

Elasticsearch works differently. Fields are parsed at ingest according to the mapping. A match query on a text field is analyzed and relevance-scored. A term query on a keyword field is an exact comparison. The investigation above becomes a bool query that combines match on message with a range on status, plus a terms aggregation on pod and a date_histogram.

The mapping decides which of these queries work on a field:

  • A term query against a text field often misses, because the indexed tokens were lowercased and split.
  • Aggregating on text fails by default.
  • terms aggregations can return approximate counts across shards.

Wrap non-scoring clauses in filter context so Elasticsearch can skip scoring.

LogQL has its own costs. It re-parses lines on every query, it has no joins, and grouping by high-cardinality extracted fields is slow. For heavier analytics, Elasticsearch fits better.

curl -sG '' \
  --data-urlencode 'query=sum by (pod) (count_over_time({app="checkout", env="prod"} |= "timeout" | json | __error__="" | status >= 500 [5m]))' \
  --data-urlencode "start=$(date -u -d '1 hour ago' +%s)" \
  --data-urlencode "end=$(date -u +%s)" \
  --data-urlencode 'step=300'

curl -s -X POST '' \
  -H 'Content-Type: application/json' -d '{
  "size": 0,
  "query": { "bool": { "filter": [
    { "term":  { "app": "checkout" } },
    { "term":  { "env": "prod" } },
    { "match": { "message": "timeout" } },
    { "range": { "status": { "gte": 500 } } },
    { "range": { "@timestamp": { "gte": "now-1h" } } }
  ] } },
  "aggs": {
    "by_pod": {
      "terms": { "field": "pod", "size": 20 },
      "aggs": {
        "over_time": { "date_histogram": { "field": "@timestamp", "fixed_interval": "5m" } }
      }
    }
  }
}'

The same investigation run against both systems: a LogQL metric query that filters lines, parses JSON at query time, and counts per pod, and the equivalent Elasticsearch bool filter query with terms and date_histogram aggregations over fields the mapping parsed at ingest.

How does query cost scale in Loki vs Elasticsearch?

Where the time goes depends on what each system can skip. Elasticsearch resolves a term query against the inverted index in every shard it touches. Its cost tracks the number of shards and segments involved, plus the cost of fetching and aggregating matching documents. A rare term over a week costs roughly what it costs over an hour, as long as the indices are the same.

Loki can only skip data by label and time. Everything inside the selected streams and time range gets decompressed and scanned, because line filters run after the chunks are read.

Illustrative estimate (assumed numbers, not a benchmark). The formula is: bytes scanned = matched streams × bytes per stream per hour × hours.

Query Streams Hours Bytes scanned (assumed 100 MB/stream/hour)
Narrow: one service, last hour 6 1 6 × 100 MB × 1 = 600 MB
Needle: trace ID, all namespaces, 3 days 2,000 72 2,000 × 100 MB × 72 = 14.4 TB

The second shape shows up during incidents. You are hunting for an ID or error string, you don't yet know which service emitted it, and the problem may have started days ago. These are the queries Loki handles worst. They are also the ones Elasticsearch handles best.

Aggregations follow the same split. A terms aggregation on a keyword field reads precomputed doc values in Elasticsearch. Grouping by an extracted field in Loki forces parsing of every line. Each distinct value also becomes a series, so high-cardinality groupings can hit the per-query series limit and fail.

Loki compensates with parallelism. The query frontend splits queries by time and shards them across queriers. This shortens wall-clock time but not the total bytes read. Query limits exist to stop one broad search from consuming the whole cluster.

What do storage and operations cost for each system?

Elasticsearch couples storage to nodes. Every gigabyte of index lives on a data node's disk, so retaining more data means running more machines, each needing heap tuning and shard placement. Teams manage this with ILM (hot/warm/delete phases), keep shards within a sensible size range so cluster state stays small and recovery stays fast, and set replica counts that multiply disk use. When the bill grows, retention is usually the first thing cut.

Loki stores compressed chunks and a small index in object storage, so storage and compute scale independently. In return, you operate distributors, ingesters, queriers, compactors, and caches. You also have to tune for object-storage behavior: per-request charges, first-byte latency, and the number of small reads a broad query triggers.

Cost driver Elasticsearch Loki
Where bytes live Node-attached disk Object storage
Durability Replica shards Provided by the object store
Main tuning work Heap, shard sizing, ILM Caches, chunk sizing, request patterns
Retention lever Delete indices early Compactor retention, usually cheaper to extend

Illustrative estimate (assumed numbers, not a benchmark). Stored bytes = daily raw × retention days × on-disk ratio × copies.

  • Assume 100 GB/day of raw logs.
  • For Elasticsearch, assume an on-disk ratio of 1.0 (index overhead offsets compression) and 1 replica, which gives 2 copies. That is 200 GB/day.
  • For Loki, assume 10:1 compression and 1 copy in object storage. That is 10 GB/day.

At 30 days of retention, that comes to 6 TB versus 0.3 TB. At 90 days, it is 18 TB versus 0.9 TB. The ratio between the two stays the same, but the absolute gap grows linearly with retention. In Elasticsearch, that gap also adds data nodes, so compute cost rises along with storage.

Who should use Loki, and who should use Elasticsearch?

The deciding factor is who is searching and what they already know when they start.

Loki suits teams that debug infrastructure and services they own. Typical examples are platform, SRE, and backend teams running workloads on Kubernetes or similar platforms where every log already has a clear origin: cluster, namespace, app, environment. Teams that already use Prometheus-style labels benefit the most, because the same label conventions and a PromQL-like query syntax carry over to logs. Loki also fits organizations that need long retention for compliance or postmortems but cannot justify node-attached disk for every retained day.

Elasticsearch suits teams that search by content rather than by origin. Typical examples:

  • Security and audit teams. They hunt for an IP address, username, or indicator across every source.
  • Support engineers. They look up a customer or order ID without knowing which service handled it.
  • Product or analytics users. They run aggregations and breakdowns over many structured fields.
  • Search-heavy use cases. Applications that need relevance ranking or full-text features such as analyzers and stemming.

Neither is a fit for a team with no capacity to operate it. Elasticsearch requires ongoing attention to heap, shards, mappings, and upgrades. Loki requires attention to label discipline, query limits, caches, and object-storage behavior. If no one owns that work, either system will degrade. The usual symptoms are an Elasticsearch cluster in a yellow or red state, or a Loki deployment where every query times out.

When should you choose Loki, Elasticsearch, or both?

Choose based on how your engineers start a search, not on which system has the longer feature list.

Access pattern Better fit Why
"Checkout pods in prod started erroring at 14:05" Loki Labels narrow the scan to a few streams before any text is read
Long retention on a fixed storage budget Loki Object storage and a small index keep cost per retained day low
Jumping from a metrics spike to its logs Loki Shared labels let the same selector drive both queries
"Has this IP or user ID appeared anywhere in the last year?" Elasticsearch The inverted index answers this without scanning every stream
Aggregations over user, tenant or request fields Elasticsearch Indexed fields support terms and stats queries directly
Relevance-ranked search across messages Elasticsearch Scoring is built into the query model

Avoid Loki when most of your questions start without a service or time window, or when they group by high-cardinality fields. Those queries turn into brute-force scans. Avoid Elasticsearch when ingest volume and retention drive the budget, because every indexed byte adds disk and data nodes.

Running both is a reasonable choice. You might send application logs to Loki for debugging and route security and audit streams to Elasticsearch. The cost is real, though. You run two clusters with two upgrade paths, and an investigation that crosses the boundary means switching tools and query languages partway through.

The time savings show up during incidents. When the tool matches how responders already think, they write a correct query on the first try. When it does not, they spend the first ten minutes working around the tool instead of finding the cause.

How can the right choice save engineering time?

Most of the engineering time spent on logging goes to recurring maintenance and to slow or failed queries, not to the initial setup. Matching the system to your access patterns removes whole categories of that work.

Less schema and capacity work with Loki. Loki has no per-field mapping to maintain. A new field in a JSON log line becomes queryable through | json the moment it appears. There is no mapping conflict to debug and no reindex to schedule. Because storage lives in object storage, extending retention is mostly a configuration change and a storage bill. Elasticsearch, by contrast, makes you add data nodes and rebalance shards. The work that remains is label review, which is a small, recurring task compared with schema migrations.

Less query rework with Elasticsearch. When people search by ID or string across all sources, Elasticsearch answers directly. Loki would make engineers first narrow the search by guessing services and time windows, then rerun the query when the guess was wrong. For security and support workflows, that difference repeats on every lookup.

Faster correlation with shared labels. When Loki stream labels match your metric labels, an engineer can move from a metric alert to the matching logs with the same selector. They don't need to translate field names between systems.

Fewer self-inflicted incidents. Both systems save time when the guardrails in the next section are in place from the start:

  • Explicit Elasticsearch mappings prevent rejected documents and forced reindexes.
  • ILM rollover prevents oversized shards and slow delete-by-query jobs.
  • Bounded Loki labels prevent stream explosions.
  • Loki query limits prevent one broad search from starving everyone else.

Each of these turns a future outage-and-cleanup cycle into a one-time decision.

Illustrative estimate (assumed numbers, not a measured result). Engineer-hours saved per month = incidents per month × responders per incident × minutes saved per incident ÷ 60.

  • Assume 15 incidents per month and 3 responders each.
  • Assume 10 minutes saved per incident because the first query works instead of needing to be reworked.

That gives 15 × 3 × 10 ÷ 60 = 7.5 engineer-hours per month, before counting avoided reindexes or capacity changes. Replace each assumed input with your own incident data. The point of the formula is to show which variable dominates for your team, not to predict a result.

How do you implement Loki or Elasticsearch without common mistakes?

Loki: keep labels boring

Pick labels that describe where a log came from, not what it says. Fields like cluster, namespace, app, and env work well because they have few values and rarely change. Each unique label combination creates a separate stream, so adding user_id or trace_id as a label multiplies streams and fragments chunks. Send those values as structured metadata instead. They stay attached to each line and can be filtered with | trace_id="abc123" after stream selection, without creating new streams.

Set query limits in limits_config before users find the expensive queries for you. max_query_length caps the time range, max_query_series caps how many series a metric query can return, and max_entries_limit_per_query caps raw line output. Without these limits, a single wide regex over thirty days can saturate the queriers that everyone else depends on.

Elasticsearch: decide the schema yourself

Define explicit mappings in an index template. Map fields you filter or aggregate on, such as status, service, and host, as keyword. Use text only for fields you search by words, typically message. Set dynamic to strict or false on the root or on noisy objects. Dynamic mapping fixes a field's type from the first document it sees. If status first arrives as a number and later as "ok", the later documents are rejected, and changing the type means a reindex. Unbounded keys in nested JSON can also push you toward index.mapping.total_fields.limit.

Attach an ILM policy that rolls over on max_primary_shard_size or max_age. Rollover keeps shards at a predictable size and lets you delete old data by dropping whole indices instead of running delete-by-query.

ES=

curl -s -X PUT "$ES/_ilm/policy/app-logs-policy" -H 'Content-Type: application/json' -d '{
  "policy": { "phases": {
    "hot":    { "actions": { "rollover": { "max_primary_shard_size": "50gb", "max_age": "7d" } } },
    "delete": { "min_age": "30d", "actions": { "delete": {} } }
  } }
}'

curl -s -X PUT "$ES/_index_template/app-logs" -H 'Content-Type: application/json' -d '{
  "index_patterns": ["app-logs-*"],
  "data_stream": {},
  "priority": 500,
  "template": {
    "settings": { "index.lifecycle.name": "app-logs-policy" },
    "mappings": {
      "dynamic": "strict",
      "properties": {
        "@timestamp": { "type": "date" },
        "service":    { "type": "keyword" },
        "host":       { "type": "keyword" },
        "status":     { "type": "keyword" },
        "trace_id":   { "type": "keyword" },
        "message":    { "type": "text" },
        "extra":      { "type": "object", "dynamic": false }
      }
    }
  }
}'

An ILM policy with size- and age-based rollover plus an index template with an explicit strict mapping, keyword fields for filtering, text for message, and a non-dynamic object for noisy keys.

How OneEnv can help

Disclosure: OneEnv is the product behind this blog.

OneEnv (oneenv.io) is a workspace-based governance platform for the things services share — environment variables, database structure, API routes and validation rules. A change to a shared value is staged on a branch, reviewed once, and then every affected service explicitly approves or rejects it before it goes live, so you can see who a change touches before it ships instead of finding out in production.

Applied to loki vs elasticsearch, this is where it helps:

Saves time

  • Every service that inherits a shared value is listed in one place, so nobody searches repos and chats to find out who is affected.
  • One review per branch replaces coordinating each team separately; each service still keeps its own approve/reject.
  • Changing a shared variable is one edit and one review instead of editing every service's .env by hand.

Saves cost

  • Fewer production incidents and rollbacks caused by a shared value, route or column changing under a consumer unannounced.
  • Less duplicated configuration to maintain: one shared definition inherited by many services.
  • A wrong or stale value is caught at review time by the services it affects, not by a failed deploy.
  • OneEnv has a free plan (1 workspace, unlimited services and members, full governance), so trying it costs nothing.

These are qualitative benefits, not measured results — how much you save depends on how many services share the value.

Want to try it? Start at oneenv.io.