This content originally appeared on Level Up Coding - Medium and was authored by Somal Chakraborty
Zero To Million Users (Part 5) : Caching & Strategies
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
- Story of “Caching & A Coffee Shop”
- How “Caching” works
- How To Implement Read Through Cache? Example in Springboot
- 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:
- 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:
- Write-through caching — data is written to the cache and the database simultaneously. Used in banking applications.
- Write-behind caching — data is written to the cache first, then to the database. Used in social media platforms.
- Refresh-ahead caching — data is refreshed in the cache before it expires. Used in e-commerce applications.
- Cache-aside caching — data is fetched from the cache if available, else fetched from the database. Used in gaming applications.
- Distributed caching — caching data across multiple nodes for scalability. Used in cloud-based applications e.g Netflix.
- In-memory caching — caching data in memory for faster access. Used in real-time systems e.g apache ignite.
- Edge caching — caching data at the edge of the network for faster delivery. Used in content delivery networks e.g Cloudflare.
- Session caching — caching user session data for faster access. Used in web applications e.g Redis
- 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 👏
Zero To Million Users (Part 5) : Caching & Strategies was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding - Medium and was authored by Somal Chakraborty
Somal Chakraborty | Sciencx (2023-05-01T03:08:00+00:00) Zero To Million Users (Part 5) : Caching & Strategies. Retrieved from https://www.scien.cx/2023/05/01/zero-to-million-users-part-5-caching-strategies/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.