Session state needs versions, ordering, and retry semantics across concurrent requests
Keep recommendation history, model artifacts, and feedback references coherent when tabs, devices, retries, and out-of-order updates share a session.
On this page
The question for today
What you’ll understand
- Explain how unconditional read-modify-write can lose a session event.
- Apply an atomic version precondition and stable event identity to a retry.
- Distinguish session revision, event order, model version, and exposure history.
A recommendation request may read recent interactions, hide items already seen, and produce new exposure records while another tab updates the same session. Retried requests and late events make the sequence more complicated than one mutable list in memory. A usable state contract identifies which revision was read, how an update competes with another, and which event has already been applied. The state version also differs from the model or index version that interpreted it, so all relevant identities must survive serving and feedback.
Define the state snapshot a request actually used
A session record might contain recent item IDs, interaction summaries, exposure exclusions, and a revision. Decide whether it is session-local or shared across a user's devices; these are different consistency domains. Attach a timestamp and a defined ordering rule to events rather than assuming arrival order equals user action order. A request should record the state revision and compatible feature, model, and index artifacts it used. That makes a later replay explainable even if the current session has already advanced or the serving model has changed.
A revision orders accepted state transitions in a particular store; it is not a universal clock, a user action timestamp, or proof that all prior events arrived. A monotonic producer sequence can help order one producer's events but does not automatically order two devices. Choose whether late interactions are appended, merged, reordered, or rejected under a documented policy. A seen-item cache that expires can legitimately permit an old item to reappear, so distinguish a soft freshness preference from a durable every-session exclusion guarantee.
A conflict retry must preserve event identity
Two writers can read revision v and each construct a replacement. An unconditional last write can erase the first writer's update. An atomic compare-and-set or equivalent transaction rejects the second stale write, which can reload and reapply its intended event. HTTP strong validators with a correctly implemented If-Match precondition express a related lost-update protection at a resource boundary. Actual stores and transaction APIs differ, so verify their supported atomic scope rather than assuming that a separate read followed by a separate check is sufficient.
Conflicts are not the same as duplicate delivery. A client may lose the successful response and retry an event that already committed. Use a stable scoped event identity and atomically record its application with the state change when exactly-once logical application is required within that scope. Retention and expiry limit the deduplication promise. Updating session state and publishing a feedback event to another system can still have a failure gap unless a defined transactional or outbox protocol connects them. A successful local CAS does not automatically solve distributed side effects.
Keep state bounded and fallback behavior observable
Bound recent-history length and storage while documenting what truncation changes. A fallback request with missing session state may produce a valid generic slate but should record that degraded context instead of pretending it used the full history. Measure conflict retries, stale reads, unavailable state, and duplicate suppression by cohort. Evaluate the effect on recommendation quality and latency together: strong coordination can add delay, while stale state can repeat items or misinterpret a recent action. The right tradeoff follows the declared user-visible semantics rather than a generic promise of perfect consistency.
The Markov lab shows version-independent mathematical transitions rather than a session store. Reset, set both p=0.25 for 0→1 and q=0.25 for 1→0, predict initial probability of state 1 zero and probability 0.25 after one Step, then inspect. A second Step gives 0.375. Step advances one transition because it adds 0.5 seconds. The model starts at state 0 and computes exact distribution probabilities, not sampled sessions. It has no event IDs, CAS, concurrency, expiry, or storage revisions; it illustrates a transition rule only and does not validate a session consistency design.
Work it through
A revision check prevents one interaction from erasing another
Session revision 7 has ten accepted interactions. Writers A and B both read it. A wants to append event eA and B wants to append distinct event eB. Assume an atomic expected-revision commit, append-once event semantics, and retained deduplication IDs.
A commits eA with expected revision 7, producing revision 8 and eleven interactions. B's attempt with expected revision 7 fails rather than replacing A's accepted update.
B reloads revision 8, reapplies distinct eB, and commits with expected revision 8. The result is revision 9 with twelve interactions; both original intended events are present under the chosen arrival-order policy.
If B loses the success response and retries eB, its stable event identity is already recorded. An atomic duplicate check preserves revision 9 and twelve interactions under this append-once contract rather than creating a thirteenth.
Revision protection and deduplication solve different failure modes within a defined atomic scope. They do not establish the events' true action-time order or a cross-system publication guarantee.
The living notebook
Explore the model
Interactive lab · markov
A deterministic transition law is not a concurrent state store
Reset, set both p=0.25 and q=0.25, predict P(state 1)=0 initially, then Step once for 0.25 and again for 0.375. The fixed two-state chain starts at state 0; each 0.5-second Step advances one transition. Playback computes exact probabilities rather than sampled users. It has no session revision, CAS, event identity, retries, storage, or expiry.
- P(state 1) at step 0
- 0%
- Stationary P(state 1)
- 60%
- Chain behavior
- Monotone convergence
Exact finite-step probabilities for a two-state chain starting in state 0, not sampled trajectories. Playback advances two transitions per second. When p = q = 0 every distribution is stationary; when p = q = 1 the chain alternates forever. A single zero transition creates an absorbing state. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.
Look forA state transition rule needs a separate protocol for concurrent, repeated, and late real events.
Pause & reason
Check your understanding
Your turn
Try it yourself
Revision 20 contains six events. Two writers read it and append distinct events. One commits first, the other reloads after a failed expected-revision check and commits. A retry of the second event is suppressed without creating a revision under the stated contract. Find final revision and event count, and state what remains unknown.
Take a moment to make a prediction or work it out on paper. Open a hint when you need one.
Give me a hint
Count successful distinct state transitions, not delivery attempts.
Compare with the solution
Two distinct commits produce revision 22 and eight events; the suppressed duplicate changes neither. This establishes the chosen logical application sequence within the atomic store contract. It does not reveal which user action happened first in real time, guarantee publication to another system, or cover retries beyond retained deduplication history.
Keep the idea
Take it with you
- State revision, action time, producer sequence, and model version identify different things.
- Atomic version checks prevent lost updates; stable event identity and atomic application handle retries.
- Document expiry, late-event, fallback, and cross-system side-effect semantics explicitly.
Read further
Original sources and deeper explanations to follow at your own pace.
Optional progress, saved in this browser when available. Revisit any lesson at any time.
Lesson not marked complete.