1

I have a SpringBoot (2.1.7) application with Hystrix (Javanica) and JCache (EHCache implementation). I have methods annotated with the @HystrixCommand annotation to enable Hystrix for them. I also have caching on those methods. It looks like this:

@Override
@HystrixCommand(fallbackMethod = "fallbackGetAllUserProfiles")
@Timed(histogram = true, percentiles = {0.75, 0.9, 0.95, 0.99})
@CacheResult
public UserProfiles getAllUserProfiles(@CacheKey String accountID, @CacheKey String userID) {
    return getAllUserProfilesFromBackend(accountID, userID);
}

UserProfiles getAllUserProfilesFromBackend(String accountID, String userID) {
   // call to backend; omitted for readability
}}

Now I am looking at the documentation and there are actually 2 different @CacheResult annotations: one from JCache (jsr-107) and one from Javanica (Hystrix).

If I also want Hystrix to use a cache, should I also specify the second @com.netflix.hystrix.contrib.javanica.cache.annotation.CacheResult?

Or can I better move the @CacheResult to the method getAllUserProfilesFromBackend and then annotate the Hystrix method with the Hystrix @CacheResult?

I am just wondering, as the Hystrix framework can use the cache in case of failing services, but it is not clear to me that it automatically uses JCache.

Sven
  • 2,343
  • 1
  • 18
  • 29
  • To make things more complicated: You can also use the Spring caching annotations. There is no need to use JSR107 annoations for a JCache cache. – cruftex Aug 29 '19 at 06:34
  • I am thinking about switching to Spring annotations, as they are more flexible with keys. I hate the way the custom key-definition works in JSR-107... – Sven Aug 29 '19 at 06:36
  • Yes, JSR107 annotations are more or less broken. If you have Spring, there is no need to use JSR107 annotations. – cruftex Aug 29 '19 at 06:39
  • I like the question a lot, since it is a good example that annotations lead to a lot of confusion. Looking at the code: What is applied first, caching or hystrix? Should the cache go first and cache good results only? However, a histogram of cached entries makes no sense. So, does the order of the annotations matter or is there an explicit order? – cruftex Aug 29 '19 at 06:44
  • You are absolutely right. Because of annotations you don't specify all those steps explicitly. But now there is not control what so ever how the code works. – Sven Aug 29 '19 at 07:49
  • Let's see if somebody else has an idea on how to combine Spring/JCache annotations with hystrix. For consistency, in terms of, knowing what is happening, I think you should use Javanica cache, however, digging in the code this seems to be a `ConcurrentHashMap` with no controls. BTW: Javainca is in hystrix-contrib. I doubt this is used by Netflix. – cruftex Aug 29 '19 at 08:33

0 Answers0