0

I just added Memcached as my second layer cache for Hibernate. Performance actually took a significant hit after installing the cache. All queries are slower. I realized that the reason is probably due to most of my queries aren't based on id, so second layer cache is not being used.

My question is shouldn't non id-based queries just go straight to the database without ever hitting the cache? Aka, the decision making of whether the query is "cache appropriate" be determined prior to hitting the cache? If so, shouldn't performance be faster?

Luke
  • 1,053
  • 2
  • 16
  • 26
  • (i) Maybe the issue is with the fact that Memcached is a remote process, to which Hibernate needs to talk over the network. An in-memory 2LC, such as Infinsipan might help speed up. (ii) Not sure your configuration, but biggest gain comes from caching most read-only entites. Query cache works in very specific use cases where there are no updates to entities. You should start with entity caching, and think which entities to cache. – Galder Zamarreño Sep 02 '13 at 13:05
  • Makes sense, I actually do have entity caching enabled as well. It's just that all my joins queries are taking much longer. – Luke Sep 03 '13 at 15:58
  • Disable query caching (if enabled), and use in-memory Infinispan based 2LC, see if that helps speed up. Some examples on how to use Infinispan 2LC can be found [here](https://github.com/galderz/secondlc) – Galder Zamarreño Sep 09 '13 at 11:32

1 Answers1

0

When I was checking Hibernate code, it looked like Hibernate cannot reuse cache when using HQL queries (it didn't have compiler from HQL to their caching mechanism).

I can recommend you rather use fjorm instead of Hibernate. Disclaimer: I'm a founder of fjorm.

Mladen Adamovic
  • 3,071
  • 2
  • 29
  • 44