Skip to content

[FEATURE] Parallelize S3 message retrieval in S3SessionManager #1164

@CrysisDeu

Description

@CrysisDeu

Problem Statement

Summary

Optimize S3SessionManager.list_messages() to retrieve message files from S3 in parallel instead of sequentially, significantly improving performance for sessions with many messages.

Problem

Currently, list_messages() loads message files one at a time:

for key in message_keys:
    message_data = self._read_s3_object(key)  # Sequential - slow!

Performance Impact:

  • 100 messages × 50ms latency = ~5 seconds
  • Network I/O bottleneck: each request waits for the previous to complete

Proposed Solution

Solution

Use ThreadPoolExecutor to fetch multiple S3 objects concurrently:

with ThreadPoolExecutor(max_workers=None) as executor:
    future_to_key = {executor.submit(self._read_s3_object, key): key 
                     for key in message_keys}
    # Process results as they complete, maintaining order

Performance Improvement:

  • 100 messages in parallel = ~50-200ms
  • 10-100x speedup for large message histories

Implementation

  • ✅ Added concurrent.futures imports
  • ✅ Parallelized list_messages() using ThreadPoolExecutor
  • ✅ Maintains message order (sorts by original index)
  • ✅ Graceful error handling (individual failures don't stop operation)
  • ✅ Backward compatible (no API changes)

Testing Checklist

  • Test with various message counts (1, 10, 100, 1000)
  • Verify message order is preserved
  • Test error handling (missing files, network errors)
  • Performance benchmarks (before/after comparison)

Use Case

any session with 5+ turns can see increased latency

Alternatives Solutions

No response

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions