---
title: "Using etcd for elections"
source: "https://www.tigera.io/blog/using-etcd-for-elections/"
---

[Technical Blog](https://www.tigera.io/category/technical-blog/)

# Using etcd for elections

By [Peter White](https://www.tigera.io/blog/author/peter-white/) on May 14, 2015 • 2 min read

We recently had a requirement to implement leader elections across multiple servers (for reasons relating to avoiding contention between multiple instances of the OpenStack Neutron plugin). It’s not the first time I’ve had to implement elections, and it’s an awkward problem. You can do something pretty simple, but there are invariably lots of weird edge cases that require lots of testing and bug fixing to get right – and you never quite have the confidence it won’t break in the real world. Alternatively, if you go down the road of implementing the [Raft](http://en.wikipedia.org/wiki/Raft_(computer_science)) algorithm or even [Paxos](http://en.wikipedia.org/wiki/Paxos_(computer_science)) you are guaranteed to be safe, but only if you get the code exactly right, and again that means a lot of testing.

Fortunately, we are using [etcd](http://github.com/coreos/etcd), and since etcd provides both TTL on objects, and a compare and swap operation, implementing an election algorithm is trivial. Unelected instances can watch “/election” (or some other well known key) and if it is empty become elected by writing their ID to it. The written value is given a TTL that removes it after a set interval, and the elected instance must rewrite it periodically to remain elected. By the use of etcd’s atomic compare and swap operation, there is no risk of a clash between two instances being undetected. Job done, in a fraction of the time to implement it from scratch. There are some edge cases if etcd goes off line or runs catastrophically slowly, but neither of these matters for our use case.

This kind of situation is a change from the software industry even of five years ago, let alone twenty years ago. Instead of writing things from scratch and having to reinvent the wheel ourselves, we are building on top of standard third party components (in this case etcd) that do much of the work for us (here implementing the Raft algorithm in a cluster). That means that building almost any complex architecture is far cheaper than it used to be, and thus that we can build bigger and better than before.

[Open Source](https://www.tigera.io/tags/open-source/)

## Related posts

[![What’s new in Calico: Spring 2026 Release](https://www.tigera.io/app/uploads/2026/06/Whats-New-in-Calico-NEW-TEMPLATE-2026.png)](https://www.tigera.io/blog/whats-new-in-calico-spring-2026-release/)

[Company Blog](https://www.tigera.io/category/company-blog/)

#### [What’s new in Calico: Spring 2026 Release](https://www.tigera.io/blog/whats-new-in-calico-spring-2026-release/)

By [Veronika Smolik](https://www.tigera.io/blog/author/veronika-smolik/)
on Jun 2, 2026

Kubernetes has come a long way since its debut in 2014. It’s gone from running a couple of containerized microservices to orchestrating fleets of production workloads spanning everything from AI agents to full scale VMs...

[Read more](https://www.tigera.io/blog/whats-new-in-calico-spring-2026-release/)

[![Kubernetes Operational Maturity: Secure and Resilient Cluster Federation with Cluster Mesh](https://www.tigera.io/app/uploads/2026/05/Kubernetes-Operational-Maturity-Secure-and-Resilient-Cluster-Federation-with-Cluster-Mesh.png)](https://www.tigera.io/blog/kubernetes-operational-maturity-secure-and-resilient-cluster-federation-with-cluster-mesh/)

#### [Kubernetes Operational Maturity: Secure and Resilient Cluster Federation with Cluster Mesh](https://www.tigera.io/blog/kubernetes-operational-maturity-secure-and-resilient-cluster-federation-with-cluster-mesh/)

By [Veronika Smolik](https://www.tigera.io/blog/author/veronika-smolik/)
on May 25, 2026

Practically no one runs a single Kubernetes cluster in production these days. Maybe that’s how it started but data sovereignty requirements, acquisitions, AI initiatives and the need for edge servers, among other considerations, have pulled...

[Read more](https://www.tigera.io/blog/kubernetes-operational-maturity-secure-and-resilient-cluster-federation-with-cluster-mesh/)

[![What’s New in Calico v3.32](https://www.tigera.io/app/uploads/2026/05/Green-Please-use-a-different-background-color-alternately-1.png)](https://www.tigera.io/blog/whats-new-in-calico-v3-32/)

#### [What’s New in Calico v3.32](https://www.tigera.io/blog/whats-new-in-calico-v3-32/)

By [Reza Ramezanpour](https://www.tigera.io/blog/author/rezar/)
on May 13, 2026

We’re excited to announce the release of Calico Open Source v3.32! 🎉 This release corresponds with Kubernetes v1.36 (Codename Haru) and it goes beyond just sharing a cat as the mascot of the release, it...

[Read more](https://www.tigera.io/blog/whats-new-in-calico-v3-32/)

<!-- plugin=object-cache-pro client=phpredis metric#hits=3287 metric#misses=33 metric#hit-ratio=99.0 metric#bytes=1565309 metric#prefetches=0 metric#store-reads=181 metric#store-writes=14 metric#store-hits=173 metric#store-misses=22 metric#sql-queries=31 metric#ms-total=740.09 metric#ms-cache=69.47 metric#ms-cache-avg=0.3581 metric#ms-cache-ratio=9.4 -->
