Machine Room

Machine Room · 11 Nov 2026 · 13:28

What Is a REST API? Endpoints, Auth, Rate Limiting and Caching Explained

Watch on YouTube

REST, auth, pagination, rate limiting, idempotency, versioning and caching are the seven levels of REST API design, and each one fixes what the last new caller broke. This episode grows one flight booking API from our own website to a mobile app, a flight comparison site and a flash sale that sends sixty thousand phones to one page.

REST endpoints name the resource and let the HTTP method (GET, POST, PUT, DELETE) say what to do, with status codes like 201 Created and 404 Not Found. Auth puts a signed JWT bearer token or an API key on every call. Cursor pagination replaces OFFSET page numbers that slow down on two million rows. A token bucket rate limiter, the algorithm Stripe uses, answers 429 Too Many Requests with a Retry-After header. An Idempotency-Key stops a retried POST from booking and charging twice. URL versioning (/v1, /v2) renames a field without breaking phones that never updated. And a Cache-Control header lets a CDN answer the flash sale with cache hits instead of a thousand calls a second to the origin server.

This episode with chapters, full transcript and sources: https://themachineroom.dev/v/levels-of-rest-api/?utm_source=youtube&utm_medium=description&utm_campaign=levels-of-rest-api

Chapters

  1. 0:00The 7 Levels: one booking app, one new caller at a time
  2. 0:26Level 1, REST: endpoints, HTTP methods and status codes
  3. 2:55Level 2, Auth: JWT tokens and API keys
  4. 4:40Level 3, Pagination: page numbers vs cursors
  5. 6:37Level 4, Rate Limiting: the token bucket and 429
  6. 8:31Level 5, Idempotency: one key per payment
  7. 10:01Level 6, Versioning: v1 and v2 in the URL
  8. 11:24Level 7, Caching: Cache-Control and a CDN

Notes

the flight app, Leo and every number in the story are a made-up example. Seven levels is our framing, not Richardson's maturity model. The cache header on screen is Cache-Control: public, max-age=60; "public" is needed because every request carries a token (RFC 9111 §3.5). Keeping idempotency keys for a day is our choice, modelled on Stripe's.

Sources

Get the next one by email

One email per video, with the sources. No newsletter padding, no selling the list. Leave any time.

Transcript

The narration, word for word, from the subtitle file that ships with the video.

0:00The 7 Levels: one booking app, one new caller at a time

The seven levels of REST API. Watch one flight booking app grow up, one new caller at a time. At first it's just our own website calling it. Then the mobile team joins, a flight comparison site wants our flights, and then a flash sale sends thousands of phones to the same page at once. Every new caller breaks something, and the next level is what fixes it.

0:26Level 1, REST: endpoints, HTTP methods and status codes

Level one is REST itself. Right now, our app is one server, one database and one website. Leo searches for a flight, the website asks the server, and the server sends the flights back as JSON. The website asks at an address on the server, called an endpoint, and the app needs one for everything it does, like searching, booking and cancelling. We wrote each endpoint on the day we needed it, and people named it like whatever came in their mind. So we have get all flights, create booking, cancel booking and fetch user bookings v2. One takes the booking number in the address, and the next one takes it in the body. Every endpoint plays by its own rules, and only the person who wrote it knows them. Then the mobile team joins on Monday. Every endpoint is a surprise to them, so for each one they have to ask us what it does and how to call it. REST is a common way to design endpoints. The address names the thing, like flights or bookings, and the method, like GET, POST or DELETE, says what to do with it. So a GET on flights returns the list, and a POST on bookings makes a new booking and answers with a 201, which means created. A PUT on a booking replaces it with the one the app sends, like when Leo changes his seat. A DELETE on a booking cancels it, and after that, asking for it gets a 404 Not Found. There are a few other methods too, but these four do most of the work. And a GET only ever reads. It never changes anything, however many times it's called. So a cache can keep a copy of the reply and hand it to the next caller, reducing unnecessary server calls. A POST, a PUT or a DELETE changes something, so those always go through to our server. Same endpoints doing the same jobs, and now they all follow one pattern. Once the mobile team has seen one, they know how the rest work. Leo's bookings have to reach only Leo, and a flight comparison site wants to call us next. So our server has to know who's calling.

2:55Level 2, Auth: JWT tokens and API keys

Level two is auth, and it's how our server knows who's calling. When Leo logs in on his phone, the server checks his password and sends a token back to our app. The token holds Leo's user ID and the time it runs out. The server signs it with a secret only the server has, so if anyone changes even one letter, the signature stops matching. That's a JWT, short for JSON Web Token. From then on, every call our app makes for Leo carries the token in a header. The server checks the signature, reads Leo's ID out of the token, and only then answers. So when Leo opens his bookings, our app sends a GET on bookings with his token, and the server returns his bookings and nobody else's, because it picks them by the ID inside the token. The comparison site never logs in, because it isn't a person. So we give it an API key, a long secret it sends with every call, and the key tells our server which app is calling. Now every call says who it's from: Leo with his token, and the comparison site with its key. Our website always searches with a route and a date, so it only ever gets back a few dozen flights. The comparison site's first call is a GET on flights with no filter at all, because it wants every flight we sell. That's about two million flights, around two gigabytes of JSON in one reply, and the server spends minutes building it.

4:40Level 3, Pagination: page numbers vs cursors

Level three is pagination. Instead of everything at once, the server sends back fifty flights, and a way to ask for the next fifty. So the whole list becomes forty thousand pages. The simple way is page numbers. The comparison site asks for page one and gets flights one to fifty. Then it asks for page two, and the server skips the first fifty and sends fifty-one to a hundred. But deep pages get slow. To return page forty thousand, the database still has to read past almost two million flights, just to throw them away and send the last fifty. So we do it another way. Every flight in our database has an ID, and the list is sorted by it. Each page ends with the ID of its last flight, so after page one, the next call asks for the fifty flights after ID fifty. That last ID is called a cursor. The database doesn't count its way there. It uses its index to jump straight to ID fifty-one, and reads the next fifty. So the comparison site walks all forty thousand pages, and the last page comes back as fast as the first. Now the comparison site gets greedy. It wants its prices fresh all the time, so its script asks for the next page the moment the last one lands, and when it reaches the end, it starts again from the top. So it's continuously hitting our server in a loop. It runs fifty of these loops at once, so that's five hundred calls a second, all day long, just to keep its prices fresh. Most of those pages haven't changed since it last asked, but our server and database still build every one of them.

6:37Level 4, Rate Limiting: the token bucket and 429

Level four is rate limiting, and it caps how often the comparison site, or anyone else, can call us. Every caller gets its own limit, tied to its API key or its login token, and ours is six hundred a minute. There are a few algorithms for it: fixed window, sliding window, token bucket and leaky bucket. We go with a token bucket, the one Stripe uses. Each caller has a bucket that holds six hundred tokens. Every call takes one, and the bucket refills at ten a second. These tokens are just permission for one call each, nothing to do with Leo's login token. When a bucket is empty, the server answers with a 429, which means too many requests, and a header that says how long to wait. So the comparison site's fifty loops empty its bucket in about a second. From then on, it gets about ten calls a second instead of five hundred, and the rest come back as 429s. Now one greedy caller can't take the whole server, so everyone gets a fair share. It also slows down attacks, like a script trying thousands of passwords on the login endpoint. And partners know exactly how much they're allowed to call. Then Leo is at the airport on slow wifi, and he taps Pay for his three hundred and forty dollar seat. The server books it and charges his card, but the reply never reaches his phone. The app says something went wrong, so Leo taps Pay again. That second tap is one normal call, far under his limit, so the server books another seat. Two seats, six hundred and eighty dollars.

8:31Level 5, Idempotency: one key per payment

Level five is idempotency, and it stops Leo's second tap from booking a second seat. It means doing the same thing twice has the same effect as doing it once. A GET, a PUT or a DELETE already works like that. Cancel Leo's booking twice, and it's still cancelled just once. A POST doesn't: every POST on bookings makes a brand new booking. So when Leo opens the Pay screen, our app makes one random ID for this payment. That's called an idempotency key, and the app sends it with every tap on Pay. The server saves each key next to the answer it gave the first time, and keeps it for a day, like Stripe does. This time, Leo taps Pay at the airport, the reply gets lost, and he taps again. The second call carries the same key. So the server doesn't book anything new. It finds the key, sends back the saved answer, and Leo has one seat for three hundred and forty dollars. Then we rename one field in the booking reply, from total to price. Our new app reads price. But phones that haven't updated still look for total, find nothing, and show Leo's booking with no price. And we can't update their phones.

10:01Level 6, Versioning: v1 and v2 in the URL

Level six is versioning, and it lets us rename a field without breaking the phones we can't update. We put a version number on our API, and every app asks for the version it was built for. Adding a field never breaks anyone, because old apps just ignore it. Renaming or removing one does, and only then do we need a new version. There are a few ways to say which version you want: in the URL, in a header, or as a query parameter. We go with the URL, because you can see it right there in the address. So phones that haven't updated keep calling v1 bookings, and still get total. The new app calls v2 bookings, and gets price. Both versions read the same database. Only the shape of the reply is different, so nobody's screen is missing a price any more. Then on Friday, we run a flash sale and send a push to every phone. Sixty thousand phones open the same sale page in the first minute, a thousand calls a second. Each phone makes just one call, far under its limit, so rate limiting lets them all through, and the server can't keep up.

11:24Level 7, Caching: Cache-Control and a CDN

Level seven is caching. Every one of those phones asked for the exact same page, so we keep a copy of the answer and hand it out without asking our server every time. There are a few places to keep that copy: on the phone, on a CDN, or on our own server in something like Redis. We go with a CDN, a content delivery network. It's a set of servers close to the phones, so most calls never even reach our server. Our reply carries a header that tells the CDN it can keep the page for sixty seconds and give the same copy to everyone. The first phone's call reaches the CDN, and the CDN has no copy yet. That's a cache miss, so the CDN asks our server once and keeps the reply. Every phone after that gets the copy straight from the CDN. That's a cache hit, and our server never hears about it. After sixty seconds the copy is too old, so the next call is a miss again, and the CDN fetches a fresh copy from our server. So on Friday, each CDN location asks our server about once a minute and answers everyone else from its copy. Our server sees a few calls a minute instead of a thousand a second. A minute-old page is fine, because the price and the seats are checked again when Leo taps Pay. That's our API now: REST endpoints, a token or a key on every call, fifty flights a page, a bucket for every caller, a key on every payment, a version in the address, and a CDN in front. Start with REST, and climb a level each time a new caller breaks something. Which level is your API on? Tell me in the comments. And if this helped, subscribe.