0

I have a Spring 4.3.6 App running on backend with Angular 2 on frontend. Every time after the server is initialized, the first request the server responds have a high TTFB (time to first byte), if compared with the subsequent requests.TTFB delay on the first request

I verified that right on the first request I got a high peak in my computer's processor. Below is my login method in my UserController:

@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login(@RequestBody Map<String, String> json) throws ServletException {

    if (json.get("stUserLogin") == null || json.get("stUserPassword") == null) {
        throw new ServletException("...");
    }

    String userLogin = json.get("stUserLogin");
    String userPassword = json.get("stUserPassword");

    UserBean userBean = userService.findByUserLogin(userLogin);

    if (userBean == null) {
        throw new ServletException("...");
    }

    if (!userPassword.equals(userBean.getStUserPassword())) {
        throw new ServletException("...");
    }

    return Jwts.builder().setSubject(userLogin).claim("roles", "user").setIssuedAt(new Date())
            .signWith(SignatureAlgorithm.HS256, "secretkey").compact();
}

This is my UserService's find user method:

@Override
public UserBean findByUserLogin(String stUserLogin) {
    return userDAO.findByStUserLogin(stUserLogin);
}

And finally my DAO's method:

UserBean findByStUserLogin(String stUserLogin);

On the first request, I verified a high amount of milliseconds expended in the Service's method, but I still have no clue about the reason behind all this.

It is worth mentioning that this happens with every mapped method, but with different scales.

I don't know if I provided enough information about this issue. Any help will be well received, thank you in advance.

Andre Knob
  • 821
  • 7
  • 7
  • A new database connection being opened? For subsequent requests it may be taken from the pool which is faster. – Roman Puchkovskiy Mar 31 '17 at 19:12
  • That could be it, but I don't know how to be sure. I'm using MySQL and the connection configuration is defined in my application.properties file. Is there a way to ensure that this connection will be already opened by the server's start? – Andre Knob Mar 31 '17 at 19:29
  • One way would be to change a real DAO talking to a real pool+database with a mock DAO which just returns some data sane enough to not crash your application and then test the resulting application. – Roman Puchkovskiy Mar 31 '17 at 19:32
  • With a mock DAO the TTFB was reduced from 700ms to around 100ms on the first call, it seems this is the way. Only need to know how to solve it now. – Andre Knob Mar 31 '17 at 19:57
  • What connection pool do you use? For DBCP https://commons.apache.org/proper/commons-dbcp/configuration.html you could play with `initialSize`/`minIdle` configuration variables to make the pool create some connections on startup. – Roman Puchkovskiy Mar 31 '17 at 20:02
  • Yes, i'm using DBCP. I defined this variables in my application.properties file, as spring.datasource.dbcp2.initial-size = 6 and spring.datasource.dbcp2.min-idle = 6. But the problem still persists. – Andre Knob Mar 31 '17 at 20:34
  • http://stackoverflow.com/questions/14581201/how-to-preinitialize-dbcp-connection-pool-on-startup They advise the following: 'Try adding init-method="getLoginTimeout" to your bean' (the pool bean, I guess); that should force the pool initialization. – Roman Puchkovskiy Apr 01 '17 at 06:33

0 Answers0