Zero To Million Users (Part 5) : Caching & Strategies

SDE Story Crafting
Level Up Coding
Published in
4 min readMay 1, 2023

--

Now that we have understanding of web tier, data tier, load balancer, database replication, it’s time to improve the load and response time. We use a technique called “caching” for it and a network to improve the response time.

Table Of Content

  1. Story of “Caching & A Coffee Shop”
  2. How “Caching” works
  3. How To Implement Read Through Cache? Example in Springboot
  4. What Are The Other Types Of Caching?

Story of “Caching & A Coffee Shop”

Once upon a time, there was a busy coffee shop with lots of customers coming in and out all day long. The baristas were struggling to keep up with all the orders, and customers were getting frustrated with the long wait times.

One day, the owner of the coffee shop had an idea. She decided to start making extra batches of the most popular drinks ahead of time and storing them in a special refrigerator behind the counter. This way, when a customer ordered one of these drinks, the baristas could simply grab it from the fridge and serve it right away, without having to make it from scratch.

This strategy worked like a charm. Customers were delighted with the faster service, and the baristas were able to keep up with the demand without feeling overwhelmed. The owner had created a cache of pre-made drinks that helped make the coffee shop run more smoothly.

In software development, caching works in a similar way. It involves storing frequently accessed data in a cache, which is a temporary storage area that can be accessed more quickly than the original source of the data. This helps improve the performance of the application by reducing the time it takes to retrieve data and reducing the load on the original data source. Just like the pre-made drinks in the coffee shop, the cached data can be quickly retrieved and used, helping to speed up the application and make it run more smoothly.

How Caching Works

Cache stores the data which are frequently accessed or has expensive response. Here’s an example of how “read through” cache works.

if data doesn’t exist in cache, web server queries the database, stores the response or query itself in cache and then return it.

How To Implement Read Through Cache? Example in Springboot

Here’s an example of how to implement ‘read-thorough’ cache in springboot:

  1. configure the cache in your application. You can do this in a configuration class annotated with @Configuration The following example sets up a cache called “myCache” with a maximum size of 100 elements and a time-to-idle expiration of 30 seconds(if an entry has not been accessed or updated for 30 seconds, it will be automatically removed from the cache.)
@Configuration
@EnableCaching
public class CacheConfig {

@Bean
public CacheManager cacheManager() {
return new EhCacheCacheManager(ehCacheManager());
}

@Bean
public EhCacheManager ehCacheManager() {
return EhCacheManagerBuilder.newCacheManagerBuilder()
.withCache("myCache", CacheConfigurationBuilder
.newCacheConfigurationBuilder(Long.class, String.class,
ResourcePoolsBuilder.heap(100))
.withExpiry(ExpiryPolicyBuilder
.timeToIdleExpiration(Duration.of(30, TimeUnit.SECONDS))))
.build(true);
}
}

2. Now, annotate the methods that you want to cache with @Cacheable. This annotation tells Spring to cache the result of the method, and if the same method is called again with the same arguments, the cached result is returned instead of executing the method again. In this example, the getData method is annotated with @Cacheable("myCache"). This tells Spring to cache the result of the method using the cache configuration we set up in the previous step.

@Service
public class MyService {

@Cacheable("myCache")
public String getData(Long id) {
// code to fetch data from database or external service
return data;
}
}

What Are The Other Types Of Caching?

There are several other types of caching besides read-through caching, including:

  1. Write-through caching — data is written to the cache and the database simultaneously. Used in banking applications.
  2. Write-behind caching — data is written to the cache first, then to the database. Used in social media platforms.
  3. Refresh-ahead caching — data is refreshed in the cache before it expires. Used in e-commerce applications.
  4. Cache-aside caching — data is fetched from the cache if available, else fetched from the database. Used in gaming applications.
  5. Distributed caching — caching data across multiple nodes for scalability. Used in cloud-based applications e.g Netflix.
  6. In-memory caching — caching data in memory for faster access. Used in real-time systems e.g apache ignite.
  7. Edge caching — caching data at the edge of the network for faster delivery. Used in content delivery networks e.g Cloudflare.
  8. Session caching — caching user session data for faster access. Used in web applications e.g Redis
  9. Object caching — caching frequently accessed objects for faster processing. Used in enterprise applications e.g Ehcache

In the next part, we will discuss in detail how Content Delivery Network works.

I publish contents every week. Follow me on Medium & let’s grow together to be a better software developer 👏

--

--