Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the hiredis Redis library create its own thread for async callbacks

I am using Redis in a multithreaded environment, and have a question as to how it operates. I am using the hiredis c library in my c++ application.

My question is this: If I am using the asynchronous mode when a callback is fired will the callback be handled in another thread created by the Redis client? As in will the thread where the call was created not be affected by the handling of the callback? Thanks!

like image 980
44701 Avatar asked Feb 16 '16 07:02

44701


1 Answers

Redis client does not create any additional clent threads, and works in existing thread.

The Redis worked in another (main) process. The redis API which you use worked in your local process and use interprocess communication to main process. Async request mean that your process or thread put task to another, and after that may do any other yout task or wait event. Some time later async reply arrived to your application and available for use. You application must use https://en.wikipedia.org/wiki/Event_loop or any async management system that notify you by calling callback to handle event (in this case redis answer).

Asyncronous architecture mean that you runing event loop that call callback handlers for each event. When callback called you can create number of asynchronous tasks. Once task created, it supposed than when task done or error occur the callback event handler will be called. Callback may be assigned to start app or to new web connection appear. When callback called you can create the redis task, and later result event callback will be called. All things in current thread. I нou have multiple thread, reasonable to expect that you have one event loop per each thread.

Single threaded nature of Redis: http://redis.io/topics/latency#single-threaded-nature-of-redis

The client socket is put in non-blocking state since Redis uses multiplexing and non-blocking I/O. http://redis.io/topics/clients This mean that your client will never be blocked.

From Redis 2.4 threads in Redis used only in order to perform some slow I/O operations in the background, mainly related to disk I/O, but this does not change the fact that Redis serves all the requests using a single thread.

like image 185
oklas Avatar answered Nov 01 '22 00:11

oklas