'Too many mysql db call issue with spring boot rest service

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


Solution 1:[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).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1