Skip to content

Conversation

@anurag4DSB
Copy link
Contributor

Architecture of Token Reservation System

Token Reservation Flow:

  1. Workers maintain a local token buffer (50 tokens per worker)
  2. Background job refills tokens every 100ms (async, non-blocking)
  3. Redis atomically grants tokens using GCRA enforcement
  4. Request handling consumes tokens from local buffer (in-memory, fast)
  5. When tokens run low, worker requests more from Redis

Key Benefits:

  • Keeps Redis out of hot path: Token consumption is pure in-memory
  • Atomic enforcement: Redis Lua script enforces node-level quota atomically
  • Dynamic work-stealing: Busy workers automatically get more tokens, idle workers get none
  • Finite buffer prevents over-bursting: 50-token limit (5 seconds @ 10 req/s avg)

@anurag4DSB anurag4DSB changed the title CMDSRV-783: implement worker sync with Token Reservation System CLDSRV-783: implement worker sync with Token Reservation System Nov 18, 2025
const { config } = require('../../../Config');

const updateCounterScript = fs.readFileSync(`${__dirname }/updateCounter.lua`).toString();
const reconcileCounterScript = fs.readFileSync(`${__dirname }/reconcileCounter.lua`).toString();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was removed in the latest commit

@codecov
Copy link

codecov bot commented Nov 18, 2025

Codecov Report

❌ Patch coverage is 44.62366% with 103 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.63%. Comparing base (fef2c04) to head (a8b289a).
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
lib/api/apiUtils/rateLimit/tokenBucket.js 43.90% 46 Missing ⚠️
lib/api/apiUtils/rateLimit/cache.js 12.82% 34 Missing ⚠️
lib/api/apiUtils/rateLimit/refillJob.js 63.41% 15 Missing ⚠️
lib/api/apiUtils/rateLimit/client.js 12.50% 7 Missing ⚠️
lib/server.js 80.00% 1 Missing ⚠️

❌ Your patch check has failed because the patch coverage (44.62%) is below the target coverage (80.00%). You can increase the patch coverage or adjust the target coverage.

Additional details and impacted files

Impacted file tree graph

Files with missing lines Coverage Δ
lib/api/apiUtils/rateLimit/cleanup.js 100.00% <100.00%> (+4.76%) ⬆️
lib/api/apiUtils/rateLimit/config.js 100.00% <ø> (ø)
lib/api/apiUtils/rateLimit/gcra.js 100.00% <100.00%> (ø)
lib/api/apiUtils/rateLimit/helpers.js 100.00% <100.00%> (ø)
lib/server.js 77.71% <80.00%> (+0.06%) ⬆️
lib/api/apiUtils/rateLimit/client.js 72.00% <12.50%> (-22.12%) ⬇️
lib/api/apiUtils/rateLimit/refillJob.js 63.41% <63.41%> (ø)
lib/api/apiUtils/rateLimit/cache.js 50.72% <12.82%> (-49.28%) ⬇️
lib/api/apiUtils/rateLimit/tokenBucket.js 43.90% <43.90%> (ø)

... and 1 file with indirect coverage changes

@@                                     Coverage Diff                                     @@
##           feature/CLDSRV-781-add-pre-and-post-MD-fetch-rate-limit    #6009      +/-   ##
===========================================================================================
- Coverage                                                    84.23%   83.63%   -0.60%     
===========================================================================================
  Files                                                          203      205       +2     
  Lines                                                        12648    12815     +167     
===========================================================================================
+ Hits                                                         10654    10718      +64     
- Misses                                                        1994     2097     +103     
Flag Coverage Δ
ceph-backend-test 64.20% <26.88%> (-0.27%) ⬇️
file-ft-tests 66.33% <26.88%> (-0.31%) ⬇️
kmip-ft-tests 27.26% <20.96%> (+<0.01%) ⬆️
mongo-v0-ft-tests 67.57% <26.88%> (-0.36%) ⬇️
mongo-v1-ft-tests 67.59% <26.88%> (-0.34%) ⬇️
multiple-backend 34.45% <26.88%> (-0.01%) ⬇️
sur-tests 34.79% <26.88%> (-0.01%) ⬇️
sur-tests-inflights 36.63% <26.88%> (-0.04%) ⬇️
unit 69.23% <29.03%> (-0.64%) ⬇️
utapi-v2-tests 33.44% <20.96%> (-0.08%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@anurag4DSB anurag4DSB marked this pull request as ready for review November 18, 2025 23:11
@anurag4DSB anurag4DSB force-pushed the feature/CLDSRV-781-add-pre-and-post-MD-fetch-rate-limit branch from 0251b1b to fef2c04 Compare November 19, 2025 15:01
Implement token reservation architecture where workers request
capacity in advance from Redis. Workers maintain local token
buffers and consume tokens in-memory (no Redis in hot path).
Background refill job requests tokens every 100ms asynchronously.

Redis enforces GCRA atomically at token grant time, ensuring
strict quota enforcement across distributed workers.

Components:
- grantTokens.lua: Redis Lua script for atomic token granting
- tokenBucket.js: WorkerTokenBucket class for local token management
- refillJob.js: Background job for async token replenishment
- client.js: Add grantTokens() method to Redis client
Switch from optimistic GCRA with reconciliation to token
reservation. Workers consume tokens from local buffer instead
of evaluating GCRA per request. This keeps Redis out of the
hot path while enforcing strict quotas.

Changes:
- helpers.js: Use token consumption instead of GCRA evaluation
- server.js: Start token refill job instead of sync job
- cleanup.js: Add token bucket cleanup
- gcra.js: Mark workers parameter unused (kept for compatibility)
Remove syncIntervalMs and reconciliationWeight fields that were
used by the old reconciliation approach. Token reservation uses
a fixed 100ms refill interval instead.

What Each File Changes:

- config.json - Removed syncIntervalMs: 1000
- config.js - Removed parsing for syncIntervalMs and reconciliationWeight
- tokenBucket.js - Linting fixes (line length, return statements, braces)
- cache.js - Updated comments
- tests/config.js - Updated interval calculations for node-level quotas
- tests/gcra.js - Updated tests showing workers are ignored
- tests/helpers.js - Pre-populate token buckets, update assertions
@anurag4DSB anurag4DSB force-pushed the feature/CLDSRV-783-implement-worker-sync branch from 79cee31 to a8b289a Compare November 19, 2025 15:02
Base automatically changed from feature/CLDSRV-781-add-pre-and-post-MD-fetch-rate-limit to improvement/CLDSRV-766/bucket_rate_limiting November 19, 2025 17:13
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.

2 participants