• Home
  • About
    • Thoughts To Pen photo

      Thoughts To Pen

      My thoughts on Computer Programming || Psychology || Personal Finances || & much more...

    • Learn More
    • Twitter
    • Instagram
    • Github
    • StackOverflow
  • Posts
    • All Posts
    • All Tags
  • Projects
  • Portfolio
  • Resources
  • About

Caching Strategies & Content Delivery Networks (CDNs)

02 May 2026

Caching and CDNs (Image generated by AI)

Stop Torturing Your Database

Imagine you run a massive recipe website. Right now, a million people are trying to look at the recipe for “Classic Chocolate Chip Cookies.”

If your architecture simply routes every user to your backend server, which then asks the database, “Hey, what are the ingredients for Chocolate Chip Cookies?”, your database is going to collapse. It is physically reading the same data from disk a million times a second.

This is where Caching comes in to save the day.


What is a Cache?

In simple terms, a cache is a high-speed data storage layer (usually stored in RAM, rather than on a hard drive) that stores a subset of data so that future requests for that data are served up faster than accessing the primary storage location.

Think of it like a fast-food drive-thru. The kitchen (Database) is massive, complex, and capable of cooking anything, but it takes time. The drive-thru window (Cache) holds 50 pre-made cheeseburgers. When a car pulls up and asks for a cheeseburger, it is handed over instantly.

The most popular tool for caching in modern System Design is Redis (an in-memory key-value store).

From-Scratch Example: Cache-Aside Strategy

The most common way to implement caching is the Cache-Aside (or Lazy Loading) strategy.

Here is how it works in plain Java pseudocode:

public class RecipeService {
    
    private Database db = new Database();
    private RedisCache cache = new RedisCache(); // Fast, in-memory!

    public String getRecipe(String recipeId) {
        
        // 1. Check the cache FIRST.
        String cachedRecipe = cache.get(recipeId);
        
        if (cachedRecipe != null) {
            System.out.println("Cache Hit! Returning instantly.");
            return cachedRecipe;
        }
        
        // 2. If it's NOT in the cache (Cache Miss), we must query the slow DB.
        System.out.println("Cache Miss. Querying slow database...");
        String dbRecipe = db.query("SELECT content FROM recipes WHERE id = " + recipeId);
        
        // 3. Save it to the cache so the NEXT person gets it instantly.
        // We set a Time-To-Live (TTL) of 60 seconds, so the cache doesn't get stale forever.
        cache.set(recipeId, dbRecipe, 60);
        
        return dbRecipe;
    }
}

If one million people request the cookie recipe within 60 seconds, the database is queried exactly one time. The other 999,999 requests are served instantly from RAM by Redis.


The Hard Part: Cache Invalidation

As the famous computer science quote goes:

“There are only two hard things in Computer Science: cache invalidation and naming things.”

If the author updates the cookie recipe to include walnuts, but the old recipe without walnuts is still sitting in the cache for another 50 minutes, users will see stale data.

How do we fix this?

  • Write-Through Caching: Every time the author updates the database, you actively update the cache at the exact same time. This ensures the cache is always fresh, but makes writing data slightly slower.
  • Time-to-Live (TTL): As seen in our code above, you simply let the cache expire after a set time.

Content Delivery Networks (CDNs)

While Redis is great for caching database queries (like JSON strings or user data), what about heavy static assets like Images, CSS, and JavaScript files?

If your server is in New York, and a user in Tokyo requests a 5MB image, that data has to travel across the Pacific Ocean. It will be slow due to physical physics (the speed of light).

A CDN is a globally distributed network of cache servers. Companies like Cloudflare or AWS CloudFront place servers in hundreds of cities worldwide.

How a CDN works:

  1. You upload your website’s logo (logo.png) to your main server in New York.
  2. The CDN automatically copies that image to its “Edge Servers” in London, Tokyo, Sydney, and Mumbai.
  3. When the user in Tokyo visits your site, their browser requests logo.png. Instead of traveling to New York, the request is intercepted by the CDN’s Tokyo server, delivering the image instantly.

Summary

Use a memory cache (like Redis) to stop querying your database for frequently accessed data. Use a CDN (like Cloudflare) to serve heavy, static files to users geographically close to them. Combine both, and your system will fly.



programmingsoftware-engineeringsystem-designcachingperformance Share Tweet Msg