At its core, Redis is a data storage engine that allows data to be stored in key-value pairs.
The key serves as a unique identifier. The value can be one of the supported data structures, such as strings, lists, sets, hashes and more.
At its core, Redis is a data storage engine that allows data to be stored in key-value pairs.
The key serves as a unique identifier. The value can be one of the supported data structures, such as strings, lists, sets, hashes and more.
To allow storing, retrieving, and manipulating data, Redis offers over 200 commands.
In this series, we will learn about 8 commonly used Redis commands — PING
, ECHO
, SET
, GET
, DEL
, KEYS
, EXPIRE
and TTL
.
The PING
command checks the connection status of a Redis server by testing whether a connection is still alive.
It returns "PONG"
if the server is running. This allows applications to ensure the availability of the Redis server before executing commands or queries.
redis> PING
“PONG”
The ECHO
command accepts an argument and simply returns it.
Similar to PING
, the ECHO
command can be used to test whether a connection is active and healthy. It is often used for diagnostic purposes and helps developers to identify and solve connection problems.
redis> ECHO “hello world!”
“hello world!”
The SET
command assigns a specific value to a key. It accepts two arguments, the key
and its corresponding value
.
redis> SET username Salvatore_Sanfilippo
OK
Here, username
is the key and Salvatore_Sanfilippo
is the corresponding value.
If the save operation is successful, Redis returns OK
. In exceptional cases, e.g. if there is a lack of memory, a corresponding error message is returned.
redis> SET new_username Linus
OK
The SET
command also updates the value of a specific key. The old value is overridden by this operation.
redis> SET new_username Linus_Torvalds
OK
The SET
command also supports several options
that modify its behavior. The use of options
makes it possible to combine several operations in a single command. This is important to reduce the number of round-trips to the server.
redis> SET key value [options]
One commonly used option
is EX
, which sets the expiry for a key.
redis> SET name Gates EX 5
OK
The above example sets the value Gates
for the key name
which would expire within 5 seconds.
The GET
command retrieves the value associated with a specific key. The key
is passed as an argument, and the corresponding value
is returned.
redis> GET username
“Salvatore_Sanfilippo”
This retrieves the value stored under the key username
, which is "Salvatore_Sanfilippo"
.
The GET
command can also be used to check the existence of a key
. If a non-existent key
is passed as an argument, Redis will return a special value, nil
.
redis> GET non_existing_key
(nil)
The DEL
command deletes one or more key-value pairs from the data store, permanently. Once deleted, the key-value pairs cannot be restored later.
The keys
are passed as arguments to the command.
redis> DEL username new_username
(integer) 2
It returns an integer
value indicating the number of keys that have been deleted from the data store.
Redis ignores the keys
that are not present in its data store.
redis> DEL username new_username name
(integer) 2
In the above example, Redis returns a response of 2. The DEL
command has ignored the third key - name
- which does not exist.
The KEYS
command retrieves all keys that match a certain pattern.
redis> KEYS *
1) username
2) new_username
The pattern *
is specified as an argument and returns all keys that are present in the data store.
redis> KEYS h?llo
1) hello
2) hpllo
3) hxllo
In the above example, the pattern ?
matches any character that is to be used in its place.
So h?llo
matches hello
, hpllo
and hxllo
.
There are several such patterns which can be used to search for the keys in Redis.
The EXPIRE
command is used to set a timeout for a key in seconds.
redis> EXPIRE username 10
(integer) 1
It is important for scenarios in which keys are to be deleted automatically after a certain time. After the specified time, the key-value pair is automatically removed from the Redis data store.
It takes the key
as an argument together with the time
in seconds.
redis> EXPIRE username 5
(integer) 1
The above example sets the timeout for the key - username
to 5 seconds
. After this timeout, username
is automatically deleted along with its value.
It returns the integer value 0
if the timeout has not been set. Otherwise, it returns the value 1
.
The TTL
command checks the remaining lifetime of a key
. The key
must have an expiry time attached to it.
This command only accepts the key
as the argument.
redis> TTL username0 8
redis> TTL username1
-1
redis> TTL username3
-2
The TTL
command returns the remaining time until the key expires.
For keys that exist but have no expiry time, -1
is returned.
For keys that do not exist or have expired, -2
is returned.
You have now mastered 8 basic Redis commands with which you can seamlessly perform the most important tasks in a key-value store.
Here's a quick overview of the commands you've learnt so far:
PING
: Checks the connection status of a Redis server.ECHO
: Accepts an argument and returns it.SET
: Assigns a value to a key.GET
: Retrieves the value associated with a key.DEL
: Permanently deletes one or more key-value pairs.KEYS
: Retrieves all keys that match a specific pattern.EXPIRE
: Defines a timeout for a key in seconds.TTL
: Checks the remaining lifetime of a key.These commands provide essential functions for storing, retrieving and managing data in Redis and facilitate efficient data operations and system maintenance.
Links for further reading: