Spring Boot cache read and scheduled evict same time - spring

I am currently developing an application that uses Spring Cache.
Currently it's schedually evicted in every 5 mins, but it's really high traffical application and it's possible that while deleting the cache any user can read the cache in same time.
I could call cacheable method inside the eviction method but are there any better practices for that behavior.

Related

Usage of micrometer-registry-prometheus slow down my Spring Boot application

I have Spring Boot application 2.5.7 where I set up a micrometer to scrape metrics
runtimeOnly("io.micrometer:micrometer-registry-prometheus")
When I make a request locally http://localhost:8081/actuator/prometheus
There are no performance problems with my application
But when I make a request to the actuator on the server with a high load
https://myserver:8081/actuator/prometheus
it returns a lot more data in response and it also slows down all request that is currently running on my server.
The problem appears even after one request to /actuator/prometheus
Is there any way to optimize the micrometer work(while returning the same ammount of metrics), so it will not slow down my application?
Without sufficient data it is hard to give a recommendation. If the slowness is due to insufficient memory/garbage collection, try increasing the memory of your application.
Reviewing the metrics being returned may also give you some ideas, for example if you have a high thread count, I think there is a pause when Micrometer iterates over the thread statuses. You could look into disabling that metric.

Caffeine Cache implementation manually

Hi I am planning to use Guava Cache or Caffeine Cache in my application as below way -
On application start DB call initiated and store the result into cache. I don't want to use expiration or recycling feature of above cache means data should present in cache all the time until application restart.
How can I implement any of this cache in my Java Spring Boot Application. I do not want to use Spring's Caching functionality.
I want to update or add the values in cache on certain condition.

Is there any best practice to make audit of Spring Rest application?

I have to improve performance of spring boot app, which is quite classical rest API + hibernate + postgres. we have 250k active users and want to extract some requests to be on slave balanced instances, and probably cache some data. For now i have only suspect that some requests need to be cached, But i want to make some audit and report, that some request called so many times that we should use other strategy, or "this" sql request fired every rest call so it's eat a lot DB lifetime which could be worked out using cache. Is there any best practice to make this kind of audit/analytic? Request-rate, request rate per user, SQL rate per request, SQL rate per user per request, and some other metrics
Spring Boot's metrics should give you a good starting point. The Spring MVC metrics should allow you to identify if there are certain types of request that are taking longer than others. Depending on how you are accessing your database, there are also DataSource metrics, Hibernate metrics, and Spring Data Repository metrics (new in Spring Boot 2.5) that may be of interest.
These metrics will be for your application as a whole rather than per-user. With over 250k active users, tagging metrics on a per-user basis almost certainly won't be practical. Unless you suspect that there are specific users that are problematic, I would at least start with the application-wide metrics and see how things go.

communication between spring instances behind a load balancer

I have a few instances of Spring apps running behind a load balancer. I am using EHCache as a caching system on each of these instances.
Let's say I receive a request that is refreshing a part of the cache on one instance. I need a way to tell the other instances to refresh their cache (or to replicate it).
I'm more interested in a solution based on Spring and not just cache replication and that's because there are other scenarios similar with this one that require the same solution.
How can I achieve this?
There is no simple Spring solution for this. Depends on the requirements. You can use any kind of PubSub like a JMS topic to notify your nodes. This way the problem can be that you cannot guarantee consistency. The other nodes can still read the old data for a while. In my current project we use Redis. We configured it as cache with Spring Data Redis and theres no need to notify the other nodes since the cache is shared. In non cache scenarios we also use redis as a PubSub service.

How to clear spring webflow scope data on exiting the application by simply closing the browser?

Details:-
Application is developed using Spring webflow 2.1.1 and Spring core framework.
Spring webflow acts as a MVC controller for application
Application is storing lot of data in spring webflow flowscope variables which is rendered on UI using jsf richfaces.
For same application in web.xml session timeout is configured to 120 minutes.
Problem is when browser is closed in between of webflow OR http session is timed out , what happens to data stored in spring webflow flowscope ? It is observed that end state is not called in both scenario. Does that data reside on Java Heap ?
Also as per memory analyser tool, 70% of heap is consumed by webAppServletContext.
Currently application has out of memory issues even after increasing the heap to 3GB.
Spring WebFlow uses HttpSession to store all its contextual information. As long as Servlet Container invalidates/destroys user associated HttpSession, all Spring WebFlow stored variables become destroyed too (well, can be garbage collected).
If you're having memory problems check your Servlet Container settings. Some have mechanisms like a periodic job to clean expired HttpSession, some others clean upon an invalid request, etc...
Anyway, you'll unlikely reach an OutOfMemoryError because of the described mechanisms, as it will show defective Servlet implementation.

Resources