Skip to content

Add migration locks in MongoDB #3149

@QUBITABHAY

Description

@QUBITABHAY

I would like to propose adding migration locks for MongoDB to ensure that migrations do not run concurrently across multiple instances of an application.

Since MongoDB is a NoSQL database, the locking approach differs from traditional RDBMS implementations. Below is a proposed approach.

  1. Use a dedicated lock collection

    • Create a collection (e.g., migration_locks) that stores a lock document.

      • Example structure:
        {
          "_id": "migration_lock",
          "lockedAt": <timestamp>,
          "lockedBy": <instance-id>
        }
    • Before running migrations:

      • Attempt to insert or acquire the lock document.
      • If the lock already exists, the migration process should exit or wait.
      • If the lock is acquired successfully, proceed with the migration.
    • After migrations complete:

      • Delete the lock document to release the lock.
  2. Use Atomic Operations

    • MongoDB operations such as:
      • findOneAndUpdate
      • insertOne with a unique _id
        can ensure atomic lock acquisition, preventing race conditions.
  3. Optional: TTL Index for Safety

    • To avoid stale locks if a migration process crashes, we can add a TTL index on lockedAt.
      • Example:
        • Automatically expire lock documents after a configurable duration.
  4. Transactions (If Available)

    • If MongoDB transactions are available (MongoDB ≥ 4.0 with replica sets):
    • Lock acquisition and migration metadata updates could run within a session transaction.
    • This would improve the reliability and atomicity of migration operations.

cc: @coolwednesday

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions