Skip to content

A working introduction to Loki for centralized logs without the cardinality trap

A practical first deployment of Loki with filesystem storage, the label model you should adopt, and the operational guardrails that prevent the index from exploding.

Loki advertises itself as “Prometheus for logs,” and a team that takes that comparison at face value usually ships a deployment that has the index of an Elasticsearch cluster and the disk profile of a write-only TSDB. Loki’s actual model is the opposite — it indexes only labels, stores log lines compressed in chunks, and forces label cardinality to stay small so the index does not become the bottleneck. This article walks through that model, a working single-binary install, and the label discipline that keeps the system fast a year in.

How to verify

Once Loki is up, three checks confirm it is alive and labeled sanely.

systemctl status loki --no-pager
curl -fsS http://127.0.0.1:3100/ready
curl -fsS http://127.0.0.1:3100/metrics | grep -E '^loki_(build_info|ingester_streams_created|index_request_errors)'
# Stream count — should be in the dozens to low thousands per tenant, not millions
curl -fsS http://127.0.0.1:3100/metrics | grep '^loki_ingester_memory_streams ' | tail -1
# List labels currently indexed
curl -fsS 'http://127.0.0.1:3100/loki/api/v1/labels' | jq

A healthy small deployment has under a few thousand streams. If loki_ingester_memory_streams is in the hundreds of thousands, you have a high-cardinality label somewhere (often request_id, user_id, or a raw URL path) and the index is doing work it should not.

What’s happening

A log line in Loki is (timestamp, line, labels). Lines from the same set of labels form a stream, which is appended to a chunk and eventually flushed to the object store (filesystem, S3, GCS). Queries with {label="value"} selectors hit the small per-stream index to find chunks, then scan the chunk contents with the LogQL filter (|=, |~, | json, etc.).

The cardinality trap: every unique combination of label values creates a new stream. If you label logs with request_id, you create one stream per request — millions of streams, an enormous index, slow queries, and an ingester that spends all its memory tracking them. The model expects labels to be low cardinality and stable: cluster, namespace, app, instance, level. Per-request and per-user identifiers live in the log line itself and are extracted at query time.

The other thing to understand up front: Loki has a write path (distributor → ingester → object store) and a read path (query-frontend → querier → ingester + object store). In single-binary mode, all of these run in one process. In production at scale, they are split — but starting with single-binary and migrating later is the path most teams should take.

The procedure

  1. Install the single binary and a filesystem store. This is enough for hundreds of GB per day on a single VM.

    LOKI_VERSION="3.2.0"
    sudo useradd --system --no-create-home --shell /usr/sbin/nologin loki
    sudo mkdir -p /etc/loki /var/lib/loki/{chunks,index,wal,compactor}
    sudo chown -R loki:loki /var/lib/loki
    curl -fLO "https://github.com/grafana/loki/releases/download/v${LOKI_VERSION}/loki-linux-amd64.zip"
    unzip loki-linux-amd64.zip
    sudo install -m 0755 loki-linux-amd64 /usr/local/bin/loki
  2. Write a minimal loki.yaml.

    # /etc/loki/loki.yaml
    auth_enabled: false
    
    server:
      http_listen_address: 127.0.0.1
      http_listen_port: 3100
    
    common:
      instance_addr: 127.0.0.1
      path_prefix: /var/lib/loki
      storage:
        filesystem:
          chunks_directory: /var/lib/loki/chunks
          rules_directory: /var/lib/loki/rules
      replication_factor: 1
      ring:
        kvstore: { store: inmemory }
    
    schema_config:
      configs:
        - from: 2024-01-01
          store: tsdb
          object_store: filesystem
          schema: v13
          index: { prefix: index_, period: 24h }
    
    limits_config:
      reject_old_samples: true
      reject_old_samples_max_age: 168h
      max_streams_per_user: 10000
      max_global_streams_per_user: 10000
      ingestion_rate_mb: 10
      ingestion_burst_size_mb: 20
      retention_period: 720h    # 30 days
    
    compactor:
      working_directory: /var/lib/loki/compactor
      retention_enabled: true
      delete_request_store: filesystem

    max_streams_per_user is the guardrail. If a misconfigured promtail starts pushing a high-cardinality label, Loki refuses new streams once the limit is hit; the alert that follows is recoverable. Without it, Loki happily creates millions of streams and OOMs.

  3. Run as a systemd service.

    # /etc/systemd/system/loki.service
    [Unit]
    Description=Loki
    After=network-online.target
    [Service]
    User=loki
    Group=loki
    ExecStart=/usr/local/bin/loki -config.file=/etc/loki/loki.yaml
    Restart=on-failure
    LimitNOFILE=65536
    [Install]
    WantedBy=multi-user.target
  4. Adopt a label discipline before you ship anything. Document the allowed labels and refuse new ones in code review.

    ALLOWED:    cluster, env, app, namespace, instance, level, job, source
    FORBIDDEN:  request_id, user_id, session_id, trace_id, path (raw),
                status_code, duration_ms, ip, uuid, anything per-request

    Per-request fields go into the log line as structured JSON and are parsed at query time with | json or | logfmt.

  5. Wire Grafana as a datasource.

    # /etc/grafana/provisioning/datasources/loki.yaml
    apiVersion: 1
    datasources:
      - name: Loki
        type: loki
        url: http://127.0.0.1:3100
        access: proxy
        isDefault: false
        editable: false
  6. Send logs in. With Promtail (sister tool), Vector, or Fluent Bit. See promtail-log-shipping for the working config.

Operational notes

  • Streams above ten thousand per tenant on a single-binary deployment are a smell, not a feature — find the high-cardinality label and pull it out.
  • Filesystem storage is fine for tens of GB per day and a single VM; past that, move to S3 or GCS via the s3: block in common.storage.
  • retention_period is enforced by the compactor — verify it actually deletes by checking loki_compactor_deleted_files_total rises after the retention window passes.
  • Loki’s /metrics endpoint should be scraped by Prometheus — alert on loki_ingester_streams_created_total rate, loki_request_duration_seconds_bucket p99, and ingester memory.
  • Single-binary mode does not survive process crashes without WAL replay; do not skip the WAL directory in your backup or rebuild path.

For the shipping side, see promtail-log-shipping. For the production HA deployment that comes after this intro, see loki-log-aggregation-deploy.


Stack Harbor runs Loki this way for clients on the managed operations tier — labels enforced in shipping configs, stream count alerted on, retention verified against the regulatory window the client actually needs to keep.