রেট লিমিটিং
token/leaky bucket, sliding window, Redis Lua আর 429 semantics।
Start with the production incident, then the design decision that fixes it.
শনিবার রাতে একটা স্ক্রিপ্ট চলো-র fare API-তে সেকেন্ডে ২০০ রিকোয়েস্ট পাঠাচ্ছে। সার্ভার CPU ১০০%, আসল ইউজাররা 503 পাচ্ছে। কোনো rate limit ছিল না—API খোলা ছিল সবার জন্য। নুসরাত emergency-তে nginx-এ IP limit বসাল, কিন্তু campus NAT-এ হাজার ইউজার একই IP; তখন আসল ইউজারও ব্লক হলো।
Rate limiting মানে প্রতিটা client-কে একটা fair budget দেওয়া, যাতে একজন বাকিদের ক্ষতি না করে। কিন্তু শুধু IP নয়—per user, per API key, per route, global backstop—স্তর অনুযায়ী limit লাগে। আর limit প্রয়োগের জায়গা (edge vs app) আর algorithm (fixed window vs token bucket) ঠিক না করলে limit নিজেই outage বানায়।
Token bucket। Bucket capacity C, refill rate R/sec। প্রতিটা request ১ token নেয়; token না থাকলে 429। Burst C পর্যন্ত allow, long-term R। Fare API-তে C=20, R=10/sec ভালো।
Leaky bucket। Queue-এ ভরে fixed rate-এ বের করে; smooth output, burst নেই। Network shaping-এ ভালো।
Fixed window। প্রতি মিনিটে counter reset; window boundary-তে ২x burst সম্ভব (শেষে ১০০, শুরুতে ১০০)। Sliding window log/counter দিয়ে মসৃণ, memory বেশি।
স্তর। Per-IP (anonymous edge), per-user/API key (authenticated), per-route (login/payment), global (Redis/edge backstop)। শুধু IP limit campus NAT-এ ভুল; login-এ per-IP failed attempt + per-account limit।
Distributed counter। একাধিক app instance-এ local counter ভুল; Redis INCR/Lua atomic। Redis down হলে local fallback + fail-open/closed decision।
Response। 429 + Retry-After, X-RateLimit-Remaining/Reset; error body স্পষ্ট। Retry-After ছাড়া client আবার সাথে সাথে চেষ্টা করবে।
| Algorithm | Burst | Memory | Use |
|---|---|---|---|
| Fixed window | boundary burst | কম | simple API |
| Sliding log | নেই | বেশি | strict |
| Sliding counter | কম | মাঝারি | general |
| Token bucket | C পর্যন্ত | কম | API burst |
| Leaky bucket | নেই | queue | shaping |
Redis Lua দিয়ে atomic token bucket:
-- KEYS[1] = bucket key, ARGV[1] = capacity, ARGV[2] = refill/sec, ARGV[3] = now ms
local capacity = tonumber(ARGV[1])
local rate = tonumber(ARGV[2])
local now = tonumber(ARGV[3])
local data = redis.call("HMGET", KEYS[1], "tokens", "ts")
local tokens = tonumber(data[1]) or capacity
local ts = tonumber(data[2]) or now
tokens = math.min(capacity, tokens + (now - ts) / 1000 * rate)
local allowed = tokens >= 1
if allowed then tokens = tokens - 1 end
redis.call("HMSET", KEYS[1], "tokens", tokens, "ts", now)
redis.call("EXPIRE", KEYS[1], 120)
return { allowed and 1 or 0, math.floor(tokens) }
def allow(user_id, route):
key = f"rl:{user_id}:{route}"
ok, remaining = redis.eval(LUA, 1, key, 20, 10, now_ms())
return bool(ok), remaining
Lua atomic, তাই check-then-set race নেই। Local fallback-এ per-instance bucket, কিন্তু global limit নয়।
১. কে limit করবে ঠিক করো। Anonymous IP, authenticated user/API key, route, global। একই limit সবখানে নয়।
২. Budget বেছে নাও। Normal usage p99 × 2–3; login/payment stricter; create link per-user quota।
৩. Algorithm। Burst দরকার হলে token bucket; strict smoothing হলে leaky; simple হলে sliding counter।
৪. Atomic counter। Redis Lua/INCR; TTL; key version; Redis down fallback policy (fail-open for read, fail-closed for payment)।
৫. Response/monitor। 429 + Retry-After; metrics per route; limit বদলালে canary; campus NAT-এর জন্য IP limit শিথিল + global backstop।
C=5, R=1/sec নিয়ে ৭টা request সাথে সাথে পাঠাও: প্রথম ৫টা allow, শেষ ২টা 429। ১ সেকেন্ড পরে আবার ১টা allow। কাগজে token হিসাব লেখো; তারপর ভাবো C=20, R=10 হলে ৩০ রিকোয়েস্ট burst-এ কত allow হবে।
Rate limit-এ user-facing error স্পষ্ট রাখো; 429 কে 500 ভাবা যাবে না। Campus/carrier NAT-এ per-IP limit খুব শিথিল রাখো, নইলে হাজার আসল ইউজার ব্লক হবে। Login-এ শুধু failed password count করো, successful login নয়; payment-এ fail-closed, feed-এ fail-open ভাবো।
See the mental model first — the animation and the steps below it say the same thing.
Concept visual
token bucket হঠাৎ burst সামলায়, sliding window সীমা কড়া রাখে—শেষে 429 আর Retry-After।
Write a short design on paper or in a note, then compare it with the rubric. This is not a judge submission.
Design practice
fare API-তে এক client ২০০ RPS; campus NAT-এ হাজার user।
One timed attempt. Solve every problem to earn the module badge.
Final checkpoint
Ready to prove this module?
Ask questions, share an approach or leave a study tip for this lesson.
Was this lesson helpful?
Your reaction helps other learners judge these notes.
0 comments
Sign in to ask a question or share an approach.
Sign in to commentNo comments yet
Be the first to ask a question or leave a study tip.
| প্রশ্ন | উত্তর |
|---|
| Burst? | token bucket C |
| Smooth? | leaky bucket |
| Distributed? | Redis Lua |
| 429? | Retry-After |
| NAT? | IP + user + global |
| Redis down? | explicit fallback |