TransWikia.com

Why are Spring Rest services slow on the first request?

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:

  • it’s probably not a database problem in itself since it clearly just initializes something upon first incoming request instead of on startup
  • In the log, it says "Initializing Spring DispatcherServlet ‘dispatcherServlet’" when the first actual request comes in (via Postman).
  • If I pull all users from the database (which currently is only one user), the first request after startup takes 140ms (according to Postman). After that, the same request needs always 10ms at max.
  • There’s a flag some answer to a similar question suggested: spring.mvc.servlet.load-on-startup=1. Though this only removes the logging (of the initialization of the DispatcherServlet) mentioned above.
  • It seems like this is standard behavior which has nothing to do with how I actually coded my entities and/or RestControllers.

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
}

One Answer

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

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP