apache / brpc

brpc is an Industrial-grade RPC framework using C++ Language, which is often used in high performance system such as Search, Storage, Machine learning, Advertisement, Recommendation etc. "brpc" means "better RPC".
https://brpc.apache.org
Apache License 2.0
16.56k stars 3.98k forks source link

Brpc server side async or not recommendation! #2755

Closed qiaobaochen closed 2 months ago

qiaobaochen commented 2 months ago

Hi, I am beginner in brpc, I decide to use brpc as a database proxy in our scenario, after I read the doc of brpc, I still cannot decide use async server or sync server in our situation.

detail:

servers get data from file or database (IO-bound service), and send data to client side through brpc.

I was wondering that if I start a background bthread bthread_start_background and run file-io requrest, is this bthread be blocked? and most important that is this pthread (which bthread is on) will be blocked ?

thanks

jamesge commented 2 months ago

The bRPC server is asynchronous at its core, a "sync service" is just a service calling "done->Run()" before the service callback ends. You should look at interfaces of the DB client first:

qiaobaochen commented 2 months ago

The bRPC server is asynchronous at its core, a "sync service" is just a service calling "done->Run()" before the service callback ends. You should look at interfaces of the DB client first:

  • If the db client has async interfaces (allowing you to pass a callback to be called when db access is done), you could call server's done->Run() in the callback. It's also possible to wrap the async DB client into a bthread-synchronous interfaces.
  • If the db client blocks (underlying pthread) until the db access is done, and the DB access is expected to last long(e.g. several seconds), you may run the DB client in separate pthreads. Creating extra bthreads in bRPC server does not help, because all bthreads are mapped to the same pool of pthread workers used by bRPC.

that's very clear, thanks a lot!