Redis : Read Through Cache
Its an Application level caching, where the application reads the data from cache. If the data not present in the cache it will read from database and updates the cache and sends the result to the application.
Steps:
The Application (fastapi) checks whether the key is present in redis.
If the key is existing in redis, then it will return the value.
If the key is not present then redis will communicate to primary database (here mongodb) to get the value.
Then the result will be set into the redis.
And then it will be returned to the application.
Implementation
We are going to create a mock todo application. Let's spin redis and mongo db using dockers,
For Redis,
docker run -p 6379:6379 --name redis-container --rm redislabs/redismod:latest
For mongodb,
docker run --name mongo_container --rm mongo
To load the data into mongo, either you can use the below script, or restore from the dump file.
or
docker exec -i mongo_container sh -c 'mongorestore --archive' < 2l_data.dump
In the redis docker image used, we have the support for redis-gears. So we have to write a python script or a module to be triggered when some commands hit.
For eg: If GET
is been hit then it will call a particular python function.
As of now there is a support for python and java modules. https://oss.redis.com/redisgears/configuration.html#plugin
Below is the python script (read_script.py) to be triggered when JSON.GET hits,
Now the script is ready so we need to send the script in RG.PYEXECUTE
(https://oss.redis.com/redisgears/commands.html#rgpyexecute) to the redis-server. We can acheive this using node.
Below is the node script (script.js) to send to redis-cli,
node script.js
Now let's design the app,
The flow will be like this,
Drawbacks
For initial every request there is a cache miss. If the requested items aren't repeating in a good percentage, then this will create latency.
What happens if the record been updated in the database, but not in redis. Do we need to write on every update ?
In a distributed system, if a node fails, it will be replaced by a new empty node. This will increase the latency. (this can be overcomed with replication of the data).
All drawbacks of cache aside also follows here.
When to use ?
If your application has a read heavy workloads.
If you not have a highly dynamic data.
Subscribe to my newsletter
Read articles from Syed Jafer K directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by