Session-Based & Real-Time RecommendationsOpen in Colab
ML project loop
Data·Hypothesis space·Objective·Optimization·Evaluation·Deployment feedback

Session-Based & Real-Time Recommendations

A user who spent six months listening to jazz opens Spotify today searching for workout tracks. A static collaborative-filtering model knows only their long-term jazz preference — it misses the current intent entirely. Session-based recommendation corrects this by treating the current browsing sequence as the primary signal, letting the system respond to what you want right now rather than what you wanted historically.

Real-world examples: DoorDash (real-time menu re-ranking as you browse), Netflix (adapt recommendations mid-session), Instacart (cart-aware suggestions), Pinterest (interest-driven feed refresh), LinkedIn (job search session modeling).

What is a session?

A session is a temporally contiguous sequence of interactions — typically delimited by a time gap (e.g., 30 minutes idle → new session). Within a session, interactions carry strong sequential signal: item A viewed → item B added to cart strongly implies interest in items similar to B, not just items correlated with A in all-time collaborative filtering.

Sessions introduce two challenges absent from classical CF:

  1. No persistent user identity: the user may be anonymous or a new visitor. All you have is the current click stream.
  2. Order matters: the transition A→B→C is semantically different from C→B→A.

That 30-minute gap is not a detail — it is your definition of a session, and no value of it is more correct than another. The session-window walkthrough measures this directly: on one event stream a 2-second gap yields 9 sessions and a 5-second gap yields 7, from identical events. Whatever you pick silently determines what every downstream sequence model is trained on.

Sequence models for recommendation

The natural tool is an autoregressive sequence model trained to predict the next item given the history of items in the session.

GRU4Rec

The first widely adopted deep session-based model: encode the item sequence with a GRU and project the final hidden state to item scores.

ht=GRU(eit, ht−1),y^=W hT\mathbf{h}_t = \text{GRU}(\mathbf{e}_{i_t},\, \mathbf{h}_{t-1}), \qquad \hat{\mathbf{y}} = \mathbf{W}\,\mathbf{h}_T

Trained with BPR-max or cross-entropy loss against in-session negatives. Simple, fast, and surprisingly strong for short sessions (≤ 20 items).

SASRec (Self-Attentive Sequential Recommendation)

Replace the GRU with a causal self-attention stack — the Transformer decoder applied to item sequences. Position encodings and left-to-right masking preserve the sequential order.

S=TransformerDecoder([ei1,ei2,…,eiT])\mathbf{S} = \text{TransformerDecoder}([\mathbf{e}_{i_1}, \mathbf{e}_{i_2},\ldots,\mathbf{e}_{i_T}])

SASRec outperforms GRU4Rec on most benchmarks by capturing long-range item dependencies within the session. Its query/key/value are item embeddings — the same architecture as an LLM, narrowed to a single domain.

BERT4Rec for bidirectional context

SASRec uses causal (left-to-right) attention. BERT4Rec instead trains with masked-item prediction (like BERT's MLM) so it can attend in both directions — useful when the task is to fill a gap rather than predict the next item. At serving time, you always predict the rightmost masked position.

Handling anonymous sessions

When there is no user ID, the model input is only the session item sequence. Two strategies:

  • Session embedding as the user representation: use the last SASRec hidden state in place of a user embedding in the two-tower retrieval model.
  • Nearest-session retrieval: embed the current session and find the kk most similar past sessions from a session store; blend their next-item distributions.

The real-time feature pipeline

Session-based models require low-latency feature computation. The standard pattern is a two-layer cache:

LayerContentsLatency target
Online cache (Redis)Current session clicks, cart contents, dwell time< 5 ms
Near-real-time store1-hour aggregates (click-through rates, trending items)< 50 ms
Batch feature storeUser long-term profile, item statistics< 1 s (pre-fetched)

DoorDash's real-time ranking system: on every page view, the session sequence is encoded in < 20 ms, merged with batch user embeddings, and passed to the ranking model.

Training-serving skew in real-time features

If training used "items clicked in the last hour" computed from logs, but serving computes it from a Redis stream, even a small difference in windowing logic causes feature skew — a major source of silent model degradation. Use the same feature computation library at both training and serving time (often via a feature store with point-in-time correctness).

Two walkthroughs make the cost of getting this wrong concrete: train-serve skew shows a one-constant mismatch that leaves offline AUC completely unchanged while deflating an affected slice's scores by 28%, and point-in-time correctness shows what a naive training join costs when a feature is caused by the label.

Contextual bandits for real-time personalization

Sequence models optimize a next-item ranking, but they don't handle exploration vs exploitation. If the model only recommends items it already knows a user likes, it never discovers that the user would enjoy a new category. Contextual bandits formalize this trade-off.

In the LinUCB family, each item ii has a reward estimate plus an uncertainty bonus:

a∗=arg⁡max⁡i[μ^i(x)+αx⊤Ai−1x]a^* = \arg\max_i \left[ \hat{\mu}_i(\mathbf{x}) + \alpha \sqrt{\mathbf{x}^\top A_i^{-1} \mathbf{x}} \right]

The uncertainty bonus x⊤Ai−1x\sqrt{\mathbf{x}^\top A_i^{-1} \mathbf{x}} inflates scores for under-explored items. As the model gathers more data about item ii in context x\mathbf{x}, the AiA_i matrix grows and the bonus shrinks, naturally shifting back to exploitation.

In practice, platforms implement a lightweight version: serve the ranking model's top-kk with probability 1−ε1-\varepsilon, and insert one randomly selected from outside the top-kk with probability ε\varepsilon (ε-greedy exploration). The random insertion doubles as a constant stream of unbiased feedback for offline evaluation.

Deep dive
Session-Based Recommendation Algorithms
Includes runnable notebook

Putting it together: DoorDash's real-time re-ranking

  1. User opens the app and types "sushi".
  2. The query encoder embeds the text query.
  3. The session encoder (SASRec) embeds their last 10 item views from this session (pushed to Redis by the click-tracking service).
  4. The two embeddings are concatenated and passed to the ranking model (GBDT over concatenated features + cross-features like restaurant–query similarity).
  5. The ranked list is returned in < 50 ms.
  6. Every click is pushed back to Redis and the session feature cache, so the next request already incorporates it.

No batch job runs between user actions — the system responds in near-real time.

Exercise

A user's all-time listening history shows jazz preferences, but in the current session they've played 5 hip-hop tracks. Which architecture is best suited to recommend the next track?

Need a hint?

Think about what signal each architecture relies on.

Exercise

Your recommendation system has high exploitation and low exploration. Which symptom best describes the resulting user experience?

Need a hint?

What happens when a system only recommends what it already thinks you like?

Exercise

A session-based recommender needs the user's last 5 item clicks, computed with < 5 ms staleness. Which storage tier should serve this feature at inference time?

Need a hint?

Consider the latency and freshness requirements.

Common mistakes

  1. Feeding long-term profiles when intent just changed. If the session signal disagrees with the historical profile, the session should win — that is the whole point of session-based models.
  2. Computing real-time features differently at train and serve time. Even small windowing differences cause silent training-serving skew; share one feature library.
  3. Serving pure exploitation. Without an exploration slot the model never collects data outside its own recommendations, and offline evaluation becomes biased.

Key takeaways

  • Session = ordered click sequence within a time window; sequential models outperform CF when current intent differs from historical preferences.
  • SASRec (causal self-attention over item sequences) is the dominant architecture; it is an LLM decoder specialized to item embeddings.
  • Real-time features require a two-layer cache (online Redis + near-real-time store) to hit < 50 ms SLAs; training-serving skew is the biggest risk.
  • Contextual bandits (LinUCB, ε-greedy) inject exploration into an otherwise exploit-only ranker, preventing filter bubbles and generating unbiased feedback.

Related concepts

Concept neighbourhood

How this concept connects — click any node to jump there, or explore the whole concept map.

Builds onRelatedGo deeper
Session-Based & Real-Time Recomme…Linear Algebra for MLFrom Batch to StreamingDeployment PitfallsFeature StoresDeep & Two-Tower Recommen…Markov Decision ProcessesDiversity, Cold Start & E…Session-Based Recommendat…