What is Redis?
Redis (Remote Dictionary Server) is an open-source, in-memory key-value data store that can be used as a database, cache, and message broker. Redis is often referred to as a NoSQL database because it does not use the traditional SQL relational model but instead uses a key-value storage model.
Why is Redis widely used?
One of the main advantages of Redis is its high performance and scalability, making it an excellent choice for applications that require fast read and write operations. Redis supports a wide range of data structures, including strings, hashes, lists, sets, and sorted sets, which can be used to store and retrieve complex data structures. It also supports transactions and pub/sub messaging, allowing it to be used for a variety of use cases such as real-time analytics, leaderboards, and chat applications.
Building our Voting Application
Before we get started,
Note: This tutorial requires basic knowledge of python;
To use Redis with Python, the recommended library is redis-py
, Which can be installed by running:
pip install redis
After installing our package, Let's build our application;
# importing our package
import redis
# Connect to the Redis database
r = redis.Redis(host='localhost', port=6379, db=0)
# casting a vote
def cast_vote(item_id):
r.incr(item_id)
#retrievng the total votes for each item
def get_votes():
items = ['item1', 'item2', 'item3']
return {item: int(r.get(item)) for item in items}
# Cast some votes by calling the function
cast_vote('item1')
cast_vote('item1')
cast_vote('item2')
# Get the total votes for each item
print(get_votes())
r = redis.Redis(host='
localhost
', port=6379, db=0)
- creates a Redis client object in Python.
The client is instantiated using the Redis class from the redis library and is connected to a Redis server running on localhost
running on port 6379
, using database number 0
. The object is assigned to the variable r
so that we can use it to interact with the Redis server by sending commands to it.
The code uses the incr
command from Redis to implement a voting system where the incr
command is used to increment a key, which in this case represents the vote count for a specific item. The vote count for each item is stored in a Redis key-value store, with the item id as the key and the vote count as the value.
The function get_votes
retrieves the vote count for each item by using the get
command to retrieve the values for each item id key.
Output would be a dictionary that shows the number of votes received by each item:
{'item1': 2, 'item2': 1, 'item3': 0}
This indicates that "item1" has received 2 votes and "item2" has received 1 vote, while "item3" has received 0 votes.
Conclusion
Redis is widely used by many companies, including Twitter, GitHub, and Snapchat, due to its ease of use, flexibility, and performance.
It has a simple, easy-to-use API and supports several programming languages, including Python, Ruby, and Java and can be easily scaled horizontally by adding additional nodes to a Redis cluster. This makes it a good choice for applications that need to handle large amounts of data or handle a large number of concurrent users.