vector icon indicating copy to clipboard operation
vector copied to clipboard

bug(elasticsearch sink): Unbounded in-flight retrying batches cause OOM during sustained downstream failures

Open yusufcemalcelebi opened this issue 7 months ago • 2 comments

Description

When the Elasticsearch sink's downstream service returns sustained 5xx errors (e.g., 503 from an Istio/Envoy gateway when the backend is unhealthy), Vector's memory grows unboundedly until the process is OOMKilled. This happens because retrying batches accumulate in memory without any limit, and no backpressure is propagated to sources or buffers.

Root Cause

The distributed_service method in src/sinks/util/service.rs (line 340-367) constructs the tower service layer with this ordering:

rate_limit → retry → buffer(1) → balance → [per-endpoint: concurrency → health → timeout → http]

The retry layer is outside the concurrency limit. This means:

  1. A request is sent → gets 503 → enters retry with fibonacci backoff
  2. The concurrency permit is released during backoff sleep
  3. service.poll_ready() returns Ready immediately (the concurrency slot is free, the buffer drains instantly because 503 responses are near-instant)
  4. The driver submits the next batch — which also fails and enters its own independent retry loop
  5. This repeats indefinitely — each batch creates a new retrying future in the driver's unbounded FuturesUnorderedCount

Since the default retry_attempts is isize::MAX (effectively unlimited), these futures never resolve during the outage. Memory grows linearly with (event_throughput / batch_size) × outage_duration.

Compare this with the single-service TowerRequestLayer path (service.rs line 391-403):

rate_limit → concurrency → retry → timeout → http

Here retry is inside concurrency — a retrying request holds its permit during backoff, naturally blocking new batches and propagating backpressure. The distributed_service path does not have this property.

Expected Behavior

When the downstream service is unavailable:

  1. Batches should retry with backoff ✅ (this works)
  2. The number of in-flight retrying batches should be bounded ❌
  3. Once the limit is reached, backpressure should propagate to the topology buffer and then to sources ❌
  4. Memory usage should remain stable during the outage ❌

Actual Behavior

  • Retrying batches accumulate without limit in the driver's FuturesUnorderedCount
  • The topology buffer never fills because the sink keeps consuming events (the problem is at the output end)
  • No backpressure reaches the sources
  • Memory grows linearly until OOMKill

Environment

  • Vector version: latest (affects all versions using distributed_service)
  • Deployment: Kubernetes (DaemonSet + Deployment/aggregator)
  • Sink: Elasticsearch sink (used with VictoriaLogs Elasticsearch-compatible API)
  • Network: Istio gateway (Envoy) between Vector and backend — returns 503 when backend pods are unhealthy
  • Throughput: ~1k-10k events/sec
  • Time to OOM: Minutes of sustained 503 responses
  • Buffer config: Default (memory buffer, no explicit config)
  • Retry config: Default (retry_attempts = isize::MAX, unlimited)

Minimal Reproduction Config

sources:
  logs:
    type: file
    include:
      - /var/log/**/*.log

sinks:
  elasticsearch:
    type: elasticsearch
    inputs:
      - logs
    endpoints:
      - http://elasticsearch:9200
    # All request/retry/batch settings are defaults

Steps to reproduce:

  1. Deploy Vector with the above config pointing to an Elasticsearch-compatible API behind an Istio gateway
  2. Generate sustained log throughput (~1k+ events/sec)
  3. Make the backend unavailable (e.g., scale to 0 replicas) so the gateway returns 503
  4. Observe Vector memory growing linearly without bound
  5. Vector is eventually OOMKilled

Workaround

Set a finite retry_attempts to prevent unbounded accumulation:

sinks:
  elasticsearch:
    request:
      retry_attempts: 15  # ~114 seconds of retrying per batch with default backoff

Note: This causes data loss when retries are exhausted. There is no workaround that preserves both bounded memory and zero data loss.

Severity

High — With default configuration, any sustained downstream failure (common in Kubernetes environments with service meshes, rolling upgrades, or pod evictions) leads to inevitable OOMKill. Users would not expect default settings to cause unbounded memory growth.

yusufcemalcelebi avatar Apr 16 '26 15:04 yusufcemalcelebi

Additional Workaround: Use the HTTP sink instead

Confirmed that switching from the Elasticsearch sink to the HTTP sink resolves the issue. The HTTP sink uses the single-service TowerRequestLayer path (service.rs:391-403) where retry is inside the concurrency limit:

rate_limit → concurrency → retry → timeout → http

With this ordering, retrying batches hold their concurrency permits during backoff, poll_ready() blocks, and backpressure correctly propagates to buffers and sources. Memory stays bounded during sustained 503 failures.

Tested with VictoriaLogs (which exposes an Elasticsearch-compatible API) behind an Istio gateway — when the backend returned 503, backpressure kicked in as expected and no unbounded memory growth was observed.

This further confirms the root cause is the distributed_service layer ordering used by the Elasticsearch sink.

Note: If using adaptive concurrency (default), also consider setting request.adaptive_concurrency.max_concurrency_limit to a reasonable value. The default of 200 means worst-case in-flight memory is 200 × batch_size × num_sinks (e.g., 200 × 1MB × 20 sinks = 4GB). A lower value like 10-20 per sink keeps the memory footprint manageable.

yusufcemalcelebi avatar Apr 16 '26 16:04 yusufcemalcelebi

Spent some time in this code today. The analysis here looks right to me, but incomplete in a way that changes what the repro should look like.

The layer ordering checks out. src/sinks/util/service.rs:40-56 spells it out: the single-service path is AdaptiveConcurrencyLimit<Retry<...>>, concurrency outside retry, while distributed_service builds Retry<Buffer<Balance<...>>> with the concurrency limit per-endpoint inside the retry. And the permit is dropped before the backoff sleep (adaptive_concurrency/future.rs:31-35), so nothing holds a slot while a batch waits.

What isn't mentioned is that distributed_service always installs a per-endpoint circuit breaker (src/sinks/util/service/health.rs), and it's on by default for ES. For the scenario in the report, sustained 503, it does trip. ElasticsearchHealthLogic::is_healthy maps status.is_server_error() to Some(false) (src/sinks/elasticsearch/health.rs:19-20), the unhealthy counter climbs, and the service starts returning Pending, which propagates backpressure to the driver. So the 503 case is bounded, roughly at max_concurrency_limit x endpoints, which lines up with the ~4GB figure computed above.

What isn't bounded is anything the health logic declines to classify. Cross-referencing is_healthy against should_retry_response:

  1. is_healthy returns Some(false) only for 5xx; 4xx falls through to None (health.rs:21-22), so the counters are never touched. Meanwhile retry.rs:114 retries it explicitly. The breaker can never trip and the retry never stops. I'd argue this is the more important case, since 429 is what Elasticsearch returns when the bulk queue fills up, which makes it a likelier production trigger than a hard 503.

Request timeouts. Elapsed doesn't downcast to HttpError::CallRequest, so it lands in the _ => None arm (health.rs:25-27). Same outcome.

200 with "errors": true under request_retry_partial. is_healthy sees a success status and returns Some(true), which actively resets the unhealthy streak, while the retry logic keeps retrying the failed items.

So bounding in-flight retries looks like the right fix either way, but the filed repro understates it. A 429 loop grows unbounded in a way the 503 case doesn't.

On the fix: tower::limit::ConcurrencyLimit holds its permit for the whole inner future, so a .concurrency_limit(n) placed outside the retry in distributed_service (service.rs:362-367) would span every attempt and its backoff. That's the property the single-service path already gets from TowerRequestLayer (service.rs:389-404), so it brings the distributed path in line with Vector's own default rather than introducing a new mechanism. Sizing it as per_endpoint * num_endpoints should make a throughput regression impossible by construction.

Happy to put a PR together if that direction sounds reasonable.

1991santhu avatar Aug 02 '26 22:08 1991santhu