LearnThatStack Ace your next interview
API Design · question
Question 23 of 92

Compare different API authentication methods.

intermediate Pro
← All API Design questions

The full answer covers the core mechanics, when this pattern wins, and the trade-offs interviewers most often probe. It includes a code sample. Read it once at your own pace, then try to recall the structure from memory before the interview. Pro also includes this question's interview lens - the likely follow-up probes and what you can say in the room.

Full answer + code samples + AI explanations

Unlock to read the complete answer for this premium question.

Guided practice for API Design One question at a time. Answer out loud, get graded, see what you missed.
Related concept

Authentication: proving who is calling

Authentication asks who you are and authorization asks what you may do. Authentication methods differ mainly in where the identity state lives.

For this question · chapters 2 and 4 of 4

2/4 Where the session lives
sessionsWhere the session livesa pointer to state, or the state itselfbalancerClientBearer jwt, 15 minstore trips per request0token expiry6105minserver 1signature ok, 15 minserver 2idleserver 3idleSession storesid 7f3auser 124rt 9c1user 124refresh tokenrt 9c1store row
sessionsWhere the session livesbalancerClientBearer jwt, 15 minexpiry6105minserver 1signature ok, 15 minserver 2idleserver 3idleSession storesid 7f3auser 124rt 9c1user 124

Where the session lives

  1. The identity badge, the proof of who is calling, still has to work on the next request. So something has to remember that badge, and the server keeps that memory in a session. The client sends Cookie: sid=7f3a, and the server looks that id up in its session store. The server finds user 124 there, so the request goes through.
  2. The same client sends a JWT instead. The server checks the signature, then reads the claims straight from the token: sub 124 with a 60 minute expiry. The identity state is inside the token itself, so the server skips the store check entirely. A JWT is signed, not encrypted, so don't keep real secrets inside it.
  3. Three servers now run behind the load balancer. Each server checks the JWT with the same key. Each server then reads the answers straight from the claims inside the token. No server reads the session store, so the servers have nothing to share. Adding a fourth server needs no shared session at all.
  4. Now ban user 124. Delete the row for sid=7f3a from the session store. The next request with that cookie finds no row, so the request gets 401. Revocation with a session store is one delete, and the ban works on the very next request.
  5. The ban is in place, and now a JWT arrives. Its signature is still valid, so server 3 answers 200 OK for a banned user. There is no session row anywhere to delete. So the token keeps working until its 60 minutes run out.
  6. A JWT is not more secure than a session cookie. The token skips the store lookup but is slower to revoke, so you pick one based on the use case. Sessions work well for an app with a login and a logout button. Tokens work well in microservices, where many services can verify a token with the same key, so there is no shared session store and no lookup cost.
Chapter 2 · step 1 of 6

The identity badge, the proof of who is calling, still has to work on the next request. So something has to remember that badge, and the server keeps that memory in a session. The client sends Cookie: sid=7f3a, and the server looks that id up in its session store. The server finds user 124 there, so the request goes through.

1 of 6 Use ← → or swipe See the concept: all 4 chapters in it

© LearnThatStack - diagrams may not be republished without permission.

Want a quick review of the fundamentals? See the API Design cheatsheet.

← Back to all API Design questions
Pro · $10/mo

74 of 92 API Design answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime