π§ What Is Caching? (Simple Definition)¶
Caching means storing data temporarily in a fast place (RAM) so that we donβt repeatedly go to a slow place (database).
Think of it like:
-
Instead of opening the fridge (slow)
-
You keep your favourite drink on your table (fast)
β‘ Why Caching Is Needed?¶
Without cache:
-
Every request β goes to DB
-
DB becomes slow
-
App becomes slow
-
High load = DB crashes
With cache:
-
Only first request hits DB
-
Next thousands of requests hit cache
-
System becomes super fast
π Understanding Your First Diagram β Cache Aside Pattern¶
This diagram shows one of the MOST COMMON caching strategies:
π Cache-Aside (Lazy Loading)
Let's break it down.

π’ 1. First Request (Cache MISS)¶
(Cache MISS = Not found in cache)
πΉ Step-by-step:¶
Step 1: Web server asks cache β "Do you have this data?"¶
Cache replies β β No
Step 2: Web server goes to the database¶
DB returns the actual data β βοΈ
Step 3: Web server stores that data into cache¶
Cache now saves the data in memory β β‘
Step 4: Web server returns the response to user¶
π‘ 2. Subsequent Requests (Cache HIT)¶
(Cache HIT = Found in cache)
Now the data is already stored in cache.
πΉ Step-by-step:¶
Step 1: Web server again asks cache¶
This time cache replies β βοΈ Yes I have it
Step 2: Cache instantly returns data¶
Only 1β5 milliseconds β β‘ SUPER FAST
No database is touched.
No extra load.
π Why This Pattern Is Called βLazy Loadingβ?¶
Because:
-
System does NOT store in cache by default
-
It waits until someone asks for the data the first time
Only when the first request comes β cache is filled.
π€ Understanding Your Second Diagram β Popular Profiles Cache¶
This is an example of using cache for hot data (frequently accessed data).

Example:¶
Think of Instagram:
-
Some users have millions of profile visits
-
Their profiles would overload DB
So what Instagram does:
π It caches βpopular profilesβ
π All requests for these profiles go directly to cache
π Flow in This Diagram¶
1οΈβ£ User requests a profile¶
Web server receives the request
2οΈβ£ Web server sends request to cache¶
If profile is popular β it already exists
3οΈβ£ Cache returns the profile¶
Super fast β no DB hit
4οΈβ£ Database is only used for NEW or LESS POPULAR users¶
Not for every request
π§° Why Redis Is Used for Cache?¶
Redis is perfect for this because:
β It stores data in RAM (insanely fast)
β Distributed (works across multiple servers)
β Supports TTL (auto-expire data)
β Good for session storage
β Good for rate limiting
β Good for real-time counters
β Most popular cache tool globally
ποΈ Full Cache Tutorial (Easy to Understand)¶
1οΈβ£ When do you use caching?¶
You use caching when:
-
Database queries are slow
-
Same data is requested frequently
-
External API calls are expensive
-
You have heavy traffic
Examples:
-
Profile info
-
Leaderboards
-
Product pages
-
Blockchain RPC data
-
Dashboard metrics
2οΈβ£ Where does Redis sit in your architecture?¶
3οΈβ£ Popular Cache Strategies (explained like a story)¶
π’ A. Cache Aside (Lazy Load) β Most used¶
Already explained using your diagram.
Use when:
-
Read-heavy apps
-
Data doesnβt change frequently
Example:
-
Profile info
-
Product details
π‘ B. Write Through Cache¶
Write goes to:
-
Cache
-
Database
At the same time.
Useful when you want cache + DB always in sync.
π C. Write Back (Write Behind)¶
Write goes only to cache β Redis writes to DB later.
Fastest writes
But can lose data if cache crashes.
4οΈβ£ Cache Expiry β TTL¶
Every cached key can expire automatically.
Example:
Meaning:
-
Store profile for 60 seconds
-
After that β auto delete
Helps avoid stale data.
5οΈβ£ Eviction Policies (When Cache Memory Is Full)¶
Redis will remove:
-
LRU β Least recently used
-
LFU β Least frequently used
-
TTL β Keys with expiry first
-
NOEVICTION β Return error if full
6οΈβ£ Cache Problems (Simple Explanation)¶
β Cache Miss¶
Data not found β Goes to DB.
β Cache Stampede¶
When cache expires β thousands of users hit DB at once.
Solution:
-
Staggered TTL
-
Cache locking
-
Background refresh
β Stale Data¶
Cache contains old information.
Solution:
-
Short TTL
-
Invalidate when updating
πͺ Real-World Example (Sui Network Monitoring)¶
When you track:
-
Epoch
-
Checkpoints
-
Transaction count
-
Validator count
-
Gas price
Instead of calling RPC every second:
π Store values in Redis for 5β10 seconds
π All dashboards read from Redis
π Backend becomes lightning fast
π RPC node is safe from heavy load