0
0
Redisquery~10 mins

Session storage pattern in Redis - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Session storage pattern
User logs in
Generate session ID
Store session data in Redis with session ID as key
User makes requests with session ID
Retrieve session data from Redis using session ID
Validate session and respond
Session expires or user logs out -> Delete session data
This flow shows how a session ID is created, stored in Redis with user data, retrieved on requests, and eventually expired or deleted.
Execution Sample
Redis
SET session:12345 "{\"user_id\":1,\"role\":\"admin\"}" EX 3600
GET session:12345
DEL session:12345
Store a session with key 'session:12345', retrieve it, then delete it to end the session.
Execution Table
StepCommandActionResult
1SET session:12345 "{\"user_id\":1,\"role\":\"admin\"}" EX 3600Store session data with 1 hour expiryOK
2GET session:12345Retrieve session data by key{"user_id":1,"role":"admin"}
3DEL session:12345Delete session data to end session1
4GET session:12345Try to retrieve deleted session-
💡 Session deleted at step 3, so retrieval at step 4 returns nil (no data).
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4
session:12345-{"user_id":1,"role":"admin"}{"user_id":1,"role":"admin"}--
Key Moments - 2 Insights
Why does GET return nil after DEL command?
Because DEL removes the session key from Redis, so the data no longer exists. See execution_table step 3 and 4.
What does EX 3600 mean in the SET command?
It sets the session to expire automatically after 3600 seconds (1 hour), so Redis deletes it if not manually removed.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the result of GET session:12345 at step 2?
AOK
B{"user_id":1,"role":"admin"}
Cnil
D1
💡 Hint
Check the 'Result' column for step 2 in execution_table.
At which step does the session data get deleted?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Command' and 'Action' columns to find when DEL is called.
If we remove EX 3600 from SET, what changes in variable_tracker?
ASession never expires automatically
BSession expires automatically after 1 hour
CSession is deleted immediately
DSession data is never stored
💡 Hint
EX sets expiry time; without it, session stays until deleted manually.
Concept Snapshot
Session storage pattern in Redis:
- Generate a unique session ID
- Store session data with SET key value EX seconds
- Retrieve session data with GET key
- Delete session with DEL key
- Expiry (EX) auto-removes session after time
- Used to track user login state efficiently
Full Transcript
The session storage pattern in Redis involves creating a unique session ID when a user logs in. This ID is used as a key to store session data, such as user ID and role, using the SET command with an expiry time (EX) to automatically remove stale sessions. When the user makes requests, the session data is retrieved with GET using the session ID. To end a session, DEL removes the session data. If the session expires or is deleted, GET returns nil indicating no session found. This pattern helps manage user sessions efficiently and securely.