Skip to content

Commit 135add4

Browse files
authored
DRIVERS-1934: withTransaction API retries too frequently (#1851)
1 parent 08ba029 commit 135add4

File tree

2 files changed

+120
-11
lines changed

2 files changed

+120
-11
lines changed

source/transactions-convenient-api/tests/README.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,72 @@ If possible, drivers should implement these tests without requiring the test run
4141
the retry timeout. This might be done by internally modifying the timeout value used by `withTransaction` with some
4242
private API or using a mock timer.
4343

44+
### Retry Backoff is Enforced
45+
46+
Drivers should test that retries within `withTransaction` do not occur immediately.
47+
48+
1. let `client` be a `MongoClient`
49+
2. let `coll` be a collection
50+
3. Now, run transactions without backoff:
51+
1. Configure the random number generator used for jitter to always return `0` -- this effectively disables backoff.
52+
53+
2. Configure a fail point that forces 13 retries like so:
54+
55+
```python
56+
set_fail_point(
57+
{
58+
"configureFailPoint": "failCommand",
59+
"mode": {
60+
"times": 13
61+
}, # sufficiently high enough such that the time effect of backoff is noticeable
62+
"data": {
63+
"failCommands": ["commitTransaction"],
64+
"errorCode": 251,
65+
},
66+
}
67+
)
68+
```
69+
70+
> Note: errorCode 251 is NoSuchTransaction.
71+
72+
3. Define the callback for the transaction as follows:
73+
74+
```python
75+
def callback(session):
76+
coll.insert_one({}, session=session)
77+
```
78+
79+
4. Let `no_backoff_time` be the duration of the withTransaction API call:
80+
81+
```python
82+
start = time.monotonic()
83+
with client.start_session() as s:
84+
s.with_transaction(callback)
85+
end = time.monotonic()
86+
no_backoff_time = end - start
87+
```
88+
4. Now run the command with backoff:
89+
1. Configure the random number generator used for jitter to always return `1`.
90+
2. Configure a fail point that forces 13 retries like in step 3.2.
91+
3. Use the same callback defined in 3.3.
92+
4. Let `with_backoff_time` be the duration of the withTransaction API call:
93+
```python
94+
start = time.monotonic()
95+
with client.start_session() as s:
96+
s.with_transaction(callback)
97+
end = time.monotonic()
98+
no_backoff_time = end - start
99+
```
100+
5. Compare the two time between the two runs.
101+
```python
102+
assertTrue(absolute_value(with_backoff_time - (no_backoff_time + 2.2 seconds)) < 1)
103+
```
104+
The sum of 13 backoffs is roughly 2.2 seconds. There is a 1-second window to account for potential variance between
105+
the two runs.
106+
44107
## Changelog
45108

109+
- 2025-11-18: Added Backoff test.
46110
- 2024-09-06: Migrated from reStructuredText to Markdown.
47111
- 2024-02-08: Converted legacy tests to unified format.
48112
- 2021-04-29: Remove text about write concern timeouts from prose test.

source/transactions-convenient-api/transactions-convenient-api.md

Lines changed: 56 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ has not been exceeded, the driver MUST retry a transaction that fails with an er
9999
"TransientTransactionError" label. Since retrying the entire transaction will entail invoking the callback again,
100100
drivers MUST document that the callback may be invoked multiple times (i.e. one additional time per retry attempt) and
101101
MUST document the risk of side effects from using a non-idempotent callback. If the retry timeout has been exceeded,
102-
drivers MUST NOT retry the transaction and allow `withTransaction` to propagate the error to its caller.
102+
drivers MUST NOT retry the transaction and allow `withTransaction` to propagate the error to its caller. When retrying,
103+
drivers MUST implement an exponential backoff with jitter following the algorithm described below.
103104

104105
If an error bearing neither the UnknownTransactionCommitResult nor the TransientTransactionError label is encountered at
105106
any point, the driver MUST NOT retry and MUST allow `withTransaction` to propagate the error to its caller.
@@ -113,7 +114,11 @@ needed (e.g. user data to pass as a parameter to the callback).
113114

114115
This method should perform the following sequence of actions:
115116

116-
1. Record the current monotonic time, which will be used to enforce the 120-second timeout before later retry attempts.
117+
1. Define the following:
118+
1. Record the current monotonic time, which will be used to enforce the 120-second / CSOT timeout before later retry
119+
attempts.
120+
2. Set `retry` to `0`. This will be used for backoff later in step 7.
121+
3. Set `TIMEOUT_MS` to be `timeoutMS` if given, otherwise 120-seconds.
117122
2. Invoke [startTransaction](../transactions/transactions.md#starttransaction) on the session. If TransactionOptions
118123
were specified in the call to `withTransaction`, those MUST be used for `startTransaction`. Note that
119124
`ClientSession.defaultTransactionOptions` will be used in the absence of any explicit TransactionOptions.
@@ -128,23 +133,35 @@ This method should perform the following sequence of actions:
128133
6. If the callback reported an error:
129134
1. If the ClientSession is in the "starting transaction" or "transaction in progress" state, invoke
130135
[abortTransaction](../transactions/transactions.md#aborttransaction) on the session.
136+
131137
2. If the callback's error includes a "TransientTransactionError" label and the elapsed time of `withTransaction` is
132-
less than 120 seconds, jump back to step two.
138+
less than TIMEOUT_MS, calculate the backoffMS to be `jitter * min(BACKOFF_INITIAL * (1.5**retry), BACKOFF_MAX)`
139+
where:
140+
141+
1. jitter is a random float between \[0, 1)
142+
2. retry is the variable defined in step 1.
143+
3. `BACKOFF_INITIAL` is 5ms
144+
4. `BACKOFF_MAX` is 500ms
145+
146+
If elapsed time + `backoffMS` > `TIMEOUT_MS`, then raise last known error. Otherwise, sleep for `backoffMS`,
147+
increment `retry`, and jump back to step two.
148+
133149
3. If the callback's error includes a "UnknownTransactionCommitResult" label, the callback must have manually
134150
committed a transaction, propagate the callback's error to the caller of `withTransaction` and return
135151
immediately.
152+
136153
4. Otherwise, propagate the callback's error to the caller of `withTransaction` and return immediately.
137154
7. If the ClientSession is in the "no transaction", "transaction aborted", or "transaction committed" state, assume the
138155
callback intentionally aborted or committed the transaction and return immediately.
139156
8. Invoke [commitTransaction](../transactions/transactions.md#committransaction) on the session.
140157
9. If `commitTransaction` reported an error:
141158
1. If the `commitTransaction` error includes a "UnknownTransactionCommitResult" label and the error is not
142-
MaxTimeMSExpired and the elapsed time of `withTransaction` is less than 120 seconds, jump back to step eight.
143-
We will trust `commitTransaction` to apply a majority write concern on retry attempts (see:
159+
MaxTimeMSExpired and the elapsed time of `withTransaction` is less than TIMEOUT_MS, jump back to step eight. We
160+
will trust `commitTransaction` to apply a majority write concern on retry attempts (see:
144161
[Majority write concern is used when retrying commitTransaction](#majority-write-concern-is-used-when-retrying-committransaction)).
145162

146163
2. If the `commitTransaction` error includes a "TransientTransactionError" label and the elapsed time of
147-
`withTransaction` is less than 120 seconds, jump back to step two.
164+
`withTransaction` is less than TIMEOUT_MS, jump back to step two.
148165

149166
3. Otherwise, propagate the `commitTransaction` error to the caller of `withTransaction` and return immediately.
150167
10. The transaction was committed successfully. Return immediately.
@@ -154,23 +171,39 @@ This method should perform the following sequence of actions:
154171
This method can be expressed by the following pseudo-code:
155172

156173
```typescript
174+
var BACKOFF_INITIAL = 5 // 5ms initial backoff
175+
var BACKOFF_MAX = 500 // 500ms max backoff
157176
withTransaction(callback, options) {
158177
// Note: drivers SHOULD use a monotonic clock to determine elapsed time
159178
var startTime = Date.now(); // milliseconds since Unix epoch
179+
// See the CSOT specification for information on calculating timeoutMS for a convenient transaction API call.
180+
var timeout = getCSOTTimeoutIfSet() ?? 120_000;
181+
var retry = 0;
160182

161183
retryTransaction: while (true) {
184+
if (retry > 0) {
185+
var backoff = Math.random() * min(BACKOFF_INITIAL * (1.5**retry),
186+
BACKOFF_MAX);
187+
188+
if (Date.now() + backoff - startTime >= timeout) {
189+
throw last_error;
190+
}
191+
sleep(backoff);
192+
}
193+
retry += 1
162194
this.startTransaction(options); // may throw on error
163195

164196
try {
165197
callback(this);
166198
} catch (error) {
199+
var last_error = error;
167200
if (this.transactionState == STARTING ||
168201
this.transactionState == IN_PROGRESS) {
169202
this.abortTransaction();
170203
}
171204

172205
if (error.hasErrorLabel("TransientTransactionError") &&
173-
Date.now() - startTime < 120000) {
206+
Date.now() - startTime < timeout) {
174207
continue retryTransaction;
175208
}
176209

@@ -198,12 +231,12 @@ withTransaction(callback, options) {
198231
*/
199232
if (!isMaxTimeMSExpiredError(error) &&
200233
error.hasErrorLabel("UnknownTransactionCommitResult") &&
201-
Date.now() - startTime < 120000) {
234+
Date.now() - startTime < timeout) {
202235
continue retryCommit;
203236
}
204237

205238
if (error.hasErrorLabel("TransientTransactionError") &&
206-
Date.now() - startTime < 120000) {
239+
Date.now() - startTime < timeout) {
207240
continue retryTransaction;
208241
}
209242

@@ -324,8 +357,8 @@ exceed the user's original intention for `maxTimeMS`.
324357
The callback may be executed any number of times. Drivers are free to encourage their users to design idempotent
325358
callbacks.
326359

327-
A previous design had no limits for retrying commits or entire transactions. The callback is always able indicate that
328-
`withTransaction` should return to its caller (without future retry attempts) by aborting the transaction directly;
360+
A previous design had no limits for retrying commits or entire transactions. The callback is always able to indicate
361+
that `withTransaction` should return to its caller (without future retry attempts) by aborting the transaction directly;
329362
however, that puts the onus on avoiding very long (or infinite) retry loops on the application. We expect the most
330363
common cause of retry loops will be due to TransientTransactionErrors caused by write conflicts, as those can occur
331364
regularly in a healthy application, as opposed to UnknownTransactionCommitResult, which would typically be caused by an
@@ -338,6 +371,16 @@ non-configurable default and is intentionally twice the value of MongoDB 4.0's d
338371
parameter (60 seconds). Applications that desire longer retry periods may call `withTransaction` additional times as
339372
needed. Applications that desire shorter retry periods should not use this method.
340373

374+
### Backoff Benefits
375+
376+
Previously, the driver would retry transactions immediately, which is fine for low levels of contention. But, as the
377+
server load increases, immediate retries can result in retry storms, unnecessarily further overloading the server.
378+
379+
Exponential backoff is well-researched and accepted backoff strategy that is simple to implement. A low initial backoff
380+
(1-millisecond) and growth value (1.25x) were chosen specifically to mitigate latency in low levels of contention.
381+
Empirical evidence suggests that 500-millisecond max backoff ensured that a transaction did not wait so long as to
382+
exceed the 120-second timeout and reduced load spikes.
383+
341384
## Backwards Compatibility
342385

343386
The specification introduces a new method on the ClientSession class and does not introduce any backward breaking
@@ -357,6 +400,8 @@ provides an implementation of a technique already described in the MongoDB 4.0 d
357400

358401
## Changelog
359402

403+
- 2025-11-20: withTransaction applies exponential backoff when retrying.
404+
360405
- 2024-09-06: Migrated from reStructuredText to Markdown.
361406

362407
- 2023-11-22: Document error handling inside the callback.

0 commit comments

Comments
 (0)