Skip to main content

Logs Are a Distributed System, Not Debug Output. Designing Reliable Logging pipeline

Why most logging systems break at scale and what actually happens under the hood ?

Updated 13 min readMarkdown
Part of seriesDistributed Systems Engineering
Logs Are a Distributed System, Not Debug Output. Designing Reliable Logging pipeline

Hello, my name is Ahmed Eid (Eidoox), a Senior Backend Engineer with over 3 years of experience building reliable, scalable, distributed, and real-time systems.

I have been designing and working with production-grade logging and observability pipelines using Promtail, Loki, and OpenTelemetry across multiple systems, where logging was never treated as a simple debugging tool, but as a core part of system reliability.

In this Production Insight, I want to share the lessons learned from building
reliable logging systems at scale, and why modern observability is no longer about “collecting logs”, but about designing the full lifecycle of how logs move through distributed systems.

This is not a tutorial on Loki, Promtail, or OpenTelemetry. It is about a deeper production reality: logs are not output, they are a distributed data pipeline, and small design decisions around labels, ingestion, and storage can determine whether a system is debuggable or blind under real production pressure.

You can learn more about my background through my Personal Website.

Logs Are Streaming Data, Not Print Statements

Most engineers start with logging like this: console.log("user created"). A simple debugging tool. A way to inspect application behavior during development. This is applicable on your laptop while development, but it breaks in production.

In distributed systems, logs are not debug output. They are continuous streams of events generated by services and containers.

Every service in the system is constantly producing events that must be: Ingested, transported, buffered, stored, indexed, and then queried.

The primary consumer becomes not a human reading a terminal in the development. The primary consumer is the observability pipeline.

Once a log leaves your application, it becomes part of a larger system involving message queues, storage backends, indexes, retention policies, and query engines. For example:

Application generates logs >> then promtail >> then Loki >> then S3 >> then Grafana dashboard querying.

The log line output is the first step in a much larger lifecycle.

When engineers think of logs as print statements, they ask: What should I log?

When engineers think of logs as streaming data, they ask: How will this log behave inside the pipeline ?

Questions become:

  • How much traffic will this generate?

  • Will this increase storage costs?

  • Can this be efficiently queried?

  • Does it contain useful metadata?

  • Does it create high-cardinality labels?

  • Can it be correlated with traces and metrics?

These are production concerns. Logs Are Streaming Data, Not Print Statements.

What Actually Happens When You Write a Log

Writing a log feels very simple logger.info("User created", { userId: 123 });

A single line of code, single event, a simple operation. At least that's what it looks like from the application's perspective.

In production systems, that log entry is only beginning its journey.

The Journey of a Log

When an application emits a log, the log rarely travels directly to its final destination.

Instead, it passes through multiple layers of infrastructure before it becomes searchable in your observability platform. A typical production path looks like this below architecture.

What Happens Behind the Scenes ?

1. The Application Produces an Event Log

The application emits a structured log:logger.info("User created", { userId: 123 });

At this point, the application's responsibility is usually complete.

The log has been produced. It has not yet been stored.

2. The Application Writes to stdout

The application forwards the log to stdout or stderr.

The log still has not reached your observability platform.

3. The Container Runtime Captures the Stream

In containerized environments, Docker or Kubernetes captures stdout and stderr.

The log is typically written to local log files on the node "server" itself. /var/log/containers/*.log

Now the log exists outside the application process and not it exists on disk.

4. A Log Agent Consumes the Stream

Agents such as Promtail continuously monitor log files.

Their responsibilities include:

  • Reading new log entries

  • Attaching metadata

  • Batching records

  • Handling retries

  • Applying backpressure

  • Shipping data to the backend "Loki"

This is where logging starts looking less like file writing and more like stream processing.

5. Logs Are Transported Across the Network

The agent sends batched logs to the ingestion layer of Loki.

Network failures, latency spikes, throttling, and retries must all be handled.

At this stage, the log has crossed process and machine boundaries.

6. The Loki Backend Stores and Indexes the Data

In Loki:

  • Labels are indexed

  • Log entries are grouped into chunks

  • Chunks are compressed

  • Chunks are stored in object storage such as AWS S3

The original log line has now become part of a distributed storage system.

What I learned hard way that logs not writing to a file.

In reality, they are producing data into a distributed ingestion pipeline.

Once logs are viewed as a stream rather than text output, new questions will be:

  • How much traffic will this generate?

  • How much storage will this consume?

  • Can this data be queried efficiently?

  • Are labels designed correctly?

  • What happens when the backend becomes unavailable?

These are distributed systems concerns, not logging concerns.

Promtail Is Not a Logger, it’s a Distributed Log Forwarder

I was describing Promtail as: "The component that reads log files and sends them to Loki." While technically correct, that description misses what Promtail actually does.

Promtail is a distributed log forwarding agent responsible for reliably moving log streams from producers to Loki. It performs many of the same responsibilities found in distributed messaging systems.

Before a log reaches Loki, Promtail must:

  • Discover log sources

  • Track read positions

  • Parse log formats

  • Attach metadata

  • Batch records

  • Handle retries

  • Manage backpressure

  • Recover from failures

  • Deliver data reliably

Promtail is much closer to a lightweight stream processor than a simple file reader.

Promtail Is a Consumer

From Promtail's perspective, log files are streams.

Consider a container log file: /var/log/containers/payments.log

New log entries are continuously appended:
INFO User Created INFO Payment Started ERROR Payment Failed

Promtail continuously consumes this stream.

Conceptually: LOG FILE >>>> Promtail Consumer

This behavior is remarkably similar to consuming events from some streaming events tool like KAFKA.

The difference is that the source is a file rather than a broker.

Position Tracking

One of Promtail's most important responsibilities is tracking progress. Imagine Promtail has processed: from position 0 to 500,000 If Promtail crashes and restarts, it cannot reread the entire file.

Doing so would create duplicate logs. Instead, Promtail stores its read position.

After restarting: Last Position = 500,000 + 1 and then continue reading "consuming"

This mechanism is similar to consumer offsets in messaging systems "in KAFKA". Without it, reliable log delivery would be impossible.

Metadata Enrichment

Raw log lines are rarely enough. For example: User Created By itself, this line provides very little context.

Promtail enriches logs with metadata such as: service=payements and env=production

These labels allow Loki to efficiently locate related logs later. Without metadata, querying logs in large environments becomes extremely difficult.

Batching

Sending every log line individually would create enormous network overhead.

Imagine: 10000 logs >>>> 10000 HTTP requests.

This would be inefficient. Instead, Promtail groups logs into batches: 10000 logs >>>> 10 batches >>>> 10 requests

Batching reduces:

  • Network traffic

  • CPU overhead

  • Connection management costs

This is one of the reasons Loki can scale efficiently.

Retry Logic

Distributed systems are never perfectly available, networks fail or nodes restart.

Services become temporarily unreachable. Suppose Loki becomes unavailable.

Without retry handling: send logs >>> loki "if failure happens: logs lost"

Promtail avoids this outcome by retaining failed batches and attempting delivery again by: send >>> failure happens >>> retry >>> retry >>>> success.

Backpressure Detection

One of the most important concepts in distributed systems is backpressure detection.

Consider: Promtail produces 1000 logs / sec , while Loki accepts 500 logs / sec
Producer rate > ingestion rate to loki.

The producer is generating data faster than the consumer can process it.

Without protection: Queue growth>> memory growth>> Out Of Memory OOM happens >>> Crash

This problem exists in every distributed pipeline.

Promtail must recognize when downstream systems are overwhelmed and adjust its behavior accordingly.

This may involve:

  • Buffering data temporarily

  • Retrying later

  • Slowing transmission rates

Promtail Reliability Is the Real Feature

Many focus on Promtail's ability to add labels. In reality, metadata enrichment is only a small part of its responsibilities.

The real value of Promtail is reliability.

Its primary job is ensuring that logs move safely from producers to Loki despite:

  • Process crashes

  • Network failures

  • Slow downstream systems

  • Infrastructure restarts

  • Temporary outages

The difficult part of logging is not generating logs. The difficult part is transporting them reliably at scale.

Loki is a Query System, Not a Log Database

Most engineers assume Loki is a database for logs. That assumption is the root of many misunderstandings. Loki does not behave like a traditional database such as PostgreSQL or Elasticsearch.

It does not store logs in a fully indexed, query-optimized structure.

Instead, Loki is a query system that reconstructs logs at read time using minimal indexing and chunk-based storage.

It is what allows Loki to scale cheaply and store massive volumes of logs in object storage.

The Core Idea

Loki separates the problem into two distinct parts:

Write Path (Ingestion)

Logs are received, grouped, and stored efficiently.

Read Path (Querying)

Logs are reconstructed from storage when needed.

Unlike traditional databases, Loki does not continuously maintain a full-text index over log content.

Instead, it relies on: Lightweight label indexing Chunk storage in object storage (S3) On-demand scanning during queries

This means: Loki does not “search stored logs” it “rebuilds logs for each query.”

Write Path: How Logs Enter Loki ?

When logs arrive from Promtail or another agent, they go through multiple components.

Distributor (Ingress Router)

The Distributor is the first layer inside Loki. It is responsible for:

Receiving log batches over HTTP/gRPC Validating incoming streams then routing logs to the correct Ingester nodes

Think of it as a load balancer for log streams, not a storage layer.

Ingester (In-Memory Log Builder)

The Ingester is where logs are temporarily held and structured. Its responsibilities:

Receive log streams from Distributors Keep recent logs in memory Group logs into chunks then Compress chunks then move chunks to object storage

A chunk is simply (chunk_1): log1 log2 log3 log4

compressed into a single file: chunk_1.gz

The Ingester is not a permanent storage system, it is a buffering and aggregation layer.

Index (Label-Based Map)

Loki does not index log content. Instead, it indexes only labels.

Example: {service="payments", env="prod"}

Index layer stores labels and chunks reference. For example: service="payments" reference to chunk_1.gz and chunk_2.gz.

Object Storage (S3)

This is where the real data lives. Chunks are flushed to object storage by ingester:
chunk_1.gz chunk_2.gz chunk_3.gz

This makes Loki extremely cost-efficient, because storage is offloaded to cheap object storage.

Read Path: How Queries to Loki Actually Work

When you run a query in Grafana: {app="payments"} |= "error"

Loki does NOT search an index of log content.

Instead, it reconstructs logs step by step.

Query Frontend (Query Planner)

The Query Frontend:

Splits large queries into smaller chunks (time-based) Parallelizes execution Caches results when possible

Querier (Execution Engine)

The Querier performs the real work:

  1. Looks up label index

  2. Finds matching chunk references

  3. Downloads chunks from S3

  4. Decompresses them

  5. Scans logs line by line

  6. Applies filters like != "error"

  7. Returns results

This is why Loki is called a log query engine, not a traditional database.

To sum up, Loki does not store logs for fast search, it stores them for cheap retrieval, then reconstructs them at query time.

S3 Object Storage Becomes the Real Source of Truth

In Loki, object storage (S3 / GCS / Blob Storage) is the real source of truth for logs.

Loki itself does not permanently store or index full log data like a traditional database.
Logs are written once, stored as immutable compressed chunks in S3, and only referenced by Loki.

So instead of: Loki stores logs, S3 stores logs, Loki reconstructs them when queried.

What S3 Stores ?

S3 contains:

  • Compressed log chunks (.gz)

  • Multiple log lines per chunk

  • Immutable data files

Each chunk contains raw logs.

What Loki Stores ?

Loki only stores lightweight metadata: indexed labels that reference to exact position in S3.

To conclude, S3 it the source of truth, while Loki is query + reconstruction engine

Where Logging Systems Break in Production

Logging systems don’t usually fail because of implementation bugs, they fail because of scale assumptions breaking in a distributed system.

High Cardinality Labels Break the Index

In Loki, labels are the index.

When labels contain dynamic values (like userId, IP), the number of unique streams explodes.

Result: index growth, memory pressure, slow queries, and ingestion instability

Labels are not metadata, they are the database index.

Backpressure Failure

When log production exceeds ingestion rate: buffers grow, retries increase ,memory fills up, and system can crash.

Logging becomes a system bottleneck instead of a supporting tool.

Query Cost Becomes Expensive

Because Loki does not index log content, queries require scanning chunks from storage which causes:

  • heavy S3 reads

  • CPU-intensive load

  • slow dashboard queries

In Loki, reads are expensive.

S3 Latency Impacts Everything

Since S3 is the source of truth:

  • every query depends on object storage

  • slow S3 = slow queries

Why Modern Systems Are Moving Toward OpenTelemetry

In the next production insight, we will go deeper into why logging alone has created production blind spots, and why i used OpenTelemetry instead and how tracing fundamentally changes debugging in distributed systems.

We’ll break down a real-world scenario:

“Database is down”, but we still don’t know which request caused it, or how it propagated through the system.

We Don’t Collect Logs, we Design Their Lifecycle

Logging is not about collecting output. It is about designing how data moves through a distributed system.

From the moment a log is emitted, it enters a lifecycle:

  • generated by an application

  • transported through agents

  • buffered and batched

  • indexed by labels

  • stored in object storage

  • reconstructed at query time

Each stage is a design decision that affects cost, reliability, and observability.


If you found this insightful, follow me for more production lessons from real distributed systems. I’ll be sharing deeper architectural insights, failures we only learn after hitting production scale, and the engineering decisions that don’t usually make it into tutorials.

More production insights and distributed systems technical articles coming soon.

No comments yet. Be the first to comment.

Distributed Systems Engineering

Part 2 of 5

A collection of deep technical articles exploring how modern distributed and real-time systems are designed, built, scaled, and operated. This series covers event-driven architectures, messaging systems such as Kafka and RabbitMQ, asynchronous processing with BullMQ, WebSockets and real-time communication, caching, data consistency, scalability patterns, reliability engineering, and the architectural principles behind production-grade backend systems.

Up nextRabbitMQ was Never Our Job Runner. We Stopped Mixing Communication with Execution.Designing Production Systems by Separating Communication Reliability From Execution Reliability.