/blogs/why-your-api-should-never-wait-for-logging

← Back to blogs
Blog

Why Your API Should Never Wait for Logging

Learn why sending logs synchronously to Grafana Loki can increase request latency, and how using Pino with Grafana Alloy/Promtail enables fast, asynchronous, production-ready logging.

·5 min read·

#I Ruined My API’s Latency by Logging to Loki. Here’s How I Fixed It.

I’ve been building an app lately, and things were going great. The core logic kept things seamless and it was time to add some proper observability.

Naturally, I chose Grafana Loki for my log aggregation. It’s lightweight, cost-effective, and integrates beautifully with Grafana.

But then I made a critical mistake. I decided to send my logs directly to Loki from my API request handlers.

I observed a gradual latency spike in API responses. What used to be a blazing fast experience was now feeling slow. I was confused. I just added a few log lines, why did my API get so slow?

Here is the hard lesson I learned about synchronous logging, and how I restructured my app to fix it.


#The Trap: Synchronous Logging

When I first set up my logging, I did what seemed logical at the time. On every request, I gathered the context and pushed it to Loki. The mistake I made:

await sendLogToLoki(log); return response;

To understand why this destroyed my latency, let’s look at the actual request flow. Before adding direct Loki logging, my flow looked like this:

Rendering diagram...

Total time: ~170ms. Fast. But when I added synchronous logging to Loki, the flow changed:

Rendering diagram...

Because I was using await, my user had to wait for the log to be successfully sent to Loki before they got their response.

If Loki was experiencing a momentary spike in load, a network blip, or just being generally slow, my API became slow too. I had tightly coupled my application's performance to my logging backend's performance.


#How the Production Systems Handle Logging?

I started digging into how large scale production systems handle this. Spoiler alert: almost nobody does await sendLog() in their request handlers.

Sending logs synchronously over HTTP (like await axios.post(lokiUrl, log)) is a massive anti-pattern for request-response paths.

So, how do you actually do it? I realized I needed to decouple my app from Loki.


#The Fix: The Standard Production Architecture

The golden rule I learned is this: Your application should never talk to Loki directly.

Instead of pushing logs over the network synchronously, you should write them locally as fast as possible, and let a dedicated log collector ship them asynchronously.

Here is the architecture I migrated to:

Rendering diagram...

Why this works:

  1. Pino writes the log to stdout or a local file. This is an incredibly fast, non-blocking I/O operation.
  2. Promtail (or Grafana Alloy) runs as a sidecar. It tails the logs and ships them to Loki in the background.
  3. Latency impact on my API: ≈ 0ms.

If Loki goes down, or the network hiccups, my API doesn't care. It keeps serving requests at lightning speed while Promtail quietly buffers and retries the logs in the background.


#What If I Have to Send Over HTTP?

Sometimes you are in an environment where you can't run a sidecar like Promtail (like certain serverless setups). If you absolutely must send logs over HTTP, do not block the main thread.

Instead, use Pino Transports. Pino supports transports that write logs on a completely separate worker thread.

Rendering diagram...

Even if the Loki HTTP API is a little slow, your main request thread isn't blocked. The response is returned to the user immediately. You can also configure these transports to handle background batching and retries.

After refactoring, here is the exact logging pipeline I’m using for my app

Rendering diagram...

The Results:

  • Near-zero request latency: My API is as fast as it was before I added logging.
  • High availability: If Loki is down, my app keeps serving users without timing out.
  • Efficiency: Automatic batching and retries are handled by the collector, not my app code.
  • Industry standard: I'm now using the exact same pattern used by large-scale production systems.

#The Takeaway

If you are adding Loki (or any centralized logging) to your app, avoid sending logs synchronously from your request handlers.

Use a fast local logger like Pino to write to stdout, and let a dedicated log collector like Grafana Alloy or Promtail forward them asynchronously. It keeps your application responsive, your users happy, and your sanity intact when the logging backend inevitably has a bad day.

702 words left