﻿# Ray generators: slow iteration does not automatically slow the producer

Review eager Ray generator execution, retained unconsumed outputs and cancellation instead of assuming Python-style demand-driven production.

Author: Retrivium team
Updated: 2026-11-06
Canonical URL: https://retrivium.ai/journal/ray-remote-generator-backpressure/
Language: en

A remote generator decodes large partitions while its caller writes results slowly. Switching from a returned list to yield reduces one local accumulation, but it does not automatically make production follow downstream demand. Ray's generator execution differs from an ordinary local Python iterator.

## Locate the production boundary

The [Ray generator guide](https://docs.ray.io/en/latest/ray-core/ray-generator.html) states that remote generator tasks execute eagerly rather than pausing solely until the caller requests another item. Identify where decoding creates a large value and where a consumer actually finishes processing it. The [Serve streaming guide](/journal/fg2-ray-serve-streaming-generator-not-unary-response-chain/) concerns choosing the proper response type; the present question is whether production and retained output remain bounded after the iterator API is already used correctly.

## Distinguish references from consumed values

Iteration returns ObjectRefs for yielded values, not the values themselves. Fetching the next reference can therefore advance iterator consumption while downstream processing remains unfinished. A caller that quickly collects every reference into a list has reconstructed a backlog. Record separately yielded, retrieved-reference, fetched-value and application-completed states. The [queue scope guide](/journal/fg2-ray-serve-max-queued-per-caller-not-global-cap/) explains why moving a backlog to another component does not establish an overall memory or work bound.

## Read private backpressure cautiously

The [development streaming internals](https://docs.ray.io/en/master/core/internals/streaming-generator.html) describe a private object-count backpressure option. That page is a master-branch implementation contract, not an undated promise of a stable released configuration. Its limit concerns generated but unconsumed objects and does not directly limit bytes or downstream processing. The described option also excludes async generators. If evaluating it, pin the actual runtime and verify support instead of copying a private setting into a general production recommendation.

## Bound the application workload deliberately

Use naturally bounded task chunks or an explicit producer-consumer acknowledgement design when the application requires end-to-end flow control. Define the maximum retained output size as well as item population, since equally counted partitions can differ greatly in bytes. Release fetched results and references after their application purpose ends. Partial persistence must retain the completed input identities so an interruption does not confuse yielded data with an accepted dataset output.

## Test slow and interrupted consumers

In a disposable fixture, delay downstream processing and observe production, reference retention and memory together. Then stop consuming while retaining the generator, and separately exercise documented cancellation. The development internals distinguish deleting the caller's stream from cancelling remote user code. Verify the actual runtime's outcome rather than assuming iterator abandonment stops decoding. Acceptance is an explained bound and stopping protocol, not a passing first-chunk demonstration or an asserted measurement of memory saved by streaming.

## Preserve terminal error handling

The generator guide distinguishes partial outputs from an eventual application or system error. Handle the returned references' errors and record which outputs were accepted before failure. A generator finishing is not equivalent to every downstream effect completing. The bounded design must also define cancellation cleanup and restart identities. It does not guarantee transactional streaming, stable private API availability, or a byte ceiling from an object-count limit, and it must not claim lazy Python semantics merely because caller syntax resembles a local for loop.
