Redis Streams: At-Most-Once vs. At-Least-Once
When using Redis Streams, you are already moving away from the "fire-and-forget" nature of Pub/Sub toward a more durable architecture. However, the way your application handles communication failures with the Redis server defines the final reliability of the message.
Below is a detailed guide on implementing both patterns using a while loop for retries.
1. At-Most-Once (Fire and Forget)
In this pattern, the application attempts to add the message to the stream exactly once. If the network is down or Redis is busy, the message is discarded. There is no retry logic.
Best For: Logging, non-critical metrics, or high-frequency updates where the "latest" data is more important than "all" data.
2. At-Least-Once (Persistent & Reliable)
This pattern uses a while loop to ensure that the message is successfully acknowledged by the Redis server. If an error occurs, the publisher retries until the message is confirmed or the maximum retry limit is reached.
Best For: Financial transactions, order processing, and critical system events.
Summary Comparison
| Strategy | Logic | Redis Command | Result on Failure |
| At-Most-Once | No Loop / Single Call | XADD | Message is lost if Redis is unreachable. |
| At-Least-Once | while Loop + Retries | XADD | Message is eventually stored (may cause duplicates). |
Important Note on Duplicates
When you use the At-Least-Once strategy with retries, you may accidentally store the same message twice if the network fails after Redis saved the message but before you received the response.
Solution: Always include a unique Idempotency Key (like a UUID) in your message body so that your Consumer can detect and ignore duplicate entries.