Use case for thenRunAsync

56 views
Skip to first unread message

Liu Haibin

unread,
Mar 5, 2020, 9:07:58 PM3/5/20
to lettuce-redis-client-users
I see the following explanation for thenRunAsync. But I still did not get it. I thought thenRun is already async. Can anyone help me here or give an example for it? Many thanks.
 
lettuce futures are completed on the netty EventLoop. Consuming and chaining futures on the default thread is always a good idea except for one case: Blocking/long-running operations. As a rule of thumb, never block the event loop. If you need to chain futures using blocking calls, use the thenAcceptAsync()/thenRunAsync() methods to fork the processing to another thread. The …​async() methods need a threading infrastructure for execution, by default the ForkJoinPool.commonPool() is used. The ForkJoinPool is statically constructed and does not grow with increasing load. Using default Executors is almost always the better idea.
 

Krishna

unread,
Mar 9, 2020, 2:53:44 AM3/9/20
to lettuce-redis-client-users
In general, this is applicable for any EventLoop (each loop runs in one main thread bound to a core) based systems like NodeJS as well, the result of the execution or callback or future is completed in the main EventLoop thread, so, if there is a compute intensive task that is going to do an expensive operation or a blocking IO, then you can use the Async methods to force the execution in a separate thread pool, this is the opposite thinking of thread per request model.

https://nodejs.org/uk/docs/guides/dont-block-the-event-loop/ may explain why you should not block the event loop?

Krishna

unread,
Mar 9, 2020, 3:00:44 AM3/9/20
to lettuce-redis-client-users
In our case of netty redis client usage, we pushed our expensive operation of decompressing/deserialization/decryption and other logic we added in Netty Codec was already happening in the IO worker thread pool already instead of running it after getting the raw payload returned in future and running this logic in event loop or separately in worker threads using .thenAsync.
Reply all
Reply to author
Forward
0 new messages