We should all have had this question,
why not just use cache everywhere if cache is faster than a database?
I want to quantify and show the difference to you today.
You are thinking, ok this read is going to be a waste of time, I am just gonna say Cache is volatile and move on to doomscroll, or vibe code or watch the latest Ai slop VC drama. Lets fix that, you have a non volatile cache and you dont have to worry about the money to make it non volatile. Can you still use Redis as your DB ?
Volatility means the cache lives on RAM, when you switch it off, all the data is gone.
Whereas DB lives on the disk, and it will survive restart.
We can make it volatile by doing snapshot of the data to a disk, or replicating data across servers.
So now that you have decided to think, can you actually make Redis your DB, given it is not volatile anymore?
Problem 1: Lets talk about the easy reasoning here - COST
Lets assume you own a bank, and it has the following things:
- 10 million total users
- 1 million Daily active users
- each active user does 20 reads/ day and 2 writes/ day
DynamoDBs Pricing:
$0.125 per million reads
$0.625 per million writes
$0.25 per GB month for storage
With 1 million active users you are doing:
- 20 million reads per day
- 2 million writes per day
So that will cost you:
- $1.25 per day for reads
- $1.25 per day for writes
- 50 GB storage about $0.42 per day
Total:
- $1065 per year
Now lets compare to what that will cost us if we use Cache which is REDIS
Redis does not charge us per read the way DynamoDB does
Instead we pay for the cluster capacity to exist all the time.
So for our situation we need 2 shards, 1 replica per shard and 4 total nodes
That will Cost:
$1.729 per hour
$41.50 per day
$15,147 per year
So we are paying ~15x more for REDIS.
But you know what, lets say money aint a problem, can you still use Redis as your DB ?
Now comes - Eviction pressure
What does it mean ?
When Redis runs out of memory, it must remove some data to make space
Redis commonly uses LRU - Least Recently Used
Which means : delete the data that hasnt been used recently
Example : User A logs in every day, then it will stay in Redis
User B hasnt logged in for a while, it gets removed from memory
So now if Redis was your source of truth, user's account details or balance could vanish.
Ok then lets disable eviction and scale infinitely to have all the RAM.
Well yea then you can do it, by spending ton of money just because you went in a rabbit hole of experimenting with the wrong tool for the job.
But you know what, rabbit holes like this are totally worth the millions of dollars.
- Hari.
Top comments (0)