DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Data
  4. More About Spring Cache Performance

More About Spring Cache Performance

Learn more about Spring Cache performance and how it can benefit your code.

Nikita Salnikov-Tarnovski user avatar by
Nikita Salnikov-Tarnovski
·
Apr. 05, 19 · Presentation
Like (7)
Save
Tweet
Share
6.30K Views

Join the DZone community and get the full member experience.

Join For Free

This is a follow up to our last post about Spring’s cache abstraction.

As engineers, you gain valuable experience by understanding the internals of some of the tools that you use. Understanding the behavior of tools helps you become more mature when making design choices. In this post, we describe a benchmarking experiment and the results which will help you understand Spring’s built-in annotations for caching.

Take a look at the following two methods:

@Cacheable(value = "time", key = "#p0.concat(#p1)")
public long annotationWithSpel(String dummy1, String dummy2) {
  return System.currentTimeMillis();
}
@Cacheable(value = "time")
public long annotationBased(String dummy1, String dummy2) {
  return System.currentTimeMillis();
}


Here, we have two very similar methods, each annotated with the built-in @Cacheable annotation from Spring Cache. The first one includes an expression written in the Spring Expression Language. The expression is used to configure how to calculate cache key using method parameters. The second one relies on Spring’s default behavior which is “all method parameters are considered as a key.” Effectively, both the methods above actually result in exactly the same external behavior.

We ran some benchmark tests that allowed us to measure their performance:

Benchmark                       Mode  Cnt     Score    Error  Units
CacheBenchmark.annotationBased  avgt    5   271.975 ± 11.586  ns/op
CacheBenchmark.spel             avgt    5  1196.744 ± 93.765  ns/op
CacheBenchmark.manual           avgt    5    16.325 ±  0.856  ns/op
CacheBenchmark.nocache          avgt    5    40.142 ±  4.012  ns/op


It turns out that the method that had a manually configured cache runs 4.4 times slower! In hindsight, this outcome seems to make sense because of overheads. The Spring framework has to parse an arbitrarily complex expression and some cycles are consumed in this computation.

Why are we writing this story? Well...

  1. We care deeply about software performance.
  2. Our own codebase has a few of these instances where we have had to trade-off performance for zero benefits.

You should examine your code base and conduct a review or audit, too. Jettison some of these instances as well and gain performance improvements. You could very well have some instances where you have manually configured cache keys as well. Remember, this is exactly the same behavior that Spring Cache would provide you by default. Definitely a win-win situation!

Spring Framework Cache (computing)

Published at DZone with permission of Nikita Salnikov-Tarnovski, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Remote Debugging Dangers and Pitfalls
  • The 12 Biggest Android App Development Trends in 2023
  • API Design Patterns Review
  • RabbitMQ vs. Memphis.dev

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: