Stack Overflow Asked by Panossa on November 10, 2021
so this question has been asked a few times already but seems like no one answered it in a way that could help me. I’m currently making a backend for a simple application handling product data. It’s not even with JSP, just a plain Rest backend. Using RestControllers by Spring.
The "problem" is: the first request after startup takes way longer to get an answer from the server than all others. (I’m just testing with Postman with a simple JPA User Entity)
Some things to consider:
How do I make the first request faster / how do I force Spring to actually initialize everything before the first request comes in?
Some code anyways:
User.java:
@Entity
@Table(name = "users")
@Data
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
@NonNull
private String firstName;
@NonNull
private String lastName;
@NonNull
@OneToOne(cascade = CascadeType.ALL)
private Address billingAddress;
//a bit more. a list and another address
}
UserController.java:
@RestController
@RequestMapping("users")
public class UserController {
private final UserRepository userRepository;
@Autowired
public UserController(UserRepository userRepository) {
this.userRepository = userRepository;
}
@GetMapping()
public List<User> getAllUsers() {
return (List<User>) userRepository.findAll();
}
//more stuff
}
You answered the question yourself. A lot of stuff in Spring is lazy init for performance and resource consumption optimizations. Is a user really going to even notice that ONE request took 140ms vs 10ms. Probably not. And mind you, that's not one request per user, that's one request per init path per start up.
All that being said... you also answered how to "fix" it. After you deploy (which I assume is automated through CI/CD), you submit a request or requests (part of your CI/CD) that will trigger the initialization down all the paths you need. I.e. if you have 5 db connections, you might need to submit 5 requests to initialize everything.
This is perfectly acceptable and is a process known as "warming up the system".
Answered by SledgeHammer on November 10, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP