Introduction
What is Redis?
Redis is a powerful in-memory data structure store often used as a database, cache, and message broker. In this guide, we'll explore the most common commands for each data type. We'll be running Redis in a docker container for local development.
Prerequisites
- Docker Desktop
Installing Redis
We'll be using docker for this tutorial for its ease of use with setting up. If you don't have it installed, and don't want to install it, then you'll need to install Redis on your machine. Instructions here
docker run -d --name redis-stack -p 6379:6379 -p 8001:8001 redis/redis-stack:latest
Your container should now be running and if you visit, http://localhost:8001, you should see Redis Insights, a great tool to visually see and interact with your Redis data. Feel free to check it out, but we won't be using it.
To play along, you'll want to go to Docker Desktop, then to your containers. Click on your redis container and go to the 'Exec' tab. Now type redis-cli and you are good to go.
String
What are they and when to use them?
Strings are the simplest type of value and can hold any kind of data. You can only store one value per key.
7 popular commands
-
SET: Use to set a key with a value in Redis.
SET mykey "value"
-
GET: Retrieve the value of a key from Redis.
GET mykey
-
MGET: Get the values of multiple keys in one command.
MGET key1 key2
-
SETEX: Same as set, but adds an expiration time.
SETEX key3 3600 "Expire out in 1 hour"
-
DEL: Delete one or more keys from Redis.
DEL key1 key2
-
INCR: Increment the integer value of a key by one.
INCR key5
-
DECR: Decrement the integer value of a key by one.
DECR key5
Lists
What are they and when to use them?
In Redis, lists are ordered collections of strings sorted by the order of insertion. They are essentially linked lists that can be accessed and manipulated from both ends, making them highly efficient for use cases that require frequent insertion and deletion operations.
Example use case: You need to manage some tasks that will be handled first in, first out. Let's use customer support tickets.
7 Popular Commands
-
LPUSH: Adds one or more values to the beginning of a list.
LPUSH support_queue "ticketuuid" "ticketuuid2"
-
RPOP: Removes and returns the last element of a list.
RPOP support_queue
-
LLEN: Returns the length of the list.
LLEN support_queue
-
LRANGE: Gets a range of elements from the list, specified by start and stop indexes.
LRANGE support_queue 0 2
-
LINDEX: Gets the element at a specified index from the list.
LINDEX support_queue 0
We don't use the two below, because we are using a Queue. If we were using a stack, we could do use these two.
-
RPUSH: Adds one or more values to the end of a list. We don't need this for our example though.
RPUSH mylist "value3" "value4"
-
LPOP: Removes and returns the first element of a list.
LPOP mylist
Sets
What are they and when to use them?
Sets in Redis are collections of unique strings, allowing for fast membership checks and set operations like intersections and unions. Use sets when you need to manage unique, unordered collections and perform efficient set operations.
Example use case: storing user interests. We want the interests to be unique, but we don't need them ordered, so a set is perfect for this.
7 popular commands
-
SADD: Add one or more members to a set.
SADD user:interests:uuid "hiking" "bowling" "gaming" SADD user:interests:uuid2 "jogging" "bowling" "walking"
-
SMEMBERS: Get all members of a set.
SMEMBERS user:interests:uuid
-
SISMEMBER: Check if a member exists in the set.
SISMEMBER user:interests:uuid "hiking"
-
SCARD: Get the number of members in a set.
SCARD user:interests:uuid
-
SINTER: Get the intersection of multiple sets. What do the sets have in common?
SINTER user:interests:uuid user:interests:uuid2
-
SREM: Remove one or more members from a set.
SREM user:interests:uuid "jogging"
-
SUNION: Get the union of multiple sets. Combine the sets.
SUNION user:interests:uuid user:interests:uuid2
Sorted Sets
What are they and when to use them?
Sorted sets in Redis store a collection of unique members, each associated with a score that allows them to be sorted. They are suitable for applications requiring ordered data with associated scores, such as leaderboards and range-based queries.
Example use case: We have a leaderboard where naturally we want to keep track of the user (member) and the score (well, score).
7 Popular Commands
-
ZADD: Add one or more members to a sorted set with their scores.
ZADD leaderboard 0 player1 0 player2 0 player3 0 player4
-
ZINCRBY: Increment the score of a member in a sorted set by a specified amount.
ZINCRBY leaderboard 3 player1
-
ZRANGE: Return a range of members from a sorted set by index. Adding withscores does exactly what you'd expect.
ZRANGE leaderboard 0 -1 withscores
-
ZREVRANGE: Return a range of members from a sorted set by index in reverse order.
ZREVRANGE leaderboard 0 -1
-
ZSCORE: Get the score of a member in a sorted set.
ZSCORE leaderboard "player1"
-
ZREM: Remove one or more members from a sorted set.
ZREM leaderboard "player2"
-
ZCARD: Get the number of members in a sorted set.
ZCARD leaderboard
Hashes
What are they and when to use them?
Hashes in Redis are maps between string fields and string values, perfect for storing objects. Use hashes when you need to store and retrieve multiple fields associated with a single key efficiently. They can be used for caching, session management and more, as they are very fast and efficient.
Example use case: Storing user profiles is a nice example of when to use hashes in Redis. It's a complex data structure with known fields.
7 Popular Commands
-
HSET: Set the field in the hash stored at key to value.
HSET user:uuid name "John" username "Johnboy" age "30"
-
HGET: Get the value associated with field in the hash stored at key.
HGET user:uuid name
-
HGETALL: Get all fields and values of the hash stored at key.
HGETALL user:uuid
-
HMGET: Get the values associated with the specified fields in the hash stored at key.
HMGET user:uuid name username
-
HDEL: Delete one or more fields from the hash stored at key.
HDEL user:uuid username
-
HEXISTS: Determine if field exists in the hash stored at key.
HEXISTS user:uuid username
-
HINCRBY: Increment the integer value of a hash field by the given number.
HINCRBY user:uuid age 1
Conclusion
Redis is widely favored among developers primarily for its caching capabilities, in which it excels. It can drastically reduce response times by a factor of 10 in certain positions. However, Redis offers much more versatility. With its AOF and RDB persistence methods, it can even serve as a robust primary database solution as well.
Redis is renowned for its lightning-fast performance and user-friendly interface, featuring distinct data types and a seamless API. If you haven't integrated Redis into your stack yet, find time.