Session storage pattern in Redis - Time & Space Complexity
When storing user sessions in Redis, it is important to understand how the time to save or retrieve a session changes as the number of sessions grows.
We want to know how the cost of session operations grows with more users.
Analyze the time complexity of the following Redis commands used for session storage.
# Store session data as a hash
HSET session:1234 user_id 42 last_active 1680000000
# Retrieve session data
HGETALL session:1234
# Delete session when user logs out
DEL session:1234
# Set expiration for session
EXPIRE session:1234 3600
This code stores, retrieves, deletes, and expires a user session using Redis hashes and keys.
Look at the main operations that happen repeatedly when managing sessions.
- Primary operation: Accessing a Redis hash for a single session key.
- How many times: Once per session operation (store, retrieve, delete).
Each session operation works on one key and its hash fields, so the time depends mostly on the size of that session's data, not the total number of sessions.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 sessions | About the same time per session operation |
| 100 sessions | Still about the same time per session operation |
| 1000 sessions | Still about the same time per session operation |
Pattern observation: Time per session operation stays roughly constant no matter how many sessions exist.
Time Complexity: O(1)
This means each session operation takes about the same time regardless of how many sessions are stored.
[X] Wrong: "As the number of sessions grows, retrieving one session will take longer because Redis has to look through all sessions."
[OK] Correct: Redis uses keys to directly access each session, so it does not scan all sessions. Access time stays constant.
Understanding how Redis handles session storage efficiently shows you know how to design fast, scalable systems that keep user data ready without delay.
"What if we stored all sessions inside one big Redis hash instead of separate keys? How would the time complexity change?"
Practice
Solution
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.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.Final Answer:
To keep temporary user data during their visit -> Option AQuick Check:
Session storage = temporary user data [OK]
- Thinking Redis stores permanent files
- Confusing session storage with database replacement
- Assuming Redis does complex calculations
Solution
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.Step 2: Identify command to save with expiration
SETEX is the command that sets a key's value and expiration time in one step.Final Answer:
SETEX -> Option DQuick Check:
Save with expiration = SETEX [OK]
- Using GET to save data
- Using DEL to save data
- Confusing HGETALL with setting data
SETEX session:12345 300 "user_data"GET session:12345What will be the output of the GET command immediately after SETEX?
Solution
Step 1: Understand SETEX effect
SETEX sets the key 'session:12345' with value 'user_data' and expiration 300 seconds.Step 2: Check GET output immediately after SETEX
Since the key exists and is not expired yet, GET returns the stored value 'user_data'.Final Answer:
"user_data" -> Option CQuick Check:
GET after SETEX returns stored value [OK]
- Expecting nil immediately after SETEX
- Confusing expiration time with returned value
- Thinking GET returns expiration time
DEL session:12345GET session:12345Solution
Step 1: Check DEL command correctness
DEL session:12345 is correct syntax to delete the key.Step 2: Understand GET after DEL
After deletion, GET returns nil because the key no longer exists.Final Answer:
No error; GET returns nil after DEL -> Option BQuick Check:
DEL deletes key, GET returns nil [OK]
- Thinking DEL syntax is wrong
- Expecting GET to return old data after DEL
- Believing DEL does not remove keys
Solution
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.Step 2: Confirm deletion after setting
DEL removes the key when session ends, so DEL after SETEX is correct.Final Answer:
SETEX session:abc 600 "data"; DEL session:abc -> Option AQuick Check:
SETEX sets with expiry, DEL deletes key [OK]
- Setting expiration after deleting key
- Using GET before setting session
- Setting expiration separately instead of SETEX
Ready to prove it?
Pass the test and this pattern moves unknown
