1

I want to use Guava caching mechanism to cache request-response pair of webservice calls to improve performance of website. But, before going ahead with solution want to know how does Guava caching stand in terms of performance?

Thanks, Ashish.

AshishK
  • 171
  • 1
  • 2
  • 8
  • 1
    It's definitely good enough for you. – Kayaman Apr 17 '18 at 13:57
  • Thanks @Kayaman But I was more interested in factual data or stats. Any idea on how it is in terms of memory usage and retrieval time compared to Java Map? – AshishK Apr 17 '18 at 14:00
  • Isn't that a bit naive to compare Guava's cache to a Map for performance? I mean after all you do know how Guava's cache is implemented, right? – Kayaman Apr 17 '18 at 14:04
  • Yes, of-course. Forgot to mention I wanted to use cache in session only and want lightweight but effective solution which can invalidate entries based on some eviction criteria. – AshishK Apr 17 '18 at 14:07
  • Wow Kayaman back off. I agree that the question can be kind of silly, but come on. Nothing wrong with asking about cache performance if he thinks he can pull off implementing his use case with a `ConcurrentMap` for example. – Fred Apr 17 '18 at 14:12
  • 4
    Yeah man just don't treat people bad that's all. We can make SO a better place by better introducing it to new users. So @AshishK, Kayaman is right about that you should google stuff and try yourself before coming to SO. Next time keep that in mind. Here's a github issue with some read/write metrics https://github.com/google/guava/issues/2063 – Fred Apr 17 '18 at 14:17
  • Guys, I tried to go through few posts but couldnot find good comparisons for my usecase, thats why thought to ask "question" on SO. But looks like my question is taken in a different way rather providing some answer. Definitely SO is not a google/forum, but it can help point people to some solution to their problems. @Kayaman I did search and found this link but it was not very clear hence asked this question. If you dont want to provide answer its okay, but donot discourage from asking questions. https://cruftex.net/2016/03/16/Java-Caching-Benchmarks-2016-Part-1.html – AshishK Apr 17 '18 at 14:31
  • I discourage asking bad questions, which is how this site works. If you have done research, you need to include those in your questions. Both as a way to prove you're not trying to get other people to do your work, and for other people not suggesting the resources you've already looked at. Your profile says you've been a member for 4+ years, so you should really know how StackOverflow works by now. – Kayaman Apr 17 '18 at 14:34
  • @Fred, thanks for providing the link, will go through it. – AshishK Apr 17 '18 at 14:36
  • @Kayaman since you like SO policies so much, here's one for you https://stackoverflow.com/help/be-nice – Fred Apr 17 '18 at 14:39
  • @Fred you're right, I should be nicer. But your "We can make SO a better place by better introducing it to new users" doesn't really apply to someone with a 4 year old profile. If it had said "joined today" I can assure you my response would have reflected that fact. – Kayaman Apr 17 '18 at 14:47
  • 3
    Guava is fine. If you have a performance problem, need additional functionality, or are on Java 8+ then consider Caffeine. Both are extensively tested and widely used, and are sibling projects. – Ben Manes Apr 17 '18 at 15:00
  • Finally used Guava cache with below initialization code: `CacheBuilder.newBuilder() .maximumSize() .expireAfterWrite(, TimeUnit.SECONDS) .build();` Thanks @Kayaman @ben-maness @RedShift for your valuable suggestions. – AshishK Sep 10 '18 at 08:29

1 Answers1

1

Any in-memory cache is always significantly faster (magnitudes) than a round-trip to a database, file, another service, ... (talking to other computers or the file system is really, REALLY expensive compared to just a fetch from memory) Google Guava's cache is basically a Map that automatically triggers some fetching code if the key you're searching for isn't present (along with some automated eviction if you so choose). The Guava wiki page on cache explains it all. If for some reason this cache becomes a bottleneck (based on profiling, not "let me wet my finger and feel which way the wind is blowing"), it's much more likely the hardware you're running on isn't sufficient for the number of requests you're trying to handle, because a Map data structure is pretty much as low level as it gets in Java.

RedShift
  • 287
  • 1
  • 6
  • 17
  • 1
    I think it also depends on the pattern of calling the cache. For instance, if I'm using the Guava cache to cache blocks of a file, and I hit the same block over and over again, the overhead of the cache lookups themselves form around 90% of the work the computer is doing. (And yes, I see this from actual profiling.) A trivial ThreadLocal to take care of the common case of hitting the same key more than once in a row, and the performance jumps up 10x. – Hakanai Oct 11 '18 at 03:02