Spring Beans related to Inbuilt and Custom validators (ConstraintValidator) are initialized ONLY during the first request. This is causing slowness like ~5seconds in an environment where the CPU is < 100m and causing the request to timeout.

Env:

Jdk 22 Spring Boot v3.3.2 Docker containers on AWS EKS

[2m2024-08-20T21:44:57.194+04:00[[TRACE[[7332[[2m---[[2m[web-webflux-trace] [nio-8080-exec-1][[2m[66c4d6192e5a47bbf7d8544f82d25b53-f7d8544f82d25b53] [0;39m[36mo.s.b.f.s.DefaultListableBeanFactory    [[2m:[Creating instance of bean 'org.hibernate.validator.internal.constraintvalidators.bv.NotBlankValidator'
[2m2024-08-20T21:44:57.194+04:00[[TRACE[[7332[[2m---[[2m[web-webflux-trace] [nio-8080-exec-1][[2m[66c4d6192e5a47bbf7d8544f82d25b53-f7d8544f82d25b53] [0;39m[36mo.s.b.f.s.DefaultListableBeanFactory    [[2m:[Finished creating instance of bean 'org.hibernate.validator.internal.constraintvalidators.bv.NotBlankValidator'
[2m2024-08-20T21:44:57.194+04:00[[TRACE[[7332[[2m---[[2m[web-webflux-trace] [nio-8080-exec-1][[2m[66c4d6192e5a47bbf7d8544f82d25b53-f7d8544f82d25b53] [0;39m[36mo.s.b.f.s.DefaultListableBeanFactory    [[2m:[Creating instance of bean 'org.hibernate.validator.internal.constraintvalidators.bv.PatternValidator'
[2m2024-08-20T21:44:57.194+04:00[[TRACE[[7332[[2m---[[2m[web-webflux-trace] [nio-8080-exec-1][[2m[66c4d6192e5a47bbf7d8544f82d25b53-f7d8544f82d25b53] [0;39m[36mo.s.b.f.s.DefaultListableBeanFactory    [[2m:[Finished creating instance of bean 'org.hibernate.validator.internal.constraintvalidators.bv.PatternValidator'

Comment From: bclozel

The logs you have shared seem to show that the constraint validator instantiation is quite fast. This doesn't support your assessment. Please advise.

Comment From: syedyusufh

Hi @bclozel , the logs shared were a snippet and from Windows OS which has 16 Core CPU and RAM is a shared 4 GB.

The mentioned slowness is observed in Linux OS using Docker containers where allocated CPU < 100m and RAM is 500MB. Note: Application functionality is not CPU intensive and application works great with < 100m CPU. Only the first request takes ~5+ seconds highlighting all the validation related ConstraintValidator bean created

Comment From: bclozel

0.1 CPU is very little for a JVM application. I would suggest provisioning more resources, or manually triggering a constraint validation during application startup to eagerly initialize the hibernate validation stack. There's not so much we can do here so I'll close this issue.

Comment From: syedyusufh

manually triggering a constraint validation during application startup to eagerly initialize the hibernate validation stack

@bclozel can you please suggest if there is a way to perform eager initialization of the entire hibernate validator stack through any bean definition?

Comment From: bclozel

@syedyusufh maybe reach out to the hibernate validation community. This lazy init behavior is not part of Spring Framework.

Comment From: syedyusufh

Sorry, one last ask. Same is the behavior seen with custom ConstraintValidator through implements ConstraintValidator<?,?> interface definition. Can this atleast be eager initialized?

Comment From: snicoll

@syedyusufh please review Brian's suggestion.

Comment From: syedyusufh

@syedyusufh maybe reach out to the hibernate validation community. This lazy init behavior is not part of Spring Framework.

Raised with Hibernate-Validator project team, please see their comments on the below link. Kindly suggest if there is a possibility to load the validator beans eagerly, sorry.

https://hibernate.atlassian.net/jira/software/c/projects/HV/issues/HV-2030?jql=project%20%3D%20%22HV%22%20ORDER%20BY%20created%20DESC

Comment From: bclozel

Can you ask this question on StackOverflow and leave the link here so we can have a look? This issue tracker is for enhancement requests and bugs only.

Comment From: bclozel

@syedyusufh I haven't seen your question on StackOverflow yet. Can you point me to it please? Since this is not technically a bug in Spring Framework, we like to keep this knowledge on StackOverflow because that's where our community seeks for help.

Comment From: syedyusufh

Hi @bclozel , sorry. It is here https://stackoverflow.com/questions/78893812/spring-boot-eager-initialize-constraintvalidator-beans