Skip to content

⚡ Optimize Blocking AWS Bedrock Calls in Async Methods#6

Open
tstapler wants to merge 1 commit intomasterfrom
perf-optimize-bedrock-blocking-calls-949472333596472778
Open

⚡ Optimize Blocking AWS Bedrock Calls in Async Methods#6
tstapler wants to merge 1 commit intomasterfrom
perf-optimize-bedrock-blocking-calls-949472333596472778

Conversation

@tstapler
Copy link
Owner

The AWS Bedrock provider was using synchronous boto3 calls (invoke_model and invoke_model_with_response_stream) directly within asynchronous methods. This caused the entire proxy server's event loop to block while waiting for Bedrock responses, effectively serializing all requests to this provider.

I have optimized both send_message and stream_message by:

  1. Offloading the initial API calls to a thread pool using asyncio.to_thread.
  2. Offloading the blocking response["body"].read() operation.
  3. Converting the synchronous EventStream iteration into an async-safe loop by wrapping each next() call in asyncio.to_thread.

Verification using concurrent benchmark scripts confirmed that the implementation is now non-blocking, reducing the total time for 3 concurrent requests from 3.00s to 1.02s.


PR created automatically by Jules for task 949472333596472778 started by @tstapler

Modified `BedrockProvider` to use `asyncio.to_thread` for all blocking operations, including:
- `invoke_model` and `invoke_model_with_response_stream` calls.
- Reading the response body in `send_message`.
- Iterating over the `EventStream` in `stream_message`.

This ensures that the FastAPI event loop is not blocked during AWS Bedrock interactions, significantly improving concurrency and throughput.

Performance measurements (for 3 concurrent requests):
- Before: 3.00s
- After: 1.02s (~66% reduction in total time for concurrent load)

Co-authored-by: tstapler <3860386+tstapler@users.noreply.github.com>
@google-labs-jules
Copy link
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@tstapler
Copy link
Owner Author

@jules Can we use anyio for our io/async here?

@google-labs-jules
Copy link
Contributor

@jules Can we use anyio for our io/async here?

Good suggestion! I've updated the implementation to use anyio for offloading blocking I/O to threads. This is consistent with FastAPI's underlying architecture and provides a clean way to handle these synchronous calls in an async context. I've also added anyio to the requirements.txt.

@tstapler
Copy link
Owner Author

We have merge conflicts now please address them @jules

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant