AWS Builder Center

Failover in 1.3 seconds: how we made our distributed file system's control plane survive a leader crash on AWS

DFSha is a distributed file system we built at EAFIT with a 3-node Raft control plane, inspired by HDFS and GFS. This article covers what it takes beyond the algorithm to survive leader loss: a leader barrier, fast step-down for isolated leaders, request forwarding, HMAC-signed sessions and client failover. Deployed on 6 EC2 instances with Terraform, it recovered from an abrupt leader crash in 1.2 to 1.3 seconds.

Systems Engineering student @ EAFIT · Backend, AWS & Data Engineering
For our Distributed Systems course at Universidad EAFIT, two classmates and I built DFSha, a block-based distributed file system inspired by HDFS and GFS. Classic HDFS has a well-known single point of failure: the NameNode. If it goes down, nobody knows where the blocks are and the whole system stops.
Our goal was to remove that single point of failure. In this article I explain how we did it with Raft, what we had to build around the algorithm, and what we measured after deploying it on six EC2 instances: after abruptly killing the leader, the cluster elected a new one and accepted writes again in 1.2 to 1.3 seconds.

The architecture in one paragraph

DFSha separates the control plane from the data plane:
  • 3 ControlNodes (FastAPI + PySyncObj) store the directory tree, file and block metadata, users, permissions and locks. They never carry user data.
  • N DataNodes (gRPC over TLS) store blocks encrypted with AES-256-GCM. Each 4 MiB block has 2 replicas on different nodes.
  • The client asks any ControlNode for metadata, then reads and writes blocks directly to the DataNodes, in parallel. Because data never flows through the control plane, high availability comes down to one question: how do we keep three identical copies of the metadata and keep working when one of them fails?

Why Raft

Raft replicates a log of commands across N nodes, with a leader elected by majority. An entry committed by a majority survives the loss of a minority, and if every node applies the same log in the same order, they all reach the same state.
With N = 3 the cluster tolerates 1 failure. If 2 nodes fail there is no quorum, and DFSha rejects writes with a  503  instead of risking inconsistency. That is a deliberate choice: we prefer consistency over availability (a CP system, in CAP terms). Tolerating 2 failures would require a 5-node cluster.
We used PySyncObj, a Raft implementation in Python. Every metadata mutation ( mkdir , a file commit, a permission change, a lock) is a log entry. State lives in an in-memory SQLite database rebuilt from the log; durability comes from Raft's journal and snapshots.
One important detail is determinism: the leader sets timestamps and identifiers before replicating an operation. If each node called  now()  or generated its own UUID, the three copies would diverge.

What Raft doesn't solve on its own

Electing a new leader is the easy part: the library does it. The hard part is making everything around the cluster notice the change and behave correctly during the transition. These are the pieces we had to add.

1. Leader barrier

A newly elected leader may have committed entries it hasn't applied yet. If it answers reads immediately, it can return stale data. So, once elected, the leader replicates a  noop  entry and only starts serving after that entry commits in its term.

2. An isolated leader must stop serving quickly

During failure testing we found that a leader cut off from the rest of the cluster kept serving reads for about 30 seconds, because it took too long to step down. The fix had two parts:
  •  leaderFallbackTimeout = 5  seconds, so a leader without contact with the majority steps down quickly.
  • Requiring  hasQuorum  before serving any request.

3. Any node answers, the leader decides

The client can talk to any of the three ControlNodes. If the node that receives a request isn't the leader, it forwards it to the leader over HTTPS. If its known leader turns out to be stale, it tries the other peers.
This exposed another real bug: after containers restarted, Docker reassigned their IPs and followers kept forwarding to a leader that no longer existed. The cause was PySyncObj's DNS cache, 600 seconds by default. We lowered it to  dnsCacheTime = 5  and added retries against the other peers.

4. Sessions that survive failover

If sessions lived in the leader's memory, every failover would log everyone out. In DFSha, session tokens are HMAC-signed and valid on any ControlNode, so they survive a leader change without the user noticing.

5. Clients and DataNodes that don't depend on a single node

  • The client knows all three ControlNodes and rotates between them on a connection error or a  503 .
  • DataNodes send heartbeats to all three ControlNodes, but only obey commands from the current leader (replicate or delete blocks).

6. Soft state and safe startup

Not everything belongs in the Raft log. The registry of live DataNodes and the leases of open file sessions live in the leader's memory and are rebuilt from heartbeats within seconds. This keeps the log free of data that expires anyway.
The trade-off is that a freshly started leader doesn't know the DataNodes yet. After a restart, the replica monitor falsely reported "block has no replicas". We fixed it with a warm-up period: after a restart, the node answers  503  until the DataNode registry is rebuilt. Similarly, a new leader grants every open session a full TTL before expiring it.

Deploying on AWS

We automated the deployment with Terraform. A single  terraform apply  creates:
  • 6 EC2 t3.small instances running Amazon Linux 2023 (3 ControlNodes and 3 DataNodes) in the default VPC in  us-east-1 .
  • Two security groups, one per role.
  • The PKI: our own CA and one certificate per node, with its public and private IPs as Subject Alternative Names.
  • The cluster secrets. It then uploads the code over SSH, starts each node with  docker compose up --build , and waits until there is a Raft leader and three live DataNodes.
The security groups mirror the architecture:
Port
Source
Purpose
8000/tcp (control)
clients, sg-data, sg-control
HTTPS REST API
4321/tcp (control)
sg-control only
Raft between ControlNodes (encrypted)
6000/tcp (data)
clients
Public gRPC over TLS
6100/tcp (data)
sg-control, sg-data
Internal gRPC over mTLS
22/tcp
administrator IP
SSH
The Raft port is reachable only from the control security group: no client or DataNode can speak the consensus protocol.

What we measured

Abrupt leader crash on AWS. In three rounds we killed the leader's container with  docker kill . Each round elected a different leader, and the service recovered in 1.2 to 1.3 seconds. That is essentially Raft's election time. Meanwhile, the client retried against the other ControlNodes with no user intervention.
Graceful leader stop (local). With  docker compose stop , writes came back in 4.9 to 5.6 seconds:
Round
Leader stopped
New leader
Seconds
1
control3
control2
5.1
2
control2
control3
5.6
3
control3
control1
4.9
A graceful stop taking longer than a crash looks counterintuitive. The difference is the time Docker takes to stop the container, which adds to the election time.
DataNode failure. Not a Raft failover, but it completes the picture: after stopping a DataNode on AWS, the 12 test blocks (1 MiB each) were back to 2 copies in 46.3 seconds, and reads continued without errors. That time is almost entirely policy: we wait 30 seconds before declaring replicas lost (to avoid copying data because of a brief restart), plus up to 10 seconds for the replica monitor cycle.
All of this is covered by automated tests. The suite has 158 pytest cases, and the failure tests run against the real Docker cluster with TLS and encryption enabled, not against mocks.

How to reproduce it

  1. Check the cluster status and note the leader:
python client/cli.py status
  1. Stop the leader ControlNode. Locally:
docker compose stop control3 # whichever node is the leader
On AWS, SSH into the leader VM and run  docker compose stop  inside  deploy/control . 3. Within seconds a new leader appears, and  put  and  get  keep working.

What we learned

  • The algorithm is the small part. Raft (and the library) handles the election. Real high availability depends on the leader barrier, forwarding between peers, stateless sessions, and clients and DataNodes that know how to fail over.
  • Defaults matter. Two of our worst bugs (the isolated leader serving for 30 seconds and the stale leader caused by the 600-second DNS cache) came from defaults that are reasonable elsewhere, but not for containers that change IPs.
  • Real failure tests find what happy-path tests don't. Neither bug showed up while all nodes were healthy.

What we'd do next

  • Spread nodes across Availability Zones. Placement doesn't consider AZs today; on AWS that's the natural next step to survive the loss of an entire AZ.
  • Move to 5 ControlNodes to tolerate 2 simultaneous failures.
  • Persist the DataNode registry and leases, to shorten the warm-up period after a full restart. In the next article I'll explain how we cut the latency of each Raft-replicated write from 111 ms to about 4 ms by changing a single option.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article