Practice questions & answers
1
Which Redis command is used to set a value for a string key only if the key does not already exist?
-
1.
SET
-
2.
SETNX
-
3.
MSET
-
4.
APPEND
Show explanation
SETNX stands for 'SET if Not eXists'. It is commonly used for distributed locking or ensuring keys are not overwritten.
2
In a Python application using the 'redis' library, what is the effect of setting 'decode_responses=True' in the Redis client constructor?
-
1.
It compresses the data before sending it to Redis.
-
2.
It automatically converts bytes returned by Redis into Python strings.
-
3.
It enables SSL encryption for the connection.
-
4.
It converts all numeric values into Python integers automatically.
Show explanation
By default, redis-py returns bytes. Setting decode_responses=True ensures that the client decodes the bytes into strings using the specified encoding (usually UTF-8).
3
Which Redis data structure is most suitable for representing a 'User' object with multiple fields like 'username', 'email', and 'age'?
-
1.
String
-
2.
List
-
3.
Hash
-
4.
Set
Show explanation
Hashes are maps between string fields and string values, making them the perfect data structure to represent objects.
4
What happens to the Time To Live (TTL) of a key if you use the SET command to overwrite its value without specifying an EX or PX argument?
-
1.
The TTL remains unchanged.
-
2.
The TTL is reset to its original value.
-
3.
The TTL is removed, and the key becomes persistent.
-
4.
The key is immediately deleted.
Show explanation
The SET command removes any existing TTL associated with a key unless specific expiration arguments (like KEEPTTL in newer versions) are provided.
5
If you use LPUSH to add 'A', then LPUSH to add 'B' to a list, what will LRANGE key 0 -1 return?
-
1.
['A', 'B']
-
2.
['B', 'A']
-
3.
['A']
-
4.
['B']
Show explanation
LPUSH inserts elements at the head (left) of the list. Inserting 'A' then 'B' results in ['B', 'A'].
6
Which caching pattern involves the application checking the cache first, and on a miss, loading data from the database and writing it to the cache?
-
1.
Write-Through
-
2.
Write-Behind
-
3.
Cache Aside
-
4.
Refresh-Ahead
Show explanation
Cache Aside (or Lazy Loading) is the most common pattern where the application manages the relationship between the database and the cache.
7
What is the time complexity of the HGET command in Redis?
-
1.
O(1)
-
2.
O(N) where N is the number of fields
-
3.
O(log N)
-
4.
O(N) where N is the total number of keys
Show explanation
HGET has a time complexity of O(1) because it performs a direct lookup of a field within a hash.
8
Which command would you use to increment a value stored in a Hash field by a specific integer?
-
1.
INCR
-
2.
HINCRBY
-
3.
INCRBY
-
4.
HINCR
Show explanation
HINCRBY is used to increment the integer value of a hash field by a given number.
9
What is the maximum size allowed for a single Redis string value?
-
1.
128 MB
-
2.
256 MB
-
3.
512 MB
-
4.
1 GB
Show explanation
A Redis string value can be at most 512 Megabytes in size.
10
In the context of caching, what is a 'Cache Stampede'?
-
1.
When the cache memory is completely full.
-
2.
When multiple application instances attempt to update the same key simultaneously.
-
3.
When a popular cached item expires and multiple requests simultaneously try to rebuild it from the database.
-
4.
When the Redis server crashes due to too many connections.
Show explanation
A Cache Stampede (or Thundering Herd) occurs when a frequently accessed key expires, causing a surge of requests to the backend database.
11
Which Redis command allows you to retrieve a range of elements from a list?
-
1.
LGET
-
2.
LINDEX
-
3.
LRANGE
-
4.
LSCAN
Show explanation
LRANGE returns the specified elements of the list stored at the key, using start and stop offsets.
12
What does the TTL command return if the key exists but has no associated expiration?
Show explanation
The TTL command returns -1 if the key exists but has no associated expire. It returns -2 if the key does not exist.
13
Which pattern involves updating the cache immediately whenever the database is updated?
-
1.
Cache Aside
-
2.
Write-Through
-
3.
Read-Through
-
4.
Write-Behind
Show explanation
In a Write-Through pattern, the application or a middleware writes data to both the database and the cache simultaneously.
14
How can you atomically remove and return the last element of a list in Redis?
-
1.
LPOP
-
2.
RPOP
-
3.
LREM
-
4.
DEL
Show explanation
RPOP removes and returns the last element (the tail) of the list.
15
Which command is used to set an expiration time on a key in seconds?
-
1.
EXPIRE
-
2.
PEXPIRE
-
3.
TTL
-
4.
SETTTL
Show explanation
EXPIRE sets a timeout on a key in seconds. PEXPIRE does the same but in milliseconds.
16
What is the result of the INCR command if the key does not exist?
-
1.
An error is returned.
-
2.
The key is created with a value of 0.
-
3.
The key is created with a value of 1.
-
4.
The command is ignored.
Show explanation
If the key does not exist, it is set to 0 before performing the operation, so the resulting value is 1.
17
In Python's 'redis' library, which method is used to set multiple hash fields at once?
-
1.
hset() with a mapping dictionary
-
2.
hmset_dict()
-
3.
hset_multiple()
-
4.
hadd()
Show explanation
In modern redis-py versions, the hset() method accepts a mapping dictionary to set multiple field-value pairs.
18
Which Redis command is used to remove a specific field from a hash?
-
1.
DEL
-
2.
HDEL
-
3.
HREM
-
4.
HPOP
Show explanation
HDEL removes one or more specified fields from a hash. DEL would delete the entire hash key.
19
What is the primary benefit of the 'Write-Behind' (Write-Back) caching pattern?
-
1.
It ensures data consistency between cache and DB.
-
2.
It reduces write latency for the application.
-
3.
It prevents cache penetration.
-
4.
It simplifies application logic.
Show explanation
Write-Behind improves performance by writing to the cache immediately and updating the database asynchronously.
20
Which command would you use to get the number of elements in a Redis list?
-
1.
LSIZE
-
2.
LLEN
-
3.
LCOUNT
-
4.
HLEN
Show explanation
LLEN returns the length of the list stored at the key.
21
What is 'Cache Penetration'?
-
1.
When the cache is so fast it 'penetrates' the network layer.
-
2.
When requests for non-existent data bypass the cache and hit the database every time.
-
3.
When the cache memory limit is exceeded.
-
4.
When a key is updated too frequently.
Show explanation
Cache penetration occurs when requests for keys that don't exist in either the cache or the DB cause every request to hit the DB.
22
Which Redis command is used to get all fields and values in a hash?
-
1.
HGET
-
2.
HKEYS
-
3.
HVALS
-
4.
HGETALL
Show explanation
HGETALL returns all fields and values of the hash stored at the key.
23
What is the purpose of the 'LTRIM' command?
-
1.
To remove whitespace from string values.
-
2.
To reduce the size of a list to a specified range of elements.
-
3.
To delete keys that have expired.
-
4.
To remove empty fields from a hash.
Show explanation
LTRIM trims an existing list so that it contains only the specified range of elements, often used to create capped lists.
24
In Redis, what does the 'PERSIST' command do?
-
1.
It saves the database to disk.
-
2.
It removes the expiration from a key.
-
3.
It makes a key read-only.
-
4.
It ensures a key is never deleted by the LRU policy.
Show explanation
PERSIST removes the existing timeout on a key, making it a persistent key.
25
Which of these is a blocking command used to implement a message queue?
-
1.
RPOP
-
2.
BRPOP
-
3.
LINDEX
-
4.
LPOP
Show explanation
BRPOP is the blocking version of RPOP. It blocks the connection if the list is empty until an element is available or a timeout is reached.
26
What is the time complexity of the STRLEN command?
-
1.
O(N) where N is the length of the string
-
2.
O(1)
-
3.
O(log N)
-
4.
O(N) where N is the number of keys
Show explanation
Redis stores the length of strings, so STRLEN is an O(1) operation.
27
Which command allows you to set a value and an expiration time atomically?
-
1.
SETEX
-
2.
EXPIRE
-
3.
SET
-
4.
Both SETEX and SET (with EX argument)
Show explanation
Both SETEX and the modern SET command with the EX (seconds) or PX (milliseconds) arguments perform the operation atomically.
28
What does the MGET command do?
-
1.
Gets the metadata of a key.
-
2.
Returns the values of all specified keys.
-
3.
Gets the memory usage of a key.
-
4.
Returns all keys matching a pattern.
Show explanation
MGET returns the values of all specified keys in a single request, which is more efficient than multiple GET calls.
29
Which Redis command is used to check if a specific field exists in a hash?
-
1.
HEXISTS
-
2.
HGET
-
3.
HCHECK
-
4.
EXISTS
Show explanation
HEXISTS returns 1 if the hash contains the specified field, and 0 otherwise.
30
What is the default eviction policy in Redis when it reaches the 'maxmemory' limit?
-
1.
allkeys-lru
-
2.
noeviction
-
3.
volatile-lru
-
4.
allkeys-random
Show explanation
By default, Redis returns an error (noeviction) when it reaches the memory limit and tries to execute commands that use more memory.
31
Which command is used to append a value to the end of an existing string?
-
1.
CONCAT
-
2.
APPEND
-
3.
PUSH
-
4.
UPDATE
Show explanation
APPEND appends the value at the end of the string. If the key doesn't exist, it is created.
32
How do you retrieve only the field names (keys) of a Hash?
-
1.
HGETALL
-
2.
HFIELDS
-
3.
HKEYS
-
4.
HSCAN
Show explanation
HKEYS returns all field names in the hash stored at the key.
33
In a Python app, using 'pipe = r.pipeline()' allows you to:
-
1.
Create a persistent connection.
-
2.
Group multiple commands into a single request to reduce round-trip time.
-
3.
Automatically retry failed commands.
-
4.
Stream data from the database.
Show explanation
Pipelining is a technique to improve performance by sending multiple commands to the server without waiting for individual replies.
34
Which command is used to atomically move an element from one list to another?
-
1.
LMOVE
-
2.
RPOPLPUSH
-
3.
MOVE
-
4.
Both LMOVE and RPOPLPUSH
Show explanation
RPOPLPUSH is the classic command, while LMOVE is a more generic version introduced in Redis 6.2.
35
What is the 'Lazy Freeing' feature in Redis?
-
1.
Deleting keys only when memory is needed.
-
2.
Deleting keys in a background thread to avoid blocking the main thread.
-
3.
Automatically expiring keys that haven't been accessed.
-
4.
A way to disable the DEL command.
Show explanation
Lazy Freeing (UNLINK command) allows Redis to reclaim memory in a background thread, preventing the server from blocking during the deletion of large objects.