Redis is a fast, in-memory database. Why do developers follow specific usage patterns when working with Redis?
Think about how Redis stores data and what affects its performance.
Redis is designed for speed and efficiency. Using recommended patterns helps use memory wisely and keeps operations fast.
Given these Redis commands executed in order:
SET user:1 "Alice" HSET user:1:stats visits 5 HINCRBY user:1:stats visits 1 GET user:1 HGET user:1:stats visits
What are the values returned by the GET and HGET commands?
Remember that HINCRBY increases the integer value of a hash field by 1.
The GET user:1 returns "Alice" because it was set. The HGET returns "6" because HINCRBY increased the visits from 5 to 6.
Choose the correct Redis command to add members 'apple', 'banana', and 'cherry' to the set 'fruits'.
Recall the command used to add members to a set in Redis.
SADD is the correct command to add one or more members to a set. The other commands are invalid or incorrect.
You want to store user sessions in Redis efficiently. Which approach optimizes memory usage best?
Think about how Redis hashes store multiple fields efficiently.
Redis hashes are memory efficient for storing many small fields per key, making them ideal for session data.
Consider this Lua script run in Redis:
local count = redis.call('INCR', KEYS[1])
if count == 1 then
redis.call('EXPIRE', KEYS[1], ARGV[1])
end
return countWhen running with redis-cli --eval script.lua , 10, it returns an error. What is the cause?
Check how keys and arguments are passed to Redis Lua scripts.
The error occurs because the script expects a key in KEYS[1], but the command line did not pass any keys, only arguments. The correct usage requires keys before the comma.