Skip to content

Redis - Basic Commands

SET

The SET command can be used to set a key-value pair. Learn More

Time Complexity: O(1)

Example usage:

Text Only
SET somekey "somevalue"
SET key2 "value"
SET myapp.user#3.loginToken "abc123"
SET myapp.user#3.name "John Smith"
Text Only
SET somenumber 3
SET myapp.cars.count 3
SET myapp.loggedinUsers.count 1

To set expiration time (in second) for a key, the parameter EX can be used. The command TTL can be used to view the expiration of a key (Time to Live):

Text Only
SET expiringkey "Gone in 20 seconds" EX 20

Text Only
TTL expiringkey
Example Output
(integer) 16

TTL = Time To Live

KEYS

Keys

The KEYS command was used in the past for listing the keys. It is no longer recommended to use the KEYS command, as it blocks the whole database operation until it outputs the data and can cause massive problems in production environments with lots of data (it's slow and takes large amount of memory with large dataset). Use SCAN instead.

The KEYS command is not recommended to be used in production, but can be used for testing with smaller amount of data. Learn More

Time Complexity: O(N) with N being the number of keys in the database.

Example usage:

Text Only
KEYS *
Text Only
KEYS app#1
KEYS user.3.details

SCAN

Keys

The KEYS command was used in the past for listing the keys. It is no longer recommended to use the KEYS command, as it blocks the whole database operation until it outputs the data and can cause massive problems in production environments with lots of data (it's slow and takes large amount of memory with large dataset). Use SCAN instead.

The SCAN command can be used to view the keys. The command uses cursor to navigate through the keys. You need to pass it the cursor starting position (initially 0) and can also pass some other extra options, like filter and count. Sorting / retrieving data in a specific order is not supported. Learn More

Time Complexity: O(1) for the call, and O(N) where N = number of keys in the database.

Example usage:

Text Only
SCAN 0
Example Output
(cursor) 0
1) "myapp.cars.count"
2) "somenumber"
3) "myapp.loggedinUsers.count"
4) "myapp.user#3.loginToken"
5) "somekey"
6) "key2"
7) "myapp.user#3.name"

Defaults to 10

By default, when not passing the COUNT parameter, it will list approximately 10 keys.

Text Only
SCAN 0 COUNT 3
Example Output
(cursor) 5
1) "myapp.cars.count"
2) "somenumber"
3) "myapp.loggedinUsers.count"
4) "myapp.user#3.loginToken"

Hint, or requirement

Note that the COUNT parameter can return more or less keys than specified. The COUNT is merely a recommendation for Redis and it can return back different amount of elements that you wanted it to, but it tries to be very close to the specified value.

Text Only
SCAN 0 MATCH myapp.*
Example Output
(cursor) 0
1) "myapp.cars.count"
2) "myapp.loggedinUsers.count"
3) "myapp.user#3.loginToken"
4) "myapp.user#3.name"
Text Only
SCAN 0 MATCH *name*
Example Output
(cursor) 0
1) "myapp.user#3.name"
Text Only
SCAN 0 MATCH *name* COUNT 1000
Example Output
(cursor) 0
1) "myapp.user#3.name"
Text Only
SCAN 3
Example Output
(cursor) 0
1) "key2"
2) "myapp.user#3.name"

DBSIZE

The DBSIZE command can be used to determine how many keys are stored in the database. Learn More

Time Complexity: O(1)

Example:

Text Only
DBSIZE
Example Output
(integer) 6

GET

The GET command can be used to get value for a key. Learn More

Time Complexity: O(1)

Example usage:

Text Only
GET somekey
Example Output
"somevalue"
Text Only
GET myapp.loggedinUsers.count
Example Output
"1"
Text Only
GET myapp.emptiness
Example Output
(nil)

MGET & MSET

Time Complexity: O(N) where N is the amount of elements retrieved or set.

The MGET and MSET commands are the same as GET and SET, but they can be used to set or get multiple elements (M standing for multiple). They work the same as GET and SET but you can just pass them multiple elements.

Text Only
MSET name "aleksi" age 10
Example Output
OK
Text Only
MGET name age
Example Output
1) "aleksi"
2) "10"

DEL

The DEL command can be used to remove a key. Learn More

Time Complexity: Removing a single key is always O(1). If multiple keys are passed, then this operation works as O(N).

Example usage:

Text Only
DEL somekey
Example Output
(integer) 1

To verify that the key was removed:

Text Only
GET somekey

EXPIRE & TTL

The EXPIRE command can be used to set key to expire (to be removed) after given time. Learn More

The TTL command can be used to determine when will the key expire. Learn More

Time Complexity: O(1)

Example usage:

Text Only
EXPIRE key2 10
Example Output
(integer) 1
Text Only
TTL key2
Example Output
(integer) 3

TTL = Time To Live

Text Only
GET key2
Example Output
(nil)

RENAME

The RENAME command can be used to rename a key. Learn More

Time Complexity: O(1)

Example usage:

Text Only
RENAME myapp.cars.count myapp.cars.amount
Example Output
OK
Text Only
KEYS myapp.cars.*
Example Output
1) "myapp.cars.amount"

TYPE & OBJECT ENCODING

The commands TYPE and OBJECT ENCODING can be used to determine the type and encoding of the given key.

Learn more about type, object encoding.

Time Complexity: O(1)

To determine the type of a key:

Text Only
TYPE myapp.user#3.name
TYPE somenumber
Example Output
string
string

Notice that even the numbers that you have defined are being internally stored as string data types.

To actually determine the object encoding of the given string, we can use the OBJECT ENCODING command:

Text Only
OBJECT ENCODING myapp.user#3.name
OBJECT ENCODING somenumber
Example Output
"embstr"
"int"

INCR & DECR

Strings stored as integers can be increment or decremented using the INCR, DECR, INCRBY, and DECRBY commands.

Learn more about INCR, DECR, INCRBY, DECRBY

Time Complexity: O(1)

To increment a value by one:

Text Only
INCR myapp.cars.amount
Example Output
(integer) 4

To decrement a value by one:

Text Only
DECR myapp.cars.amount
Example Output
(integer) 3

And to increment or decrement by a given value, we can use these commands:

Text Only
INCRBY myapp.cars.amount 4
DECRBY myapp.cars.amount 2
Example Output
(integer) 7
(integer) 5

STRLEN

The STRLEN command can be used to determine the count of characters in a string. Learn More

Time Complexity: O(1)

Example:

Text Only
STRLEN myapp.user#3.loginToken
Example Output
(integer) 6

FLUSHDB

The FLUSHDB command can be used to remove all keys from the database. USE WITH CAUTION! Learn More

Time Complexity: O(N)

Example:

Text Only
FLUSHDB
Example Output
OK