You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/commands/xadd.md
+5-3Lines changed: 5 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -187,11 +187,13 @@ Prevents the creation of a new stream if the key does not exist. Available since
187
187
Enables idempotent message processing (at-most-once production) to prevent duplicate entries. Available since Redis 8.6.
188
188
189
189
-`IDMPAUTO producer-id`: Automatically generates a unique idempotent ID (iid) for the specified producer-id. Redis tracks this iid to prevent duplicate messages from the same producer-id.
190
-
-`IDMP producer-id idempotent-id`: Uses the specified idempotent-id for the given producer-id. If this producer-id/idempotent-id combination was already used, the command returns the idempotent-id of the existing entry instead of creating a duplicate.
190
+
-`IDMP producer-id idempotent-id`: Uses the specified idempotent-id for the given producer-id. If this producer-id/idempotent-id combination was already used, the command returns the ID of the existing entry instead of creating a duplicate.
191
191
192
192
The producer-id identifies the source of the message, while the idempotent-id ensures uniqueness within that producer-id's message stream. Redis maintains an internal map of recent producer-id/idempotent-id combinations to detect and prevent duplicates.
193
193
194
-
Use [`XCFGSET`]({{< relref "/commands/xcfgset" >}}) to configure how long idempotent IDs are retained (`DURATION`) and the maximum number tracked per producer (`MAXSIZE`).
194
+
Both modes can only be specified when the entry ID is `*` (auto-generated).
195
+
196
+
Use [`XCFGSET`]({{< relref "/commands/xcfgset" >}}) to configure how long idempotent IDs are retained (`IDMP-DURATION`) and the maximum number tracked per producer (`IDMP-MAXSIZE`).
195
197
196
198
See [Idempotent message processing]({{< relref "/develop/data-types/streams/idempotency" >}}) for more information.
197
199
@@ -318,7 +320,7 @@ XADD mystream IDMP producer1 msg1 * field value
318
320
XADD mystream IDMP producer1 msg1 * field different_value
Sets the IDMP (Idempotent Message Processing) configuration parameters for a stream. This command configures how long idempotent IDs are retained and the maximum number of idempotent IDs tracked per producer.
@@ -74,15 +74,15 @@ The name of the stream key. The stream must already exist.
Sets the duration in seconds that each idempotent ID (iid) is kept in the stream's IDMP map. Valid range: 1-86400 seconds. Default: 100 seconds.
79
+
Sets the duration in seconds that each idempotent ID (iid) is kept in the stream's IDMP map. Valid range: 1-86,400 seconds. Default: 100 seconds.
80
80
81
81
When an idempotent ID expires, it can be reused for new messages. This provides an operational guarantee that Redis will not forget an idempotency ID before the duration elapses (unless capacity is reached).
Sets the maximum number of most recent idempotent IDs kept for each producer in the stream's IDMP map. Valid range: 1-10,000 entries. Default: 100 entries.
88
88
@@ -92,18 +92,18 @@ When the capacity is reached, the oldest idempotent IDs for that producer are ev
92
92
93
93
## Behavior
94
94
95
-
- Calling `XCFGSET` clears all existing producer IDMP maps for the stream
96
-
- At least one of `DURATION` or `MAXSIZE` must be specified
97
-
- The stream must exist before calling this command
98
-
- Configuration changes apply immediately to all future IDMP operations
95
+
- Calling `XCFGSET` clears all existing producer IDMP maps for the stream.
96
+
- At least one of `IDMP-DURATION` or `IDMP-MAXSIZE` must be specified.
97
+
- The stream must exist before calling this command.
98
+
- Configuration changes apply immediately to all future IDMP operations.
Copy file name to clipboardExpand all lines: content/develop/data-types/streams/idempotency.md
+50-29Lines changed: 50 additions & 29 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,6 +17,22 @@ weight: 10
17
17
18
18
In Redis 8.6, streams support idempotent message processing (at-most-once production) to prevent duplicate entries when using at-least-once delivery patterns. This feature enables reliable message submission with automatic deduplication.
19
19
20
+
Idempotent message processing ensures that handling the same message multiple times produces the same system state as handling it once.
21
+
22
+
Beginning with Redis 8.6, streams support idempotent message processing (at-most-once production) to prevent duplicate entries when producers resend messages.
23
+
24
+
Producers may need to resend messages under two scenarios:
25
+
26
+
1. Producer-Redis network issues (disconnection and reconnection).
27
+
28
+
If a disconnection occurs after the producer executes `XADD`, but before it receives the reply, the producer has no way of knowing if that message was delivered.
29
+
30
+
1. The producer crashes and restarts.
31
+
32
+
If the producer crashes after calling `XADD` but before receiving the reply and marking a message as delivered, after a restart, the producer has no way of knowing if that message was delivered.
33
+
34
+
In both cases, to guarantee that the message is added to the stream, the producer must call `XADD` again with the same message. Without idempotent message processing, a retry may result in a message being delivered twice. With idempotent message processing, producers can guarantee at-most-once production even under such scenarios.
35
+
20
36
A unique ID, called an idempotent ID or *iid*, is associated with each message that is added to a stream.
21
37
There are two ways to assign iids:
22
38
@@ -31,8 +47,8 @@ For (1), this is the producer’s responsibility, and for (2), Redis will calcul
31
47
Use the [`XADD`]({{< relref "/commands/xadd" >}}) command with idempotency parameters, `IDMP` or `IDMPAUTO`:
32
48
33
49
```
34
-
XADD mystream IDMP producer1 msg1 * field value # producer-provided iid
35
-
XADD mystream IDMPAUTO producer2 * field value # Redis-generated iid
50
+
XADD mystream IDMP producer-1 iid-1 * field value # producer-1 (pid) and iid-1 (iid) are provided manually
51
+
XADD mystream IDMPAUTO producer-2 * field value # producer-2 (pid) is provided manually, Redis provides the iid
36
52
```
37
53
38
54
### Manual mode (`IDMP`)
@@ -61,6 +77,13 @@ XADD mystream IDMPAUTO producer1 * field value
61
77
- Content-based: The same content produces the same iid.
62
78
- Performance: Slightly slower due to hash calculation.
63
79
80
+
For both IDMP and IDMPAUTO, each producer application is required to use the same pid after it restarts.
81
+
82
+
For IDMP, each producer application is responsible for:
83
+
84
+
- Providing a unique iid for each entry (either globally, or just for each pid).
85
+
- Reusing the same (pid, iid) when resending a message (even after it restarts).
86
+
64
87
Here's an illustration of how message processing in Redis Streams works with and without idempotent production:
65
88
66
89
{{< image filename="images/dev/stream/stream-idempotency.png" alt="Idempotent message processing in Redis Streams" >}}
@@ -70,54 +93,53 @@ Here's an illustration of how message processing in Redis Streams works with and
70
93
Configure idempotency settings for a stream using [`XCFGSET`]({{< relref "/commands/xcfgset" >}}):
-`DURATION`: How long (in seconds) to retain iids (1-86400 seconds, the default is 100).
79
-
-`MAXSIZE`: The maximum number of per-producer iids to track (1-10,000 entries, the default is 100).
101
+
-`IDMP-DURATION`: How long (in seconds) to retain iids (1-86400 seconds, the default is 100).
102
+
-`IDMP-MAXSIZE`: The maximum number of per-producer iids to track (1-10,000 entries, the default is 100).
80
103
81
104
### Expiration behavior
82
105
83
106
Idempotent IDs are removed when either condition is met:
84
107
85
-
- Time-based: iids expire after the configured `DURATION`.
86
-
- Capacity-based: Oldest iids are evicted when `MAXSIZE` is reached.
108
+
- Time-based: iids expire after the configured `IDMP-DURATION`.
109
+
- Capacity-based: Oldest iids are evicted when `IDMP-MAXSIZE` is reached. Redis never keeps more than `IDMP-MAXSIZE` iids per pid. In other words, `IDMP-MAXSIZE` is *stronger* than `IDMP-DURATION`.
87
110
88
111
### Determine optimal configuration values
89
112
90
-
`DURATION` is an operational guarantee: Redis will not discard a previously sent iid for the specified duration (unless reaching `MAXSIZE` iids for that producer).
91
-
If a producer application crashes and stops sending messages to Redis, Redis will keep each iid for `DURATION` seconds, after which they will be discarded.
92
-
You should know how long it may take your producer application to recover from a crash and start resending messages.
93
-
`DURATION` should be set accordingly.
94
-
If `DURATION` is set too high, Redis will waste memory by retaining iids for a longer duration than necessary.
113
+
`IDMP-DURATION` is an operational guarantee: Redis will not discard a previously sent iid for the specified duration (unless reaching `IDMP-MAXSIZE` iids for that producer).
114
+
If a producer application crashes and stops sending messages to Redis, Redis will keep each iid for `IDMP-DURATION` seconds, after which they will be discarded.
115
+
You should know how long it may take your producer application to recover from a crash and start resending messages, so you should set `IDMP-DURATION` accordingly.
116
+
If `IDMP-DURATION` is set too high, Redis will waste memory by retaining iids for a longer duration than necessary.
95
117
96
-
**Example**: if a producer crashes, it may take up to 1,000 seconds until it recovers and restarts sending messages. You should set `DURATION` to 1000.
118
+
**Example**: if a producer crashes, it may take up to 1,000 seconds until it recovers and restarts sending messages. You should set `IDMP-DURATION` to 1000.
97
119
98
120
When a producer application retrieves an `XADD` reply from Redis, it usually marks the message as *delivered* in a transaction database or log file.
99
121
If the application crashes, it needs to resend undelivered messages after recovering from the crash.
100
122
Since a few messages may have not been marked as delivered as a result of the crash, the application will likely resend these messages.
101
123
Using iids will allow Redis to detect such duplicate messages and filter them.
102
-
Setting `MAXSIZE` correctly ensures that Redis retains a sufficient number of recent iids.
103
-
If `MAXSIZE` is set too high, Redis will waste memory by retaining too many iids.
124
+
Setting `IDMP-MAXSIZE` correctly ensures that Redis retains a sufficient number of recent iids.
125
+
If `IDMP-MAXSIZE` is set too high, Redis will waste memory by retaining too many iids.
104
126
Usually this number can be very small, and often, even *one* is enough.
105
-
If your application marks messages as delivered asynchronously, you should know how long it may take from the time it retrieved a `XADD` reply from Redis until the message is marked as delivered; this duration is called *mark-delay*. `MAXSIZE` should be set to
127
+
If your application marks messages as delivered asynchronously, you should know how long it may take from the time it retrieved a `XADD` reply from Redis until the message is marked as delivered; this duration is called *mark-delay*. `IDMP-MAXSIZE` should be set to
106
128
107
129
`mark-delay [in msec] * (messages / msec) + some margin.`
108
130
109
-
**Example**: a producer is sending 1K msgs/sec (1 msg/msec), and takes up to 80 msec to mark each message as delivered, `MAXSIZE` should be set to `1 * 80 + margin = 100` iids.
131
+
**Example**: a producer is sending 1K msgs/sec (1 msg/msec), and takes up to 80 msec to mark each message as delivered, `IDMP-MAXSIZE` should be set to `1 * 80 + margin = 100` iids.
110
132
111
133
## Producer isolation
112
134
113
135
Each producer maintains independent idempotency tracking:
114
136
115
137
```
116
-
XADD mystream IDMP producer1 msg1 * field value # Producer 1's tracking
117
-
XADD mystream IDMP producer2 msg1 * field value # Producer 2's tracking (independent)
138
+
XADD mystream IDMP producer-1 iid-1 * field value # producer-1 is tracking
139
+
XADD mystream IDMP producer-2 iid-1 * field value # producer-2 is tracking (independent)
118
140
```
119
141
120
-
Producers can use the same idempotent ID without conflicts.
142
+
Producers can use the same iid without conflicts, as long as long as the pids are different.
121
143
122
144
## Monitoring
123
145
@@ -127,13 +149,13 @@ Use [`XINFO STREAM`]({{< relref "/commands/xinfo-stream" >}}) to monitor idempot
127
149
XINFO STREAM mystream
128
150
```
129
151
130
-
Returns additional fields when idempotency is begin used:
152
+
Returns additional fields when idempotency is being used:
131
153
132
154
-`idmp-duration`: Current duration setting.
133
155
-`idmp-maxsize`: Current maxsize setting.
134
-
-`pids-tracked`: Number of active producers.
135
-
-`iids-tracked`: Total idempotent IDs currently stored.
136
-
-`iids-added`: Lifetime count of idempotent messages added.
156
+
-`pids-tracked`: The number of pids currently tracked in the stream
157
+
-`iids-tracked`: Total number of iids currently tracked.
158
+
-`iids-added`: Lifetime count of messages with idempotent IDs.
137
159
-`iids-duplicates`: Lifetime count of duplicates prevented.
138
160
139
161
## Best practices
@@ -142,8 +164,7 @@ Returns additional fields when idempotency is begin used:
142
164
143
165
Use globally unique, persistent producer IDs:
144
166
145
-
- Recommended: UUID v4 for global uniqueness.
146
-
- Alternative: `hostname:process_id` or application-assigned IDs.
167
+
- Recommended: Use short producer IDs to save memory and increase performance.
147
168
- Persistence: Use the same producer ID after restarts to maintain idempotency tracking.
0 commit comments