Jun 29, 2019 There are two ways to start Redis server. Either you can start Redis as a service so that it will keep running in the background with the following command: brew services start redis Or, if you don't want a background service you can just run below command. Follow my occasional rants on twitter or facebook.From time to time I will also post videos on my YouTube channel. Geeks should know that my public work is on Github!
Redis is a very popular data store used across all kinds of web apps it’s supported natively by Mac and Linux operating systems. Heck, you can even use Docker to run Redis.
The purpose of this guide is to setup Redis so we can use in our Python applications including Django, Flask, Fastapi, Celery, and pure python apps.
Are you a windows user? Check out this redis install guide
Do you need to use Redis on Docker? The windows install guide shows you how.
3.1. Install Redis on macOS¶
3.1.1. Install Homebrew¶
3.1.2. Install redis¶
Every once and a while run brewupgraderedis
3.1.3. Start / Stop Redis¶
Starting redis this way turns redis into a background service. You can easily stop redis with:
3.1.4. Verify redis is running:¶
What result do you see?
PONG
– great, redis is working and ready.CouldnotconnecttoRedisat127.0.0.1:6379:Connectionrefused
– this means that: (1) you did not install redis correctly or (2) redis is not running.
3.2. Install Redis on Linux (using Ubuntu)¶
Below was adapted from the production-ready guide in our series Hello Linux
3.2.1. Update System¶
3.2.2. Install redis-server
¶
3.2.3. Update configuration¶
Update supervised
to the following:
Save and exit nano.
Restart running service:
3.2.4. Verify installation:¶
What result do you see?
PONG
– great, redis is working and ready.CouldnotconnecttoRedisat127.0.0.1:6379:Connectionrefused
– this means that: (1) you did not install redis correctly, (2) redis is not running, or (3) the system service did not start (check withsudosystemctlstatusredis
)
3.3. Install on Windows 10 using Docker¶
Naturally, you need to install docker desktop on your windows 10. There’s a huge caveat that might push you towards using Memurai:
Docker requires you to have Windows 10 professional
Windows 10 professional allows for the OS virtualization that is just not provided in Windows 10 home.
Are you new to docker? Check out this basic intro
3.3.1. Verify Docker¶
Open PowerShell and run:
3.3.2. Create a project directory¶
3.3.3. Update (or create) a Dockerfile
¶
You don’t have to build the redis image yourself but I prefer to so I can make changes easily and it’s already prepared for it.
3.3.4. Build our docker image:¶
Personally, I like adding commands like dockerbuild-t'cfe-redis'.
to a build.ps1
file so I can just call ./build.ps1
if I ever want to build it again in the future.
3.3.5. Run our docker image:¶
A couple things to note:
-it--rm
is so this docker container only runs when we want it to and the container will be removed after we’re done running it.
-p6379:6379
is exposing the port 6379
on our local system AND in our docker container.
cfe-redis
is merely the tag name of the image we created in the build process.
Just like with build.ps1
, make a run.ps1
file containing dockerrun-it--rm-p6379:6379'cfe-redis'
as a shortcut to running this container.
3.3.6. Verify Docker connection¶
On Linux and Mac, you can just run redis-cliping
but, since we’re using docker, we don’t have the redis-cli
command available on our system.
We need another way to test. Since I use redis
in my Python projects, I’ll create a simple python program to ensure our non-container items can connect to redis
.
I already have Python 3.8 installed. If you don’t, check out this guide.
3.3.6.1. Create virtual environment¶
You can use pipenv
as well. I’m just keeping things simple by using the built-in virtual environment manager
3.3.6.2. Activate virtual environment¶
3.3.6.3. Run pipinstallredis
¶
This command does not install the redis server; it just installs a redis connector that we can use in python.
3.3.6.4. Create ping_redis.py
¶
Notice the port=6379
I used? This is the default redis port and it matches to what I exposed above.
3.3.6.5. Run ping_redis.py
¶
What result do you have?
Trueb'bar'
– this means redis is running correctlyNoconnectioncouldbemadebecausethetargetmachineactivelyrefusedit.
– this means your version of redis is either (1) not running or (2) running incorrectly or (3) you got the port number wrong.
3.3.7. Future running¶
Now, whenever you need to use redis
in your projects. Just run:
Anywhere on your system.
3.4. Install Redis on Windows using Memurai.com¶
I haven’t used memurai much myself but it seems promising. Let’s see
3.4.2. Run downloaded installer¶
3.4.3. Agree to all defaults except port.¶
For the port
, I’ll use 6380
.
I’m leaving 6379
open. It’s the default redis port and I’m not 100% convinced I’ll use memurai
in the future. I’m positive I’ll use Docker and so I’ll leave 6379
open for docker versions of redis like above.
3.4.4. Verify in Python – Create a virtual environment.¶
memurai
should be running by default after you installed it so we need to verify.
Assuming you did not do the docker portion above, let’s create a new virtual environment to test redis is running via memurai. Open up PowerShell
and run:
I already have Python 3.8 installed. If you don’t, check out this guide.
3.4.4.1. Activate virtual environment¶
3.4.4.2. Run pipinstallredis
¶
Mac Redis Cli
This command does not install the redis server; it just installs a redis connector that we can use in python.
3.4.4.3. Create ping_redis.py
¶
Notice the port=6380
I used? This is the redis port I used during the memurai
installation.
3.4.4.4. Run ping_redis.py
¶
What result do you have?
Trueb'bar'
– this means redis is running correctlyNoconnectioncouldbemadebecausethetargetmachineactivelyrefusedit.
– this means your version of redis is either (1) not running or (2) running incorrectly or (3) you got the port number wrong.
3.4.4.5. Future running¶
If you installed the defaults, memurai should start with your system startup just remember which port (6380
) you used for the memurai redis.
In this article, we will be creating a simple CRUD spring boot application and integrating it with Redis Cache using annotations.
Let’s consider a scenario, we are building a web application for an e-commerce platform. We have created API’s for showing products, offers, categories on the home page of our application. If we created these API’s without using any cache then every time a request will come, data will be fetched from the database. In that case, If our application is being used by millions of users at a time then we can face multiple issues like performance of website decreases, loading time increases, load on server as well as database etc. So in that case If we added caching for API’s, then only for first request data will be fetched from the database. Whenever other requests will come data will be directly served from Cache. In this, there will be no execution of query on database, data will be directly fetched from cache (in-memory store), application response time will be increased and we can give faster user experience.
Above is the one scenario we discussed, but there are multiple scenarios in which we can use caching for that.
Before going further in this article we will look at common terminologies like Redis and Jedis.
Redis
Redis, which stands for Remote Dictionary Server, is an open-source in-memory data store written in C. It’s often used as a database, cache, and may be used as a message broker.
Data is going to be stored in Redis within the simple key-value pairs where the key is used to extract values. Redis allows us to store different types of data structures like strings, lists, maps, sets, bitmaps, streams, spatial indexes.
Redis is usually used for cache management by microservices to reduce the number of database calls to the server.
Jedis
To define connections settings from application client to Redis server, we need Redis client which will be helpful to interact with Redis server; it’s something similar to JdbcTemplate to connect with MySql Server.
Prerequisites
Installing Redis
You can easily set up a Redis server on the machine using the following command
After successfully installing the redis-server package, let’s check the status of the Redis process to verify the installation:
The result shows the status of the redis-server, the bind-address and the port on which redis-server is listening on:
By default, the Redis configuration will be available in the /etc/redis/redis.conf file.
Enable Remote Connection to Redis
By default, Redis is accessible only from localhost. To enable remote connection to our Redis server, update the bind-address in Redis configuration to 0.0.0.0:
bind 0.0.0.0 ::1
Once updated, restart it:
Spring Boot Redis Project Setup
We can use spring-boot-starter-data-redis maven artifact provided by Spring boot to integrate Redis with the spring boot project. Below are the maven dependencies that we require for the integration.
We need to configure spring data redis in the application.properties file for hostname, port number
Also, use the @EnableCaching annotation on Spring Boot main class:
Defining the Entity
Create a simple User class that includes first name, last name, email and mobile. Added Lombok dependency. It is providing helpful annotations like @NoArgsConstructor, @AllArgsConstructor, and @Data instead of generating getters and setters in our code itself.
User.java
Define Controller
In that UserDto class is a simple model class that includes only getters. Always use a model class to accept the request body rather than an entity class.
UserController.java
UserDto.java
Define Service
We will create a custom mapper class for converting a source class object to a destination class object. Added ModelMapperdependency for that. You can use any other dependency for doing the conversion.
UserServiceImpl.java
Mapper.java
Define DAO layer
We will add caching in the DAO layer using @CachePut, @CacheEvict, @Cacheable, @Caching annotations.
The reason we are going to use the DAO layer for caching is that sometimes we need to do some operations on results in the service layer based on parameters passed or we can call the same methods from different locations. It’s better to maintain a separate DAO layer for caching purposes and others too.
Let’s see how to add caching on methods.
Case 1: Add and Get User
First, we will create two methods one is for adding users and the other is for getting users by id.
In the saveUser()method, We have added @CachePutannotation for updating or adding the particular entity in the cache, and @CacheEvictis used because If we have created a cache for users list then if the user is updated or deleted then we are going to evict or flush the cache with the given value to makes sure that it should also update in Redis Cache.
In the userById()method, we have used @Cacheableannotation with key as “userId”and value as “user”. So that when this method is called, a record of the user with userId will be stored in Redis cache.
So, Let’s see this by calling the API from Postman.
Before calling add-user API first open the terminal and run the below command to view whether the record is inserted in the Redis cache or not.
Call the add-user API from the postman with the user required fields.
After calling the API we will be able to see the logs below in redis-cli monitor. That means the record is successfully added to the Redis cache with given key and values.
You can see the “SET”operation with value and key as “user::1”. It means the user is stored in Redis cache.
Now we will call “users/{userId}” ex. “users/1”API to get the user details by Id.
In redis-cli logs, We can see the “GET”operation with key-value as “user::1”.
There is no “SET”operation performed. But when you explicitly run the redis-cli flushall command or cache expires then after calling the same API, the first “SET”operation is called, and then the “GET”operation is called.
Case 2: Users List with Pagination
Consider there are thousands of records in the database and you want to return the records based on the pagination. In that case, we will do caching for a list of records using the key as page numberand value as usersList.
By doing so we can achieve a faster response in terms of accessing records on the basis of pagination.
For this, we will create a method that will return a list of users.
In the above method, we have added @Cacheable annotation with value as “usersList”and key as “page”.
Now let’s call the users list API.
Brew Redis Cluster
Check the redis-cli monitor logs in the terminal.
Case 3: Delete user
If some data is to be deleted from the actual Database, there won’t be a point to keep it in cache anymore. We can clear cache data using @CacheEvict annotation.
In the deleteUser() method, we have added an array of @CacheEvict as we have maintained a users list array.
Let’s do this by calling delete user API.
We can see below logs in redis-cli as the user record is deleted for id.
Install Redis Mac
Key Points to Remember
- Define TTLs : Time-to-live (TTL), is the time span after which your Cache will be deleting an entry. If you want to fetch data only once a minute, just guard it with a @Cacheable Annotation and set the TTL to 1 minute.
- Implement Serializable: If you are adding an object in Redis cache then the object should implement a Serializable interface.
- Redis Cache Limits: When cache size reaches the memory limit, old data is removed to make a place for a new one. Although Redis is very fast, it still has no limits on storing any amount of data on a 64-bit system. It can only store 3GB of data on a 32-bit system.
- Never Call Cacheable Method from the same class: The reason is that Spring proxy the access to these methods to make the Cache Abstraction work. When you call it within the same class this Proxy mechanic is not kicking in. By this, you basically bypass your Cache and make it non-effective.
- Use Lettuce, If you need something highly scalable:Lettuce is a scalable thread-safe, non-blocking Redis client based on netty and Reactor. Jedis is easy to use and supports a vast number of Redis features, however, it is not thread-safe and needs connection pooling to work in a multi-threaded environment.
Conclusion
We learned a lot in this article. We installed the Redis server on a machine, we have created a spring boot application using annotations like @Cacheable, @Caching, @CachePut, and @CacheEvict.
In case you have some questions feel free to write to us 🙂
Comments are closed.