Page 1 of 8~245 min topic

Rate limiting lab

Define the production target for rate limiting

Ship a falsifiable slice of **layered token-bucket limiter for multi-tenant RAG API** — success is StormCo at 200 RPM gets 429 after burst; Globex at 40 RPM unaffected, not a polished screenshot.

~25 min this pageLab goal

1Try it yourself

Decision drill

Rate limit lab

Protect your app and wallet — throttle, backoff, or open the circuit.

API resilience65%

1/31000 req/s burst hits /api/chat from one API key.

2Learn the idea

Read

Name the operable slice

This lab builds layered token-bucket limiter for multi-tenant RAG API. The human in the loop is platform SRE stopping noisy neighbor tenant StormCo. Scope is intentionally narrower than “make AI reliable”: you will prove one oracle — StormCo at 200 RPM gets 429 after burst; Globex at 40 RPM unaffected — and one invariant — tenant RPM soft=60 hard=100; global 5k RPM; 429 includes Retry-After. Record non-goals in your notes so a later change cannot silently expand authority. The incident mnemonic for the chapter is RL-FAILOPEN-61; design as if that ticket is already written and you are filling evidence.

Read

Write the acceptance contract

Turn the oracle into a table: input fixture, expected observable, prohibited side effect, owner, latency/cost ceiling. Separate model taste from software correctness — transport, auth, parsing, and termination must be deterministic even when generated text varies. Primary metric family: 429_ratio_by_tenant and provider_429_ratio ≤ 0.01. Averages without a denominator or revision label do not gate release. Fake external dependencies in unit tests; live calls wait until fakes pass.

Read

Implementation artifact

tenant_rpm: { soft: 60, hard: 100 }
global_rpm: 5000
redis_fail_mode: closed  # serve 503 with retry, do not bypass

Read

Freeze the first red test

Before implementation, encode a failing check that would have caught Redis down → fail-open floods provider → shared 429s. That failure is the pedagogical north star for later pages: contracts reject it, happy path never performs it, validation asserts it, failure-handling contains it, observability detects it, security-ops prevents privilege tricks around it, and mastery replays it in a drill. Endpoint under study: POST /v1/rag/answer.

Read

Stage depth

Capacity note for planners: estimate peak demand on POST /v1/rag/answer and the cost ceiling for a failed retry storm. Write the abort conditions — unbounded spend, cross-tenant leakage, or inability to roll back — before you enjoy the first green test. Prefer synthetic fixtures shaped like production over anonymized production dumps you cannot share in class. When you are tempted to widen scope, re-read the oracle (StormCo at 200 RPM gets 429 after burst; Globex at 40 RPM unaffected) and cut features that do not serve it. The teaching outcome is judgment under constraints: platform SRE stopping noisy neighbor tenant StormCo gets a trustworthy control, not a kitchen-sink framework. Keep the language of release decisions: promote, hold, or roll back — never “see if it gets better.”

Read

Field notes for `rate-limiting-lab` / `lab-goal`

Decide what will live in version control on day one: fixtures, contract markdown, and a failing test name. Write the cost ceiling as a hard number with currency and period. If the lab involves clusters, name the non-prod context you will use and forbid prod kubecontexts in scripts. Capture the baseline metric once before changing code so later gains are comparative. Refuse tools that hide the request path behind magic macros until the oracle is green on fakes. Your README section for this page should be five lines or fewer and still falsifiable. In this chapter the product is layered token-bucket limiter for multi-tenant RAG API, the human stakeholder is platform SRE stopping noisy neighbor tenant StormCo, and the incident id you design against is RL-FAILOPEN-61. Re-state the oracle in your notes — StormCo at 200 RPM gets 429 after burst; Globex at 40 RPM unaffected — and keep the invariant visible: tenant RPM soft=60 hard=100; global 5k RPM; 429 includes Retry-After. Track 429_ratio_by_tenant and provider_429_ratio ≤ 0.01 as the scoreboard. Surface under change control: POST /v1/rag/answer. If you only have forty minutes, finish the fixture for Redis down → fail-open floods provider → shared 429s before polishing UI. Promotion language stays ternary: promote, hold, or roll back based on evidence, not hope.

Go deeper

Before you start

Why this matters

Write the single done-definition a reviewer would accept for Rate limiting lab (RL-FAILOPEN-61). Include the numeric gate hidden in this oracle: StormCo at 200 RPM gets 429 after burst; Globex at 40 RPM unaffected. Then name the fake success you refuse: a demo that ignores Redis down → fail-open floods provider → shared 429s. Keep the sentence beside your editor; every later page should make this sentence easier to prove.

Check your understanding

Page assessment

Answer from memory. Completion is saved from this evidence, not from opening the next page.

1. Is the oracle (StormCo at 200 RPM gets 429 after burst; Globex at 40 RPM unaffected) falsifiable from a fixture?
2. Is the invariant (tenant RPM soft=60 hard=100; global 5k RPM; 429 includes Retry-After) stated without hand-waving?
3. Does the contract name RL-FAILOPEN-61 as a risk you design against?

All responses are required.