Redis not-persistent Pub/Sub Added #42
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Motivation / Background
We use Watermill with Redis to enable communication between our NestJS backend and the system drivers running on an embedded Raspberry Pi–based system.
We chose the PUBLISH/SUBSCRIBE mechanism instead of XADD with Streams for two main reasons:
The second point is crucial in our setup: Redis receives on average one message per second. Persisting messages would quickly increase memory usage to unsustainable levels and could overwhelm the system.
Details
To make the implementation more explicit and easier to understand, I introduced two separate packages:
redisstream
redispubsub
Both packages implement the same file structure, structs, methods, and tests.
The
redispubsub
package provides aSubscriber
andPublisher
based directly on RedisSubscribe
andPublish
. Since Redis Pub/Sub does not persist messages, we need to ensure that the subscriber is actively listening to the channel before returning. Otherwise, messages could be lost.The key logic for this is in
subscriber.go
(around line 121):