Marcio Cunha

How to use Grafana Mimir and Loki without memory leaks

Learn how to structure Grafana Mimir and Loki in Kubernetes to centralize logs and metrics at scale without excessive RAM consumption or crashes.

Marcio Cunha•4 min
Also available in:PortuguêsEspañol
Summary
  • The clean separation between ingestors and long-term storage prevents traffic spikes from crashing Kubernetes nodes due to memory exhaustion.
  • The smart use of compressed chunks in S3-compatible storage drastically reduces the local storage footprint within the cluster.
  • Retention strategies and tenant rate limiting protect the infrastructure against sudden telemetry data leaks or runaway applications.
  • Proper tuning of Go heap memory limits prevents abrupt Garbage Collector pauses in high-volume environments.
  • The distributed architecture ensures high availability without relying on ephemeral local disks that vanish when pods restart.

The challenge of collecting metrics and logs at scale

Running applications in Kubernetes without proper visibility is like flying an airplane blindfolded. As the number of microservices grows, the generation of metrics (numerical data measuring system behavior over time) and logs (textual event records) explodes. Traditional monolithic tools quickly suffer from severe bottlenecks, crashing due to RAM exhaustion caused by heavy local disk indexing. In practice, this means that the more your company grows, the more expensive and unstable your observability infrastructure becomes.

To solve this dilemma without sacrificing budget or stability, engineers rely on highly specialized and decoupled tools. Grafana Mimir handles metrics exclusively, offering full compatibility with the Prometheus ecosystem, while Grafana Loki manages logs using a minimalist approach that indexes only metadata. When combined in a refined architecture, these technologies process petabytes of data by intelligently splitting the workload across multiple cluster nodes. To ensure your database handles extreme write peaks without chronic slowdowns, it is also worth exploring advanced tuning strategies, such as what we discuss regarding how to scale PostgreSQL for high data volume in demanding scenarios.

Decentralized architecture: separating ingestion, compaction, and queries

The secret to preventing Mimir and Loki from exhausting Kubernetes memory lies in component decoupling. Instead of running everything on a single beefy server, the distributed architecture splits functions into specific blocks: distributors that receive data, ingestors that keep it temporarily in memory, and long-term storages that save everything compressed in object storage like AWS S3 or MinIO. Thus, if a log spike occurs, only the ingestion component experiences momentary pressure, isolating the issue.

In practice, distributors act as gatekeepers that validate and distribute incoming streams evenly among ingestors using consistent hashing. Meanwhile, ingestors accumulate data in RAM blocks for a short period (usually two hours) before permanently writing them to object storage. This design prevents complex long-term queries from hurting real-time ingestion performance. Furthermore, this modular separation simplifies resource tuning, allowing you to horizontally scale only the service experiencing a bottleneck.

Configuring memory limits and the Go Garbage Collector

Since both Mimir and Loki are built in Go, RAM management depends directly on how the Garbage Collector handles temporary object allocation. In Kubernetes environments, setting strict resource limits without calibrating internal runtime variables can cause sudden kernel terminations via OOM (Out Of Memory). To avoid unpleasant surprises during peak hours, it is essential to adjust the GOGC environment variable to control memory cleanup aggressiveness.

Another critical point is sizing query and index caches in memory. If the cache is too large, it depletes the pod's RAM; if it is too small, performance plummets and generates high CPU usage during searches. The practical recommendation is to monitor real consumption through Prometheus internal metrics and configure eviction policies to automatically discard less-used items. This way, the system strikes the ideal balance between response speed and operational stability.

Retention strategies and efficient object storage

Keeping telemetry data indefinitely on high-performance local disks is a severe financial and architectural mistake. The secret to economical scalability with Mimir and Loki is offloading raw volume to cost-effective object storage and configuring strict retention policies. In Loki, for instance, indexes are kept lightweight (using BoltDB-shipper or TSDB), while text chunks compressed via Snappy or Zstd travel directly to the cloud bucket.

When older data must be queried for auditing or trend analysis, the system fetches it directly from object storage without needing to keep heavy indexes permanently in RAM. This reduces infrastructure costs by up to 80% compared to traditional Elasticsearch-based solutions. To ensure your network handles sudden bursts of heavy packets heading to the cloud without degrading other server applications, check out how to configure QoS and fq_codel to prevent bottlenecks.

Best practices for secure deployment via Helm Charts

Deploying complex stacks in Kubernetes manually invites human error. Using official Helm Charts provided by Grafana Labs ensures that all dependencies, network policies, and persistent volumes are created with community-recommended parameters. However, default values in these packages are usually generic and targeted at lab environments, requiring fine-tuning before production use.

Before applying manifests to your production cluster, carefully review these fundamental operational steps:

  1. Adjust CPU and memory resource requests and limits across all microservices based on prior load testing.
  2. Properly configure storage classes and secure access credentials for the external object bucket using Kubernetes Secrets.
  3. Enable multi-tenancy if you need to isolate data from different teams or clients within the same observability cluster.

Following this validation order ensures your infrastructure spins up predictably and securely, shielding the environment against startup failures caused by missing credentials or incorrect permissions.

Final considerations

Centralizing logs and metrics in Kubernetes using Grafana Mimir and Loki stops being a headache when architecture prioritizes decoupling and object storage. By isolating ingestors, distributors, and compactors, and by rigorously tuning Go memory limits, your company gains a robust, scalable, and financially sustainable control tower. Observability shifts from being an operational burden to becoming the foundation for delivering software with greater speed, security, and peace of mind.