Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Boost::Beast for CPU-heavy REST APIs, Should I use Async or Sync way to implement them to expect the better latency?

I'm trying to use boost::beast to implement a web service providing some REST APIs. These APIs are CPU-heavy, almost no disk or db I/O. My goal is to optimize for latency with OK throughput. Should I use sync or async way to implement them?

Thanks!

like image 908
NonStatic Avatar asked Oct 23 '25 12:10

NonStatic


2 Answers

Generally speaking, when you are doing I/O-intensive work with little to no CPU-overhead, non-blocking or async is best. When the operations are, however, CPU-intensive, a threaded model tends to make more sense.

The reason for this is simple: It's usually a bad idea to block the event loop for longer periods of time - as would happen when using an asynchronous model for CPU-heavy computations.

When you start blocking the event loop, things like timers don't behave like they should, since they can only trigger once you return control to the event loop. This is usually not what you want.

like image 182
Martijn Otto Avatar answered Oct 26 '25 01:10

Martijn Otto


If you want timeouts, you have no choice but to use the asynchronous APIs provided by Boost.Beast / Boost.Asio / Asio / Networking TS.

like image 40
Vinnie Falco Avatar answered Oct 26 '25 02:10

Vinnie Falco