Redis System Design

Vipul Vyas
4 min readMay 16, 2021

Hello guys, Today I am talk about Redis. but for that we need to understand about caching. I know that you know caching that’s why you are learning redis. I will talk about how redis system design so you can understand how it is working.

There are many use case of redis but main thing is caching. so, let’s talk about caching.

What is best practice for caching ?

Validity — means data is in cache is right or not.

Hit VS Miss in cache is if users requested data is in cache then it get hit if not in cache then request take time and need to fetch data from database .

High hit rate — store high hit rate in caching so get more hit on request.

cache miss — when ever data is not in cache it set cache miss and fetch from database.

TTL — Time to live (TTL) is the time that an object is stored in a caching system before it’s deleted or refreshed.

What you need for your system means feature ?

Storage — you need mere and more storage in cache so you can store more data in caching.

Query per Second — means need run more number( around 50k to 1M ) of query in second.

Low Latency — need low latency as much as possible.

Least Recent Used ( LRU )— Least Recent Used items first.

100% Availability — High Availability.

Scalability —High Scalable Systems

Cache Access Pattern

Write Through — in this method first write in Cache then write to Database after getting Acknowledgement(AKN) from Database give response to user.

C =cache , DB = Database

write through

Write Around — first write into database and then into cache.

write around

Write Back — first write to cache then give response to user and then add or update data into database. in this case service and cache connected with synchronous and database and cache is connected with asynchronous.

write back

We can implement based on our requirement.

Let’s talk about Redis now…

Redis is implemented based on hash table. Let see small introduction of hash table.

Hash Table : hash table means create any hash or divide our data into some division or some part so we can find it easily not to search in all data.

like here i created hash table by dividing by 10 to value.

And if 2 or more key are added into 1 block suppose forth key which value is 93 are added then it is also stored in third block. in this case what we can do ?

We can do like create linked list and add to it. good we solve this problem but Think about worst case like value of all key’s are divide by 10 is 3 in this case all key are in block 3. okay so what is the problem ? problem is that search time for key is O(n) if there is n key. what we need we need O(1) time. also there is second problem if all block are full then which key we should remove and which we should not for that we can use circular or doubly linked list.

Redis is using doubling linked list with address. we are storing the address of the node in hash table and using doubly linked list we can decide which key are access last and which one first. our scenario recently access key is third key so whenever new key come and suppose there is no space then we can use LRU (least recent use) we can remove first key and add it to recent use and add before third key. And if same key come like second key come again so add second key to before third key.

if second key come again
if there on space in block then remove start node and add new node at the end

If second key come again then not change the address of the node because we are not changing it’s value and for remove node in doubly linked list time complexity is O(1) because we are removing from last and adding first so it’s time complexity is O(1).

And Last thing redis is same as node.js means it is using event loop with event queue and also it has same environment as node.js . you can refer https://www.simplilearn.com/understanding-node-js-architecture-article

This is how redis is working. now next step is to learn syntax and then advance redis topic like Redis: Persistence, Redis: Replication, Redis: Partitioning, Redis: Eviction Policies, Redis: Client side caching and then clustering.

--

--