0

I am developing one rest application with spring boot and mysql. Currently I am still in development phase, hence I have not enabled any caching as such. I future as the load increases, I will use memcached(as we already use memcached for other apps in our org) or redis to cache the resulsts of db queries. Right now I have not even enabled hibernate second level caching, so I am seeing too many db calls. I would like to enable caching preferably ehcache to reduce the db calls.

Mine is a rest api, currently we are running it in 3 pods in aws.

What sort of caching should I enable (not memcached or redis)

  1. Spring cache (ehcache)
  2. Hibernate second level cache

My main objective right now is to reduce number of db calls.

should I use only hibernate second level cache or spring cache ?

Here is my default configs:

  jpa:
    open-in-view: false
    properties:
      hibernate.jdbc.time_zone: UTC
      hibernate.id.new_generator_mappings: true
      hibernate.connection.provider_disables_autocommit: true
      hibernate.cache.use_second_level_cache: false
      hibernate.cache.use_query_cache: true
      hibernate.generate_statistics: false
      # modify batch size as necessary
      hibernate.jdbc.batch_size: 25
      hibernate.order_inserts: true
      hibernate.order_updates: true
      hibernate.query.fail_on_pagination_over_collection_fetch: true
      hibernate.query.in_clause_parameter_padding: true
      hibernate.dialect: org.hibernate.dialect.MySQL5Dialect
      spring.jpa.show-sql: true
Tuhin Subhra Mandal
  • 473
  • 1
  • 5
  • 15
  • What is too many queries, isn't this a result of how you have modelled things in your JPA Model in the first place? Or relying on your JPA model and Java to prepare a response instead of a proper dedicated query that does this in 1 go? Throwing caching at it might not even be the right solution in the first place due to how you are approaching things. – M. Deinum Feb 08 '22 at 08:26

1 Answers1

1

It depends on your needs, if you want to cache an entity that is often accessed by your unique identifier (EntityManager.find()) and rarely edited, I think that it is a good candidate for L2C.
I tend to use Spring Cache when I build responses from other long service calls or projections from DB.

By the way you will need an implementation for L2C too (which can be EhCache).

Mickaël B.
  • 325
  • 4
  • 14
  • thanks, I also realised after reading blogs that probably I need to use both. My data is not edited 95% times. We insert data to couple of tables, but does not happen frequently. Do you mind sharing any example that includes springboot+ehcache both as response and entity level cache ? – Tuhin Subhra Mandal Feb 08 '22 at 08:26
  • Unfortunately I have no example containing twice that I can share to you. But it's pretty "easy" to set-up, the L2C is a little bit trickier. The baeldung articles explains well how they work https://www.baeldung.com/hibernate-second-level-cache https://www.baeldung.com/spring-cache-tutorial – Mickaël B. Feb 08 '22 at 08:31
  • Thanks. I have followed the above guide and enable caching at the entity level. However statistics shows 0 hits. 167962 nanoseconds spent preparing 2 JDBC statements; 4124766 nanoseconds spent executing 2 JDBC statements; 0 nanoseconds spent executing 0 JDBC batches; 0 nanoseconds spent performing 0 L2C puts; 0 nanoseconds spent performing 0 L2C hits; 0 nanoseconds spent performing 0 L2C misses; – Tuhin Subhra Mandal Feb 08 '22 at 09:22
  • Your problem is too wide for the comment section. Please create a new question with more information. – Mickaël B. Feb 08 '22 at 09:29
  • Please check this post: https://stackoverflow.com/questions/71031686/0-nanoseconds-spent-performing-0-l2c-hits – Tuhin Subhra Mandal Feb 08 '22 at 09:46