Performance Zone is brought to you in partnership with:

I am a software developer from Poland, currently working in banking industry. For the past few years I have been writing software in Java, however I actively seek for a close alternative. Certified in SCJP, SCJD, SCWCD and SCBCD, used to be active on StackOverflow. I feel comfortable at the back-end, however recently rediscovered front-end development. In spare time I love cycling. Tomasz is a DZone MVB and is not an employee of DZone and has posted 88 posts at DZone. You can read more from them at their website. View Full User Profile

@Cacheable overhead in Spring

01.15.2013
| 9492 views |
  • submit to reddit
Spring 3.1 introduced great caching abstraction layer. Finally we can abandon all home-grown aspects, decorators and code polluting our business logic related to caching. Since then we can simply annotate heavyweight methods and let Spring and AOP machinery do the work:
@Cacheable("books")
public Book findBook(ISBN isbn) {...}
"books" is a cache name, isbn parameter becomes cache key and returned Book object will be placed under that key. The meaning of cache name is dependant on the underlying cache manager (EhCache, concurrent map, etc.) - Spring makes it easy to plug different caching providers. But this post won't be about caching feature in Spring...

Some time ago my teammate was optimizing quite low-level code and discovered an opportunity for caching. He quickly applied @Cacheable just to discover that the code performed worse then it used to. He got rid of the annotation and implemented caching himself manually, using good old java.util.ConcurrentHashMap. The performance was much better. He blamed @Cacheable and Spring AOP overhead and complexity. I couldn't believe that a caching layer can perform so poorly until I had to debug Spring caching aspects few times myself (some nasty bug in my code, you know, cache invalidation is one of the two hardest things in CS). Well, the caching abstraction code is much more complex than one would expect (after all it's just get and put!), but it doesn't necessarily mean it must be that slow?

In science we don't believe and trust, we measure and benchmark. So I wrote a benchmark to precisely measure the overhead of @Cacheable layer. Caching abstraction layer in Spring is implemented on top of Spring AOP, which can further be implemented on top of Java proxies, CGLIB generated subclasses or AspectJ instrumentation. Thus I'll test the following configurations:


  • no caching at all - to measure how fast the code is with no intermediate layer

  • manual cache handling using ConcurrentHashMap in business code

  • @Cacheable with CGLIB implementing AOP

  • @Cacheable with java.lang.reflect.Proxy implementing AOP

  • @Cacheable with AspectJ compile time weaving (as similar benchmark shows, CTW is slightly faster than LTW)

  • Home-grown AspectJ caching aspect - something between manual caching in business code and Spring abstraction

Let me reiterate: we are not measuring the performance gain of caching and we are not comparing various cache providers. That's why our test method is as fast as it can be and I will be using simplest ConcurrentMapCacheManager from Spring. So here is a method in question:
public interface Calculator {
 
    int identity(int x);
 
}
 
public class PlainCalculator implements Calculator {
 
    @Cacheable("identity")
    @Override
    public int identity(int x) {
        return x;
    }
 
}
I know, I know there is no point in caching such a method. But I want to measure the overhead of caching layer (during cache hit to be specific). Each caching configuration will have its own ApplicationContext as you can't mix different proxying modes in one context:
public abstract class BaseConfig {
 
    @Bean
    public Calculator calculator() {
        return new PlainCalculator();
    }
 
}
 
@Configuration
class NoCachingConfig extends BaseConfig {}
 
@Configuration
class ManualCachingConfig extends BaseConfig {
    @Bean
    @Override
    public Calculator calculator() {
        return new CachingCalculatorDecorator(super.calculator());
    }
}
 
@Configuration
abstract class CacheManagerConfig extends BaseConfig {
 
    @Bean
    public CacheManager cacheManager() {
        return new ConcurrentMapCacheManager();
    }
 
}
 
@Configuration
@EnableCaching(proxyTargetClass = true)
class CacheableCglibConfig extends CacheManagerConfig {}
 
@Configuration
@EnableCaching(proxyTargetClass = false)
class CacheableJdkProxyConfig extends CacheManagerConfig {}
 
@Configuration
@EnableCaching(mode = AdviceMode.ASPECTJ)
class CacheableAspectJWeaving extends CacheManagerConfig {
 
    @Bean
    @Override
    public Calculator calculator() {
        return new SpringInstrumentedCalculator();
    }
 
}
 
@Configuration
@EnableCaching(mode = AdviceMode.ASPECTJ)
class AspectJCustomAspect extends CacheManagerConfig {
 
    @Bean
    @Override
    public Calculator calculator() {
        return new ManuallyInstrumentedCalculator();
    }
 
}
Each @Configuration class represents one application context. CachingCalculatorDecorator is a decorator around real calculator that does the caching (welcome to the 1990s):
public class CachingCalculatorDecorator implements Calculator {
 
    private final Map<Integer, Integer> cache = new java.util.concurrent.ConcurrentHashMap<Integer, Integer>();
 
    private final Calculator target;
 
    public CachingCalculatorDecorator(Calculator target) {
        this.target = target;
    }
 
    @Override
    public int identity(int x) {
        final Integer existing = cache.get(x);
        if (existing != null) {
            return existing;
        }
        final int newValue = target.identity(x);
        cache.put(x, newValue);
        return newValue;
    }
}
SpringInstrumentedCalculator and ManuallyInstrumentedCalculator are exactly the same as PlainCalculator but they are instrumented by AspectJ compile-time weaver with Spring and custom aspect accordingly. My custom caching aspect looks like this:
public aspect ManualCachingAspect {
 
    private final Map<Integer, Integer> cache = new ConcurrentHashMap<Integer, Integer>();
 
    pointcut cacheMethodExecution(int x): execution(int com.blogspot.nurkiewicz.cacheable.calculator.ManuallyInstrumentedCalculator.identity(int)) && args(x);
 
    Object around(int x): cacheMethodExecution(x) {
        final Integer existing = cache.get(x);
        if (existing != null) {
            return existing;
        }
        final Object newValue = proceed(x);
        cache.put(x, (Integer)newValue);
        return newValue;
    }
 
}
After all this preparation we can finally write the benchmark itself. At the beginning I start all the application contexts and fetch Calculator instances. Each instance is different. For example noCaching is a PlainCalculator instance with no wrappers, cacheableCglib is a CGLIB generated subclass while aspectJCustom is an instance of ManuallyInstrumentedCalculator with my custom aspect woven.
private final Calculator noCaching = fromSpringContext(NoCachingConfig.class);
private final Calculator manualCaching = fromSpringContext(ManualCachingConfig.class);
private final Calculator cacheableCglib = fromSpringContext(CacheableCglibConfig.class);
private final Calculator cacheableJdkProxy = fromSpringContext(CacheableJdkProxyConfig.class);
private final Calculator cacheableAspectJ = fromSpringContext(CacheableAspectJWeaving.class);
private final Calculator aspectJCustom = fromSpringContext(AspectJCustomAspect.class);
 
private static <T extends BaseConfig> Calculator fromSpringContext(Class<T> config) {
    return new AnnotationConfigApplicationContext(config).getBean(Calculator.class);
}
I'm going to exercise each Calculator instance with the following test. The additional accumulator is necessary, otherwise JVM might optimize away the whole loop (!):
private int benchmarkWith(Calculator calculator, int reps) {
    int accum = 0;
    for (int i = 0; i < reps; ++i) {
        accum += calculator.identity(i % 16);
    }
    return accum;
}
Here is the full caliper test without parts already discussed:
public class CacheableBenchmark extends SimpleBenchmark {
 
    //...
 
    public int timeNoCaching(int reps) {
        return benchmarkWith(noCaching, reps);
    }
 
    public int timeManualCaching(int reps) {
        return benchmarkWith(manualCaching, reps);
    }
 
    public int timeCacheableWithCglib(int reps) {
        return benchmarkWith(cacheableCglib, reps);
    }
 
    public int timeCacheableWithJdkProxy(int reps) {
        return benchmarkWith(cacheableJdkProxy, reps);
    }
 
    public int timeCacheableWithAspectJWeaving(int reps) {
        return benchmarkWith(cacheableAspectJ, reps);
    }
 
    public int timeAspectJCustom(int reps) {
        return benchmarkWith(aspectJCustom, reps);
    }
}
I hope you are still following our experiment. We are now going to execute Calculate.identity() millions of times and see which caching configuration performs best. Since we only call identity() with 16 different arguments, we hardly ever touch the method itself as we always get cache hit. Curious to see the results?
                  benchmark      ns linear runtime
                  NoCaching    1.77 =
              ManualCaching   23.84 =
         CacheableWithCglib 1576.42 ==============================
      CacheableWithJdkProxy 1551.03 =============================
CacheableWithAspectJWeaving 1514.83 ============================
              AspectJCustom   22.98 =

Caliper

Interpretation

Let's go step by step. First of all calling a method in Java is pretty darn fast! 1.77 nanoseconds, we are talking here about 3 CPU cycles on my Intel(R) Core(TM)2 Duo CPU T7300 @ 2.00GHz! If this doesn't convince you that Java is fast, I don't know what will. But back to our test.

Hand-made caching decorator is also pretty fast. Of course it's slower by an order of magnitude compared to pure function call, but still blazingly fast compared to all @Scheduled benchmarks. We see a drop by 3 orders of magnitude, from 1.8 ns to 1.5 μs. I'm especially disappointed by the @Cacheable backed by AspectJ. After all caching aspect is precompiled directly into my Java .class file, I would expect it to be much faster compared to dynamic proxies and CGLIB. But that doesn't seem to be the case. All three Spring AOP techniques are similar.

The greatest surprise is my custom AspectJ aspect. It's even faster than CachingCalculatorDecorator! maybe it's due to polymorphic call in the decorator? I strongly encourage you to clone this benchmark on GitHub and run it (mvn clean test, takes around 2 minutes) to compare your results.

Conclusions

You might be wondering why Spring abstraction layer is so slow? Well, first of all, check out the core implementation in CacheAspectSupport - it's actually quite complex. Secondly, is it really that slow? Do the math - you typically use Spring in business applications where database, network and external APIs are the bottleneck. What latencies do you typically see? Milliseconds? Tens or hundreds of milliseconds? Now add an overhead of 2 μs (worst case scenario). For caching database queries or REST calls this is completely negligible. It doesn't matter which technique you choose.

But if you are caching very low-level methods close to the metal, like CPU-intensive, in-memory computations, Spring abstraction layer might be an overkill. The bottom line: measure!

PS: both benchmark and contents of this article in Markdown format are freely available.


Published at DZone with permission of Tomasz Nurkiewicz, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Sinuhé Pop replied on Wed, 2013/01/16 - 11:50am

Amazing numbers! I'm wondering if this overhead is common in all other Spring proxies. It's true that this is nothing compared to an I/O access, but I thought all these mechanisms (CGLIB, JDK, AspectJ) were lighter. Thanks for your work.

Jim O'callaghan replied on Thu, 2013/01/24 - 7:07am

Thanks for the comprehensive figures.  It's interesting that you have such a high figure for the CacheableWithAspectJWeaving test.  I have (repeatable) results with minor variations with the following figures:

  benchmark  ns linear runtime
  NoCaching  5.59 =
  ManualCaching  53.95 =
  CacheableWithCglib 1595.60 ============================
  CacheableWithJdkProxy 1681.04 ==============================
  CacheableWithAspectJWeaving  5.62 =
  AspectJCustom  5.58 =

Using java version "1.6.0_33" on a fairly low-spec PC (i3 quad @3.3 GHz 3GB RAM Win XP).


Tomasz Nurkiewicz replied on Thu, 2013/01/24 - 7:48am in response to: Jim O'callaghan

@Jim O'callaghan - have a look at this discussion  (bottom) - seems like you have the same issue.

Jim O'callaghan replied on Thu, 2013/01/24 - 10:29am in response to: Tomasz Nurkiewicz

 Thanks Tomasz.  Adding:

<forceAjcCompile>true</forceAjcCompile>

to the aspectj-maven-plugin configuration block allows a repeatable test.  As you noted, the plugin was skipping compilation except following a clean (No modifications found skipping aspectJ compile).

Tomasz Nurkiewicz replied on Thu, 2013/01/24 - 1:03pm in response to: Jim O'callaghan

Thanks! Indeed this was the culprit, I pushed your fix. Moreover you can see the results on Travis CI  (not sure how isolated and stable their machines are). Every pull request is automatically built as well.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.