From 111 ms to 4 ms: The One Raft Setting That Was Slowing Down Every Metadata Write
In DFSha, every metadata write (mkdir, file commit, permission change) goes through a 3-node Raft cluster, and early on each one took about 111 ms while reads took 2 to 3 ms. The cause was PySyncObj’s default batching, which waits for a 100 ms tick before sending entries. Disabling appendEntriesUseBatch brought writes down to about 4 ms (3.2 ms median locally, 10.2 ms on EC2). Here’s why, and when batching still makes sense.
In my previous article I explained how DFSha, the distributed file system we built for our Distributed Systems course at Universidad EAFIT, uses a 3-node Raft cluster to survive the loss of its leader in about 1.3 seconds.
High availability had a price we didn't expect. Early on, every metadata write took about 111 ms: creating a directory, committing a file, changing a permission. A read of the same metadata took 2 or 3 ms. This article explains where those 100 extra milliseconds came from and how a single configuration option brought writes down to about 4 ms.
Why every metadata write goes through Raft
DFSha separates the control plane from the data plane. File contents travel directly between the client and the DataNodes, but all metadata lives in three ControlNodes that replicate it with Raft, using the Python library PySyncObj.
Every mutation is an entry in the Raft log:
mkdir , the commit of a new file, a permission change, a lock. The leader adds the entry to its log, sends it to the followers, waits until a majority has stored it and only then applies it and answers the client. Reads like stat or ls are answered locally by the leader, without a consensus round.So the expected cost of a write is roughly a read plus one network round trip to the followers. On a single host, with the network on loopback, that round trip should cost about a millisecond. 111 ms was far too much.
The symptom
Our benchmark script (
scripts/benchmark.py ) runs 100 operations of each type, one after another, and reports the median and the 95th percentile. On a single machine with Docker, reads like stat took 2 to 3 ms, while each mkdir , a Raft write, took about 111 ms.That is roughly 100 ms more than a read: far more than a loopback round trip could explain.
The cause: batching and the 100 ms tick
PySyncObj has an option called
appendEntriesUseBatch , and it is enabled by default. Its documentation describes the trade-off clearly:- Enabled: better overall performance, measured in requests per second.
- Disabled: better speed for a single request, because the library doesn't wait until a batch is ready.
With batching enabled, a new command doesn't go to the followers right away. It waits in a queue and is sent with the next periodic
append_entriesmessage, together with any other commands that arrived in the meantime. The period of that message is controlled byappendEntriesPeriod, which defaults to 0.1 seconds.
That matches the number. Our benchmark is sequential: each
mkdir starts right after the previous one finishes, and each new entry can wait up to a full period, 100 ms, before it is even sent to the followers. Add the few milliseconds of real work and you get close to 111 ms.Batching is a reasonable default. If thousands of clients send small writes at the same time, grouping them in one message per period reduces the number of messages and increases throughput. But that is not our workload: metadata writes in DFSha are small and relatively infrequent, the heavy traffic (file contents) doesn't go through Raft, and a person using the shell feels every millisecond of a
mkdir .The fix
We disabled batching in the
SyncObjConf of each ControlNode:conf = SyncObjConf( # ... appendEntriesPeriod=float(os.environ.get("RAFT_APPEND_PERIOD", "0.02")), appendEntriesUseBatch=False, # send each entry immediately instead of waiting for the period autoTickPeriod=0.01, # ...)With
appendEntriesUseBatch=False , PySyncObj changes its behavior in three places (version 0.3.17, the one we use):- When a command arrives, it wakes up the event loop immediately through an internal pipe, instead of waiting for the next tick.
- As soon as the leader adds the entry to its log, it sends
append_entriesto the followers right away.
- When the commit index advances, it sends another
append_entriesimmediately, so followers learn about the commit without waiting for the period. We also loweredappendEntriesPeriodfrom the default 100 ms to 20 ms. With batching disabled, that period no longer limits write latency: in PySyncObj it is the interval of the periodicappend_entriesmessage, which with nothing new to send works as the leader's heartbeat to the followers.
The result
After the change, on the same machine and with the same benchmark:
Operation | Median (ms) | p95 (ms) |
|---|---|---|
mkdir (Raft write) | 3.2 | 3.6 |
stat (local read) | 2.6 | 3.3 |
ls of a directory | 2.7 | 4.1 |
Create an empty file (open + commit) | 7.0 | 8.1 |
A write replicated by Raft now costs almost the same as a local read: the median difference is less than a millisecond. The dominant cost of both is HTTPS and request handling, not consensus.
We then repeated the benchmark on AWS, with the 6 nodes on separate EC2 instances and the script running from a control VM.
mkdir took 10.2 ms (p95 12.3 ms), stat 6.9 ms and creating an empty file 22.6 ms. The numbers are about three times higher than on a single host because the messages now cross a real network between instances, but writes still cost only a few milliseconds more than reads.What we didn't measure
To be fair to the default: our heaviest concurrent tests used up to 8 clients uploading files at the same time, and we didn't benchmark metadata write throughput under a much higher load. In that scenario, sending one message per entry could reduce throughput compared to batching. For a file system where people create directories and upload files, single-request latency matters more, but a system with a different workload should measure both before choosing.
What we learned
- A suspiciously round number is a clue. The extra cost was close to 100 ms, the same value as a default period in the library. When a delay matches a configuration value, look at that configuration first.
- Defaults are optimized for someone else's workload. PySyncObj's defaults favor throughput; ours needed latency. The trade-off is documented in the option itself, which is why it pays to read the configuration of a consensus library, not only its API.
- Compare against a baseline. We only noticed the problem because we measured reads and writes side by side. Without the read as a reference, 111 ms might have looked like "the normal cost of consensus".
- Consensus doesn't have to be slow. With the right configuration, a Raft write on loopback costs less than a millisecond more than a local read.
How to reproduce it
With the local cluster running (
docker compose up ), run the benchmark and look at the metadata latency table:python scripts/benchmark.pyTo see the old behavior, change
appendEntriesUseBatch to True in control_node/cluster.py , start the cluster with RAFT_APPEND_PERIOD=0.1 (the library default), rebuild the ControlNodes and run the benchmark again. Use --quick for a shorter run (30 operations of each type instead of 100).In the next article I'll move to a different project: an auto-scaling controller I built on AWS with EC2, Auto Scaling Groups and CloudWatch for our Cloud Computing course.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article