
[ad_1]
Guidelines for spinning up a Redis master-slave cluster and implementing a distributed caching-enabled Spring Boot app on a Kubernetes cluster.

hello dude,
I am working on a performance optimization project right now. The purpose of the project is quite unrealistic as we are trying to achieve <= 1second response times, and we are working with over 1 billion data sets. When I got the project requirements the first thing that came to my mind was 'why not caching?' Certainly caching should play an important role in this requirement.
So when we start the project we start some PoC to build the ecosystem, but the challenge is I don’t see a comprehensive guideline in this. kubernetes + redis + springboot Anywhere on the Internet. So I decided it was worth sharing my implementation steps with the dev community.
Note:The scope of this article is to provide comprehensive guidelines to spin up a Redis master-slave cluster on the Kubernetes cluster and implement a distributed caching-enabled Sprinboot app.A comprehensive introduction to Kubernetes/Redis/Spring boot is out of the scope of this article.
Condition
- Kubernetes cluster up and running
- Node.js v16.15.0 or latest on your local
- Your favorite IDE or text editor on your local
- Java 8 or latest version on your local
Deploying Redis Cluster on Kubernetes
The following steps explain how to set up a Redis master-slave cluster on Kubernetes. I highly recommend some study on K8S Storage Classes / Persistent Volumes / ConfigMap objects before deploying to production. If you need to get comprehensive knowledge about following steps please read it Tutorial.
- create namespace
Run the flow command on your K8S cluster to create the namespace, this will allow you to more effectively manage your objects on the K8S cluster.
kubectl create ns redis
2. Define the Storage Class
Now we will create a storage class which will be applicable to the whole cluster. To create a storage class, run the flowing command on your K8S cluster. storage-class.yaml
Configuration is included.
kubectl apply -f storage-class.yaml
3. Create Consistent Volume
In this solution, we create 3 nodes on redis cluster, so we need 3 persistent volumes. To create a persistent volume, run the command flowing on your K8S cluster. persistent-volume.yaml
Configuration is included.
kubectl apply -f persistent-volume.yaml
4. Create Config Map
You can get the configuration of ConfigMap appears here, please make sure to change it masterauth
And requirepass
value. Those two variables are the passwords of the redis cluster master and slave nodes. It will be easier to maintain if you use the same value for both.
kubectl apply -n redis -f redis-config.yaml
5. Deploy Redis Using StatefulSet
StatefulSet manages pods whenever there is a need to control master-slave behavior. To create a persistent volume, run the command flowing on your K8S cluster. persistent-volume.yaml
Configuration is included.
kubectl apply -n redis -f redis-statefulset.yaml
6. Create a Load Balancer Service
In the final step of the Kubernetes deployment, we are going to expose the Redis server through the public IP. To do this, we deploy the LoadBalancer service.
kubectl apply -n redis -f redis-lb.yaml
6.1 Check External IP
Once you have deployed the load balancer, after a few minutes the cluster will assign a public external IP. To check that run the following command in a few minutes.
kubectl get service -n redis
access redis cluster from local
For this step, you need to have Node.js v16.15.0 or latest installed on your local machine. This step is optional, but to check the logs/verify connectivity, it is better that you have a way to access the remote redis cluster redis-cli
, For this, you don’t need to install Redis server on your local machine.
- install redis-cli
npm install -g redis-cli
2. Access to Redis Cluster
Once npm is successfully installed, you can run any redis-cli
Supported commands on terminal after following command.
rdcli -h {host} -a {password} -p {port}
3.redis-cli . Monitor Redis cluster using
I will share some useful commands for basic level maintenance of redis cluster. You can flush all the keys and check the values stored on the cluster using the following command.
3.1 Verify stored values in Redis Cluster
rdcli -h {ip} -a {password} -p {port}xxx.xxx.xxx.xxx:xx> KEYS *
3.2 Flush the cache on the redis cluster
rdcli -h {ip} -a {password} -p {port} FLUSHALL
Springboot application implementation
Now we are going to implement SpringBoot application with distributed caching capabilities. This app has a GET service return string value, but controls the response time using Thread.sleep
, Let’s see how the distributed caching solution helps us overcome this slowness.
- Add the following dependencies to the pom
2. Redis Configuration
To access Redis remote server we need to add some properties in properties file and implement RedisConfiguration class on root package.
3. Build RESTful Web API
Up to this point, we have discussed the configuration related to the remote Redis cluster. Now we are going to implement a specific Spring Boot Rest API and the only change is that we provide some annotations in main class and service classes to configure cache enablement.
curl -X GET \
http://localhost:8080/booking
Once you have completed the implementation of Spring Boot Rest Service you can spin up the server and try the above GET method. The initial call should take >5 seconds because Thread sleep
, But when you try second time the response should take < 1 second because the response process from redis cache server instead of killing the service layer.
Also, note that we set the cache time to the live property application.properties
file So when you execute the GET request again in 10 seconds you will see that the request is again being processed by the service layer and it takes 5 seconds.
I hope this guide will save your time.
Happy coding…
[ad_2]
Source link
#Comprehensive #Guide #Distributed #Caching #Denuvan #Himanga #Hettiarachchi