https://kotlinlang.org logo
#ktor
Title
# ktor
j

Janez Kolar

10/06/2020, 5:51 PM
Hi all, I am quite new to backend and Ktor development, but look very interesting and easy to learn. Now I came to few questions. I have server based on Netty engine. Is it possible to hande 10k REST request per second. Server return json with cca list of 50 objects (like Person(name, surname, age, gender)). List is in memory cache. How and where should I configure Netty to achive high performance? I created:
Copy code
fun main(args: Array<String>): Unit = io.ktor.server.netty.EngineMain.main(args)
val demo: Demo = Demo()
val compute = newFixedThreadPoolContext(10, "compute")

@Suppress("unused") // Referenced in application.conf
@kotlin.jvm.JvmOverloads
fun Application.module(testing: Boolean = false) {

     demo.generateflyingDemo()

    GlobalScope.launch(Dispatchers.Default) {
        while(isActive)
        {
            demo.demoMoving()
            delay(1000)
        }
    }

    install(Compression) {
        gzip {
            priority = 1.0
            minimumSize(256)
        }
        deflate {
            priority = 10.0
            minimumSize(1024) // condition
        }
    }
    install(CORS) {
        method(HttpMethod.Options)
        method(HttpMethod.Put)
        method(HttpMethod.Delete)
        method(HttpMethod.Patch)
        method(HttpMethod.Get)

        header(HttpHeaders.Authorization)
        header(HttpHeaders.Origin)
        header(HttpHeaders.XRequestId)
        allowCredentials = true
        anyHost() // @TODO: Don't do this in production if possible. Try to limit it.
    }
    install(WebSockets) {
        pingPeriod = Duration.ofSeconds(15)
        timeout = Duration.ofSeconds(15)
        maxFrameSize = Long.MAX_VALUE
        masking = false
    }
    install(Authentication) {
        basic("myBasicAuth") {
            realm = "Ktor Server"
            validate { if (it.name == "test" && it.password == "password") UserIdPrincipal(it.name) else null }
        }
    }
    install(ContentNegotiation) {
        gson {
        }
    }

    routing {

     
        get("rc/activePeople") {

            call.respondHandlingActivePeople()
        }


        get("/") {
            call.respondText("HELLO WORLD!", contentType = ContentType.Text.Plain)
        }

    }
}


private suspend fun ApplicationCall.respondHandlingActivePeople() {
   withContext(compute) {
        respond(demo.activePeople(age = 15))
    }
}
j

jorge.rego

10/08/2020, 8:06 AM
You may need to externally define a load balancer with multiple instances of your backend to handle that performance. Not sure if Netty has any option to do what you say, but for sure you should try to scale up at a proxy/load balancer level.