Proper pattern for connectPubSub() with multiple listeners

152 views
Skip to first unread message

Tony H

unread,
Dec 18, 2020, 11:43:14 PM12/18/20
to lettuce-redis-client-users
Hello,

What is the proper pattern for using connectPubSub() with multiple listeners and multiple channels?

For example, if I have a use case that would like to subscribe to two channels, should we: 

1. Create a single connection by calling connectPubSub() once, and add logic to direct each message received to the proper listener.
2. Create two connections by calling connectPubSub() twice, where each connection will have their own single distinct listener.

Tony H

unread,
Dec 19, 2020, 12:03:24 AM12/19/20
to lettuce-redis-client-users
I forgot to add this in the original post, but my example would be calling subscribe() twice to different channels.

A 3rd option: Create a single connection by calling connectPubSub() once, add two different listeners (with each listener having logic in there to check the channel they are interested in)

Mark Paluch

unread,
Dec 22, 2020, 4:43:21 AM12/22/20
to lettuce-redis-client-users
Hi Tony, 

A single connection with 0-N listeners is fine. The number of listeners is up to you, depending on your channel/pattern structure and how you want to handle all this in your code.

The single connection makes sense (with multiple calls to subscribe) since many connections typically don't improve on performance. Note to avoid blocking calls in your listeners. If you need to interact with a database, Redis, …, then offload the listener callback onto a thread/thread pool since the listener gets called on an EventLoop thread.

Cheers,
Mark

Tony H

unread,
Dec 22, 2020, 11:35:52 AM12/22/20
to lettuce-redis-client-users
Perfect, thank you for the response Mark! The note about blocking calls helped solve an issue I was working on as well.
Reply all
Reply to author
Forward
0 new messages