Skip to content

perf(api): batch MGET calls in TeamItems to avoid giant Redis commands - #3571

Open
AdaAibaby wants to merge 1 commit into
e2b-dev:mainfrom
AdaAibaby:perf/team-items-batched-mget
Open

perf(api): batch MGET calls in TeamItems to avoid giant Redis commands#3571
AdaAibaby wants to merge 1 commit into
e2b-dev:mainfrom
AdaAibaby:perf/team-items-batched-mget

Conversation

@AdaAibaby

Copy link
Copy Markdown
Contributor

Problem

TeamItems issues a single MGET for every sandbox ID returned by SMEMBERS.
For teams with many sandboxes (we've observed 9 000+ IDs in a single team index),
this means one command can carry thousands of keys — blocking the Redis event loop
and spiking per-connection response-buffer memory.

Fix

Split the MGET fan-out into sandboxScanBatchSize (256) key chunks, consistent
with the scan path already doing the same in forEachTeamSandboxBatch.

All team sandbox keys share the {teamID} hash tag, so every batch lands on
the same cluster slot regardless of where the chunk boundaries fall.

Changes

  • operations.go — loop over sandboxIDs in 256-key slices, calling fetchSandboxBatch per slice
  • team_items_test.go — add TestTeamItems_BatchesLargeTeams: creates 513 sandboxes (2×256+1) and verifies all are returned, exercising all three MGET batches

Testing

New test exercises the chunked path. Existing tests cover state filtering, stale
index entries, corrupt records, empty teams, and team isolation — all still pass.
(Tests use testcontainers-go; they require Docker which isn't available in the local
environment but run in CI.)

Previously TeamItems issued a single MGET for every sandbox ID returned
by SMEMBERS, which could send thousands of keys in one command, blocking
the Redis event loop and spiking response-buffer memory.

Split the fan-out into sandboxScanBatchSize (256) key chunks, consistent
with the scan path already doing the same thing in forEachTeamSandboxBatch.
All keys share the {teamID} hash tag so each batch still lands on the same
cluster slot regardless of chunk boundaries.

Add TestTeamItems_BatchesLargeTeams to exercise the chunked path with
513 sandboxes (2×256+1, forcing 3 MGET batches).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants