Introduction to Redis
Learn what Redis is, when to use it, how to install it, how to connect to a Redis client, and basic Redis operations such as setting, retrieving, and deleting keys.
Table of Contents 📖
- What is Redis?
- When to use Redis
- Installing Redis
- Starting Redis
- Connecting a Redis Client
- Setting and Getting Redis Values
- Checking Redis Key Exists
- Deleting a Redis Key
- Checking Type of Redis Value
- Setting Redis Key Expiration
- Summary
What is Redis?
Remote dictionary server, or Redis, is a NoSQL data store that can be used as a cache, database, and more. Redis is fast because it uses memory as its main storage, writing to disk only for persistence. Redis also supports rich data types when compared to typical key-value data stores, making it applicable to a variety of use cases.
When to use Redis
As Redis is memory oriented, it is great for working real-time data that is frequently updated. For example, Redis would be a great choice for displaying the latest users posts on a website that is frequently receiving new user posts. This is not only because Redis uses memory as its main storage, but also because it provides some excellent data structures for such a use case.
Installing Redis
To install Redis, follow the instructions for the specific operating system by following the link below. However, it is reccommended to install Redis from source (explained in the link).
https://redis.io/docs/getting-started/
Starting Redis
Once Redis is installed, it can be started by running the following command.
redis-server
Once Redis is running, it can be stopped by entering Ctrl-C into the console.
Connecting a Redis Client
Now, open up a second terminal window and run the following command.
redis-cli
redis-cli (Redis command line interface) is a terminal program used to interact with the Redis server. Running this command should open up the following Redis prompt.
127.0.0.1:6379>
By default, external programs speak with Redis using a TCP socket on port 6379. Now, type "PING" into the command prompt and look for the server response.
127.0.0.1:6379> PING
PONG
Setting and Getting Redis Values
Now, before we go any further, lets talk about how to actually store/retrieve values in Redis. Values are stored/retrieved in Redis using GET/SET.
SET myKey myValue
GET myKey
"myValue"
It should be noted that the SET command replaces any existing value, even if the value is of a different data type.
SET myKey myNewValue
GET myKey
"myNewValue"
Also, there are options/configurations that can be supplied to the SET and GET commands of which we will explore throughout this series. For example, we can set/retrieve multiple keys with MSET/MGET.
MSET key1 value1 key2 value2 key3 value3
MGET key1 key2 key3
1) "value1"
2) "value2"
3) "value3"
Checking Redis Key Exists
We can check for the existence of a key in Redis using the EXISTS command which returns a 0 or 1 to signal if a given key exists in the database.
EXISTS key1
(integer) 1
EXISTS key4
(integer) 0
Deleting a Redis Key
We can remove a key from Redis using the DEL command which returns 0 (key doesn't exist) or 1 (it existed and was deleted).
DEL key1
(integer) 1
DEL key1
(integer) 0
Checking Type of Redis Value
Finally, as Redis can store values of different data types, we can check the type of the stored value using the TYPE command.
TYPE key2
string
Setting Redis Key Expiration
We can also create a time to live, or expiration, for a Redis key. When the TTL elapses the key is destroyed. To set a key's expiration, we use the EXPIRE command.
SET myExpireKey myExpireValue
EXPIRE myExpireKey 3
-- Before 3 seconds
GET myExpireKey
"myExpireValue"
-- After 3 seconds
GET myExpireKey
(nil)
We can check the time a key has remaining using the TTL command.
SET myExpireKey myExpireValue
EXPIRE myExpireKey 3
-- 1 second has passed
TTL myExpireKey
(integer) 2
Summary
Redis is great for building high performance applications due to its in memory storage and variety of data structures. If this article was helpful, please consider donating at the top of the page, sharing this article, and subscribing to my YouTube channel WittCode.