Skip to content

⚡ Bolt: optimize rate limiter Redis round-trips#215

Draft
hackerxj2010 wants to merge 1 commit into
mainfrom
bolt/optimize-rate-limiter-redis-roundtrips-11518129073275994119
Draft

⚡ Bolt: optimize rate limiter Redis round-trips#215
hackerxj2010 wants to merge 1 commit into
mainfrom
bolt/optimize-rate-limiter-redis-roundtrips-11518129073275994119

Conversation

@hackerxj2010
Copy link
Copy Markdown
Owner

⚡ Bolt Performance Optimization

Mission: Optimize the SlidingWindowRateLimiter to reduce latency by minimizing Redis round-trips.

Implementation Details:

  • Modified SlidingWindowRateLimiter.consume in packages/cache/src/index.ts to include the zrange(key, 0, 0, "WITHSCORES") call within the initial Redis pipeline.
  • Refactored the method logic to utilize the oldest member's score from the pipeline result, eliminating the need for a separate await this.redis.zrange(...) call in both the "allowed" and "denied" paths.
  • Preserved existing logic for removing the member if the limit was exceeded (zrem).

Performance Impact:

  • Allowed Requests: Reduced from 2 round-trips (Pipeline + zrange) to 1 round-trip.
  • Denied Requests: Reduced from 3 round-trips (Pipeline + zrem + zrange) to 2 round-trips.
  • This change significantly reduces the tail latency of authenticated API requests in the gateway.

Verification:

  • Created a comprehensive test suite in packages/cache/src/index.test.ts that mocks ioredis to verify the exact number of round-trips.
  • Confirmed that all tests pass and that resetAt / retryAfterMs calculations remain correct.
  • Ran pnpm lint and pnpm test (filtered for the affected package).

Measurement:
Run pnpm exec vitest run packages/cache/src/index.test.ts to verify the round-trip counts.


PR created automatically by Jules for task 11518129073275994119 started by @hackerxj2010

Reduced Redis network round-trips in \`SlidingWindowRateLimiter.consume\` from 2 to 1 for successful requests and from 3 to 2 for denied requests by consolidating the \`zrange\` call into the initial pipeline.

💡 What: Included \`zrange\` in the initial pipeline to fetch the oldest member's score.
🎯 Why: Minimizing network round-trips is critical for the API Gateway as it runs rate limiting on every request.
📊 Impact: Reduces rate limiting latency by ~50% for successful requests and ~33% for denied requests (excluding Redis execution time).
🔬 Measurement: Verified via \`packages/cache/src/index.test.ts\` counting mocked Redis calls.

Co-authored-by: hackerxj2010 <198651211+hackerxj2010@users.noreply.github.com>
@google-labs-jules
Copy link
Copy Markdown
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.

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