TechSchool
Home / Interview questions / Redis

Redis interview questions

52 questions about Redis with short answers and code. Each answer links to a full lesson on this site. Free, with no signup and no ads.

Basics

1. What is Redis?

Redis is an open-source in-memory database. It stores data as key-value pairs. It is often used for caching and message queues.

Full lesson: Redis intro

2. How do you set a string value in Redis?

Use the SET command. Give the key name and the value. Redis returns OK when the write succeeds.

SET weather "sunny"

Full lesson: Strings set

3. How do you read a string value in Redis?

Use the GET command with the key name. Redis returns the value. If the key does not exist, GET returns nil.

GET weather

Full lesson: Strings get

4. How do you find the data type of a key in Redis?

Use the TYPE command with the key name. Redis returns a type such as string, list, set, zset, or hash. It returns none when the key does not exist.

TYPE weather

Full lesson: Keys type

5. How do you add an element to the head of a Redis list?

Use the LPUSH command. Give the key name and one or more values. Redis returns the new list length.

LPUSH list1 "foo"

Full lesson: Lists lpush

6. How do you add an element to the tail of a Redis list?

Use the RPUSH command. Give the key name and one or more values. Redis returns the new list length.

RPUSH list1 "bar"

Full lesson: Lists rpush

7. How do you get the length of a Redis list?

Use the LLEN command with the key name. Redis returns the number of elements. If the key does not exist, LLEN returns 0.

LLEN list1

Full lesson: Lists llen

8. How do you read a list element by its index in Redis?

Use the LINDEX command with the key name and the index. Index 0 is the first element. A negative index counts from the end, so -1 is the last element.

LINDEX mylist 0

Full lesson: Lists lindex

9. How do you remove the first element of a Redis list?

Use the LPOP command with the key name. Redis removes the first element and returns it. If the key does not exist, LPOP returns nil.

LPOP list1

Full lesson: Lists lpop

10. How do you remove the last element of a Redis list?

Use the RPOP command with the key name. Redis removes the last element and returns it. If the key does not exist, RPOP returns nil.

RPOP mylist

Full lesson: Lists rpop

11. How do you change one list element by index in Redis?

Use the LSET command with the key name, the index, and the new value. Redis returns OK on success. If the index is out of range, Redis returns an error.

LSET mylist 0 "hello"

Full lesson: Lists lset

12. How do you keep only a range of elements in a Redis list?

Use the LTRIM command with the key name, the start index, and the stop index. Redis removes all elements outside the range. Index -1 means the last element.

LTRIM mylist 0 1

Full lesson: Lists ltrim

13. How do you remove list elements that match a value in Redis?

Use the LREM command with the key, a count, and the value. A positive count searches from the head. A count of 0 removes all matches.

LREM mylist 0 "hello"

Full lesson: Lists lrem

14. How do you get the difference between Redis sets?

Use the SDIFF command with the first key and the other keys. Redis returns the members that are only in the first set. Missing keys count as empty sets.

SDIFF key1 key2 key3

Full lesson: Sets sdiff

15. How do you wait for a list element in Redis?

Use the BLPOP command with one or more keys and a timeout. Redis blocks until an element arrives or the timeout ends. The reply holds the key name and the value.

BLPOP list1 100

Full lesson: Lists blpop

16. How do you increase a number value in Redis?

Use the INCR command with the key name. Redis adds 1 and returns the new value. If the key does not exist, Redis starts from 0.

INCR counter

Full lesson: Strings incr

17. How do you decrease a number value in Redis?

Use the DECR command with the key name. Redis subtracts 1 and returns the new value. If the key does not exist, Redis starts from 0.

DECR counter

Full lesson: Strings decr

Intermediate

18. How do you set many string values in one Redis call?

Use the MSET command with key-value pairs. Redis sets all pairs in one step. It returns OK when the write succeeds.

MSET key1 "a" key2 "b"

Full lesson: Strings mset

19. How do you read many string values in one Redis call?

Use the MGET command with the key names. Redis returns the values in the same order. A missing key returns nil in its position.

MGET key1 key2

Full lesson: Strings mget

20. What mistake happens when you push to a key that holds a string?

LPUSH and RPUSH work only on list keys. If the key holds a string, Redis returns an error. Check the type with TYPE before you push.

SET list1 "x"
LPUSH list1 "foo"

Full lesson: Lists lpush

21. What mistake happens when you use LSET on a missing key?

LSET needs an existing list and a valid index. On an empty or missing list, Redis returns an error. Create the list with RPUSH first.

LSET mylist 0 "hello"

Full lesson: Lists lset

22. What mistake happens when you call LPOP on a missing key?

LPOP does not raise an error on a missing key. It returns nil. Check for nil in your code before you use the result.

LPOP missing_list

Full lesson: Lists lpop

23. What mistake happens when you use LINDEX with a large index?

LINDEX returns nil when the index is outside the list range. It does not return an error. Test the result before you use it.

LINDEX mylist 99

Full lesson: Lists lindex

24. What mistake happens when you use BLPOP without a timeout plan?

BLPOP blocks the connection until an element arrives or the timeout ends. A timeout of 0 blocks forever. Always set a timeout value.

BLPOP list1 100

Full lesson: Lists blpop

25. What mistake happens when you use LTRIM with the wrong range?

LTRIM deletes all elements outside the start and stop range. A wrong range removes data that you want to keep. Test the range on a copy first.

LTRIM mylist 0 1

Full lesson: Lists ltrim

26. What does the Redis LPUSH command return?

LPUSH returns the length of the list after the insert. The list length is the result, and the insert happens first.

LPUSH list1 "foo"
(integer) 1
LPUSH list1 "bar"
(integer) 2

Full lesson: Lists lpush

27. What does the Redis RPUSH command return?

RPUSH returns the new list length after the insert. The command adds values at the tail, then it reports the size.

RPUSH mylist "hello"
(integer) 1
RPUSH mylist "foo"
(integer) 2

Full lesson: Lists rpush

28. How do you read all elements of a Redis list?

Use LRANGE with start 0 and end -1. The index -1 means the last element, so you get the full list.

RPUSH mylist "one"
RPUSH mylist "two"
LRANGE mylist 0 -1

Full lesson: Lists lrange

29. What does the Redis LTRIM command return?

LTRIM returns OK after it keeps the given range. Elements outside the range are removed from the list.

RPUSH mylist "hello"
LTRIM mylist 0 0
LRANGE mylist 0 -1

Full lesson: Lists ltrim

30. How do you add a value to the head of a list only if the list exists?

Use LPUSHX on an existing list. If the list does not exist, the command does nothing and returns 0.

LPUSH list1 "foo"
LPUSHX list1 "bar"
(integer) 2
LPUSHX list2 "bar"
(integer) 0

Full lesson: Lists lpushx

31. How do you add a value to the tail of a list only if the list exists?

Use RPUSHX on an existing list. If the list is missing, the operation is invalid and the return value is 0.

RPUSH mylist "hello"
RPUSHX mylist "foo"
(integer) 2
RPUSHX mylist2 "bar"
(integer) 0

Full lesson: Lists rpushx

32. How do you set a string value and then read it back in Redis?

Use SET to write the value, then GET to read it. SET overwrites an old value at the same key.

SET key "value"
OK
GET key
"value"

Full lesson: Strings set

33. What does the Redis GET command return for a missing key?

GET returns nil when the key does not exist. The nil result tells you that no value is stored.

GET db
(nil)
SET db redis
OK
GET db
"redis"

Full lesson: Strings get

34. How do you set many key-value pairs in one Redis command?

Use MSET with pairs of keys and values. MSET always returns OK after it sets all pairs.

MSET key1 "Hello" key2 "World"
OK
GET key1
"Hello"

Full lesson: Strings mset

Common mistakes

35. How do you read many keys in one Redis command?

Use MGET with a list of keys. A missing key returns nil in the result list.

SET key1 "hello"
SET key2 "world"
MGET key1 key2 someOtherKey
1) "hello"
2) "world"
3) (nil)

Full lesson: Strings mget

36. What happens when you run INCR on a key that does not exist?

Redis first sets the key to 0, then it adds one. The command returns the new value, which is 1.

INCR page_view
(integer) 1
INCR page_view
(integer) 2

Full lesson: Strings incr

37. What happens when you run DECR on a key that does not exist?

Redis first sets the key to 0, then it subtracts one. The command returns the new value, which is -1.

DECR failure_times
(integer) -1
SET failure_times 10
DECR failure_times
(integer) 9

Full lesson: Strings decr

38. What mistake happens when you use GET on a key that holds a list?

GET returns an error because the value is not a string. Use list commands such as LRANGE for list keys.

LPUSH db redis mongodb mysql
GET db
(error) WRONGTYPE Operation against a key holding the wrong kind of value

Full lesson: Strings get

39. What mistake happens when you use LPUSHX on a missing key and expect a new list?

LPUSHX does nothing when the key does not exist. It returns 0 and no list is created, so use LPUSH to create a list.

LPUSHX list2 "bar"
(integer) 0
LRANGE list2 0 -1
(empty list or set)

Full lesson: Lists lpushx

40. What mistake happens when you use RPUSHX on a missing key and expect a new list?

RPUSHX is invalid when the list does not exist. It returns 0 and creates no list, so use RPUSH to create one.

RPUSHX mylist2 "bar"
(integer) 0
LRANGE mylist2 0 -1
(empty list or set)

Full lesson: Lists rpushx

41. What does the Redis INCR command return?

The INCR command returns the value of the key after the increment. Redis adds one to the stored number. If the key does not exist, Redis first sets it to 0.

redis> SET page_view 20
OK
redis> INCR page_view
(integer) 21

Full lesson: Strings incr

42. What does the Redis DECR command return?

The DECR command returns the value of the key after the decrement. Redis subtracts one from the stored number. If the key does not exist, Redis first sets it to 0.

redis> SET failure_times 10
OK
redis> DECR failure_times
(integer) 9

Full lesson: Strings decr

43. What does the Redis MSET command return?

The MSET command always returns OK. It sets one or more key-value pairs at the same time. The command runs as one atomic step.

redis 127.0.0.1:6379> MSET key1 "Hello" key2 "World"
OK

Full lesson: Strings mset

44. What does the Redis MGET command return for a missing key?

The MGET command returns a list of values for the given keys. A missing key returns the special value nil. The other keys still return their values.

redis 127.0.0.1:6379> MGET key1 key2 someOtherKey
1) "Hello"
2) "World"
3) (nil)

Full lesson: Strings mget

45. How do you read a range of list elements in Redis?

Use the LRANGE command with a start index and a stop index. The command returns the elements in that range. A stop index of -1 means the last element.

redis 127.0.0.1:6379> LRANGE mylist 0 -1
1) "hello"
2) "world"

Full lesson: Lists lrange

46. What does the Redis BLPOP command do?

The BLPOP command removes and returns the first element of a list. If the list is empty, Redis waits until an element arrives or the timeout ends. The timeout is given in seconds.

redis 127.0.0.1:6379> BLPOP mylist 5
1) "mylist"
2) "hello"

Full lesson: Lists blpop

47. What does the Redis BRPOP command do?

The BRPOP command removes and returns the last element of a list. If the list is empty, Redis waits until an element arrives or the timeout ends. The timeout is given in seconds.

redis 127.0.0.1:6379> BRPOP mylist 5
1) "mylist"
2) "world"

Full lesson: Lists brpop

48. What is a Redis list?

A Redis list is an ordered collection of string elements. You can add elements to the head or the tail. Lists can hold many elements and support fast access at both ends.

Full lesson: Redis lists

49. What mistake happens when you run INCR on a key that holds a non-numeric string?

Redis returns an error because the string cannot be read as a number. The stored value does not change. Always store numeric strings before you use INCR or DECR.

redis> SET name "tom"
OK
redis> INCR name
(error) ERR value is not an integer or out of range

Full lesson: Strings incr

50. What mistake happens when you use MSET with an odd number of arguments?

Redis returns an error because MSET needs key-value pairs. Each key must have one value. Count the arguments before you send the command.

redis 127.0.0.1:6379> MSET key1 "Hello" key2
(error) ERR wrong number of arguments for 'mset' command

Full lesson: Strings mset

51. What mistake happens when you use MGET with no keys?

Redis returns an error because MGET needs at least one key. The command does not run. Pass one or more key names to MGET.

redis 127.0.0.1:6379> MGET
(error) ERR wrong number of arguments for 'mget' command

Full lesson: Strings mget

52. What mistake happens when you use BLPOP with a timeout of zero and no producer?

A timeout of zero makes BLPOP wait forever. The client blocks until an element arrives. Always set a real timeout when no producer is guaranteed.

redis 127.0.0.1:6379> BLPOP mylist 0
(blocks until an element arrives)

Full lesson: Lists blpop

More on TechSchool
See the Redis cheat sheet Python JavaScript TypeScript HTML CSS React Vue 3 Node.js Java PHP C C++ C# Go SQL MongoDB Linux Git Docker Data Structures