Getting started with Redis: It’s easier than you think
‘Redis’, which stands for Remote Dictionary Server. According to Redis official, Redis is an open-source (BSD licensed), in-memory data structure store, used as a database, cache, and message broker. It is a data store that offers performance benefits over traditional databases.
In this quick post, we’ll break down what Redis is and how it works. More importantly, we’ll also see what Redis is not so that you can make good decisions about how to use it. Along the way, we’ll also introduce some example code so you can get a feel for how it works.
Redis Architecture
Redis has two key features that make it an ideal solution for some software challenges:
Of course, the fact that Redis operates in-memory has consequences for data persistence. So, Redis does have the ability to save a current snapshot of data to disk as well.
What Redis architecture means practically
These architecture decisions behind Redis lead to interesting consequences:
Pros:
Cons:
Recommended by LinkedIn
What can I use Redis for?
Redis has a host of potential applications for scenarios where you need quick access to data:
Command to start Redis stack
This will pull the image if not available locally on the system and run the container :
docker run -d --name redis-stack -p 6379:6379 -p 8001:8001 redis/redis-stack:latest
The redis server is now running on your localhost at port 6379 and you can access it through redis stack insight which is a GUI for same on port 8001, some thing like this :
Let's start with Redis sets :
It can store key value pairs where these values are actually sets. So you can add members into it and the key can be any string you want. You can also specify a TTL value that is time to live which actually defines the time for which this set would be available in seconds. After that, it would be removed. You can choose to not fill TTL if you want it to be persistent in redis.
For the above example the key is "Friends" and the value is the set of all names {Ajinkya,Abhay, .....}
Python code for operation on Redis sets is as below :
import redis
# Create a Redis client
redis_client = redis.Redis(host='localhost', port=6379, db=0)
# Add elements {'banana', 'apple', 'orange'} to the set 'myset'
redis_client.sadd('myset', 'apple', 'banana', 'orange')
# Check if an element is a member of a set
print(redis_client.sismember('myset', 'apple')) # True
print(redis_client.sismember('myset', 'grape')) # False
# Get all elements of a set
print(redis_client.smembers('myset')) # {'banana', 'apple', 'orange'}
# Remove an element from a set
redis_client.srem('myset', 'orange')
# Get the number of elements in a set
print(redis_client.scard('myset')) # 2
# Get the union of two sets
redis_client.sadd('myset2', 'apple', 'pear', 'kiwi')
print(redis_client.sunion('myset', 'myset2')) # {'banana', 'apple', 'pear', 'kiwi'}
# Get the intersection of two sets
print(redis_client.sinter('myset', 'myset2')) # {'apple'}
# Get the difference between two sets
print(redis_client.sdiff('myset', 'myset2')) # {'banana'}
# Remove all elements from a set
redis_client.delete('myset')
redis_client.delete('myset2')
In the upcoming edits of this article we would be discussing about the Sorted Sets, String, Json, Pub/Sub queues etc.
If you loved reading this arcticle, please upvote it, it keeps me motivated to continue writing such articles.