Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Optimization] Provide option to reuse Netty's selectors to handle requests #3577

Open
fwbrasil opened this issue Mar 7, 2024 · 2 comments
Labels

Comments

@fwbrasil
Copy link
Contributor

fwbrasil commented Mar 7, 2024

Tapir version: 1.9.11

Scala version: 3.3.3

Describe the bug

A common strategy to boost the performance of HTTP servers in benchmarks is to reuse Netty's event loop selectors to handle requests directly in the thread that receives the payload. This avoids the overhead of switching the execution to another thread and, once the request finishes processing, writing back to the channel is cheaper. If another thread handles the request, Netty also has to use a more expensive mechanism to pass the response back to the selector.

This mode of execution is something that makes sense only for extremely low-latency endpoints with very little CPU usage, which is common for benchmarks. In real production workloads, it's generally better to ensure the event loop is always available to handle payloads otherwise, there can be a large latency cost. We had remarkable latency reductions at Twitter by just switching the request handling to another thread pool. The only services that didn't perform well were the ones that did simple in-memory lookups.

How to reproduce?

I've added an option to the Tapir integration in Kyo to disable the forking. It's possible to check the performance by setting it on/off: https://github.com/getkyo/kyo/blob/main/kyo-tapir/src/main/scala/kyo/server/NettyKyoServerOptions.scala#L17

@fwbrasil fwbrasil changed the title [Optimization [Optimization] Provide option to reuse Netty's selectors to handle requests Mar 7, 2024
@kciesielski
Copy link
Member

Thanks, I wasn't aware of this possibility. It sounds like something that we might want to configure on the endpoint level instead of having a global forkExecution setting. This would require changes in a few places to properly propagate it, wdyt?

@fwbrasil
Copy link
Contributor Author

Good point! I normally see this kind of configuration globally for a server but having endpoint-level control of the behavior could be very useful since typically only a few endpoints can benefit of the optimization in production.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants