Redis is a key-value store with different data structures than relational databases. Which reason best explains why data modeling in Redis differs?
Think about the types of data Redis can store beyond simple strings.
Redis supports multiple data structures such as lists, sets, sorted sets, and hashes. This flexibility means data modeling must consider these structures rather than just tables and rows.
Given the following Redis commands, what is the output of LRANGE mylist 0 -1?
LPUSH mylist "apple" LPUSH mylist "banana" LPUSH mylist "cherry"
Remember that LPUSH adds elements to the head (left) of the list.
LPUSH adds elements to the start of the list, so the last pushed element is first. The list order is cherry, banana, apple.
Choose the correct Redis command syntax to add the member "orange" to the set "fruits".
Sets in Redis use a specific command to add members.
The SADD command adds one or more members to a set. ADD and SET are invalid commands for sets, and LPUSH is for lists.
You want to store user session data in Redis for quick access. Which data modeling approach is best for fast retrieval by session ID?
Consider how to quickly access a session by its unique ID.
Storing each session as a separate key with a hash allows direct access by session ID, which is fast. Lists and streams require scanning, which is slower.
Given this Lua script run in Redis, why does the field "count" not update as expected?
local key = KEYS[1]
local field = ARGV[1]
local increment = tonumber(ARGV[2])
local current = redis.call('HGET', key, field)
if not current then
current = 0
else
current = tonumber(current)
end
redis.call('HSET', key, field, current + increment)Check what happens if the field exists but contains a non-numeric value.
If 'current' is nil, the script sets it to 0 as a number, which is correct. However, if 'current' is a string that cannot convert to number, tonumber(current) returns nil, causing an error in addition.