Skip to content
Leader docs0.4

MongoDB backend

Latest stable Based on Leader release 0.4.0

Library module

Implements single/group election with token-owned MongoDB documents, atomic findOneAndUpdate, expiry, TTL indexes, and blocking/coroutine factories.

Choose it when MongoDB is already a durable application dependency and coordination in the same replica set is operationally simpler.

Artifact: io.github.bluetape4k.leader:bluetape4k-leader-mongodb

dependencies {
implementation(platform("io.github.bluetape4k:bluetape4k-dependencies:<version>"))
implementation("io.github.bluetape4k.leader:bluetape4k-leader-mongodb")
}

The collection stores lock documents with owner token and expireAt; atomic filters acquire and extend. TTL index cleanup is not the correctness clock.

val collection = database.getCollection<Document>("bluetape4k_leader_locks")
val elector = MongoLeaderElector(collection)
elector.runIfLeader("tenant-aggregation") { aggregate() }

Use blocking/suspend single and group electors, factories, extensions, and optional Mongo history sink/indexer. Collection lifecycle is caller-owned.

Use majority write concern in replica sets, stable collection/name prefixes, idempotent actions, and owner-token conditions on extend/release.

Spring can build factories from a MongoClient. tenant-aggregator demonstrates a partitioned workload.

Configure database/collection, wait/lease/minimum lease, retry delay, group size, write concern, and history TTL/index policy.

Contention returns null. Network, primary election, write-concern, permission, and index errors propagate. TTL deletion delay must not control acquisition.

Monitor command latency, write concern errors, primary changes, retry rate, stale documents, TTL monitor lag, extension failures, and history growth.

Use a replica-set Testcontainer for two-client contention, failover behavior, TTL/expiry, stale owner, group slots, suspend cancellation, and index creation.

Run tenant-aggregator, then compare MongoDB with SQL when deciding document versus transactional coordination.

Replica-set configuration affects split-brain risk. MongoDB lease ownership does not fence writes to another system.

These diagrams are loaded directly from README assets published with the 0.4.0 release and pinned to its immutable commit. They describe this manual’s released structure and runtime flows, not later Snapshot changes. Select a preview to open the SVG at the same release commit.

leader mongodb Class Structure diagram

Release README: leader-mongodb/README.md

Elector · Lock implementation · Stable guide