skip to content

Server-Side Sessions

Stateful authentication: the server keeps the state and hands out a reference to it, then decides how that reference is issued, rotated and killed. Interviewers start here: most sites run on it.

on this pageshow

questions

22

Why does a guest's held campsite selection vanish at sign-in unless the server copies it into the new server-side session record?

level: juniorimportance: must knowfreq 52%

answer

  1. cookie holds a reference, not the data
  2. new identifier names a new record
  3. carrying it is code you write
  4. copy named items, not everything
  5. destroy the old record afterwards

basics

~20 s

Because the selection is filed server-side under the identifier the visitor had before signing in, and sign-in issues a new one. Carrying it over is an explicit step in the login handler, and not every item should be carried.

solid answer

~40 s

An anonymous visitor already has a server-side session record; the cookie only carries an opaque reference to it. Sign-in issues a **new** identifier, so the request that arrives next names a different record — the guest data is still sitting under the old key until the login handler copies it, or it is deleted with that record. So "the cart survives login" is not a property of sessions; it is code you write. The copy is also a filter rather than a bulk move: the held dates and permits come across because losing them costs the visitor the transaction, the anti-forgery token is re-issued rather than copied because it is bound to the identifier that just changed, and the old record is destroyed afterwards so a stale tab cannot keep writing to it.

code

pseudocode · 7 lines
pseudocode
on login_completed(old_record, new_session):
    new_session.held_dates   = old_record.held_dates      # carried: losing it costs the booking
    new_session.vehicle_permits = old_record.vehicle_permits
    new_session.language     = old_record.language        # carried: harmless preference
    new_session.anti_forgery_token = issue_anti_forgery_token()  # re-issued, never copied
    # consent choices are NOT copied: the guest record is not evidence of this human
    old_record.destroy()                                  # no orphan still accepting writes

go deeper

for a junior

Recall that the cookie holds only a reference and the data sits in a server-side record. When sign-in issues a new identifier, the old record is still there — it just is not the one being read any more.

for a middle

Explain the copy as a filter: which items are carried, which are re-issued because they were bound to the old identifier, and why the old record is destroyed rather than left to expire.

for a senior

Bring up the cases that make it real: a retried sign-in running the handler twice, a stale tab writing to the orphan record, and guest data that should not be carried because the anonymous record is not evidence of a person.

for a principal

Decide the default: an allowlist of carried items reviewed like any other contract, so the set does not quietly grow as attributes are added and nobody re-asks whether they belong on the authenticated side.

## Where a guest's selection actually lives A visitor walks up to the campsite reservation counter and picks a pitch, three nights and two vehicle permits before anyone has asked who they are. All of that is real state and it has to be somewhere. In a server-side session design it is in a **record on the server**, filed under an opaque identifier; the visitor's browser holds only that identifier, in a cookie. The record is the state, the cookie is the reference to it. That separation is the whole answer to this question. The browser is not carrying the selection, so nothing the browser does preserves it; only the record does, and the record is reachable through exactly one value. ## What sign-in changes When the visitor identifies themselves and completes the login, the server issues a **new session identifier** for the authenticated session. That is standard practice and it is not this decision — take it as given. What it means for the selection is mechanical: - the request after sign-in presents the new identifier; - the new identifier names a different record — a fresh one; - the guest's selection is still filed under the old identifier, untouched; - when the old record is cleaned up, the selection goes with it. So the selection disappears not because anything deleted it, but because nothing brought it along. Carrying guest state across sign-in is **an explicit step in the login handler**, and a team that has never written that step has a service where every guest who signs in loses their work — usually discovered the week after two-step login is added, because that is when more people sign in mid-flow. ## The copy is a filter, not a bulk move It is tempting to move every attribute across in one loop. Each item deserves a decision: | Guest-session item | Fate at sign-in | Why | |---|---|---| | Held pitch, dates, vehicle permits | **carry** | Losing it costs the visitor the transaction and they will not redo it | | Display language, accessibility preference | **carry** | Harmless preference, no authority attached | | Anti-forgery token | **re-issue** | It is bound to the session identifier that just changed, so copying it carries a value tied to a session that no longer exists | | The pre-authentication handle from the login itself | **destroy** | Its attempt is over; it must not outlive the sign-in it completed | | Anything the signed-in account already has its own copy of | **the account's copy wins, or ask** | The account's data was saved by a proven human; the guest record's was not | The last row is where this stops being a checklist. A guest record is not evidence of a person — at a counter terminal used by several people across a shift, the selection sitting in it may belong to whoever was there before. That is why identity-bearing guest data, such as recorded consent choices or a typed email address, is carried only when the anonymous record can be shown to have been the same human, which in that setting it usually cannot. ## Doing the copy Three rules keep the step boring: 1. **Copy named items, not everything.** An explicit list is readable, reviewable, and it does not silently start carrying whatever attribute someone adds next year. 2. **Destroy the old record afterwards.** If it survives, a tab the visitor still has open keeps writing to a record nobody will ever read again, and the visitor sees their later changes vanish. 3. **Make it safe to run twice.** A retried or double-submitted sign-in can run the handler again, and "copy the cart" run twice is how a visitor ends up holding two of everything. ## What this is not This is not about the cookie's own attributes, and it is not about whether the identifier should change at sign-in — it does, and that rule is settled elsewhere. It is only about what happens to state that existed on the anonymous side of that change. The interview version of this question is short, and the answer that lands is: *the state is filed under the old identifier, so it moves only if I move it, and I move it item by item.*

  • Why not simply keep the same session identifier at sign-in, so nothing needs copying?
    Because the identifier is re-issued at authentication for reasons that have nothing to do with the cart, and that rule is not negotiable to save a copy step. Treat the change as fixed and design the migration around it; the copy is a handful of lines and it is the part you control.
  • Should the old anonymous record be destroyed immediately or left to expire?
    Destroyed as part of the login handler. Leaving it to expire means a tab the visitor still has open keeps writing to a record nothing reads, so their later edits appear to vanish, and it leaves a second record naming the same selection for as long as the clock runs.
  • What if a guest never signs in at all?
    Nothing changes for them: the anonymous record lives on its own clock and expires with the selection in it. The migration question only arises at the moment the identifier changes, which is why this is a sign-in problem rather than a cart problem.

saying these in an interview costs you the question

  • The cart is in the cookie, so sign-in cannot lose it.
  • Copy every attribute across in one loop, it is simpler.
  • Carry the anti-forgery token over so open forms keep working.
  • Leave the old anonymous record alone; it expires anyway.
  • Guest state survives login automatically, that is what sessions do.
open as a page

A playout scheduler's session cookie carries `presenter=dmorgan; role=engineer` in plain text — what stops a presenter becoming the engineer?

level: juniorimportance: must knowfreq 72%

basics

~20 s

Nothing stops them. A cookie value is input the caller can retype, so a plain-text role claim is simply edited. The value must be an unguessable reference to a record the scheduler holds, or state the server can prove it wrote.

open as a page

A server-side session runs on an idle clock and an absolute clock — what does each one defend against?

level: juniorimportance: must knowfreq 74%

basics

~20 s

The idle clock restarts on every request and ends a session left unattended. The absolute clock counts from the sign-in that created the record, never restarts, and caps how long one authentication may keep counting.

open as a page

What handle should a server issue between a successful password step and a verified second factor, and how must it differ from a signed-in session?

level: middleimportance: must knowfreq 58%

basics

~20 s

A short-lived, single-purpose pre-authentication handle bound to one login attempt and to the factor still pending, carrying no authority to reach application endpoints. It must be its own credential, not a signed-in session record wearing a pending flag.

open as a page

Your server pins each stored session record to the client address seen at login — what does that buy, and what breaks?

level: middleimportance: must knowfreq 62%

basics

~20 s

Pinning raises the cost of using a stolen session reference — the thief must also call from that network — but never prevents it. The price is paid by legitimate callers whose address moves: roaming clients, carrier re-addressing and shared egress pools.

open as a page

Your appointment desk authenticates front-desk staff with a session cookie — which endpoints need a CSRF check, and where does it run?

level: middleimportance: must knowfreq 70%

basics

~20 s

Every state-changing endpoint an automatically-attached session cookie can reach needs an anti-forgery check or a written reason it does not. Run it at one choke point in front of the handlers, so protection is the default and exemptions are named.

open as a page

How many random bits does the session identifier in a cookie need, and how do you derive that number?

level: middleimportance: must knowfreq 60%

basics

~20 s

Derive it from four measured quantities: live sessions, the guess rate your front door permits, the window identifiers stay valid, and the hit probability you accept. Expected hits are about N x R x T / 2^B, which puts the floor near 64 bits and the norm at 128.

open as a page

How do you end every server-side session for one user without deleting each record, while sparing the device in their hand?

level: seniorimportance: must knowfreq 55%

basics

~20 s

Keep a monotonic version on the user record and a copy in every session record. Bumping it invalidates the whole set in one write, and the per-request check becomes a comparison. The counter cannot spare a row, so re-issue the current device after the bump.

open as a page

Sign-out destroys the stored session record — what else must end with it, and which item do teams forget?

level: seniorimportance: must knowfreq 66%

basics

~20 s

Destroying the record is one item on a list. A persistent-login cookie, the anti-forgery token, credentials obtained downstream, connections authenticated at handshake and already-rendered pages all outlive it. The forgotten one is whatever can create a new session unaided.

open as a page

The session records for eleven staffed lanes at a ferry boarding desk live in a tier another team operates — what does a failover of it cost you, and how do you bound that in advance?

level: seniorimportance: must knowfreq 55%

basics

~20 s

A session-tier fault invalidates a whole signed-in population at once rather than degrading one request. Bound the radius in advance: keep irreplaceable records off the regenerable tier, and choose the degraded mode before the incident.

open as a page

Your server drops a session when the request's User-Agent differs from the one recorded at login — why do real users hit this?

level: juniorimportance: should knowfreq 45%

basics

~20 s

The User-Agent string is chosen and sent by the client, and it changes on its own — an automatic update rewrites it mid-session. Binding a server-side session record to it logs out honest users while a thief simply replays the string.

open as a page

What must each row of a signed-in-devices list carry, and how far can a user trust those fields?

level: middleimportance: should knowfreq 45%

basics

~20 s

Four fields do the work: device label, last-seen time, approximate location, and which row is the current session. Only the last is a client fact the server establishes itself; the label is asserted by the client, the location inferred from an address.

open as a page

Beyond first sign-in, when must a server re-issue its session identifier, and what breaks the day a team turns that on?

level: middleimportance: should knowfreq 58%

basics

~20 s

A server re-issues the session identifier at authentication and at every privilege change: a second factor accepted, a role elevated, an acting-as session entered or left. What breaks is state that was keyed to the identifier just replaced.

open as a page

What happens to a pre-authentication handle when a half-finished login is abandoned, when the factor is requested again, and when it finally succeeds?

level: seniorimportance: should knowfreq 41%

basics

~20 s

Abandonment ends the attempt when the handle's short absolute clock passes, and the visitor restarts from the password step. Requesting the factor again keeps the same handle and the same deadline. Success consumes it: destroy the handle, then issue a fresh session.

open as a page

A guest's held pitch and the account's own held pitch collide at sign-in — which one survives, and how do you keep the merge idempotent?

level: seniorimportance: should knowfreq 37%

basics

~20 s

Neither is discarded automatically: surface both and let the camper choose, keeping both holds until they do. Idempotency comes from marking the guest record migrated before destroying it, so a retried sign-in finds nothing left to copy.

open as a page

Which observations about a live session record honestly indicate hijack, and what should the server do with that verdict?

level: seniorimportance: should knowfreq 52%

basics

~20 s

Three families: use from two places that cannot both be true at once, an established client property changing mid-session, and a request rate or ordering no human produces. The verdict routes to record-only, challenge, or end — never straight to end.

open as a page

An external laboratory posts signed result notifications to your practice's receiver endpoint — why does it need no CSRF check, and what keeps that exemption safe?

level: seniorimportance: should knowfreq 55%

basics

~20 s

A receiver endpoint authenticated by a signature over the request body needs no anti-forgery check because no user agent attaches that credential by itself, so a hostile page cannot make an authenticated request arrive. Scope the exemption to that path.

open as a page

A scheduler's session cookie carries the presenter's identity and roster instead of a server-side record — when does signing that value suffice, and when must it be encrypted?

level: seniorimportance: should knowfreq 42%

basics

~20 s

Signing stops alteration but not reading, so it suffices only when the contents are safe for the holder to see. Encrypt — with authenticated encryption, never bare ciphertext — when the roster or the roles are themselves worth hiding. Both replay until they expire.

open as a page

How do you roll the key that signs a scheduler's session cookies without signing out every presenter in the same minute?

level: seniorimportance: should knowfreq 38%

basics

~20 s

Overlap in a strict order: every instance must accept the new key for verification before any instance signs with it, and the retiring key stays in the verification set for at least the longest remaining lifetime of anything it signed.

open as a page

Three nurses share one cabled ward terminal per shift — how do you decide whether to bind their sessions at all?

level: principalimportance: should knowfreq 38%

basics

~20 s

Price both sides before choosing. On a fixed shared terminal the address and agent never vary, so a binding yields no true positives and only correlated false positives when a fleet update lands. The defensible answer there is to bind nothing and detect instead.

open as a page

A per-user session version is compared on every request; where do you hold it, and what revocation lag do you accept?

level: principalimportance: should knowfreq 35%

basics

~20 s

Read it wherever the request already loads the user, and the kill is immediate. Cache it and the kill lags by the cache lifetime, so pick that lag against the threat behind the button, write through on the revoke path, and publish the number.

open as a page

A shared session tier comes back empty mid-sailing and all eleven boarding lanes re-authenticate within the same minute — how do you plan the login path's capacity for that in advance?

level: principalimportance: should knowfreq 28%

basics

~20 s

Recovery from a session-tier fault is a synchronised step, not a retry curve: a whole population hits the most expensive path in the system at once. Size the credential path against that number, admit traffic deliberately, and rehearse it.

open as a page