Spring Book – Chapter 10 – Caching

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: 10479

The following two tabs change content below.
Tomcy John

Tomcy John

Blogger & Author at javacodebook
He is an Enterprise Java Specialist holding a degree in Engineering (B-Tech) with over 10 years of experience in several industries. He's currently the Senior Technical Engineer at Emirates Group IT since 2005. Prior to this he has worked with Oracle Corporation and Ernst & Young. His main specialization is on various web technologies and acts as chief mentor and Principal Architect to facilitate incorporating Spring as Corporate Standard in the organization.

4 thoughts on “Spring Book – Chapter 10 – Caching

    1. I don’t know whether i should take your comment as positive or negative.. :). If it is positive, thank you. If it is negative, please let me know what i can improve on to make it positive.. 🙂

      Regards
      Tomcy John

  1. Hi,

    Is there a PDF version of this article, or at least a Web version somewhere else? This Website is really bad (full of ads) and the code samples are incomplete.

    Thanks,
    Lukasz

    1. Hi Lukasz,

      I plan to have a ad free version of the entire book. For that, though, i plan to distribute with a small charge. By end of October I plan to release using “Gumroad”. If you are still interested, with a small fee you can download and enjoy an ad free version of the book.

      Regards
      Tomcy John

Leave a Reply

Your email address will not be published. Required fields are marked *