Bird
Raised Fist0
Redisquery~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using Redis for session storage?
easy
A. To keep temporary user data during their visit
B. To permanently store user files
C. To perform complex calculations
D. To replace the database for all data

Solution

  1. Step 1: Understand session storage concept

    Session storage is used to keep temporary data about a user while they interact with a website or app.
  2. Step 2: Identify Redis role in session storage

    Redis is used to store this temporary data quickly and with expiration to remove it after the session ends.
  3. Final Answer:

    To keep temporary user data during their visit -> Option A
  4. Quick Check:

    Session storage = temporary user data [OK]
Hint: Session storage = temporary user data storage [OK]
Common Mistakes:
  • Thinking Redis stores permanent files
  • Confusing session storage with database replacement
  • Assuming Redis does complex calculations
2. Which Redis command is used to save session data with an expiration time?
easy
A. GET
B. HGETALL
C. DEL
D. SETEX

Solution

  1. Step 1: Recall Redis commands for session storage

    GET retrieves data, DEL deletes data, HGETALL gets all hash fields, SETEX sets a key with expiration.
  2. Step 2: Identify command to save with expiration

    SETEX is the command that sets a key's value and expiration time in one step.
  3. Final Answer:

    SETEX -> Option D
  4. Quick Check:

    Save with expiration = SETEX [OK]
Hint: SETEX sets value with expiration in Redis [OK]
Common Mistakes:
  • Using GET to save data
  • Using DEL to save data
  • Confusing HGETALL with setting data
3. Given the commands:
SETEX session:12345 300 "user_data"
GET session:12345
What will be the output of the GET command immediately after SETEX?
medium
A. SyntaxError
B. nil
C. "user_data"
D. 300

Solution

  1. Step 1: Understand SETEX effect

    SETEX sets the key 'session:12345' with value 'user_data' and expiration 300 seconds.
  2. Step 2: Check GET output immediately after SETEX

    Since the key exists and is not expired yet, GET returns the stored value 'user_data'.
  3. Final Answer:

    "user_data" -> Option C
  4. Quick Check:

    GET after SETEX returns stored value [OK]
Hint: GET returns value if key exists and not expired [OK]
Common Mistakes:
  • Expecting nil immediately after SETEX
  • Confusing expiration time with returned value
  • Thinking GET returns expiration time
4. What is wrong with this Redis command sequence for session deletion?
DEL session:12345
GET session:12345
medium
A. DEL command syntax is incorrect
B. No error; GET returns nil after DEL
C. GET will still return data after DEL
D. DEL does not delete keys

Solution

  1. Step 1: Check DEL command correctness

    DEL session:12345 is correct syntax to delete the key.
  2. Step 2: Understand GET after DEL

    After deletion, GET returns nil because the key no longer exists.
  3. Final Answer:

    No error; GET returns nil after DEL -> Option B
  4. Quick Check:

    DEL deletes key, GET returns nil [OK]
Hint: DEL removes key; GET returns nil after deletion [OK]
Common Mistakes:
  • Thinking DEL syntax is wrong
  • Expecting GET to return old data after DEL
  • Believing DEL does not remove keys
5. You want to store user session data in Redis that expires after 10 minutes. Which sequence correctly sets and later deletes the session?
hard
A. SETEX session:abc 600 "data"; DEL session:abc
B. SET session:abc "data"; DEL session:abc; EXPIRE session:abc 600
C. SET session:abc 600 "data"; EXPIRE session:abc 600; DEL session:abc
D. GET session:abc; SET session:abc 600 "data"; DEL session:abc

Solution

  1. Step 1: Identify correct way to set with expiration

    SETEX sets the key with value and expiration in one command, which is efficient and correct.
  2. Step 2: Confirm deletion after setting

    DEL removes the key when session ends, so DEL after SETEX is correct.
  3. Final Answer:

    SETEX session:abc 600 "data"; DEL session:abc -> Option A
  4. Quick Check:

    SETEX sets with expiry, DEL deletes key [OK]
Hint: Use SETEX to set with expiry, then DEL to delete [OK]
Common Mistakes:
  • Setting expiration after deleting key
  • Using GET before setting session
  • Setting expiration separately instead of SETEX
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard