🔥 The DB Grill 🔥

Where database blog posts get flame-broiled to perfection

MongoDB High Availability: Replica Set in a Docker Lab
Originally from dev.to/feed/franckpachot
August 2, 2025 Read Original Article

Alright, gather 'round, folks, because here we go again. MongoDB, the undisputed champion of convincing people that eventual consistency is a feature, is apparently now guaranteeing consistent and durable write operations. Oh, really? Because last I checked, that was the baseline expectation for anything calling itself a database, not some revolutionary new parlor trick. They’re doing this with... wait for it... write-ahead logging! My word, has anyone informed the relational database world, which has only been doing that since, oh, the dawn of time? And they flush the journal to disk! I'm genuinely shocked, truly. I thought Mongo just kinda, whispered data into the ether and hoped for the best.

Then, they trot out the "synchronous replication to a quorum of replicas" and the claim that "replication and failover are built-in and do not require external tools." Yes, because every other modern database system requires you to hire a team of dedicated medieval alchemists to conjure up a replica set. Imagine that, a database that replicates itself without needing a separate enterprise-grade forklift and a team of consultants for every single failover. The audacity! And to set it up, you just... start three mongod instances. It’s almost like they're trying to make it sound complicated when it's just, you know, how these things work.

But here’s where the innovation truly blossoms. To "experiment with replication," they ran it in a lab with Docker Compose. A lab! With Docker Compose! Groundbreaking. But the networks were too perfect, you see. So, they had to bring out the big guns: tc and strace. Yes, the tools every seasoned sysadmin has had in their kit since forever are now being wielded like enchanted artifacts to "inject some artificial latencies." Because simulating reality is apparently a Herculean task when your core product struggles with it natively. They’re manually adding network delays and disk sync delays just to prove a point about... well, about how slow things can get when you force them to be slow. Who knew? It's like rigging a race so your slowest runner looks like they're trying really hard to finish last.

They write to the primary and read from each node to "explain the write concern and its consequences for latency." You mean, if I write something and don't wait for it to be replicated, I might read an old value? Stop the presses! The fundamental trade-off between consistency and availability, re-discovered in a Docker container with tc and strace! And bless their hearts, they even provided the Dockerfile and docker-compose.yml because setting up a basic three-node replica set in containers is apparently rocket science that requires bespoke NET_ADMIN and SYS_PTRACE capabilities. I particularly enjoyed the part where they inject a 50 millisecond fdatasync delay. Oh, the horror! My goodness, who would have thought that writing to disk takes time?

Then they discover that if you set w=0—that's "write to no one, tell no one"—your writes are fast, but your reads are "stale." Imagine! If you tell a system not to wait for acknowledgement, it, get this, doesn't wait for acknowledgement, and then other nodes might not have the data yet. This isn't just an introduction, it's a profound, spiritual journey into the heart of distributed systems. And the pièce de résistance: "the client driver is part of the consensus protocol." My sides. So, my Node.js driver running on some budget server in Ohio is actively participating in a Raft election? I thought it just sent requests. What a multi-talented piece of software.

Finally, they switch to w=1, journal=false and proudly announce that this "reduces write latency to just the network time," but with the caveat that "up to 100 milliseconds of acknowledged transactions could be lost" if the Linux instance crashes. But if the MongoDB instance fails, "there is no data loss, as the filesystem buffers remain intact." Oh, good, so as long as your kernel doesn't panic, your data's safe. It's a "feature," they say, for "IoT scenarios" where "prioritizing throughput is crucial, even if it means accepting potential data loss during failures." Sounds like a fantastic business requirement to build upon. "Sure, we're losing customer orders, but boy, are we losing them fast!"

In summary, after all this groundbreaking lab work, what do we learn? MongoDB allows you to balance performance and durability. You mean, like every single database ever built? They’ve essentially reinvented the wheel, added some shiny Docker paint, and called it a masterclass in distributed systems. My prediction? Someone, somewhere, will read this, excitedly deploy w=1, journal=false to "prioritize throughput," and then come crying to Stack Overflow when their "IoT" data vanishes into the digital ether. But hey, at least they’ll have the docker compose up --build command handy for the next time they want to watch their data disappear.