Day 7๏ธ : Speed Optimization & The Power of Caching!
Welcome Back, Performance Hackers!
At this point, your app is growing, scaling, and handling more traffic than ever. ๐ Everything seems perfectโฆ until it isnโt.
One day, your founder storms into your workspace:
โThe website is slow! Customers are complaining! Do something!โ ๐ก
As a developer, you:
โ
Check CDN โ Everything looks fine.
โ
Load the homepage โ Speed seems okay.
โ
Open product pages โ BOOM! ๐ฅ It takes forever to load! why??
Time to investigate! ๐
๐ The Root Cause: Slow API Calls
Using network logs, you discover that:
- Fetching images? Fast (served from CDN).
- Fetching product details? SLOW! (served from the database).
The issue? Product details are coming directly from the server every time! As user comes on the product page, browser makes the api request but the query is really slow, that is why its taking a lot of time to populate the products data.
Question? How to reduce the latency?
Solution? Bring data closer to the user!
โญFundamental approach to reducing a latency in computer science is getting data that a system needs close to it.
๐ Speeding Up Data Fetching with Caching
1๏ธโฃ Frontend (Browser) Caching
Instead of fetching product data every time a user visits a page, we:
โ
Pre-fetch data when they visit the homepage.
โ
Store data in the browser (localStorage/sessionStorage).
๐ Result? The product page loads instantly! cause the request is already resolved. We already have the data to show on product page.
This concept of taking a temporary copy of the data and storing somewhere for low latency is called caching.
Here we did browser caching, we have more better option โฆ
2๏ธโฃ Server-Side Caching (Faster API Calls)
The goal is still same, having data to show on ui as fast as possible. So just like we stored data on browser, we can store on server as well.
Keep frequently accessed data as close to the request as possible.
๐น Every time a user requests product data, instead of querying the database, we store it in server memory for a short time.
๐น The next time a user requests it, we serve it instantly from memory.
๐ ๏ธ Tools for Server-Side Caching
- Redis (Best for key-value caching)
- Memcached (Fast but volatile)
- Varnish Cache (For web acceleration)
๐ Result? ๐ API calls are 10x faster!

๐ก Whatโs the Best Caching Strategy?
Your founder understood the caching, but he is confused about
how can I store this data down the server ?
We can store it as a map. we can store it as a key which is product id then you have a value next to it so in a map. You have a key and a value of the product id and then you have all the data for the product to store this map. itโs quite safe But there are libraries out there in the world.
There are two ways to store data. Lets Understand them.
1๏ธโฃ In-Memory Cache (Super Fast, but Limited)

Assume So if your architecture has 6 small servers, so all 6 servers will be storing the cache in memory and anytime they get any request, they will serve the cache data as response.
The benefit of this is that this cache is extremely fast the moment you get a request all the server has to do is to go and fetch a line from its cache memory so this would be in microseconds
- Each server keeps its own cache.
- If one server fails, its cache disappears.
- Data consistency issues if multiple servers hold different cache copies.
Data consistency ensures that all servers show the same updated information. For example, if a user places an order for a T-shirt and the stock was originally 10, it should be reduced to 9 everywhere. However, if only one cache server updates the stock to 9, while others still show 10, a consistency issue arises. This means some servers might still allow orders based on the old stock count, which can lead to overselling and inventory mismatches.
๐ Use when: You need ultra-low latency and can afford occasional data inconsistencies.
2๏ธโฃ Centralized Cache (Better for Large-Scale Apps)

- A dedicated cache server (Redis/Memcached) stores all cached data.
- All application servers query this cache first before hitting the database.
- No duplication of cached data across servers.
๐ Use when: You need consistent data across multiple servers.
๐ Choosing Between In-Memory vs. Centralized Cache

๐ Final Decision? โ Use a centralized Redis cache for our app.
๐ ๏ธ Scaling Caches: How Do We Distribute Data?
With millions of products, a single cache server wonโt be enough.
๐ก Solution? Use Multiple Cache Servers!
Additionally, we need to distribute the load to all the multiple cache servers. There are certain issues with the data, lets look all the possible cases.
๐น Option 1: Replication (Each Cache Stores Everything)
โ๏ธ High availability
โ๏ธ Each server has full product data
โ Lots of data duplication
Thoughts ๐ญ: โ ๏ธ Never gonna use this, so much data duplication. Already terrifying.
๐น Option 2: Sharding (Each Cache Stores Different Data)
Sharding is the process of dividing a large dataset and distributing it across multiple servers based on a specific property to improve performance and scalability.
For instance, So in our cache servers, to speed up product searches. Instead of storing all product data in a single cache, we shard the data based on categories:
- Cache Server 1: Stores data for Electronics (e.g., Phones, Laptops, Cameras)
- Cache Server 2: Stores data for Clothing (e.g., T-Shirts, Shoes, Jackets)
- Cache Server 3: Stores data for Home Appliances (e.g., Refrigerators, Microwaves, ACs)
When a user searches for a laptop, the request goes directly to Cache Server 1, instead of scanning all data. This reduces query time, prevents overloading a single server, and ensures faster responses.
โ๏ธ More efficient (Less duplication)
โ๏ธ Faster queries
โ Complex setup
๐ Final Decision? โ Use Sharding!
๐ข Sharding: How Do We Assign Data to Cache Servers?
To distribute product data, we need a consistent way to decide:
๐ Which cache server should store which product?
Another example is you can also say that this cache is going to be storing data only for the user ID from 1 to 200. This cache is going to be storing data only for user ids from 100 to 200 the next 200 right? There are assigned ranges. So basically we are assigning the ranges to the data that each cache server manages.

Also to avoid duplication we can use hashing.
๐ข Hash-Based Load Balancing
In hash-based load balancing, each product is assigned a unique hash value, which determines which cache server stores it. This ensures that all future requests for the same product are directed to the same cache server, improving efficiency and reducing unnecessary lookups.
Assume our app has three cache servers that store product data. Instead of randomly assigning products to servers, we use a hash function to determine their placement.
How it works:
- Generate a hash value for each product based on its unique ID.
- Map the hash value to a specific cache server (e.g., using
hash(productID) % number_of_servers
). - All future requests for the same product go to the same cache server, ensuring fast retrieval.
Product Distribution Across Cache Servers:

Benefits of Hash-Based Load Balancing:
The benefit of this is that, since the hash is constant, since the hash of product ID cannot change because the product itself cannot change. The cache server that is going to respond to this request as always going to be the same. So there wonโt be any duplication of product data between the caches. Amazing right ?
โ
Efficient storage and retrieval โ Requests for the same product always hit the same cache server.
โ
Even distribution of data โ The hash function ensures products are spread across cache servers.
โ
Scalability โ New products are automatically assigned based on the hash function.
- This is great; So we are able to horizontally scale up our cache.
- We are able to load balance in our cache.
- We are using two fundamental concepts of computer science together (sharding and hashing).
๐ Result?
๐ Efficient data distribution
โก No duplicate cache data
โ
Load balanced across multiple cache servers
๐ Final Takeaways from Day 7
โ
Browser caching speeds up page loads.
โ
Server-side caching (Redis) makes API calls 10x faster.
โ
Sharding & Hashing distribute cache efficiently across servers.
โ
Caching = Lower database load + Faster response times!
Great!! See you in the next blog.