Skip to content

Add example demonstrating batch write optimization for streaming calls#12528

Closed
James-4u wants to merge 1 commit intogrpc:masterfrom
James-4u:james/batch-write-optimization
Closed

Add example demonstrating batch write optimization for streaming calls#12528
James-4u wants to merge 1 commit intogrpc:masterfrom
James-4u:james/batch-write-optimization

Conversation

@James-4u
Copy link
Copy Markdown

Summary

This PR adds a new example (BatchWriteOptimizationExample) that demonstrates the batch write optimization pattern for streaming gRPC calls. This example shows that the pull/batch write feature is already implemented and available through the isReady() and setOnReadyHandler() API.

Motivation

Several older issues requested support for batch writes to optimize flushing and reduce buffer allocations when sending many small messages. This example demonstrates that this functionality has been available for years through the manual flow control API.

This example confirms that #454 can be closed, open issues makes visitors confusing.

What This Example Demonstrates

  1. Batch Writing Pattern: Shows how to write multiple messages in a tight loop while isReady() returns true, which naturally batches messages before flushing

  2. Backpressure Handling: Demonstrates how isReady() becomes false when backpressure occurs, and how setOnReadyHandler() is called when the transport is ready again

  3. Performance Metrics: Includes counters that show:

    • Total messages written
    • Number of batches (flushes)
    • Average messages per batch
  4. Both Client and Server: Shows the pattern on both client-side and server-side streaming

Key Code Pattern

requestStream.setOnReadyHandler(() -> {
    // Write multiple messages in a batch while isReady() is true
    while (requestStream.isReady() && hasMoreData()) {
        requestStream.onNext(nextMessage());  // Batched!
    }
    // When isReady() becomes false, this handler will be called again
});

Files Changed

✅ examples/src/main/java/io/grpc/examples/manualflowcontrol/BatchWriteOptimizationExample.java - New example with detailed comments
✅ examples/src/main/java/io/grpc/examples/manualflowcontrol/README.md - Updated with new section

Contribution by Gittensor, learn more at https://gittensor.io/

@James-4u James-4u closed this Nov 27, 2025
@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Feb 26, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant