PAU OJ Online Judge
ProblemsLearnStudy plansContestsSubmissionsRank

Updates

Announcements and upcoming contests

Loading announcements…
Sign in for personal alertsBrowse contests
Log inSign up

PAU OJ

PrimeAsia University Online Judge

A real online judge for programming practice, university contests and coding assessments. Write code in your browser, get sandboxed verdicts, and track your progress.

Platform

  • Problems
  • Contests
  • Submissions
  • Ranking
  • Reviewers

Resources

  • User guide
  • Changelog
  • How judging works
  • System health
  • Create an account

© 2026 PrimeAsia University Online Judge. All rights reserved.

v0.9.0

  1. All tracks
  2. Master class
  3. Rate Limiting

Rate Limiting

রেট লিমিটিং

Topic 26 of 41Story · visual · design practice

In this lesson

  • Story & concept
    • কেন দরকার
    • মূল ধারণা
    • কোডে দেখি
    • ধাপে ধাপে
    • সাধারণ ভুল
      • ল্যাব: token bucket হাতে চালাও
      • চিট শিট
      • প্রোডাকশন নোট
  • Visual story
  • Design practice
  • Checkpoint
  • Discussion
Back to track

0/0 solved

token/leaky bucket, sliding window, Redis Lua আর 429 semantics।

In this lesson

  • Story & concept
  • Visual story
  • Design practice
  • Checkpoint
  • Discussion
01

Step 1: Story & concept

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 আবার সাথে সাথে চেষ্টা করবে।

AlgorithmBurstMemoryUse
Fixed windowboundary burstকমsimple API
Sliding logনেইবেশিstrict
Sliding counterকমমাঝারিgeneral
Token bucketC পর্যন্তকমAPI burst
Leaky bucketনেইqueueshaping

কোডে দেখি

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।

সাধারণ ভুল

  • শুধু per-IP limit। Campus/carrier NAT-এ হাজার আসল ইউজার এক IP; per-user/global backstop দরকার।
  • Local in-memory counter। একাধিক instance-এ limit ফাঁকি; Redis atomic counter।
  • Fixed window boundary ভুলে যাওয়া। শেষ-শুরুতে ২x burst; sliding counter/token bucket।
  • 429-এ Retry-After না দেওয়া। Client সাথে সাথে retry করে load বাড়ায়।
  • Login-এ সব attempt গোনা। শুধু failed password limit; successful login নয়।
  • Redis down-এ silent unlimited। Read-এ fail-open, payment-এ fail-closed—policy লিখে রাখো।

ল্যাব: token bucket হাতে চালাও

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 ভাবো।

02

Step 2: Visual story

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।

1token bucket
2leaky bucket
3fixed window
4sliding window
5429

token bucket → leaky bucket → fixed window → sliding window → 429

  1. 1token bucket ভরোcapacity ১০০, প্রতি সেকেন্ডে ২০ যোগ
  2. 2leaky bucket দিয়ে সমান করোবাইরে বেরোনোর হার স্থির
  3. 3fixed window-এর ফাঁক দেখোদুই window-র সন্ধিতে দ্বিগুণ যায়
  4. 4sliding window দিয়ে ঠিক করোগত ৬০ সেকেন্ডের হিসাব
  5. 5429-এ Retry-After দাওক্লায়েন্ট জানে কখন ফিরবে
5টি ধাপে দেখা হচ্ছেগতি কমালে স্থির
03

Step 3: Design practice

Write a short design on paper or in a note, then compare it with the rubric. This is not a judge submission.

Design practice

One script, 200 RPS

20 min

fare API-তে এক client ২০০ RPS; campus NAT-এ হাজার user।

তোমার কাজ

  1. 1per-IP/user/route/global limit design করো।
  2. 2token bucket vs sliding window বাছো।
  3. 3429/Retry-After ও Redis down policy লেখো।

ভালো উত্তরে কী থাকবে

  • layer/limit justified
  • algorithm/atomic counter
  • 429/fallback
04

Step 4: Checkpoint

One timed attempt. Solve every problem to earn the module badge.

Final checkpoint

Ready to prove this module?

45 minutes3 problemsScale Architect badge
Go to checkpoint
05

Step 5: Discussion

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.

Sign in to react

Discussion

0 comments

Sign in to ask a question or share an approach.

Sign in to comment

No comments yet

Be the first to ask a question or leave a study tip.

Topic 26 of 41

PreviousRedundancy & Failoverরিডান্ডেন্সি ও ফেইলওভারTopic 25 of 41
NextTimeouts & Retriesটাইমআউট ও রিট্রাইTopic 27 of 41
Back to track: System Design MasterclassAll learning tracks
প্রশ্নউত্তর
Burst?token bucket C
Smooth?leaky bucket
Distributed?Redis Lua
429?Retry-After
NAT?IP + user + global
Redis down?explicit fallback