<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Get started on Grafana Labs</title><link>https://grafana.com/docs/loki/v2.9.x/get-started/</link><description>Recent content in Get started on Grafana Labs</description><generator>Hugo -- gohugo.io</generator><language>en</language><atom:link href="/docs/loki/v2.9.x/get-started/index.xml" rel="self" type="application/rss+xml"/><item><title>Loki overview</title><link>https://grafana.com/docs/loki/v2.9.x/get-started/overview/</link><pubDate>Thu, 10 Apr 2025 12:15:54 +0000</pubDate><guid>https://grafana.com/docs/loki/v2.9.x/get-started/overview/</guid><content><![CDATA[&lt;h1 id=&#34;loki-overview&#34;&gt;Loki overview&lt;/h1&gt;
&lt;p&gt;Loki is a horizontally-scalable, highly-available, multi-tenant log aggregation system inspired by &lt;a href=&#34;https://prometheus.io/&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Prometheus&lt;/a&gt;. Loki differs from Prometheus by focusing on logs instead of metrics, and collecting logs via push, instead of pull.&lt;/p&gt;
&lt;p&gt;Loki is designed to be very cost effective and highly scalable. Unlike other logging systems, Loki does not index the contents of the logs, but only indexes metadata about your logs as a set of labels for each log stream.&lt;/p&gt;
&lt;p&gt;A log stream is a set of logs which share the same labels. Labels help Loki to find a log stream within your data store, so having a quality set of labels is key to efficient query execution.&lt;/p&gt;
&lt;p&gt;Log data is then compressed and stored in chunks in an object store such as Amazon Simple Storage Service (S3) or Google Cloud Storage (GCS), or even, for development or proof of concept, on the filesystem. A small index and highly compressed chunks simplify the operation and significantly lower the cost of Loki.&lt;/p&gt;
&lt;figure
    class=&#34;figure-wrapper figure-wrapper__lightbox w-100p &#34;
    style=&#34;max-width: px;&#34;
    itemprop=&#34;associatedMedia&#34;
    itemscope=&#34;&#34;
    itemtype=&#34;http://schema.org/ImageObject&#34;
  &gt;&lt;a
        class=&#34;lightbox-link captioned&#34;
        href=&#34;../loki-overview-2.png&#34;
        itemprop=&#34;contentUrl&#34;
      &gt;&lt;div class=&#34;img-wrapper w-100p h-auto&#34;&gt;&lt;img
          class=&#34;lazyload mb-0&#34;
          data-src=&#34;../loki-overview-2.png&#34;alt=&#34;**Loki logging stack**&#34;title=&#34;**Loki logging stack**&#34;/&gt;
        &lt;noscript&gt;
          &lt;img
            src=&#34;../loki-overview-2.png&#34;
            alt=&#34;**Loki logging stack**&#34;title=&#34;**Loki logging stack**&#34;/&gt;
        &lt;/noscript&gt;&lt;/div&gt;&lt;figcaption class=&#34;w-100p caption text-gray-13  &#34;&gt;&lt;strong&gt;Loki logging stack&lt;/strong&gt;&lt;/figcaption&gt;&lt;/a&gt;&lt;/figure&gt;
&lt;p&gt;A typical Loki-based logging stack consists of 3 components:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Agent&lt;/strong&gt; - An agent or client, for example Promtail, which is distributed with Loki, or the Grafana Agent. The agent scrapes logs, turns the logs into streams by adding labels, and pushes the streams to Loki through an HTTP API.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Loki&lt;/strong&gt; - The main server, responsible for ingesting and storing logs and processing queries. It can be deployed in three different configurations, for more information see &lt;a href=&#34;../deployment-modes/&#34;&gt;deployment modes&lt;/a&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;a href=&#34;https://github.com/grafana/grafana&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Grafana&lt;/a&gt;&lt;/strong&gt; for querying and displaying log data. You can also query logs from the command line, using &lt;a href=&#34;../../query/logcli/&#34;&gt;LogCLI&lt;/a&gt; or using the Loki API directly.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;loki-features&#34;&gt;Loki features&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Scalability&lt;/strong&gt; - Loki is designed for scalability, and can scale from as small as running on a Raspberry Pi to ingesting petabytes a day.
In its most common deployment, “simple scalable mode”, Loki decouples requests into separate read and write paths, so that you can independently scale them, which leads to flexible large-scale installations that can quickly adapt to meet your workload at any given time.
If needed, each of Loki&amp;rsquo;s components can also be run as microservices designed to run natively within Kubernetes.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Multi-tenancy&lt;/strong&gt; - Loki allows multiple tenants to share a single Loki instance. With multi-tenancy, the data and requests of each tenant is completely isolated from the others.
Multi-tenancy is &lt;a href=&#34;../../operations/multi-tenancy/&#34;&gt;configured&lt;/a&gt; by assigning a tenant ID in the agent.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Third-party integrations&lt;/strong&gt; - Several third-party agents (clients) have support for Loki, via plugins. This lets you keep your existing observability setup while also shipping logs to Loki.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Efficient storage&lt;/strong&gt; - Loki stores log data in highly compressed chunks.
Similarly, the Loki index, because it indexes only the set of labels, is significantly smaller than other log aggregation tools.
By leveraging object storage as the only data storage mechanism, Loki inherits the reliability and stability of the underlying object store. It also capitalizes on both the cost efficiency and operational simplicity of object storage over other storage mechanisms like locally attached solid state drives (SSD) and hard disk drives (HDD).&lt;br /&gt;
The compressed chunks, smaller index, and use of low-cost object storage, make Loki less expensive to operate.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;LogQL, Loki&amp;rsquo;s query language&lt;/strong&gt; - &lt;a href=&#34;../../query/&#34;&gt;LogQL&lt;/a&gt; is the query language for Loki.  Users who are already familiar with the Prometheus query language, &lt;a href=&#34;https://prometheus.io/docs/prometheus/latest/querying/basics/&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;PromQL&lt;/a&gt;, will find LogQL familiar and flexible for generating queries against the logs.
The language also facilitates the generation of metrics from log data,
a powerful feature that goes well beyond log aggregation.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Alerting&lt;/strong&gt; - Loki includes a component called the &lt;a href=&#34;../../alert/&#34;&gt;ruler&lt;/a&gt;, which can continually evaluate queries against your logs, and perform an action based on the result. This allows you to monitor your logs for anomalies or events. Loki integrates with &lt;a href=&#34;https://prometheus.io/docs/alerting/latest/alertmanager/&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Prometheus Alertmanager&lt;/a&gt;, or the &lt;a href=&#34;/docs/grafana/latest/alerting/&#34;&gt;alert manager&lt;/a&gt; within Grafana.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Grafana integration&lt;/strong&gt; - Loki integrates with Grafana, Mimir, and Tempo, providing a complete observability stack, and seamless correlation between logs, metrics and traces.&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
]]></content><description>&lt;h1 id="loki-overview">Loki overview&lt;/h1>
&lt;p>Loki is a horizontally-scalable, highly-available, multi-tenant log aggregation system inspired by &lt;a href="https://prometheus.io/" target="_blank" rel="noopener noreferrer">Prometheus&lt;/a>. Loki differs from Prometheus by focusing on logs instead of metrics, and collecting logs via push, instead of pull.&lt;/p></description></item><item><title>Loki architecture</title><link>https://grafana.com/docs/loki/v2.9.x/get-started/architecture/</link><pubDate>Thu, 10 Apr 2025 12:15:54 +0000</pubDate><guid>https://grafana.com/docs/loki/v2.9.x/get-started/architecture/</guid><content><![CDATA[&lt;h1 id=&#34;loki-architecture&#34;&gt;Loki architecture&lt;/h1&gt;
&lt;h2 id=&#34;multi-tenancy&#34;&gt;Multi-tenancy&lt;/h2&gt;
&lt;p&gt;All data, both in memory and in long-term storage, may be partitioned by a
tenant ID, pulled from the &lt;code&gt;X-Scope-OrgID&lt;/code&gt; HTTP header in the request when Grafana Loki
is running in multi-tenant mode. When Loki is &lt;strong&gt;not&lt;/strong&gt; in multi-tenant mode, the
header is ignored and the tenant ID is set to &amp;ldquo;fake&amp;rdquo;, which will appear in the
index and in stored chunks.&lt;/p&gt;
&lt;h2 id=&#34;chunk-format&#34;&gt;Chunk Format&lt;/h2&gt;

&lt;div class=&#34;code-snippet code-snippet__mini&#34;&gt;&lt;div class=&#34;lang-toolbar__mini&#34;&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet code-snippet__border&#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-none&#34;&gt;  ----------------------------------------------------------------------------
  |                        |                       |                         |
  |     MagicNumber(4b)    |     version(1b)       |      encoding (1b)      |
  |                        |                       |                         |
  ----------------------------------------------------------------------------
  |                      #structuredMetadata (uvarint)                       |
  ----------------------------------------------------------------------------
  |      len(label-1) (uvarint)      |          label-1 (bytes)              |
  ----------------------------------------------------------------------------
  |      len(label-2) (uvarint)      |          label-2 (bytes)              |
  ----------------------------------------------------------------------------
  |      len(label-n) (uvarint)      |          label-n (bytes)              |
  ----------------------------------------------------------------------------
  |                      checksum(from #structuredMetadata)                  |
  ----------------------------------------------------------------------------
  |           block-1 bytes          |           checksum (4b)               |
  ----------------------------------------------------------------------------
  |           block-2 bytes          |           checksum (4b)               |
  ----------------------------------------------------------------------------
  |           block-n bytes          |           checksum (4b)               |
  ----------------------------------------------------------------------------
  |                           #blocks (uvarint)                              |
  ----------------------------------------------------------------------------
  | #entries(uvarint) | mint, maxt (varint)  | offset, len (uvarint)         |
  ----------------------------------------------------------------------------
  | #entries(uvarint) | mint, maxt (varint)  | offset, len (uvarint)         |
  ----------------------------------------------------------------------------
  | #entries(uvarint) | mint, maxt (varint)  | offset, len (uvarint)         |
  ----------------------------------------------------------------------------
  | #entries(uvarint) | mint, maxt (varint)  | offset, len (uvarint)         |
  ----------------------------------------------------------------------------
  |                          checksum(from #blocks)                          |
  ----------------------------------------------------------------------------
  | #structuredMetadata len (uvarint) | #structuredMetadata offset (uvarint) |
  ----------------------------------------------------------------------------
  |     #blocks len (uvarint)         |       #blocks offset (uvarint)      |
  ----------------------------------------------------------------------------&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;code&gt;mint&lt;/code&gt; and &lt;code&gt;maxt&lt;/code&gt; describe the minimum and maximum Unix nanosecond timestamp,
respectively.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;structuredMetadata&lt;/code&gt; section stores non-repeated strings. It is used to store label names and label values from
&lt;a href=&#34;../labels/structured-metadata/&#34;&gt;structured metadata&lt;/a&gt;.
Note that the labels strings and lengths within the &lt;code&gt;structuredMetadata&lt;/code&gt; section are stored compressed.&lt;/p&gt;
&lt;h3 id=&#34;block-format&#34;&gt;Block Format&lt;/h3&gt;
&lt;p&gt;A block is comprised of a series of entries, each of which is an individual log
line.&lt;/p&gt;
&lt;p&gt;Note that the bytes of a block are stored compressed. The following is their form when uncompressed:&lt;/p&gt;

&lt;div class=&#34;code-snippet code-snippet__mini&#34;&gt;&lt;div class=&#34;lang-toolbar__mini&#34;&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet code-snippet__border&#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-none&#34;&gt;-----------------------------------------------------------------------------------------------------------------------------------------------
|  ts (varint)  |  len (uvarint)  |  log-1 bytes  |  len(from #symbols)  |  #symbols (uvarint)  |  symbol-1 (uvarint)  | symbol-n*2 (uvarint) |
-----------------------------------------------------------------------------------------------------------------------------------------------
|  ts (varint)  |  len (uvarint)  |  log-2 bytes  |  len(from #symbols)  |  #symbols (uvarint)  |  symbol-1 (uvarint)  | symbol-n*2 (uvarint) |
-----------------------------------------------------------------------------------------------------------------------------------------------
|  ts (varint)  |  len (uvarint)  |  log-3 bytes  |  len(from #symbols)  |  #symbols (uvarint)  |  symbol-1 (uvarint)  | symbol-n*2 (uvarint) |
-----------------------------------------------------------------------------------------------------------------------------------------------
|  ts (varint)  |  len (uvarint)  |  log-n bytes  |  len(from #symbols)  |  #symbols (uvarint)  |  symbol-1 (uvarint)  | symbol-n*2 (uvarint) |
-----------------------------------------------------------------------------------------------------------------------------------------------&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;code&gt;ts&lt;/code&gt; is the Unix nanosecond timestamp of the logs, while len is the length in
bytes of the log entry.&lt;/p&gt;
&lt;p&gt;Symbols store references to the actual strings containing label names and values in the
&lt;code&gt;structuredMetadata&lt;/code&gt; section of the chunk.&lt;/p&gt;
&lt;h2 id=&#34;storage&#34;&gt;Storage&lt;/h2&gt;
&lt;h3 id=&#34;single-store&#34;&gt;Single Store&lt;/h3&gt;
&lt;p&gt;Loki stores all data in a single object storage backend. This mode of operation became generally available with Loki 2.0 and is fast, cost-effective, and simple, not to mention where all current and future development lies. This mode uses an adapter called &lt;a href=&#34;../../operations/storage/boltdb-shipper/&#34;&gt;&lt;code&gt;boltdb_shipper&lt;/code&gt;&lt;/a&gt; to store the &lt;code&gt;index&lt;/code&gt; in object storage (the same way we store &lt;code&gt;chunks&lt;/code&gt;).&lt;/p&gt;
&lt;h3 id=&#34;deprecated-multi-store&#34;&gt;Deprecated: Multi-store&lt;/h3&gt;
&lt;p&gt;The &lt;strong&gt;chunk store&lt;/strong&gt; is Loki&amp;rsquo;s long-term data store, designed to support
interactive querying and sustained writing without the need for background
maintenance tasks. It consists of:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;An index for the chunks. This index can be backed by:
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://aws.amazon.com/dynamodb&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Amazon DynamoDB&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://cloud.google.com/bigtable&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Google Bigtable&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://cassandra.apache.org&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Apache Cassandra&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;A key-value (KV) store for the chunk data itself, which can be:
&lt;ul&gt;
&lt;li&gt;&lt;a href=&#34;https://aws.amazon.com/dynamodb&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Amazon DynamoDB&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://cloud.google.com/bigtable&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Google Bigtable&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://cassandra.apache.org&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Apache Cassandra&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://aws.amazon.com/s3&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Amazon S3&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&#34;https://cloud.google.com/storage/&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Google Cloud Storage&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;blockquote&gt;
&lt;p&gt;Unlike the other core components of Loki, the chunk store is not a separate
service, job, or process, but rather a library embedded in the two services
that need to access Loki data: the &lt;a href=&#34;../components/#ingester&#34;&gt;ingester&lt;/a&gt; and &lt;a href=&#34;../components/#querier&#34;&gt;querier&lt;/a&gt;.&lt;/p&gt;&lt;/blockquote&gt;
&lt;p&gt;The chunk store relies on a unified interface to the
&amp;ldquo;&lt;a href=&#34;https://en.wikipedia.org/wiki/NoSQL&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;NoSQL&lt;/a&gt;&amp;rdquo; stores (DynamoDB, Bigtable, and
Cassandra) that can be used to back the chunk store index. This interface
assumes that the index is a collection of entries keyed by:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;A &lt;strong&gt;hash key&lt;/strong&gt;. This is required for &lt;em&gt;all&lt;/em&gt; reads and writes.&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;range key&lt;/strong&gt;. This is required for writes and can be omitted for reads,
which can be queried by prefix or range.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The interface works somewhat differently across the supported databases:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;DynamoDB supports range and hash keys natively. Index entries are thus
modelled directly as DynamoDB entries, with the hash key as the distribution
key and the range as the DynamoDB range key.&lt;/li&gt;
&lt;li&gt;For Bigtable and Cassandra, index entries are modelled as individual column
values. The hash key becomes the row key and the range key becomes the column
key.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;A set of schemas are used to map the matchers and label sets used on reads and
writes to the chunk store into appropriate operations on the index. Schemas have
been added as Loki has evolved, mainly in an attempt to better load balance
writes and improve query performance.&lt;/p&gt;
&lt;h2 id=&#34;read-path&#34;&gt;Read Path&lt;/h2&gt;
&lt;p&gt;To summarize, the read path works as follows:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The querier receives an HTTP/1 request for data.&lt;/li&gt;
&lt;li&gt;The querier passes the query to all ingesters for in-memory data.&lt;/li&gt;
&lt;li&gt;The ingesters receive the read request and return data matching the query, if
any.&lt;/li&gt;
&lt;li&gt;The querier lazily loads data from the backing store and runs the query
against it if no ingesters returned data.&lt;/li&gt;
&lt;li&gt;The querier iterates over all received data and deduplicates, returning a
final set of data over the HTTP/1 connection.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&#34;write-path&#34;&gt;Write Path&lt;/h2&gt;
&lt;p&gt;&lt;img
  class=&#34;lazyload d-inline-block&#34;
  data-src=&#34;../chunks_diagram.png&#34;
  alt=&#34;chunk_diagram&#34;/&gt;&lt;/p&gt;
&lt;p&gt;To summarize, the write path works as follows:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The distributor receives an HTTP/1 request to store data for streams.&lt;/li&gt;
&lt;li&gt;Each stream is hashed using the hash ring.&lt;/li&gt;
&lt;li&gt;The distributor sends each stream to the appropriate ingesters and their
replicas (based on the configured replication factor).&lt;/li&gt;
&lt;li&gt;Each ingester will create a chunk or append to an existing chunk for the
stream&amp;rsquo;s data. A chunk is unique per tenant and per labelset.&lt;/li&gt;
&lt;li&gt;The distributor responds with a success code over the HTTP/1 connection.&lt;/li&gt;
&lt;/ol&gt;
]]></content><description>&lt;h1 id="loki-architecture">Loki architecture&lt;/h1>
&lt;h2 id="multi-tenancy">Multi-tenancy&lt;/h2>
&lt;p>All data, both in memory and in long-term storage, may be partitioned by a
tenant ID, pulled from the &lt;code>X-Scope-OrgID&lt;/code> HTTP header in the request when Grafana Loki
is running in multi-tenant mode. When Loki is &lt;strong>not&lt;/strong> in multi-tenant mode, the
header is ignored and the tenant ID is set to &amp;ldquo;fake&amp;rdquo;, which will appear in the
index and in stored chunks.&lt;/p></description></item><item><title>Loki deployment modes</title><link>https://grafana.com/docs/loki/v2.9.x/get-started/deployment-modes/</link><pubDate>Thu, 10 Apr 2025 12:15:54 +0000</pubDate><guid>https://grafana.com/docs/loki/v2.9.x/get-started/deployment-modes/</guid><content><![CDATA[&lt;h1 id=&#34;loki-deployment-modes&#34;&gt;Loki deployment modes&lt;/h1&gt;
&lt;p&gt;Loki is a distributed system consisting of many microservices. It also has a unique build model where all of those microservices exist within the same binary.&lt;/p&gt;
&lt;p&gt;You can configure the behavior of the single binary with the &lt;code&gt;-target&lt;/code&gt; command-line flag to specify which microservices will run on startup. You can further configure each of the components in the &lt;code&gt;loki.yaml&lt;/code&gt; file.&lt;/p&gt;
&lt;p&gt;Because Loki decouples the data it stores from the software which ingests and queries it, you can easily redeploy a cluster under a different mode as your needs change, with minimal or no configuration changes.&lt;/p&gt;
&lt;h2 id=&#34;simple-scalable&#34;&gt;Simple Scalable&lt;/h2&gt;
&lt;p&gt;The simple scalable deployment mode, is the preferred way to deploy Loki for most installations. The simple scalable deployment is the default configuration installed by the &lt;a href=&#34;../../setup/install/helm/&#34;&gt;Loki Helm Chart&lt;/a&gt;. This deployment mode is the easiest way to deploy Loki at scale. It strikes a balance between deploying in &lt;a href=&#34;#monolithic-mode&#34;&gt;monolithic mode&lt;/a&gt; or deploying each component as a &lt;a href=&#34;#microservices-mode&#34;&gt;separate microservice&lt;/a&gt;.&lt;/p&gt;


&lt;div class=&#34;admonition admonition-note&#34;&gt;&lt;blockquote&gt;&lt;p class=&#34;title text-uppercase&#34;&gt;Note&lt;/p&gt;&lt;p&gt;This deployment mode is sometimes referred to by the acronym SSD for simple scalable deployment, not to be confused with solid state drives. Loki uses an object store.&lt;/p&gt;&lt;/blockquote&gt;&lt;/div&gt;

&lt;p&gt;Loki’s simple scalable deployment mode separates execution paths into read, write, and backend targets. These targets can be scaled independently, letting you customize your Loki deployment to meet your business needs for log ingestion and log query so that your infrastructure costs better match how you use Loki.&lt;/p&gt;
&lt;p&gt;The simple scalable deployment mode can scale up to a few TBs of logs per day, however if you go much beyond this, the microservices mode will be a better choice for most users.&lt;/p&gt;
&lt;p&gt;&lt;img
  class=&#34;lazyload d-inline-block&#34;
  data-src=&#34;../scalable-monolithic-mode.png&#34;
  alt=&#34;Simple scalable mode diagram&#34;/&gt;&lt;/p&gt;
&lt;p&gt;The three execution paths in simple scalable mode are each activated by appending the following arguments to Loki on startup:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;-target=write&lt;/code&gt; - The write target is stateful and is controlled by a Kubernetes StatefulSet. It contains the following components:
&amp;ndash; Distributor
&amp;ndash; Ingester&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-target=read&lt;/code&gt; - The read target is stateless and can be run as a Kubernetes Deployment that can be scaled automatically (Note that in the official helm chart it is currently deployed as a stateful set). It contains the following components:
&amp;ndash; Query front end
&amp;ndash; Queriers&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-target=backend&lt;/code&gt; - The backend target is stateful, and is controlled by a Kubernetes StatefulSet. Contains the following components:
&amp;ndash; Compactor
&amp;ndash; Index gateways
&amp;ndash; Query scheduler
&amp;ndash; Ruler&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The simple scalable deployment mode requires a reverse proxy to be deployed in front of Loki, to direct client API requests to either the read or write nodes. The Loki Helm chart includes a default reverse proxy configuration, using Nginx.&lt;/p&gt;
&lt;h2 id=&#34;monolithic-mode&#34;&gt;Monolithic mode&lt;/h2&gt;
&lt;p&gt;The simplest mode of operation is the monolithic deployment mode. You enable monolithic mode by setting the &lt;code&gt;-target=all&lt;/code&gt; command line parameter. This mode runs all of Loki’s microservice components inside a single process as a single binary or Docker image.&lt;/p&gt;
&lt;p&gt;&lt;img
  class=&#34;lazyload d-inline-block&#34;
  data-src=&#34;../monolithic-mode.png&#34;
  alt=&#34;monolithic mode diagram&#34;/&gt;&lt;/p&gt;
&lt;p&gt;Monolithic mode is useful for getting started quickly to experiment with Loki, as well as for small read/write volumes of up to approximately 20GB per day.&lt;/p&gt;
&lt;p&gt;You can horizontally scale a monolithic mode deployment to more instances by using a shared object store, and by configuring the 
    &lt;a href=&#34;/docs/loki/v2.9.x/configure/#common&#34;&gt;&lt;code&gt;ring&lt;/code&gt; section&lt;/a&gt; of the &lt;code&gt;loki.yaml&lt;/code&gt; file to share state between all instances, but the recommendation is to use simple scalable mode if you need to scale your deployment.&lt;/p&gt;
&lt;p&gt;You can configure high availability by running two Loki instances using &lt;code&gt;memberlist_config&lt;/code&gt; configuration and a shared object store and setting the &lt;code&gt;replication_factor&lt;/code&gt; to &lt;code&gt;3&lt;/code&gt;. You route traffic to all the Loki instances in a round robin fashion.&lt;/p&gt;
&lt;p&gt;Query parallelization is limited by the number of instances and the setting &lt;code&gt;max_query_parallelism&lt;/code&gt; which is defined in the &lt;code&gt;loki.yaml&lt;/code&gt; file.&lt;/p&gt;
&lt;h2 id=&#34;microservices-mode&#34;&gt;Microservices mode&lt;/h2&gt;
&lt;p&gt;The microservices deployment mode runs components of Loki as distinct processes. Each process is invoked specifying its &lt;code&gt;target&lt;/code&gt;:
For release 2.9 the components are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Cache Generation Loader&lt;/li&gt;
&lt;li&gt;Compactor&lt;/li&gt;
&lt;li&gt;Distributor&lt;/li&gt;
&lt;li&gt;Index-gateway&lt;/li&gt;
&lt;li&gt;Ingester&lt;/li&gt;
&lt;li&gt;Ingester-Querier&lt;/li&gt;
&lt;li&gt;Overrides Exporter&lt;/li&gt;
&lt;li&gt;Querier&lt;/li&gt;
&lt;li&gt;Query-frontend&lt;/li&gt;
&lt;li&gt;Query-scheduler&lt;/li&gt;
&lt;li&gt;Ruler&lt;/li&gt;
&lt;li&gt;Table Manager (deprecated)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;TIP: You can see the complete list of targets for your version of Loki by running Loki with the flag &lt;code&gt;-list-targets&lt;/code&gt;, for example:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;Bash&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-bash&#34;&gt;docker run docker.io/grafana/loki:2.9.4 -config.file=/etc/loki/local-config.yaml -list-targets&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;&lt;img
  class=&#34;lazyload d-inline-block&#34;
  data-src=&#34;../microservices-mode.png&#34;
  alt=&#34;Microservices mode diagram&#34;/&gt;&lt;/p&gt;
&lt;p&gt;Running components as individual microservices provides more granularity, letting you scale each component as individual microservices, to better match your specific use case.&lt;/p&gt;
&lt;p&gt;Microservices mode deployments can be more efficient Loki installations. However, they are also the most complex to set up and maintain.&lt;/p&gt;
&lt;p&gt;Microservices mode is only recommended for very large Loki clusters or for operators who need more precise control over scaling and cluster operations.&lt;/p&gt;
&lt;p&gt;Microservices mode is designed for Kubernetes deployments.
A &lt;a href=&#34;https://github.com/grafana/helm-charts/tree/main/charts/loki-distributed&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;community-supported Helm chart&lt;/a&gt; is available for deploying Loki in microservices mode.&lt;/p&gt;
]]></content><description>&lt;h1 id="loki-deployment-modes">Loki deployment modes&lt;/h1>
&lt;p>Loki is a distributed system consisting of many microservices. It also has a unique build model where all of those microservices exist within the same binary.&lt;/p></description></item><item><title>Loki components</title><link>https://grafana.com/docs/loki/v2.9.x/get-started/components/</link><pubDate>Thu, 10 Apr 2025 12:15:54 +0000</pubDate><guid>https://grafana.com/docs/loki/v2.9.x/get-started/components/</guid><content><![CDATA[&lt;h1 id=&#34;loki-components&#34;&gt;Loki components&lt;/h1&gt;
&lt;p&gt;&lt;img
  class=&#34;lazyload d-inline-block&#34;
  data-src=&#34;../loki_architecture_components.svg&#34;
  alt=&#34;components_diagram&#34;/&gt;&lt;/p&gt;
&lt;h2 id=&#34;distributor&#34;&gt;Distributor&lt;/h2&gt;
&lt;p&gt;The &lt;strong&gt;distributor&lt;/strong&gt; service is responsible for handling incoming streams by
clients. It&amp;rsquo;s the first stop in the write path for log data. Once the
distributor receives a set of streams, each stream is validated for correctness
and to ensure that it is within the configured tenant (or global) limits. Valid
chunks are then split into batches and sent to multiple &lt;a href=&#34;#ingester&#34;&gt;ingesters&lt;/a&gt;
in parallel.&lt;/p&gt;
&lt;p&gt;It is important that a load balancer sits in front of the distributor in order to properly balance traffic to them.&lt;/p&gt;
&lt;p&gt;The distributor is a stateless component. This makes it easy to scale and offload as much work as possible from the ingesters, which are the most critical component on the write path. The ability to independently scale these validation operations mean that Loki can also protect itself against denial of service attacks (either malicious or not) that could otherwise overload the ingesters. They act like the bouncer at the front door, ensuring everyone is appropriately dressed and has an invitation. It also allows us to fan-out writes according to our replication factor.&lt;/p&gt;
&lt;h3 id=&#34;validation&#34;&gt;Validation&lt;/h3&gt;
&lt;p&gt;The first step the distributor takes is to ensure that all incoming data is according to specification. This includes things like checking that the labels are valid Prometheus labels as well as ensuring the timestamps aren&amp;rsquo;t too old or too new or the log lines aren&amp;rsquo;t too long.&lt;/p&gt;
&lt;h3 id=&#34;preprocessing&#34;&gt;Preprocessing&lt;/h3&gt;
&lt;p&gt;Currently the only way the distributor mutates incoming data is by normalizing labels. What this means is making &lt;code&gt;{foo=&amp;quot;bar&amp;quot;, bazz=&amp;quot;buzz&amp;quot;}&lt;/code&gt; equivalent to &lt;code&gt;{bazz=&amp;quot;buzz&amp;quot;, foo=&amp;quot;bar&amp;quot;}&lt;/code&gt;, or in other words, sorting the labels. This allows Loki to cache and hash them deterministically.&lt;/p&gt;
&lt;h3 id=&#34;rate-limiting&#34;&gt;Rate limiting&lt;/h3&gt;
&lt;p&gt;The distributor can also rate limit incoming logs based on the maximum per-tenant bitrate. It does this by checking a per tenant limit and dividing it by the current number of distributors. This allows the rate limit to be specified per tenant at the cluster level and enables us to scale the distributors up or down and have the per-distributor limit adjust accordingly. For instance, say we have 10 distributors and tenant A has a 10MB rate limit. Each distributor will allow up to 1MB/second before limiting. Now, say another large tenant joins the cluster and we need to spin up 10 more distributors. The now 20 distributors will adjust their rate limits for tenant A to &lt;code&gt;(10MB / 20 distributors) = 500KB/s&lt;/code&gt;! This is how global limits allow much simpler and safer operation of the Loki cluster.&lt;/p&gt;


&lt;div class=&#34;admonition admonition-note&#34;&gt;&lt;blockquote&gt;&lt;p class=&#34;title text-uppercase&#34;&gt;Note&lt;/p&gt;&lt;p&gt;The distributor uses the &lt;code&gt;ring&lt;/code&gt; component under the hood to register itself amongst its peers and get the total number of active distributors. This is a different &amp;ldquo;key&amp;rdquo; than the ingesters use in the ring and comes from the distributor&amp;rsquo;s own 
    &lt;a href=&#34;/docs/loki/v2.9.x/configure/#distributor&#34;&gt;ring configuration&lt;/a&gt;.&lt;/p&gt;&lt;/blockquote&gt;&lt;/div&gt;

&lt;h3 id=&#34;forwarding&#34;&gt;Forwarding&lt;/h3&gt;
&lt;p&gt;Once the distributor has performed all of its validation duties, it forwards data to the ingester component which is ultimately responsible for acknowledging the write.&lt;/p&gt;
&lt;h4 id=&#34;replication-factor&#34;&gt;Replication factor&lt;/h4&gt;
&lt;p&gt;In order to mitigate the chance of &lt;em&gt;losing&lt;/em&gt; data on any single ingester, the distributor will forward writes to a &lt;em&gt;replication_factor&lt;/em&gt; of them. Generally, this is &lt;code&gt;3&lt;/code&gt;. Replication allows for ingester restarts and rollouts without failing writes and adds additional protection from data loss for some scenarios. Loosely, for each label set (called a &lt;em&gt;stream&lt;/em&gt;) that is pushed to a distributor, it will hash the labels and use the resulting value to look up &lt;code&gt;replication_factor&lt;/code&gt; ingesters in the &lt;code&gt;ring&lt;/code&gt; (which is a subcomponent that exposes a &lt;a href=&#34;https://en.wikipedia.org/wiki/Distributed_hash_table&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;distributed hash table&lt;/a&gt;). It will then try to write the same data to all of them. This will error if less than a &lt;em&gt;quorum&lt;/em&gt; of writes succeed. A quorum is defined as &lt;code&gt;floor(replication_factor / 2) &#43; 1&lt;/code&gt;. So, for our &lt;code&gt;replication_factor&lt;/code&gt; of &lt;code&gt;3&lt;/code&gt;, we require that two writes succeed. If less than two writes succeed, the distributor returns an error and the write can be retried.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Caveat: If a write is acknowledged by 2 out of 3 ingesters, we can tolerate the loss of one ingester but not two, as this would result in data loss.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;Replication factor isn&amp;rsquo;t the only thing that prevents data loss, though, and arguably these days its main purpose is to allow writes to continue uninterrupted during rollouts &amp;amp; restarts. The &lt;code&gt;ingester&lt;/code&gt; component now includes a &lt;a href=&#34;https://en.wikipedia.org/wiki/Write-ahead_logging&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;write ahead log&lt;/a&gt; which persists incoming writes to disk to ensure they&amp;rsquo;re not lost as long as the disk isn&amp;rsquo;t corrupted. The complementary nature of replication factor and WAL ensures data isn&amp;rsquo;t lost unless there are significant failures in both mechanisms (i.e. multiple ingesters die and lose/corrupt their disks).&lt;/p&gt;
&lt;h3 id=&#34;hashing&#34;&gt;Hashing&lt;/h3&gt;
&lt;p&gt;Distributors use consistent hashing in conjunction with a configurable
replication factor to determine which instances of the ingester service should
receive a given stream.&lt;/p&gt;
&lt;p&gt;A stream is a set of logs associated to a tenant and a unique labelset. The
stream is hashed using both the tenant ID and the labelset and then the hash is
used to find the ingesters to send the stream to.&lt;/p&gt;
&lt;p&gt;A hash ring stored in &lt;a href=&#34;https://www.consul.io&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Consul&lt;/a&gt; is used to achieve
consistent hashing; all &lt;a href=&#34;#ingester&#34;&gt;ingesters&lt;/a&gt; register themselves into the hash
ring with a set of tokens they own. Each token is a random unsigned 32-bit
number. Along with a set of tokens, ingesters register their state into the
hash ring. The state JOINING, and ACTIVE may all receive write requests, while
ACTIVE and LEAVING ingesters may receive read requests. When doing a hash
lookup, distributors only use tokens for ingesters who are in the appropriate
state for the request.&lt;/p&gt;
&lt;p&gt;To do the hash lookup, distributors find the smallest appropriate token whose
value is larger than the hash of the stream. When the replication factor is
larger than 1, the next subsequent tokens (clockwise in the ring) that belong to
different ingesters will also be included in the result.&lt;/p&gt;
&lt;p&gt;The effect of this hash set up is that each token that an ingester owns is
responsible for a range of hashes. If there are three tokens with values 0, 25,
and 50, then a hash of 3 would be given to the ingester that owns the token 25;
the ingester owning token 25 is responsible for the hash range of 1-25.&lt;/p&gt;
&lt;h3 id=&#34;quorum-consistency&#34;&gt;Quorum consistency&lt;/h3&gt;
&lt;p&gt;Since all distributors share access to the same hash ring, write requests can be
sent to any distributor.&lt;/p&gt;
&lt;p&gt;To ensure consistent query results, Loki uses
&lt;a href=&#34;https://www.cs.princeton.edu/courses/archive/fall15/cos518/studpres/dynamo.pdf&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Dynamo-style&lt;/a&gt;
quorum consistency on reads and writes. This means that the distributor will wait
for a positive response of at least one half plus one of the ingesters to send
the sample to before responding to the client that initiated the send.&lt;/p&gt;
&lt;h2 id=&#34;ingester&#34;&gt;Ingester&lt;/h2&gt;
&lt;p&gt;The &lt;strong&gt;ingester&lt;/strong&gt; service is responsible for writing log data to long-term
storage backends (DynamoDB, S3, Cassandra, etc.) on the write path and returning
log data for in-memory queries on the read path.&lt;/p&gt;
&lt;p&gt;Ingesters contain a &lt;em&gt;lifecycler&lt;/em&gt; which manages the lifecycle of an ingester in
the hash ring. Each ingester has a state of either &lt;code&gt;PENDING&lt;/code&gt;, &lt;code&gt;JOINING&lt;/code&gt;,
&lt;code&gt;ACTIVE&lt;/code&gt;, &lt;code&gt;LEAVING&lt;/code&gt;, or &lt;code&gt;UNHEALTHY&lt;/code&gt;:&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Deprecated: the WAL (write ahead log) supersedes this feature&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;PENDING&lt;/code&gt; is an Ingester&amp;rsquo;s state when it is waiting for a handoff from
another ingester that is &lt;code&gt;LEAVING&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;JOINING&lt;/code&gt; is an Ingester&amp;rsquo;s state when it is currently inserting its tokens
into the ring and initializing itself. It may receive write requests for
tokens it owns.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;ACTIVE&lt;/code&gt; is an Ingester&amp;rsquo;s state when it is fully initialized. It may receive
both write and read requests for tokens it owns.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;LEAVING&lt;/code&gt; is an Ingester&amp;rsquo;s state when it is shutting down. It may receive
read requests for data it still has in memory.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;code&gt;UNHEALTHY&lt;/code&gt; is an Ingester&amp;rsquo;s state when it has failed to heartbeat to
Consul. &lt;code&gt;UNHEALTHY&lt;/code&gt; is set by the distributor when it periodically checks the ring.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Each log stream that an ingester receives is built up into a set of many
&amp;ldquo;chunks&amp;rdquo; in memory and flushed to the backing storage backend at a configurable
interval.&lt;/p&gt;
&lt;p&gt;Chunks are compressed and marked as read-only when:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;The current chunk has reached capacity (a configurable value).&lt;/li&gt;
&lt;li&gt;Too much time has passed without the current chunk being updated&lt;/li&gt;
&lt;li&gt;A flush occurs.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Whenever a chunk is compressed and marked as read-only, a writable chunk takes
its place.&lt;/p&gt;
&lt;p&gt;If an ingester process crashes or exits abruptly, all the data that has not yet
been flushed will be lost. Loki is usually configured to replicate multiple
replicas (usually 3) of each log to mitigate this risk.&lt;/p&gt;
&lt;p&gt;When a flush occurs to a persistent storage provider, the chunk is hashed based
on its tenant, labels, and contents. This means that multiple ingesters with the
same copy of data will not write the same data to the backing store twice, but
if any write failed to one of the replicas, multiple differing chunk objects
will be created in the backing store. See &lt;a href=&#34;#querier&#34;&gt;Querier&lt;/a&gt; for how data is
deduplicated.&lt;/p&gt;
&lt;h3 id=&#34;timestamp-ordering&#34;&gt;Timestamp Ordering&lt;/h3&gt;
&lt;p&gt;Loki is configured to 
    &lt;a href=&#34;/docs/loki/v2.9.x/configure/#accept-out-of-order-writes&#34;&gt;accept out-of-order writes&lt;/a&gt; by default.&lt;/p&gt;
&lt;p&gt;When not configured to accept out-of-order writes, the ingester validates that ingested log lines are in order. When an
ingester receives a log line that doesn&amp;rsquo;t follow the expected order, the line
is rejected and an error is returned to the user.&lt;/p&gt;
&lt;p&gt;The ingester validates that log lines are received in
timestamp-ascending order. Each log has a timestamp that occurs at a later
time than the log before it. When the ingester receives a log that does not
follow this order, the log line is rejected and an error is returned.&lt;/p&gt;
&lt;p&gt;Logs from each unique set of labels are built up into &amp;ldquo;chunks&amp;rdquo; in memory and
then flushed to the backing storage backend.&lt;/p&gt;
&lt;p&gt;If an ingester process crashes or exits abruptly, all the data that has not yet
been flushed could be lost. Loki is usually configured with a &lt;a href=&#34;../../operations/storage/wal/&#34;&gt;Write Ahead Log&lt;/a&gt; which can be &lt;em&gt;replayed&lt;/em&gt; on restart as well as with a &lt;code&gt;replication_factor&lt;/code&gt; (usually 3) of each log to mitigate this risk.&lt;/p&gt;
&lt;p&gt;When not configured to accept out-of-order writes,
all lines pushed to Loki for a given stream (unique combination of
labels) must have a newer timestamp than the line received before it. There are,
however, two cases for handling logs for the same stream with identical
nanosecond timestamps:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;If the incoming line exactly matches the previously received line (matching
both the previous timestamp and log text), the incoming line will be treated
as an exact duplicate and ignored.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;If the incoming line has the same timestamp as the previous line but
different content, the log line is accepted. This means it is possible to
have two different log lines for the same timestamp.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&#34;handoff---deprecated-in-favor-of-the-wal&#34;&gt;Handoff - Deprecated in favor of the &lt;a href=&#34;../../operations/storage/wal/&#34;&gt;WAL&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;By default, when an ingester is shutting down and tries to leave the hash ring,
it will wait to see if a new ingester tries to enter before flushing and will
try to initiate a handoff. The handoff will transfer all of the tokens and
in-memory chunks owned by the leaving ingester to the new ingester.&lt;/p&gt;
&lt;p&gt;Before joining the hash ring, ingesters will wait in &lt;code&gt;PENDING&lt;/code&gt; state for a
handoff to occur. After a configurable timeout, ingesters in the &lt;code&gt;PENDING&lt;/code&gt; state
that have not received a transfer will join the ring normally, inserting a new
set of tokens.&lt;/p&gt;
&lt;p&gt;This process is used to avoid flushing all chunks when shutting down, which is a
slow process.&lt;/p&gt;
&lt;h3 id=&#34;filesystem-support&#34;&gt;Filesystem Support&lt;/h3&gt;
&lt;p&gt;While ingesters do support writing to the filesystem through BoltDB, this only
works in single-process mode as &lt;a href=&#34;#querier&#34;&gt;queriers&lt;/a&gt; need access to the same
back-end store and BoltDB only allows one process to have a lock on the DB at a
given time.&lt;/p&gt;
&lt;h2 id=&#34;query-frontend&#34;&gt;Query frontend&lt;/h2&gt;
&lt;p&gt;The &lt;strong&gt;query frontend&lt;/strong&gt; is an &lt;strong&gt;optional service&lt;/strong&gt; providing the querier&amp;rsquo;s API endpoints and can be used to accelerate the read path. When the query frontend is in place, incoming query requests should be directed to the query frontend instead of the queriers. The querier service will be still required within the cluster, in order to execute the actual queries.&lt;/p&gt;
&lt;p&gt;The query frontend internally performs some query adjustments and holds queries in an internal queue. In this setup, queriers act as workers which pull jobs from the queue, execute them, and return them to the query-frontend for aggregation. Queriers need to be configured with the query frontend address (via the &lt;code&gt;-querier.frontend-address&lt;/code&gt; CLI flag) in order to allow them to connect to the query frontends.&lt;/p&gt;
&lt;p&gt;Query frontends are &lt;strong&gt;stateless&lt;/strong&gt;. However, due to how the internal queue works, it&amp;rsquo;s recommended to run a few query frontend replicas to reap the benefit of fair scheduling. Two replicas should suffice in most cases.&lt;/p&gt;
&lt;h3 id=&#34;queueing&#34;&gt;Queueing&lt;/h3&gt;
&lt;p&gt;The query frontend queuing mechanism is used to:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Ensure that large queries, that could cause an out-of-memory (OOM) error in the querier, will be retried on failure. This allows administrators to under-provision memory for queries, or optimistically run more small queries in parallel, which helps to reduce the TCO.&lt;/li&gt;
&lt;li&gt;Prevent multiple large requests from being convoyed on a single querier by distributing them across all queriers using a first-in/first-out queue (FIFO).&lt;/li&gt;
&lt;li&gt;Prevent a single tenant from denial-of-service-ing (DOSing) other tenants by fairly scheduling queries between tenants.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3 id=&#34;splitting&#34;&gt;Splitting&lt;/h3&gt;
&lt;p&gt;The query frontend splits larger queries into multiple smaller queries, executing these queries in parallel on downstream queriers and stitching the results back together again. This prevents large (multi-day, etc) queries from causing out of memory issues in a single querier and helps to execute them faster.&lt;/p&gt;
&lt;h3 id=&#34;caching&#34;&gt;Caching&lt;/h3&gt;
&lt;h4 id=&#34;metric-queries&#34;&gt;Metric Queries&lt;/h4&gt;
&lt;p&gt;The query frontend supports caching metric query results and reuses them on subsequent queries. If the cached results are incomplete, the query frontend calculates the required subqueries and executes them in parallel on downstream queriers. The query frontend can optionally align queries with their step parameter to improve the cacheability of the query results. The result cache is compatible with any loki caching backend (currently memcached, redis, and an in-memory cache).&lt;/p&gt;
&lt;h4 id=&#34;log-queries---coming-soon&#34;&gt;Log Queries - Coming soon!&lt;/h4&gt;
&lt;p&gt;Caching log (filter, regexp) queries are under active development.&lt;/p&gt;
&lt;h2 id=&#34;querier&#34;&gt;Querier&lt;/h2&gt;
&lt;p&gt;The &lt;strong&gt;querier&lt;/strong&gt; service handles queries using the &lt;a href=&#34;../../query/&#34;&gt;LogQL&lt;/a&gt; query
language, fetching logs both from the ingesters and from long-term storage.&lt;/p&gt;
&lt;p&gt;Queriers query all ingesters for in-memory data before falling back to
running the same query against the backend store. Because of the replication
factor, it is possible that the querier may receive duplicate data. To resolve
this, the querier internally &lt;strong&gt;deduplicates&lt;/strong&gt; data that has the same nanosecond
timestamp, label set, and log message.&lt;/p&gt;
&lt;p&gt;At read path, &lt;a href=&#34;#replication-factor&#34;&gt;replication factor&lt;/a&gt; also plays a role here. For example with &lt;code&gt;replication-factor&lt;/code&gt; of &lt;code&gt;3&lt;/code&gt;, we require that two queries to be running.&lt;/p&gt;
]]></content><description>&lt;h1 id="loki-components">Loki components&lt;/h1>
&lt;p>&lt;img
class="lazyload d-inline-block"
data-src="../loki_architecture_components.svg"
alt="components_diagram"/>&lt;/p>
&lt;h2 id="distributor">Distributor&lt;/h2>
&lt;p>The &lt;strong>distributor&lt;/strong> service is responsible for handling incoming streams by
clients. It&amp;rsquo;s the first stop in the write path for log data. Once the
distributor receives a set of streams, each stream is validated for correctness
and to ensure that it is within the configured tenant (or global) limits. Valid
chunks are then split into batches and sent to multiple &lt;a href="#ingester">ingesters&lt;/a>
in parallel.&lt;/p></description></item><item><title>Understand labels</title><link>https://grafana.com/docs/loki/v2.9.x/get-started/labels/</link><pubDate>Thu, 10 Apr 2025 12:15:54 +0000</pubDate><guid>https://grafana.com/docs/loki/v2.9.x/get-started/labels/</guid><content><![CDATA[&lt;h1 id=&#34;understand-labels&#34;&gt;Understand labels&lt;/h1&gt;
&lt;p&gt;Labels are key value pairs and can be defined as anything! We like to refer to them as metadata to describe a log stream. If you are familiar with Prometheus, there are a few labels you are used to seeing like &lt;code&gt;job&lt;/code&gt; and &lt;code&gt;instance&lt;/code&gt;, and I will use those in the coming examples.&lt;/p&gt;
&lt;p&gt;The scrape configs we provide with Grafana Loki define these labels, too. If you are using Prometheus, having consistent labels between Loki and Prometheus is one of Loki&amp;rsquo;s superpowers, making it incredibly &lt;a href=&#34;/blog/2019/05/06/how-loki-correlates-metrics-and-logs--and-saves-you-money/&#34;&gt;easy to correlate your application metrics with your log data&lt;/a&gt;.&lt;/p&gt;
&lt;h2 id=&#34;how-loki-uses-labels&#34;&gt;How Loki uses labels&lt;/h2&gt;
&lt;p&gt;Labels in Loki perform a very important task: They define a stream. More specifically, the combination of every label key and value defines the stream. If just one label value changes, this creates a new stream.&lt;/p&gt;
&lt;p&gt;If you are familiar with Prometheus, the term used there is series; however, Prometheus has an additional dimension: metric name. Loki simplifies this in that there are no metric names, just labels, and we decided to use streams instead of series.&lt;/p&gt;


&lt;div class=&#34;admonition admonition-note&#34;&gt;&lt;blockquote&gt;&lt;p class=&#34;title text-uppercase&#34;&gt;Note&lt;/p&gt;&lt;p&gt;Structured metadata do not define a stream, but are metadata attached to a log line.
See &lt;a href=&#34;structured-metadata/&#34;&gt;structured metadata&lt;/a&gt; for more information.&lt;/p&gt;&lt;/blockquote&gt;&lt;/div&gt;

&lt;h2 id=&#34;format&#34;&gt;Format&lt;/h2&gt;
&lt;p&gt;Loki places the same restrictions on label naming as &lt;a href=&#34;https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Prometheus&lt;/a&gt;:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;It may contain ASCII letters and digits, as well as underscores and colons. It must match the regex &lt;code&gt;[a-zA-Z_:][a-zA-Z0-9_:]*&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Note: The colons are reserved for user defined recording rules. They should not be used by exporters or direct instrumentation.&lt;/p&gt;&lt;/blockquote&gt;
&lt;h2 id=&#34;loki-labels-demo&#34;&gt;Loki labels demo&lt;/h2&gt;
&lt;p&gt;This series of examples will illustrate basic use cases and concepts for labeling in Loki.&lt;/p&gt;
&lt;p&gt;Let&amp;rsquo;s take an example Promtail/Alloy config file:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;YAML&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-yaml&#34;&gt;scrape_configs:
 - job_name: system
   pipeline_stages:
   static_configs:
   - targets:
      - localhost
     labels:
      job: syslog
      __path__: /var/log/syslog&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This config will tail one file and assign one label: &lt;code&gt;job=syslog&lt;/code&gt;. This will create one stream in Loki.&lt;/p&gt;
&lt;p&gt;You could query it like this:&lt;/p&gt;

&lt;div class=&#34;code-snippet code-snippet__mini&#34;&gt;&lt;div class=&#34;lang-toolbar__mini&#34;&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet code-snippet__border&#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-none&#34;&gt;{job=&amp;#34;syslog&amp;#34;}&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now let’s expand the example a little:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;YAML&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-yaml&#34;&gt;scrape_configs:
 - job_name: system
   pipeline_stages:
   static_configs:
   - targets:
      - localhost
     labels:
      job: syslog
      __path__: /var/log/syslog
 - job_name: apache
   pipeline_stages:
   static_configs:
   - targets:
      - localhost
     labels:
      job: apache
      __path__: /var/log/apache.log&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now we are tailing two files. Each file gets just one label with one value, so Loki will now be storing two streams.&lt;/p&gt;
&lt;p&gt;We can query these streams in a few ways:&lt;/p&gt;

&lt;div class=&#34;code-snippet code-snippet__mini&#34;&gt;&lt;div class=&#34;lang-toolbar__mini&#34;&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet code-snippet__border&#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-none&#34;&gt;{job=&amp;#34;apache&amp;#34;} &amp;lt;- show me logs where the job label is apache
{job=&amp;#34;syslog&amp;#34;} &amp;lt;- show me logs where the job label is syslog
{job=~&amp;#34;apache|syslog&amp;#34;} &amp;lt;- show me logs where the job is apache **OR** syslog&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;In that last example, we used a regex label matcher to view log streams that use the job label with one of two possible values. Now consider how an additional label could also be used:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;YAML&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-yaml&#34;&gt;scrape_configs:
 - job_name: system
   pipeline_stages:
   static_configs:
   - targets:
      - localhost
     labels:
      job: syslog
      env: dev
      __path__: /var/log/syslog
 - job_name: apache
   pipeline_stages:
   static_configs:
   - targets:
      - localhost
     labels:
      job: apache
      env: dev
      __path__: /var/log/apache.log&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now instead of a regex, we could do this:&lt;/p&gt;

&lt;div class=&#34;code-snippet code-snippet__mini&#34;&gt;&lt;div class=&#34;lang-toolbar__mini&#34;&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet code-snippet__border&#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-none&#34;&gt;{env=&amp;#34;dev&amp;#34;} &amp;lt;- will return all logs with env=dev, in this case this includes both log streams&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Hopefully now you are starting to see the power of labels. By using a single label, you can query many streams. By combining several different labels, you can create very flexible log queries.&lt;/p&gt;
&lt;p&gt;Labels are the index to Loki&amp;rsquo;s log data. They are used to find the compressed log content, which is stored separately as chunks. Every unique combination of label and values defines a stream, and logs for a stream are batched up, compressed, and stored as chunks.&lt;/p&gt;
&lt;p&gt;For Loki to be efficient and cost-effective, we have to use labels responsibly. The next section will explore this in more detail.&lt;/p&gt;
&lt;h2 id=&#34;cardinality&#34;&gt;Cardinality&lt;/h2&gt;
&lt;p&gt;The two previous examples use statically defined labels with a single value; however, there are ways to dynamically define labels. Let&amp;rsquo;s take a look using the Apache log and a massive regex you could use to parse such a log line:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;nohighlight&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-nohighlight&#34;&gt;11.11.11.11 - frank [25/Jan/2000:14:00:01 -0500] &amp;#34;GET /1986.js HTTP/1.1&amp;#34; 200 932 &amp;#34;-&amp;#34; &amp;#34;Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 GTB6&amp;#34;&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;YAML&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-yaml&#34;&gt;- job_name: system
   pipeline_stages:
      - regex:
        expression: &amp;#34;^(?P&amp;lt;ip&amp;gt;\\S&amp;#43;) (?P&amp;lt;identd&amp;gt;\\S&amp;#43;) (?P&amp;lt;user&amp;gt;\\S&amp;#43;) \\[(?P&amp;lt;timestamp&amp;gt;[\\w:/]&amp;#43;\\s[&amp;#43;\\-]\\d{4})\\] \&amp;#34;(?P&amp;lt;action&amp;gt;\\S&amp;#43;)\\s?(?P&amp;lt;path&amp;gt;\\S&amp;#43;)?\\s?(?P&amp;lt;protocol&amp;gt;\\S&amp;#43;)?\&amp;#34; (?P&amp;lt;status_code&amp;gt;\\d{3}|-) (?P&amp;lt;size&amp;gt;\\d&amp;#43;|-)\\s?\&amp;#34;?(?P&amp;lt;referer&amp;gt;[^\&amp;#34;]*)\&amp;#34;?\\s?\&amp;#34;?(?P&amp;lt;useragent&amp;gt;[^\&amp;#34;]*)?\&amp;#34;?$&amp;#34;
    - labels:
        action:
        status_code:
   static_configs:
   - targets:
      - localhost
     labels:
      job: apache
      env: dev
      __path__: /var/log/apache.log&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;This regex matches every component of the log line and extracts the value of each component into a capture group. Inside the pipeline code, this data is placed in a temporary data structure that allows using it for several purposes during the processing of that log line (at which point that temp data is discarded). Much more detail about this can be found in the &lt;a href=&#34;../../send-data/promtail/pipelines/&#34;&gt;Promtail pipelines&lt;/a&gt; documentation.&lt;/p&gt;
&lt;p&gt;From that regex, we will be using two of the capture groups to dynamically set two labels based on content from the log line itself:&lt;/p&gt;
&lt;p&gt;action (for example, &lt;code&gt;action=&amp;quot;GET&amp;quot;&lt;/code&gt;, &lt;code&gt;action=&amp;quot;POST&amp;quot;&lt;/code&gt;)&lt;/p&gt;
&lt;p&gt;status_code (for example, &lt;code&gt;status_code=&amp;quot;200&amp;quot;&lt;/code&gt;, &lt;code&gt;status_code=&amp;quot;400&amp;quot;&lt;/code&gt;)&lt;/p&gt;
&lt;p&gt;And now let&amp;rsquo;s walk through a few example lines:&lt;/p&gt;

&lt;div class=&#34;code-snippet &#34;&gt;&lt;div class=&#34;lang-toolbar&#34;&gt;
    &lt;span class=&#34;lang-toolbar__item lang-toolbar__item-active&#34;&gt;nohighlight&lt;/span&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
    &lt;div class=&#34;lang-toolbar__border&#34;&gt;&lt;/div&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet &#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-nohighlight&#34;&gt;11.11.11.11 - frank [25/Jan/2000:14:00:01 -0500] &amp;#34;GET /1986.js HTTP/1.1&amp;#34; 200 932 &amp;#34;-&amp;#34; &amp;#34;Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 GTB6&amp;#34;
11.11.11.12 - frank [25/Jan/2000:14:00:02 -0500] &amp;#34;POST /1986.js HTTP/1.1&amp;#34; 200 932 &amp;#34;-&amp;#34; &amp;#34;Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 GTB6&amp;#34;
11.11.11.13 - frank [25/Jan/2000:14:00:03 -0500] &amp;#34;GET /1986.js HTTP/1.1&amp;#34; 400 932 &amp;#34;-&amp;#34; &amp;#34;Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 GTB6&amp;#34;
11.11.11.14 - frank [25/Jan/2000:14:00:04 -0500] &amp;#34;POST /1986.js HTTP/1.1&amp;#34; 400 932 &amp;#34;-&amp;#34; &amp;#34;Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 GTB6&amp;#34;&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;In Loki the following streams would be created:&lt;/p&gt;

&lt;div class=&#34;code-snippet code-snippet__mini&#34;&gt;&lt;div class=&#34;lang-toolbar__mini&#34;&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet code-snippet__border&#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-none&#34;&gt;{job=&amp;#34;apache&amp;#34;,env=&amp;#34;dev&amp;#34;,action=&amp;#34;GET&amp;#34;,status_code=&amp;#34;200&amp;#34;} 11.11.11.11 - frank [25/Jan/2000:14:00:01 -0500] &amp;#34;GET /1986.js HTTP/1.1&amp;#34; 200 932 &amp;#34;-&amp;#34; &amp;#34;Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 GTB6&amp;#34;
{job=&amp;#34;apache&amp;#34;,env=&amp;#34;dev&amp;#34;,action=&amp;#34;POST&amp;#34;,status_code=&amp;#34;200&amp;#34;} 11.11.11.12 - frank [25/Jan/2000:14:00:02 -0500] &amp;#34;POST /1986.js HTTP/1.1&amp;#34; 200 932 &amp;#34;-&amp;#34; &amp;#34;Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 GTB6&amp;#34;
{job=&amp;#34;apache&amp;#34;,env=&amp;#34;dev&amp;#34;,action=&amp;#34;GET&amp;#34;,status_code=&amp;#34;400&amp;#34;} 11.11.11.13 - frank [25/Jan/2000:14:00:03 -0500] &amp;#34;GET /1986.js HTTP/1.1&amp;#34; 400 932 &amp;#34;-&amp;#34; &amp;#34;Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 GTB6&amp;#34;
{job=&amp;#34;apache&amp;#34;,env=&amp;#34;dev&amp;#34;,action=&amp;#34;POST&amp;#34;,status_code=&amp;#34;400&amp;#34;} 11.11.11.14 - frank [25/Jan/2000:14:00:04 -0500] &amp;#34;POST /1986.js HTTP/1.1&amp;#34; 400 932 &amp;#34;-&amp;#34; &amp;#34;Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7 GTB6&amp;#34;&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Those four log lines would become four separate streams and start filling four separate chunks.&lt;/p&gt;
&lt;p&gt;Any additional log lines that match those combinations of label/values would be added to the existing stream. If another unique combination of labels comes in (for example, &lt;code&gt;status_code=&amp;quot;500&amp;quot;&lt;/code&gt;) another new stream is created.&lt;/p&gt;
&lt;p&gt;Imagine now if you set a label for &lt;code&gt;ip&lt;/code&gt;. Not only does every request from a user become a unique stream. Every request with a different action or status_code from the same user will get its own stream.&lt;/p&gt;
&lt;p&gt;Doing some quick math, if there are maybe four common actions (GET, PUT, POST, DELETE) and maybe four common status codes (although there could be more than four!), this would be 16 streams and 16 separate chunks. Now multiply this by every user if we use a label for &lt;code&gt;ip&lt;/code&gt;.  You can quickly have thousands or tens of thousands of streams.&lt;/p&gt;
&lt;p&gt;This is high cardinality, and it can lead to significant performance degredation.&lt;/p&gt;
&lt;p&gt;When we talk about &lt;em&gt;cardinality&lt;/em&gt; we are referring to the combination of labels and values and the number of streams they create. High cardinality is using labels with a large range of possible values, such as &lt;code&gt;ip&lt;/code&gt;, &lt;strong&gt;or&lt;/strong&gt; combining many labels, even if they have a small and finite set of values, such as using &lt;code&gt;status_code&lt;/code&gt; and &lt;code&gt;action&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;High cardinality causes Loki to build a huge index and to flush thousands of tiny chunks to the object store. Loki currently performs very poorly in this configuration. If not accounted for, high cardinality will significantly reduce the operability and cost-effectiveness of Loki.&lt;/p&gt;
&lt;h2 id=&#34;optimal-loki-performance-with-parallelization&#34;&gt;Optimal Loki performance with parallelization&lt;/h2&gt;
&lt;p&gt;Now you may be asking: If using too many labels—or using labels with too many values—is bad, then how am I supposed to query my logs? If none of the data is indexed, won&amp;rsquo;t queries be really slow?&lt;/p&gt;
&lt;p&gt;As we see people using Loki who are accustomed to other index-heavy solutions, it seems like they feel obligated to define a lot of labels in order to query their logs effectively. After all, many other logging solutions are all about the index, and this is the common way of thinking.&lt;/p&gt;
&lt;p&gt;When using Loki, you may need to forget what you know and look to see how the problem can be solved differently with parallelization. Loki&amp;rsquo;s superpower is breaking up queries into small pieces and dispatching them in parallel so that you can query huge amounts of log data in small amounts of time.&lt;/p&gt;
&lt;p&gt;This kind of brute force approach might not sound ideal, but let me explain why it is.&lt;/p&gt;
&lt;p&gt;Large indexes are complicated and expensive. Often a full-text index of your log data is the same size or bigger than the log data itself. To query your log data, you need this index loaded, and for performance, it should probably be in memory. This is difficult to scale, and as you ingest more logs, your index gets larger quickly.&lt;/p&gt;
&lt;p&gt;Now let&amp;rsquo;s talk about Loki, where the index is typically an order of magnitude smaller than your ingested log volume. So if you are doing a good job of keeping your streams and stream churn to a minimum, the index grows very slowly compared to the ingested logs.&lt;/p&gt;
&lt;p&gt;Loki will effectively keep your static costs as low as possible (index size and memory requirements as well as static log storage) and make the query performance something you can control at runtime with horizontal scaling.&lt;/p&gt;
&lt;p&gt;To see how this works, let&amp;rsquo;s look back at our example of querying your access log data for a specific IP address. We don&amp;rsquo;t want to use a label to store the IP address. Instead we use a &lt;a href=&#34;../../query/log_queries/#line-filter-expression&#34;&gt;filter expression&lt;/a&gt; to query for it:&lt;/p&gt;

&lt;div class=&#34;code-snippet code-snippet__mini&#34;&gt;&lt;div class=&#34;lang-toolbar__mini&#34;&gt;
    &lt;span class=&#34;code-clipboard&#34;&gt;
      &lt;button x-data=&#34;app_code_snippet()&#34; x-init=&#34;init()&#34; @click=&#34;copy()&#34;&gt;
        &lt;img class=&#34;code-clipboard__icon&#34; src=&#34;/media/images/icons/icon-copy-small-2.svg&#34; alt=&#34;Copy code to clipboard&#34; width=&#34;14&#34; height=&#34;13&#34;&gt;
        &lt;span&gt;Copy&lt;/span&gt;
      &lt;/button&gt;
    &lt;/span&gt;
  &lt;/div&gt;&lt;div class=&#34;code-snippet code-snippet__border&#34;&gt;
    &lt;pre data-expanded=&#34;false&#34;&gt;&lt;code class=&#34;language-none&#34;&gt;{job=&amp;#34;apache&amp;#34;} |= &amp;#34;11.11.11.11&amp;#34;&lt;/code&gt;&lt;/pre&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Behind the scenes, Loki will break up that query into smaller pieces (shards), and open up each chunk for the streams matched by the labels and start looking for this IP address.&lt;/p&gt;
&lt;p&gt;The size of those shards and the amount of parallelization is configurable and based on the resources you provision. If you want to, you can configure the shard interval down to 5m, deploy 20 queriers, and process gigabytes of logs in seconds. Or you can go crazy and provision 200 queriers and process terabytes of logs!&lt;/p&gt;
&lt;p&gt;This trade-off of smaller index and parallel brute force querying vs. a larger/faster full-text index is what allows Loki to save on costs versus other systems. The cost and complexity of operating a large index is high and is typically fixed &amp;ndash; you pay for it 24 hours a day if you are querying it or not.&lt;/p&gt;
&lt;p&gt;The benefits of this design mean you can make the decision about how much query power you want to have, and you can change that on demand. Query performance becomes a function of how much money you want to spend on it. Meanwhile, the data is heavily compressed and stored in low-cost object stores like S3 and GCS. This drives the fixed operating costs to a minimum while still allowing for incredibly fast query capability.&lt;/p&gt;
]]></content><description>&lt;h1 id="understand-labels">Understand labels&lt;/h1>
&lt;p>Labels are key value pairs and can be defined as anything! We like to refer to them as metadata to describe a log stream. If you are familiar with Prometheus, there are a few labels you are used to seeing like &lt;code>job&lt;/code> and &lt;code>instance&lt;/code>, and I will use those in the coming examples.&lt;/p></description></item><item><title>Consistent hash rings</title><link>https://grafana.com/docs/loki/v2.9.x/get-started/hash-rings/</link><pubDate>Mon, 29 Apr 2024 15:52:20 +0000</pubDate><guid>https://grafana.com/docs/loki/v2.9.x/get-started/hash-rings/</guid><content><![CDATA[&lt;h1 id=&#34;consistent-hash-rings&#34;&gt;Consistent hash rings&lt;/h1&gt;
&lt;p&gt;&lt;a href=&#34;https://en.wikipedia.org/wiki/Consistent_hashing&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;Consistent hash rings&lt;/a&gt;
are incorporated into Loki cluster architectures to&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;aid in the sharding of log lines&lt;/li&gt;
&lt;li&gt;implement high availability&lt;/li&gt;
&lt;li&gt;ease the horizontal scale up and scale down of clusters.
There is less of a performance hit for operations that must rebalance data.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Hash rings connect instances of a single type of component when&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;there are a set of Loki instances in monolithic deployment mode&lt;/li&gt;
&lt;li&gt;there are multiple read components or multiple write components in
simple scalable deployment mode&lt;/li&gt;
&lt;li&gt;there are multiple instances of one type of component in microservices mode&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Not all Loki components are connected by hash rings.
These components need to be connected into a hash ring:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;distributors&lt;/li&gt;
&lt;li&gt;ingesters&lt;/li&gt;
&lt;li&gt;query schedulers&lt;/li&gt;
&lt;li&gt;compactors&lt;/li&gt;
&lt;li&gt;rulers&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;These components can optionally be connected into a hash ring:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;index gateway&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;In an architecture that has three distributors and three ingesters defined,
the hash rings for these components connect the instances of same-type components.&lt;/p&gt;
&lt;p&gt;&lt;img
  class=&#34;lazyload d-inline-block&#34;
  data-src=&#34;../ring-overview.png&#34;
  alt=&#34;Distributor and ingester rings&#34;/&gt;&lt;/p&gt;
&lt;p&gt;Each node in the ring represents an instance of a component.
Each node has a key-value store that holds communication information
for each of the nodes in that ring.
Nodes update the key-value store periodically to keep the contents consistent
across all nodes.
For each node, the key-value store holds:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;an ID of the component node&lt;/li&gt;
&lt;li&gt;component address, used by other nodes as a communication channel&lt;/li&gt;
&lt;li&gt;an indication of the component node&amp;rsquo;s health&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;configuring-rings&#34;&gt;Configuring rings&lt;/h2&gt;
&lt;p&gt;Define 
    &lt;a href=&#34;/docs/loki/v2.9.x/configure/#common&#34;&gt;ring configuration&lt;/a&gt; within the &lt;code&gt;common.ring_config&lt;/code&gt; block.&lt;/p&gt;
&lt;p&gt;Use the default &lt;code&gt;memberlist&lt;/code&gt; key-value store type unless there is
a compelling reason to use a different key-value store type.
&lt;code&gt;memberlist&lt;/code&gt; uses a &lt;a href=&#34;https://en.wikipedia.org/wiki/Gossip_protocol&#34; target=&#34;_blank&#34; rel=&#34;noopener noreferrer&#34;&gt;gossip protocol&lt;/a&gt;
to propagate information to all the nodes
to guarantee the eventual consistency of the key-value store contents.&lt;/p&gt;
&lt;p&gt;There are additional configuration options for distributor rings,
ingester rings, and ruler rings.
These options are for advanced, specialized use only.
These options are defined within the &lt;code&gt;distributor.ring&lt;/code&gt; block for distributors,
the &lt;code&gt;ingester.lifecycler.ring&lt;/code&gt; block for ingesters,
and the &lt;code&gt;ruler.ring&lt;/code&gt; block for rulers.&lt;/p&gt;
&lt;h2 id=&#34;about-the-distributor-ring&#34;&gt;About the distributor ring&lt;/h2&gt;
&lt;p&gt;Distributors use the information in their key-value store
to keep a count of the quantity of distributors in the distributor ring.
The count further informs cluster limits.&lt;/p&gt;
&lt;h2 id=&#34;about-the-ingester-ring&#34;&gt;About the ingester ring&lt;/h2&gt;
&lt;p&gt;Ingester ring information in the key-value stores is used by distributors.
The information lets the distributors shard log lines,
determining which ingester or set of ingesters a distributor sends log lines to.&lt;/p&gt;
&lt;h2 id=&#34;about-the-query-scheduler-ring&#34;&gt;About the query scheduler ring&lt;/h2&gt;
&lt;p&gt;Query schedulers use the information in their key-value store
for service discovery of the schedulers.
This allows queriers to connect to all available schedulers,
and it allows schedulers to connect to all available query frontends,
effectively creating a single queue that aids in balancing the query load.&lt;/p&gt;
&lt;h2 id=&#34;about-the-compactor-ring&#34;&gt;About the compactor ring&lt;/h2&gt;
&lt;p&gt;Compactors use the information in the key-value store to identify
a single compactor instance that will be responsible for compaction.
The compactor is only enabled on the responsible instance,
despite the compactor target being on multiple instances.&lt;/p&gt;
&lt;h2 id=&#34;about-the-ruler-ring&#34;&gt;About the ruler ring&lt;/h2&gt;
&lt;p&gt;The ruler ring is used to determine which rulers evaluate which rule groups.&lt;/p&gt;
&lt;h2 id=&#34;about-the-index-gateway-ring&#34;&gt;About the index gateway ring&lt;/h2&gt;
&lt;p&gt;The index gateway ring is used to determine which gateway is responsible for which tenant&amp;rsquo;s indexes when queried by rulers or queriers.&lt;/p&gt;
]]></content><description>&lt;h1 id="consistent-hash-rings">Consistent hash rings&lt;/h1>
&lt;p>&lt;a href="https://en.wikipedia.org/wiki/Consistent_hashing" target="_blank" rel="noopener noreferrer">Consistent hash rings&lt;/a>
are incorporated into Loki cluster architectures to&lt;/p>
&lt;ul>
&lt;li>aid in the sharding of log lines&lt;/li>
&lt;li>implement high availability&lt;/li>
&lt;li>ease the horizontal scale up and scale down of clusters.
There is less of a performance hit for operations that must rebalance data.&lt;/li>
&lt;/ul>
&lt;p>Hash rings connect instances of a single type of component when&lt;/p></description></item></channel></rss>