-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Open
Description
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.
-
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> }
- Example structure:
-
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.
-
-
Use Atomic Operations
- MongoDB operations such as:
findOneAndUpdateinsertOnewith aunique _id
can ensure atomic lock acquisition, preventing race conditions.
- MongoDB operations such as:
-
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.
- Example:
- To avoid stale locks if a migration process crashes, we can add a TTL index on lockedAt.
-
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
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels