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
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.
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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