Application with Caching Layer
Introducing caching layer to your application brings the following key features into the table. They can be summarized as follows:
- Reduction in new objects created in Java heap and CPU memory. This is achieved by reusing the repeatedly used objects from the cache rather than going to and fro to the actual database holding the data.
- Reducing your applications reliance on the database. This is again achieved by storing data in the cache.
- Reliance of your application on the network is reduced (network traffic) as data already resides in your application layer as opposed in the infrastructure layer where database resides.
- Since data resides in application layer in the required format, additional marshalling in the application layer is avoided which results in reducing CPU usage of your application.
Now let’s see how caching layer in your applications brings in additional benefits. These are summarized as bullet points below for your understanding:
- Reduces creation of repeated objects in the Java heap and CPU memory.
- Objects already in the cache will be picked from here rather than creating these objects again in the application layer.
- Data already residing in application layer will be picked from here and this reduces network usages which are expensive in nature.
- Since data is picked directly from application layer, response times are much better.
- Network calls reduced drastically.
- Less Java heap usage as repeated objects are not created, which reduces repeated usage of Java garbage collection.
- Objects reside in application layer which reduces the actual marshalling of objects reducing CPU usage.
Figure 10-2. Application interactions with caching layer
Figure 10-2 shows a typical application with caching layer. As depicted in the figure, the cache resides in the application layer, which brings in all the advantages which the caching framework has to offer for your application.
JSR107: JCache–Java Temporary Caching API
Specifies API and semantics for temporary, in memory caching of Java objects, including object creation, shared access, spooling, invalidation, and consistency across JVM’s.
– JSR description (jcp.org)
I wanted to bring your attention to this JSR, similar to other JSR’s already discussed in this book because there has been wide adoption of this JSR by major players like Terracotta’s EhCache, Oracle’s Coherance, SpringSource’s Gemfire etc., who provides cache frameworks of which some have been already supported by Spring Framework.
Reading through the previous sections of this chapter, we know that caching is one of the way to increase performance and scalability in your application, yet Java does not have a completed standard caching mechanism yet. JSR 107 tries to have this standard caching mechanism.
The benefits of this specification can be summarized as below:
- All caching solutions works with same set of API’s
- Easy to implement, as opposed to learning each caching provider API’s
- Easy to change caching solutions at will with minimal impact on the actual application’s business logic
- Common caching annotations, easy to understand and use in your application.
As we go through the chapter we will be seeing various caching annotations and their usage through code snippets, which are indirectly part of this specification.
Page Visitors: 12453