Stage 2 · Building blocks · Medium · 15 min

Authentication and authorization

Who is this user, and what may they do? Sessions against signed tokens, signing in through another site, and roles.

What each interview level is expected to know

Learn first: HTTP, TCP, UDP and connections, Reverse proxies and API gateways

After this lesson you can:

  • Tell who a user is apart from what they are allowed to do.
  • Compare sessions with signed tokens, including what happens at logout.
  • Explain how signing in through another site works, and what a leaked signing key breaks.

Start here

See it in 60 seconds

A session or a signed token

After one login, a server remembers you with a session it looks up on every request, or a signed token it only checks.

Scene 1 of 7 · Log in once

Asha types her password once on each site, and each server checks that it is right.

With a sessionWith a signed tokenBrowser, AshaBrowserAshaServer, keeps sessionsServerkeeps sessionspassword is rightSession store, on the server sideSession storeon the server sideBrowser, AshaBrowserAshaServer, signs tokensServersigns tokenspassword is right
  • Browser — Asha — healthy
  • Server — keeps sessions — healthy — password is right
  • Session store — on the server side — healthy
  • Browser — Asha — healthy
  • Server — signs tokens — healthy — password is right
Session store reads
0
Signature checks
0
  1. Log in once. Asha types her password once on each site, and each server checks that it is right.
  2. A session id comes back. The first server saves a session in its store, and sends back only a random id.
  3. Every request, a lookup. The browser sends that id with every request, and the server reads the store to learn who it is.
  4. A signed token comes back. The second server saves nothing: it sends a token that names Asha, signed with its secret key.
  5. Every request, a check. On each request the server checks the signature with its key, and needs no store at all.
  6. Log out: the session. Asha logs out, the server deletes the session, and a stolen copy of the id is refused.
  7. Log out: the token. This server keeps no list of ended tokens, so a stolen copy works until its 15 minutes run out.
  8. Now you try. Asha logs out, and her token was new at that moment. A thief uses stolen copies some minutes later. Which still work? For example: The session id is refused, because its record is gone. The token still works for 10 more minutes. The server cannot take a token back without a list to look up, so tokens are given short lives.

Scene 1 of 7: Log in once. Asha types her password once on each site, and each server checks that it is right.

Key points

  1. A session lives on the server; the browser holds only a random id, usually in a cookie.
  2. A signed token carries who you are, and a server with the right key checks it without a lookup.
  3. Sessions cost a lookup on every request, but logging out works at once.
  4. With no lookup, a token cannot be taken back, so keep its life short, such as 15 minutes.

Go deeper in the API gateway lab →

In plain words

Asha logs in to a shop with her password. The server must remember her on the next click, without asking again. One way is a : the server saves a record and gives her browser a random id, such as 7f3a. Another way is a : a short note that says "this is Asha, until 10:15", with a signature only the server can make. Checking who someone is is called . Deciding what they may do, such as view an order but not refund it, is called .

On every later click, the server makes two checks in order. First it reads the session id or the token and works out that this is Asha. With a session it looks the id up in its store; with a token it checks the signature, with no lookup. Then it checks what Asha may do, such as view her own order. A request that fails the first check gets 401, and one that fails the second gets 403.

The problem it solves

forgets. Each request arrives alone, with nothing to say it comes from the person who logged in a minute ago. And a system with ten services cannot ask for the password ten times, or let each service keep its own copy of every password.

Why it matters

Every design has a login. An interviewer who hears "the gateway checks the token" will ask what a token is and who made it. Then they ask what happens when a user logs out.

A worked example

A token made at 10:00 with a 15-minute life is accepted until 10:15. Say it is stolen at 10:05, and the user logs out at 10:06. With no list of ended tokens, the thief can use it for 9 more minutes, until 10:15. A session deleted at 10:06 is refused from 10:06.

Think of it like…

Think of a swimming pool. A session is a locker key with a number: the desk looks the number up in its book. A token is a stamped day ticket: any guard can check the stamp, with no book.

Where this analogy stops working

A lookup against a stamp is the useful part. The comparison fails with copies. A paper ticket is hard to copy, but a stolen token is a perfect copy, and the guard cannot tell. That is why tokens are given short lives.

Try it

See it working

Live simulationOpen the full lab →

Lab · Building blocks

API gateway

Three clients send requests through one gateway to three services. The gateway checks each API key, applies each client's rate limit, and routes by path. See what it stops, what a retry hides, and why one gateway is a single point of failure.

Try it

Lab time 3.0 s
/users/orders/searchMobile app, 8 requests/sMobile app8 requests/s4/10 tokensPartner, 6 requests/sPartner6 requests/s10/10 tokensBot, off, IDLEBotoffno valid API keyIDLEAPI gateway, 1 of 1 instance upAPI gateway1 of 1 instance uplimit 10/s per clientUsers, 40 ms a callUsers40 ms a call20 callsOrders, 80 ms a callOrders80 ms a call13 callsSearch, 120 ms a callSearch120 ms a call20 calls

The lab is busy, so only some requests are drawn as dots, few enough to follow. The numbers still count every request.

  • Mobile app — 8 requests/s — healthy — 4/10 tokens
  • Partner — 6 requests/s — healthy — 10/10 tokens
  • Bot — off — IDLE — no valid API key
  • API gateway — 1 of 1 instance up — healthy — limit 10/s per client
  • Users — 40 ms a call — healthy — 20 calls
  • Orders — 80 ms a call — healthy — 13 calls
  • Search — 120 ms a call — healthy — 20 calls
  • Request
  • Response
  • Turned away (401, 429, 503) or error
  • Retry
Answered
19/s
Stopped at the gateway
0/s
bad API key (401) or over the limit (429)
Failed after routing
0/s
Orders requests failed
0%
Orders calls per request
1.00
Gateway instances up
1 of 1

Numbers add up:53 sent = 0 stopped (401) + 0 stopped (429) + 52 answered + 0 failed + 0 no gateway + 1 in flightThe lab checks this sum as it runs, so a counting mistake would show here.

Controls

Clients
6 requests/s

The mobile app always sends 8 a second.

Sends 4 requests a second.

Rate limit
10/s
Limit

Each client has its own token bucket.

Services
0% of calls

The gateway calls the service a second time after an error.

Gateway
Gateway instances

Each client

ClientSent401429503OKFailed
Mobile app33000320
Partner20000200
Bot000000

401: no valid API key. 429: over the limit. The gateway stopped both. 503: the load balancer found no running gateway to take the request. Only OK and Failed requests reached a service.

Requests per second
AnsweredStopped at the gatewayNo gateway (503)Failed after routing
06.312.518.8251s2s3s
Show data table
TimeAnsweredStopped at the gatewayNo gateway (503)Failed after routing
1s12000
1s14000
2s18000
2s16.5000
3s17.5000
3s19000

What just happened

  1. 0.0sInfo: Each client with a valid API key may send 10 requests a second.
What this model simplifies
  • Time is slowed so you can watch each request. Service times are fixed and illustrative: Users 40 ms, Orders 80 ms, Search 120 ms.
  • Every call here reads data, so a retry is safe. A gateway should not retry a write unless it carries an idempotency key.
  • The gateway checks a token and a limit in 5 ms and never runs short of capacity itself. A real gateway can become a bottleneck.
  • A real gateway also ends TLS, rewrites requests and writes logs. Those are left out because they change no numbers here.
  • Real gateways also limit by IP address before the key check, so a flood of bad requests still costs them a little work.
  • A restarted instance takes traffic at once. A real load balancer waits for a passing health check, and often answers 502 for a crashed instance.
  • The load balancer in front of the gateway instances is not drawn, and it never fails.

Also try: Rate limiting

Mechanics

How it works

The pictures follow Asha from this page's example. The first compares the two ways a server remembers her login. The second shows the two checks on every request: who she is, then what she may do.

  1. Check the password once

    A careful server does not store the password itself. It stores a hash: a scrambled form that cannot be turned back. It mixes a random value, called a salt, into each one, so equal passwords get different hashes. At login it scrambles what was typed the same way and compares the two. The method, such as bcrypt, is slow on purpose, so guessing millions of passwords costs too much.

  2. Remember the login with a session

    The server saves a record such as "7f3a is Asha" in a fast store, and sends the id to the browser in a . A cookie is a small value that the browser sends back with every request. Each request brings the cookie, and the server looks the id up. Deleting the record logs her out at once.

  3. Or remember it with a signed token

    Pick a case to follow below

    With this case: With a session, every click cost a lookup, but logout ended the login at once: the copied cookie got 401.

    Browser, AshaBrowserAshaShop server, checks each requestShop serverchecks each requestSession store, id → user, NOWSession storeid → user7f3a → AshaNOW
    • Browser — Asha — healthy
    • Shop server — checks each request — healthy
    • Session store — id → user — healthy — NOW — 7f3a → Asha
    • Request
    • Refused
    Step 1 of 4

    Step 1 of 4 · Session store

    Done: Asha logs in, and the server saves a session

    What happens
    The server saves the record "7f3a is Asha" in the store, and sends the id 7f3a to her browser in a cookie.
    Why
    The id is random and means nothing by itself. Only the store knows whose it is.
    What changes
    The browser sends the cookie with every request from now on.
    Next
    Her next click: the server looks the id up
    The server checks
    a lookup
    in the session store
    Store lookups
    0
    A copied login after logout
    not tried yet
    Asha has not logged out

    Step 1 of 4: Session store. Asha logs in, and the server saves a session. The server saves the record "7f3a is Asha" in the store, and sends the id 7f3a to her browser in a cookie.

    The server sends a token that names the user and an expiry time, and signs it. A is the common format. A server that has the key checks the signature with no lookup. If anyone changes the token, the signature no longer matches. Often the login service signs with a private key, and other servers check with a public key that cannot sign. With no lookup, a token cannot be taken back before it expires.

    In the picture, Asha's token says she is logged in until 10:15. Step through both cases: she clicks, logs out at 10:05, and at 10:10 someone sends a copy of her login.

    The session needs a lookup on every click, but deleting the record ends the login at once, so the copy gets 401. The token needs no lookup, but nothing can cancel it, so the copy works until 10:15. The next step shows how short-lived tokens limit that risk.

  4. Keep tokens short-lived

    Call this short-lived token an , and give it a life such as 15 minutes. A second, long-lived gets a new one, and the login service looks that refresh token up each time. Logout deletes the refresh token. So new access tokens stop, and the last one dies within 15 minutes.

  5. Let another site check the user

    With "Sign in with Google", the user types the password at Google only. Google sends the app a code that works once, and the app's server swaps it for a token that says who the user is. These steps are a standard called with OpenID Connect.

  6. Then check what they may do

    Pick a case to follow below

    With this case: Asha passed both checks: the server knew who she was, and a viewer may view.

    Browser, AshaBrowserAshaShop server, checks each request, NOWShop serverchecks each requestNOWSession store, id → userSession storeid → user
    • Browser — Asha — healthy
    • Shop server — checks each request — healthy — NOW
    • Session store — id → user — healthy
    • Request
    • Answer
    Step 1 of 3

    Step 1 of 3 · Shop server

    Done: A request arrives: GET /orders/7

    What happens
    It brings Asha's login.
    Why
    Every request is checked, not only the first one.
    What changes
    First the server checks who is asking.
    Next
    Authentication: it is Asha
    Who is it?
    –
    not checked yet
    May they do it?
    –
    a viewer may view
    Answer
    –

    Step 1 of 3: Shop server. A request arrives: GET /orders/7. It brings Asha's login.

    What these pictures simplify. These pictures follow one user, Asha, at stated times. A real login service also limits password guesses, and a real token is often refreshed every few minutes. The API gateway lab above checks logins on live traffic.

    Authorization runs on every request, after authentication. A common rule uses roles: a viewer may read, an editor may also edit, an admin may also delete. Check it in the service that owns the data, not only at the gateway. A request with no valid login gets a 401 answer. A known user who may not do the action gets a 403.

    In the picture, Asha is signed in to the shop's admin page as a viewer. Switch between the three requests to see which check stops each one.

Use it when

  • Sessions for a single web app: they are simple, and logout works at once.
  • Signed tokens when many services or regions must check a login without sharing one session store.
  • Sign-in through Google or a company login, so your app never handles passwords.

Skip it when

  • A token that lasts for weeks with no way to refuse it: a stolen copy works for weeks.
  • Secrets or personal data inside a token: it is signed, not hidden, so anyone who holds it can read it.
  • Roles checked only in the browser or only at the gateway: a request that reaches the service another way skips the check.

Check yourself

What is the difference between authentication and authorization?

Also

More in 60 seconds

What "Sign in with Google" does

Signing in with Google lets an app learn who you are without ever seeing your password: Google checks you, then tells the app.

Scene 1 of 6 · Click the button

You click "Sign in with Google", and the notes app never asks you for a password.

Your browser, you are AshaYour browseryou are AshaNotes app, wants to know youNotes appwants to know youGoogle, checks who you areGooglechecks who you are
  • Your browser — you are Asha — healthy
  • Notes app — wants to know you — healthy
  • Google — checks who you are — healthy
Passwords the notes app saw
0
  1. Click the button. You click "Sign in with Google", and the notes app never asks you for a password.
  2. Sent to Google. The app sends your browser on to Google, with a note saying which app is asking.
  3. Sign in at Google. You type your password at Google only, and agree to share your name and email.
  4. Back with a code. Google sends your browser back to the app with a short code that works only once.
  5. Code swapped for a token. The app's server sends Google the code and its own secret, and gets back a signed token about you.
  6. The app logs you in. The app reads your name from the token and starts its own session, as after a password.
  7. Now you try. Asha is signed in to the notes app. Her role decides what she may do. Pick her role. For example: As a viewer, Asha may read a note. The app refuses the other 3 actions with a 403 answer, which means "not allowed". Signing in proved who she is; the role check, run on every request, decides what she may do.

Scene 1 of 6: Click the button. You click "Sign in with Google", and the notes app never asks you for a password.

Key points

  1. Your password goes to Google only; the app never sees it.
  2. The code in the browser works once, and is useless without the app's own secret.
  3. The token travels from server to server, so it never passes through your browser.
  4. Proving who you are is authentication; what you may do next is authorization.

Judgement

Trade-offs and how it fails

What you give up

Sessions
Easy to end and to change, but every request costs a lookup, and every server needs the same store.
Tokens
No lookup and no shared store, but hard to take back, so they need short lives and a refresh step.

Failure modes

  • The session store goes down, and every user is logged out at once.
  • A signing key leaks: anyone can make a valid token for any user until the key is changed.
  • A stolen token is used after logout, because nothing checks a list of ended tokens.
  • A service trusts a user id sent by the client, and one user reads another user's orders.

In the real world

  • A JWT is three parts joined by dots: a header, the facts about the user, and a signature. The facts are encoded, not hidden.
  • OpenID Connect adds an ID token to OAuth 2.0, so an app learns who signed in. "Sign in with Google" uses it. Source: OpenID Foundation: how OpenID Connect works (checked 14 October 2026)
  • Auth0, Okta and the open-source Keycloak run login as a service, so an app does not store passwords itself.
  • Redis is a common session store, because it keeps data in memory and a lookup by id is fast: often about a millisecond, network included.

Trade-offs to be ready to discuss: Sessions vs signed tokens · Short vs long token life

Check yourself

Why give an access token a short life, such as 15 minutes?

Interview

What interviewers look for

  • Say where the login is checked: "The gateway checks the token's signature; each service still checks what this user may do."
  • Name the trade-off before you are asked: sessions end at once, and tokens scale without a shared store.
  • For logout with tokens, say: "short-lived access tokens, and a refresh token that the login service can refuse".

Say it out loud: Why do access tokens have short lives?

Asha logs out. Her app uses signed tokens with a 15-minute life and keeps no list of ended tokens. Her token was made 2 minutes ago, and a thief has a copy. What happens?

Expected depth by interview level

Levels describe the depth interviewers probe, not years of experience. Choose a level on the learning path to highlight it.

JuniorUseful
What a session and a token are, and where a login is checked.
MidExpected
Sessions against tokens, token expiry, and where roles are checked.
SeniorExpected
Logout and stolen tokens, sign-in through another site, and checks between services.
StaffUseful
Which services accept which tokens across teams and regions, and one sign-in for many apps.

Practice

Apply it

Three harder questions

The first two use the idea on a new case. The last asks you to judge a design.

Apply it

Asha is logged in with the viewer role. She sends a request to delete an order. What should the service answer?

Apply it

Two users both pick the password summer2026. The server stores salted hashes. What does the database hold?

Judge a design

A bank app must be able to log a user out of every device at once, such as after a phone is stolen. The plan uses signed tokens that last 24 hours, with no lookup. What is better?

Next: CDNs, which keep copies of your files on servers near your users.

Go deeper

Sources and further reading

First-hand sources to check this page against and to go further. They are standards, official docs, original papers, and guides from engineering and security teams. Each link was opened on 8 November 2026.