-
Notifications
You must be signed in to change notification settings - Fork 1.5k
JAVA-5950 Update Transactions Convenient API with exponential backoff on retries #1852
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: backpressure
Are you sure you want to change the base?
JAVA-5950 Update Transactions Convenient API with exponential backoff on retries #1852
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
This PR implements exponential backoff with jitter for transaction retries in MongoDB's withTransaction convenience API. The implementation adds a configurable backoff mechanism that applies delays between retry attempts when transient transaction errors occur, following the MongoDB specification with a growth factor of 1.5 for transactions.
Key Changes
- Introduces
ExponentialBackoffutility class with factory methods for transaction retries (5ms base, 500ms max, 1.5x growth) and command retries (100ms base, 10s max, 2.0x growth) - Integrates backoff logic into
ClientSessionImpl.withTransaction()to delay between retry attempts - Adjusts test configuration to verify backoff behavior with multiple retry attempts
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| driver-core/src/main/com/mongodb/internal/ExponentialBackoff.java | New utility class implementing exponential backoff with jitter using ThreadLocalRandom |
| driver-sync/src/main/com/mongodb/client/internal/ClientSessionImpl.java | Adds backoff delay before transaction retries and uses CSOT timeout when available |
| driver-core/src/test/unit/com/mongodb/internal/ExponentialBackoffTest.java | Comprehensive unit tests validating backoff calculations, growth factors, and maximum caps |
| driver-sync/src/test/functional/com/mongodb/client/WithTransactionProseTest.java | New functional test verifying exponential backoff behavior and adjusted existing test configuration |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
driver-sync/src/main/com/mongodb/client/internal/ClientSessionImpl.java
Outdated
Show resolved
Hide resolved
| AtomicInteger retryCount = new AtomicInteger(0); | ||
|
|
||
| session.withTransaction(() -> { | ||
| retryCount.incrementAndGet(); // Count the attempt before the operation that might fail |
Copilot
AI
Dec 9, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test verifies the retry count but does not validate that exponential backoff delays are actually applied. Consider measuring elapsed time and asserting minimum expected delays to ensure backoff is functioning correctly. For example, with 3 retries at delays of ~5ms, ~7.5ms, and ~11.25ms, the total elapsed time should be at least the sum of minimum expected delays.
…Impl.java Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
JAVA-5950