Sample questions
Redis Expiration EvictionDifficulty 1
Which command sets a time-to-live (in seconds) on an existing Redis key?
- a
TTL key 60 - b
EXPIRE key 60✓ - c
PERSIST key 60 - d
DEL key 60
Explanation:EXPIRE key seconds sets a TTL on an existing key, after which it will be automatically deleted. TTL only reads the remaining time, PERSIST removes a TTL, and DEL deletes immediately.
Redis Expiration EvictionDifficulty 1
What does TTL key return when the key exists but has no expiration set?
Explanation:TTL returns -1 for a key that exists but has no associated expiration, and -2 if the key does not exist at all. A positive number is the remaining seconds.
Redis Expiration EvictionDifficulty 1
What does TTL key return when the key does not exist at all?
Explanation:-2 specifically means the key does not exist. -1 means it exists without a TTL. This distinction matters for correctly branching in application code.
Redis Expiration EvictionDifficulty 1
Which command removes an existing TTL from a key, making it persist forever (until explicitly deleted)?
- a
EXPIRE key -1 - b
TTL key 0 - c
UNSET key - d
PERSIST key✓
Explanation:PERSIST key removes the existing expiration on a key, turning it back into a key with no TTL. EXPIRE key -1 actually deletes the key immediately rather than clearing its TTL.
Redis Expiration EvictionDifficulty 2
A key has a TTL set. An application then runs a plain SET key newvalue (without KEEPTTL) to overwrite it. What happens to the TTL?
- aThe TTL is preserved automatically, since
SET only changes the value - bThe TTL is removed unless
KEEPTTL is passed✓ - cThe TTL restarts counting from zero with the same duration
- d
SET is rejected on a key that already has a TTL
Explanation:By default, SET clears any existing TTL on the key, making it persistent. Passing the KEEPTTL option preserves the original expiration instead.
Redis Expiration EvictionDifficulty 2
Which SET option combination sets a string value and gives it a 30-second TTL in a single atomic command?
- a
SET key value followed separately by TTL key 30 - b
SET key value NX 30 - c
SET key value EX 30✓ - d
SET key value PERSIST 30
Explanation:SET key value EX 30 sets the value and a 30-second TTL atomically in one round trip. PX does the same in milliseconds. TTL is a read-only command with no such argument form.