Redis: MongoDB Caching with Redis

Harshal Jethwa
3 min readMay 16, 2024

--

In this comprehensive guide, we’ll walk through the step-by-step process of setting up Redis on Kubernetes and leveraging MongoDB caching with Redis.

In continue to below :

Part 1: https://medium.com/@harshaljethwa19/redis-deploying-redis-on-kubernetes-building-chat-applications-with-redis-pub-sub-on-kubernetes-f81a56ec0273

Part 2: https://medium.com/@harshaljethwa19/redis-master-and-slave-architecture-with-kubernetes-b46c360d16c2

Part 3: MongoDB Caching with Redis

We will explore the integration of MongoDB caching with Redis to optimize data access in real-time applications.

We walk through an example of caching MongoDB query results in Redis, reducing database load and improving application performance. With a Flask application serving as our frontend, we demonstrate how Redis caching enhances data retrieval efficiency and scalability.

Setup 1: Install MongoDB in Ubuntu/Linux/Windows:

Install the MongoDB from below :
https://www.mongodb.com/docs/manual/installation/

Install MongoDB :
https://www.mongodb.com/try/download/shell

Setup 2: Create Database cluster, Collection :

Setup 3: Create Python Flask file

Save below file as Mongo.py and execute it.

import redis
import json
from pymongo import MongoClient
from flask import Flask, jsonify

app = Flask(__name__)
redis_client = redis.StrictRedis(host='IP_ADDR1', port=6379, decode_responses=True)
mongo_client = MongoClient('mongodb+srv://admin:admin@redis.7zqfwsd.mongodb.net/?appName=mongosh+2.2.5')
db = mongo_client['redis-user']
collection = db['Users']

@app.route('/data')
def get_data():
key = 'cached_data'
cached_data = redis_client.get(key)
#cached_data = 0
if cached_data:
return jsonify(cached_data)
else:
#data = list(collection.find())
data = list(collection.find({},{'_id':0, 'name':1, 'age':1}))
if data:
redis_client.set(key, json.dumps(data), ex=3600)
return jsonify(data)
else:
return 'Data not found', 404

if __name__ == '__main__':
app.run(debug=True, host='IP_ADDR2')

Note: Replace the IP_ADDR1 with your master pod IP address.
You can get the master pod IP_ADDR with this command

kubectl describe pod MASTER_POD_NAME

And For IP_ADDR2 replace it with our server IP Address

pip install redis pymongo flask redis
python3 mongo.py

Part 1: https://medium.com/@harshaljethwa19/redis-deploying-redis-on-kubernetes-building-chat-applications-with-redis-pub-sub-on-kubernetes-f81a56ec0273

Part 2: https://medium.com/@harshaljethwa19/redis-master-and-slave-architecture-with-kubernetes-b46c360d16c2

In conclusion, Redis, Kubernetes, and MongoDB offer a powerful trio of technologies for building and scaling real-time applications. By following the steps outlined in this guide, you’ll gain hands-on experience with deploying Redis on Kubernetes, implementing real-time messaging with Redis Pub/Sub, setting up master-slave replication, and leveraging Redis caching for MongoDB. Armed with this knowledge, you’ll be well-equipped to tackle the challenges of modern application development and deliver robust, scalable solutions.

Follow me :

Linkedin: https://www.linkedin.com/in/harshaljethwa/

GitHub: https://github.com/HARSHALJETHWA19/

Twitter: https://twitter.com/harshaljethwaa

Thank You!!!

--

--

Harshal Jethwa

DevOps | Docker | Linux | Jenkins | AWS | Git | Terraform | Technical Blogger