mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-14 08:58:26 +00:00
docs: improve Caching docs
This commit is contained in:
+48
-37
@@ -1,27 +1,27 @@
|
||||
---
|
||||
title: Caching
|
||||
category: Behavioral
|
||||
category: Performance optimization
|
||||
language: en
|
||||
tag:
|
||||
- Caching
|
||||
- Performance
|
||||
- Cloud distributed
|
||||
---
|
||||
|
||||
## Intent
|
||||
|
||||
The caching pattern avoids expensive re-acquisition of resources by not releasing them immediately
|
||||
after use. The resources retain their identity, are kept in some fast-access storage, and are
|
||||
re-used to avoid having to acquire them again.
|
||||
The caching pattern avoids expensive re-acquisition of resources by not releasing them immediately after use. The resources retain their identity, are kept in some fast-access storage, and are re-used to avoid having to acquire them again.
|
||||
|
||||
## Also known as
|
||||
|
||||
* Cache
|
||||
* Temporary Storage
|
||||
|
||||
## Explanation
|
||||
|
||||
Real world example
|
||||
|
||||
> A team is working on a website that provides new homes for abandoned cats. People can post their
|
||||
> cats on the website after registering, but all the new posts require approval from one of the
|
||||
> site moderators. The user accounts of the site moderators contain a specific flag and the data
|
||||
> is stored in a MongoDB database. Checking for the moderator flag each time a post is viewed
|
||||
> becomes expensive and it's a good idea to utilize caching here.
|
||||
> A team is working on a website that provides new homes for abandoned cats. People can post their cats on the website after registering, but all the new posts require approval from one of the site moderators. The user accounts of the site moderators contain a specific flag and the data is stored in a MongoDB database. Checking for the moderator flag each time a post is viewed becomes expensive, and it's a good idea to utilize caching here.
|
||||
|
||||
In plain words
|
||||
|
||||
@@ -29,19 +29,11 @@ In plain words
|
||||
|
||||
Wikipedia says:
|
||||
|
||||
> In computing, a cache is a hardware or software component that stores data so that future
|
||||
> requests for that data can be served faster; the data stored in a cache might be the result of
|
||||
> an earlier computation or a copy of data stored elsewhere. A cache hit occurs when the requested
|
||||
> data can be found in a cache, while a cache miss occurs when it cannot. Cache hits are served by
|
||||
> reading data from the cache, which is faster than recomputing a result or reading from a slower
|
||||
> data store; thus, the more requests that can be served from the cache, the faster the system
|
||||
> performs.
|
||||
> In computing, a cache is a hardware or software component that stores data so that future requests for that data can be served faster; the data stored in a cache might be the result of an earlier computation or a copy of data stored elsewhere. A cache hit occurs when the requested data can be found in a cache, while a cache miss occurs when it cannot. Cache hits are served by reading data from the cache, which is faster than recomputing a result or reading from a slower data store; thus, the more requests that can be served from the cache, the faster the system performs.
|
||||
|
||||
**Programmatic Example**
|
||||
|
||||
Let's first look at the data layer of our application. The interesting classes are `UserAccount`
|
||||
which is a simple Java object containing the user account details, and `DbManager` interface which handles
|
||||
reading and writing of these objects to/from database.
|
||||
Let's first look at the data layer of our application. The interesting classes are `UserAccount` which is a simple Java object containing the user account details, and `DbManager` interface which handles reading and writing of these objects to/from database.
|
||||
|
||||
```java
|
||||
@Data
|
||||
@@ -74,15 +66,11 @@ In the example, we are demonstrating various different caching policies
|
||||
when the cache is full
|
||||
* Cache-aside pushes the responsibility of keeping the data synchronized in both data sources to
|
||||
the application itself
|
||||
* Read-through strategy is also included in the aforementioned strategies and it returns data from
|
||||
* Read-through strategy is also included in the aforementioned strategies, and it returns data from
|
||||
the cache to the caller if it exists, otherwise queries from DB and stores it into the cache for
|
||||
future use.
|
||||
|
||||
The cache implementation in `LruCache` is a hash table accompanied by a doubly
|
||||
linked-list. The linked-list helps in capturing and maintaining the LRU data in the cache. When
|
||||
data is queried (from the cache), added (to the cache), or updated, the data is moved to the front
|
||||
of the list to depict itself as the most-recently-used data. The LRU data is always at the end of
|
||||
the list.
|
||||
The cache implementation in `LruCache` is a hash table accompanied by a doubly linked-list. The linked-list helps in capturing and maintaining the LRU data in the cache. When data is queried (from the cache), added (to the cache), or updated, the data is moved to the front of the list to depict itself as the most-recently-used data. The LRU data is always at the end of the list.
|
||||
|
||||
```java
|
||||
@Slf4j
|
||||
@@ -151,8 +139,7 @@ public class LruCache {
|
||||
}
|
||||
```
|
||||
|
||||
The next layer we are going to look at is `CacheStore` which implements the different caching
|
||||
strategies.
|
||||
The next layer we are going to look at is `CacheStore` which implements the different caching strategies.
|
||||
|
||||
```java
|
||||
@Slf4j
|
||||
@@ -214,11 +201,7 @@ public class CacheStore {
|
||||
}
|
||||
```
|
||||
|
||||
`AppManager` helps to bridge the gap in communication between the main class and the application's
|
||||
back-end. DB connection is initialized through this class. The chosen caching strategy/policy is
|
||||
also initialized here. Before the cache can be used, the size of the cache has to be set. Depending
|
||||
on the chosen caching policy, `AppManager` will call the appropriate function in the `CacheStore`
|
||||
class.
|
||||
`AppManager` helps to bridge the gap in communication between the main class and the application's back-end. DB connection is initialized through this class. The chosen caching strategy/policy is also initialized here. Before the cache can be used, the size of the cache has to be set. Depending on the chosen caching policy, `AppManager` will call the appropriate function in the `CacheStore` class.
|
||||
|
||||
```java
|
||||
@Slf4j
|
||||
@@ -312,7 +295,7 @@ public class App {
|
||||
|
||||
public void useReadThroughAndWriteBehindStrategy() { /* ... */ }
|
||||
|
||||
public void useCacheAsideStategy() { /* ... */ }
|
||||
public void useCacheAsideStrategy() { /* ... */ }
|
||||
}
|
||||
```
|
||||
|
||||
@@ -322,14 +305,39 @@ public class App {
|
||||
|
||||
## Applicability
|
||||
|
||||
Use the Caching pattern(s) when
|
||||
Use the Caching pattern when
|
||||
|
||||
* Repetitious acquisition, initialization, and release of the same resource cause unnecessary
|
||||
performance overhead.
|
||||
* Repetitious acquisition, initialization, and release of the same resource cause unnecessary performance overhead
|
||||
* In scenarios where the cost of recomputing or re-fetching data is significantly higher than storing and retrieving it from cache
|
||||
* For read-heavy applications with relatively static data or data that changes infrequently
|
||||
|
||||
## Known Uses
|
||||
|
||||
* Web page caching to reduce server load and improve response time
|
||||
* Database query caching to avoid repeated expensive SQL queries
|
||||
* Caching results of CPU-intensive computations
|
||||
* Content Delivery Networks (CDNs) for caching static resources like images, CSS, and JavaScript files closer to the end users
|
||||
|
||||
## Consequences
|
||||
|
||||
Benefits:
|
||||
|
||||
* Improved Performance: Significantly reduces data access latency, leading to faster application performance
|
||||
* Reduced Load: Decreases the load on the underlying data source, which can lead to cost savings and increased longevity of the resource
|
||||
* Scalability: Enhances the scalability of applications by efficiently handling increases in load without proportional increases in resource utilization
|
||||
|
||||
Trade-Offs:
|
||||
|
||||
* Complexity: Introduces complexity in terms of cache invalidation, consistency, and synchronization
|
||||
* Resource Utilization: Requires additional memory or storage resources to maintain the cache
|
||||
* Stale Data: There's a risk of serving outdated data if the cache is not properly invalidated or updated when the underlying data changes
|
||||
|
||||
## Related patterns
|
||||
|
||||
* [Proxy](https://java-design-patterns.com/patterns/proxy/)
|
||||
* [Proxy](https://java-design-patterns.com/patterns/proxy/): Caching can be implemented using the Proxy pattern, where the proxy object intercepts requests and returns cached data if available
|
||||
* [Observer](https://java-design-patterns.com/patterns/observer/): Can be used to notify the cache when the underlying data changes, so that it can be updated or invalidated accordingly
|
||||
* [Decorator](https://java-design-patterns.com/patterns/decorator/): Can be used to add caching behavior to an existing object without modifying its code
|
||||
* [Strategy](https://java-design-patterns.com/patterns/strategy/): Different caching strategies can be implemented using the Strategy pattern, allowing the application to switch between them at runtime
|
||||
|
||||
## Credits
|
||||
|
||||
@@ -340,3 +348,6 @@ Use the Caching pattern(s) when
|
||||
* [Java Performance: In-Depth Advice for Tuning and Programming Java 8, 11, and Beyond](https://www.amazon.com/gp/product/1492056111/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=1492056111&linkId=7e553581559b9ec04221259e52004b08)
|
||||
* [Effective Java](https://www.amazon.com/gp/product/B078H61SCH/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=B078H61SCH&linkId=f06607a0b48c76541ef19c5b8b9e7882)
|
||||
* [Java Performance: The Definitive Guide: Getting the Most Out of Your Code](https://www.amazon.com/gp/product/1449358454/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=1449358454&linkId=475c18363e350630cc0b39ab681b2687)
|
||||
* [Patterns of Enterprise Application Architecture](https://amzn.to/3PMAHRZ)
|
||||
* [Scalable Internet Architectures](https://amzn.to/48V3ni9)
|
||||
* [High Performance Browser Networking](https://amzn.to/3TiNNY4)
|
||||
|
||||
@@ -31,6 +31,6 @@ services:
|
||||
MONGO_INITDB_ROOT_USERNAME: root
|
||||
MONGO_INITDB_ROOT_PASSWORD: rootpassword
|
||||
ports:
|
||||
- 27017:27017
|
||||
- '27017:27017'
|
||||
volumes:
|
||||
- ./mongo-data/:/data/db
|
||||
- ./mongo-data/:/data/db
|
||||
|
||||
Reference in New Issue
Block a user