Skip to content

Conversation

@diydriller
Copy link

@diydriller diydriller commented Dec 7, 2025

Summary

This PR addresses a concurrency issue in the MongoDBJobRepository.

The current implementation generates numeric identifiers for job/step executions through a sequence document.
Under concurrency, especially with transactional writes, this causes frequent WriteConflict errors from MongoDB.

This PR replaces the numeric sequence generation with application-generated, unique, time-ordered identifier.
This eliminates write conflicts caused by sequence updates.

Problem

MongoDB uses optimistic concurrency control, and concurrent writes to the same document cause conflicts.

collection.findOneAndUpdate(
    new Document("_id", sequenceName),
    new Document("$inc", new Document("count", 1)),
    options
).getLong("count");

This produces high contention on a single document under parallel workloads.

Solution

Replace sequence-based numeric ID generation with TSID.
Implements TSID algorithm(42-bit timestamp + 10-bit node ID + 12-bit sequence)

TSID characteristics

  • time-ordered
  • unique
  • 64-bit long value

Fixes #4960

Signed-off-by: Hyun Jong Park <dhrhd080@naver.com>

private final String sequenceName;
public MongoSequenceIncrementer() {
this.nodeId = (int) (System.nanoTime() & NODE_MASK);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's still possible same nodeId is used by multiple instances resulting in conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

WriteConflict in MongoSequenceIncrementer during parallel job executions

2 participants