Bitpath

Boss lesson · Unit 03

Protect the API

Token buckets, 429 and Retry-After, quotas, signed webhooks and what CORS is not

13 exercisesabout 3 min

What you practise

  • A token bucket worked as arithmetic, 429 and Retry-After, quotas per key, signed webhooks, CORS in one diagram

Try a question from this lesson

Pick one

One customer's buggy script sends 5,000 requests a second, and every other customer starts timing out. The API checks keys but nothing else. What is missing?

The ideas

Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.

Concept

The token bucket

Each caller gets a bucket that holds up to capacity tokens and is refilled at a steady rate. A request takes one token. No token, no service. (These tokens are plain counters, nothing to do with access tokens.)

A full bucket allows a short burst. Over time, the caller cannot average more than the refill rate.

capacity 10   refill 2 per second

burst of 10   -> bucket empty
1 s later     -> 2 tokens
5 s later     -> full again (10)

Concept

Rate limits and quotas

A rate limit works over seconds or minutes and protects the service: no caller may flood it.

A quota works over a day or a month and enforces a plan: so many calls for what you pay.

Both are counted per key, and both answer 429 when used up. For a quota, the wait in Retry-After can be hours.

Concept

CORS in one diagram

A browser will not let a page read replies from another origin unless that origin says so. That is CORS.

It protects users in browsers. It is not authentication, and it does not protect the API: curl, scripts and other servers never ask and are never stopped.

page from app.example, in a browser
   | fetch api.example/orders
   v
browser sends  Origin: https://app.example
API replies    Access-Control-Allow-Origin
browser: match? then the page may read it

9 more exercises in the app

  • 3× type the answer
  • 1× fill the blanks
  • 4× multiple choice
  • 1× true or false

Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.

One email when Bitpath is ready to try. Nothing else. Privacy