Redis : Read Through Cache

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:

  1. The Application (fastapi) checks whether the key is present in redis.

  2. If the key is existing in redis, then it will return the value.

  3. If the key is not present then redis will communicate to primary database (here mongodb) to get the value.

  4. Then the result will be set into the redis.

  5. And then it will be returned to the application.

Implementation

Github: https://github.com/syedjaferk/redis-cache

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

  1. 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.

  2. What happens if the record been updated in the database, but not in redis. Do we need to write on every update ?

  3. 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).

  4. All drawbacks of cache aside also follows here.

When to use ?

  1. If your application has a read heavy workloads.

  2. If you not have a highly dynamic data.

Did you find this article valuable?

Support Syed Jafer K by becoming a sponsor. Any amount is appreciated!