Bird
Raised Fist0
Redisquery~20 mins

Session storage pattern in Redis - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Redis Session Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
1:30remaining
What is the output of this Redis command sequence?
Given these Redis commands executed in order:

1. SET session:123 user1
2. EXPIRE session:123 10
3. TTL session:123

What does the TTL command return immediately after setting the expiration?
Redis
SET session:123 user1
EXPIRE session:123 10
TTL session:123
A-2
B-1
C10
D0
Attempts:
2 left
💡 Hint
TTL returns the remaining time to live in seconds for a key with expiration.
📝 Syntax
intermediate
1:30remaining
Which Redis command syntax correctly stores a session with a 30-minute expiration?
You want to store a session key 'session:456' with value 'user2' that expires in 30 minutes. Which command is correct?
ASETEX session:456 1800 user2
BSET session:456 user2 EX 1800
CSET session:456 user2 PX 1800000
DEXPIRE session:456 1800 SET user2
Attempts:
2 left
💡 Hint
SETEX sets a key with expiration in seconds in one command.
❓ optimization
advanced
2:00remaining
Which approach optimizes session storage for frequent updates in Redis?
You store user sessions in Redis and update session data frequently. Which approach is best to optimize performance and reduce network overhead?
AStore session data as a Redis hash and update fields with HSET commands
BStore session data as a JSON string and overwrite with SET on every update
CStore session data in multiple keys per field and update keys individually
DStore session data in a Redis list and push updates to the list
Attempts:
2 left
💡 Hint
Hashes allow updating parts of data without rewriting the whole value.
🔧 Debug
advanced
2:00remaining
Why does this Redis session expiration not work as expected?
You run these commands:
1. SET session:789 user3
2. EXPIRE session:789 60
3. SET session:789 user3_updated

After 60 seconds, the key still exists. Why?
Redis
SET session:789 user3
EXPIRE session:789 60
SET session:789 user3_updated
AThe key was deleted before expiration
BEXPIRE command was not executed correctly
CRedis does not support expiration on string keys
DThe last SET command removes the expiration from the key
Attempts:
2 left
💡 Hint
Setting a key again resets its expiration unless specified.
🧠 Conceptual
expert
2:30remaining
What is the main advantage of using Redis session storage over traditional database sessions?
Why do many web applications prefer Redis for session storage instead of storing sessions in a traditional relational database?
ARedis supports complex SQL queries on session data
BRedis provides faster read/write performance due to in-memory storage
CRedis sessions never expire unless manually deleted
DRedis automatically encrypts session data for security
Attempts:
2 left
💡 Hint
Think about speed and how Redis stores data.

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