javakotlinmicronaut

Micronaut is slow for the first request after startup, after that its not


I have a doubt regarding micronaut with kotlin, I created a simple api with two routes, one is for hello world and another one is for returning the received query params, when I hit any request to the web service for the first time after startup it takes around 100-200ms, but any requests after the first one takes around 10 ms. Also, request for any invalid endpoints too behaves the same, first invalid endpoint will have a spike in response time but the successive requests for unique invalid endpoints won't take long time. I have already searched it on the internet, but couldn't find useful reference. Your help would be highly appreciated :)

Video link

// micronaut controller
package com.example.controller

import io.micronaut.http.HttpRequest
import io.micronaut.http.HttpResponse
import io.micronaut.http.MediaType
import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get
import io.micronaut.http.annotation.Produces

@Controller("/api")
class HelloController {

    @Get
    @Produces(MediaType.TEXT_PLAIN)
    fun hello() = "Hello world"

    @Get("/params")
    fun getHttpParams(request: HttpRequest<*>): HttpResponse<*> {
        val params = request.parameters.asMap()
        return HttpResponse.ok(params)
    }

}

I built it using gradle shadowJar task and ran using the below command.

./gradlew optimizedJitJarAll
java -jar myapp-0.1-all-optimized.jar
  1. first request after server startup to any route takes 200ms after that every route takes around 10 ms, same with 404 routes
  2. I can confirm, no caching is involved here. I tested it with browser developer tools and rest api clients, every result is the same.

Solution

  • This is probably because of either JIT compilation, classloading and initialization, and other JVM warmup-related things such as internal optimizations and adjustments, which particularly happen during the first few requests.

    These things improve performance for subsequent requests but can cause performance issues at first.

    There's a general way to solve this, using something called Crac.