Skip to main content

Claude/ChatGPT Prompt to Design a Distributed Rate Limiter

Design a multi-tier distributed rate limiter with Redis token buckets, per-user/per-IP limits, and Redis-down failure handling.

Vul de plaatshouders in

Edit the values, then copy your finished prompt.

Jouw Prompt
prompt.txt

                                

What this prompt does

This prompt directs the AI to design a distributed rate limiter tightly enough to build, with real tradeoffs rather than generic advice. You provide the [limit_dimensions], the [tiers], the [backend_store], and the [failure_policy]. It returns an algorithm comparison (fixed window, sliding window, token bucket), a store-backed token bucket that enforces limits across many app servers, per-dimension keys and quotas, client-facing headers with a Retry-After contract, the failure behaviour when the store is unreachable, and an atomicity strategy so concurrent requests don't over-admit.

The structure works because a sloppy limiter either lets abuse through or takes the whole API down when the store hiccups. Deciding fail-open versus fail-closed deliberately is the call most people skip, and making the bucket update atomic prevents two requests both winning the last token. [limit_dimensions] defines the keys, [tiers] map to refill rates, [backend_store] backs the shared counter, and [failure_policy] dictates behaviour during an outage.

When to use it

  • You're building an API limiter and need it consistent across many servers.
  • You're choosing between fixed window, sliding window, and token bucket.
  • You need per-user and per-IP limits with tier-based quotas.
  • You want correct rate-limit headers and a Retry-After contract for clients.
  • You must decide fail-open versus fail-closed when the store is down.
  • You need atomic bucket updates so concurrent requests can't over-admit.

Example output

You get the design plus sketch code for the core limiter check: an algorithm choice comparing fixed window, sliding window, and token bucket with tradeoffs for [tiers]; a [backend_store]-backed token bucket enforcing limits consistently across many app servers; per-dimension keys and quotas for [limit_dimensions] with tiers mapping to refill rates; client-facing headers (limit, remaining, reset) and a Retry-After contract; the [failure_policy] behaviour when the store is unreachable, fail-open versus fail-closed, and why; and an atomicity strategy such as a Lua script so concurrent requests don't over-admit.

Pro tips

  • Decide [failure_policy] deliberately — fail-open keeps the API up but lets abuse through during an outage, while fail-closed protects the system but can block legitimate traffic; pick the one your service can live with.
  • Make the bucket update atomic from day one (Lua or equivalent), or two concurrent requests will both win the last token and you'll over-admit under load.
  • Set [limit_dimensions] to exactly what you key on, since each dimension needs its own counter and quota.
  • Map [tiers] to clear refill rates so free, pro, and enterprise behave predictably and quotas are easy to reason about.
  • Match [backend_store] to what you actually run; the atomicity approach depends on the store's scripting or transaction support.
  • Return the rate-limit headers consistently and document the Retry-After contract, so well-behaved clients can back off instead of hammering you.

Frequently Asked Questions

Which rate-limiting algorithm does it recommend?
It compares fixed window, sliding window, and token bucket with the tradeoffs for your `[tiers]`, then designs around a token bucket backed by your store. Token bucket handles bursts gracefully while enforcing an average rate, which suits tiered quotas with different refill rates.
How does it stay consistent across multiple app servers?
By backing the token bucket with a shared `[backend_store]` rather than per-server memory, so every server reads and updates the same counter. The atomicity strategy, such as a Lua script, ensures concurrent requests against that shared counter cannot over-admit.
What happens when the backend store goes down?
That is governed by `[failure_policy]`. Fail-open keeps the API serving but lets abuse through during the outage, while fail-closed protects the system but can block legitimate traffic. The prompt makes you choose deliberately and explains why, since this is the call people skip.
Why is atomicity emphasized?
Without an atomic bucket update, two concurrent requests can both read the last available token and both succeed, over-admitting beyond the limit. Deliverable 6 uses a Lua script or equivalent so the check-and-decrement happens as one operation, which matters most exactly when traffic spikes.
Does it define the client-facing rate-limit headers?
Yes. Deliverable 4 specifies the limit, remaining, and reset headers plus a Retry-After contract. Returning these consistently lets well-behaved clients back off on their own instead of retrying immediately and amplifying the load you are trying to limit.
Engr Mejba Ahmed

Need this built for real?

Engr Mejba Ahmed

AI Developer · Software Engineer

I'm Mejba — I design and ship production AI systems, automations, and full-stack apps. If you want this turned into a working solution for your team, let's talk.

Meer in System Design Prompts

Engr Mejba Ahmed

Engr Mejba Ahmed

Claude Code Expert · Online

👋

Hey there!

Quick Actions

WhatsApp Instant reply

Chat on WhatsApp

+880 1723 741224 · Instant reply

Popular Questions

Engr Mejba Ahmed is connected
Engr Mejba Ahmed is typing...
Engr Mejba Ahmed avatar

✉ Want me to follow up? Drop your email

Engr Mejba Ahmed avatar

📞 Connect Directly

Choose how you'd like to reach me

WhatsApp

+880 1723 741224

Email

[email protected]

✓ Details sent! I'll get back to you shortly.

Powered by OpenAI

335+

Blog Posts

25

AI Courses

63

Projects

Services & Expertise

Pricing & Process

Learning & Resources

Connect & Support