DEV Community

RyanAbdaulaziz
RyanAbdaulaziz

Posted on

Why the heck Redis ?

In a world where every single line of code can cost you bucks, you should be careful with what, why, and how you code.

Database queries sometimes can be expensive... Very Expensive, BUT, if you can't afford the expensive, buy the cheap one <;

What Is "In-Memory"?

Redis is an in-memory database, before I move on, what does it mean "In-memory" here ?
We have 2 types of storages, the disk, and the ram, the disk is where you have almost everything whenever you shutdown your laptop and power it up again you still find them and nothing disappeared, like the downloaded apps, your pictures with your family, the beach trip pictures when you were 4 years old and so forth.
The in-memory storage is a temporary disk, when you shutdown the machine for example it just. disappears, like the running apps for example, I'm running Google Chrome now to write this article here, if I shutdown my laptop, and power it up again, I have to open it again, it got deleted once I shutdown the laptop.

So,

in-memory = temporary disk
Enter fullscreen mode Exit fullscreen mode

Remember this.

What Is Redis Used For?

Back to Redis, So Redis is an in-memory database, so it's unlike Postgres and MySql, it's not meant to store users data forever or something, but it's meant for many things but most commonly "Caching".

Holly mama, hold on a second, you said when you can't afford buying the expensive, but the cheap one, what do you mean with caching??
I don't have cash at all, it's the middle of the m...

What Is Caching?

Wait wait, Caching here is not the cash in your pocket "IF EXISTS“,
but it means storing data in-memory, but what makes it different that it stores data in pairs, (Key, value) pairs;

key:value
user3322name:"Tasneem"
user_id:3322
user3322_note:"Bothering Ryan"
Enter fullscreen mode Exit fullscreen mode

Now, when I want to get any of these info, I can simply do this:

GET user3322name
output: Tasneem
Enter fullscreen mode Exit fullscreen mode

Wowww, that's so boring, what's interesting ?

Expiring Data With TTL

Redis can store them temporarily as well (:

By setting up a timer for the pair to disappear, because this is not meant to store data forever, so we want to cache the info temporarily, Redis storage is expensive, we don't have much space to work with.

So I can put a rule that any info stored in the cache can stand for only one minute.

By doing this:

SETEX user3322name 60 "Tasneem"
Enter fullscreen mode Exit fullscreen mode

This way, after one minute, this pair will disappear.

To believe me do this:

TTL user3322name
output: 53
TTL user3322name
output: 44
TTL user3322name
output: 20
...

TTL user3322name
nil
Enter fullscreen mode Exit fullscreen mode

This means that the key expired now.

Real-Life Example

After explaining a little what does it mean caching let me give you a real-life example:

Assume you have a query that loads all users notes in your notes manager app, you are a very creepy person and want to read users' notes.

Your database is running on a server in Russia and the query is so slow, every request to load the notes take you 5 seconds.

You decided to do something smart, why not caching them ? so you integrated Redis with your project and twisted your workflow to instead of checking the database first, it checks the Redis, and if it doesn't exist it checks the database.

Before:

Request -> api -> DB 
Enter fullscreen mode Exit fullscreen mode

Now:

Request -> api -> redis -> DB 
Enter fullscreen mode Exit fullscreen mode

Redis is INCREDIBLY fast, when you store the notes there, it could take you 0.18 ms to load the notes, not 5 seconds !!

Again, why ? Because the data is in here, in my memory, it's not in China or Texas, it's in my laptop, so that's why it's fast.

Not Everything Needs Caching

BUT, not everything needs to be cached, sometimes caching is non-sense when there is a problem in your database, always think of the tradeoffs, think why, how, and what before you order your slave "AI" to code it for you.

Learn More

I'm not here to teach you Redis, but I wanted to whet your appetite to learn it and add it to your toolkit.

Check out this video if you want to learn Redis in deep:

It's not my video, I'm not marketing to myself, but I just want to help you.

Happy learning journey!

Top comments (0)