---
title: "Using etcd for elections"
source: "https://wordpress-1075849-4005834.cloudwaysapps.com/blog/using-etcd-for-elections/"
---

[Technical Blog](https://wordpress-1075849-4005834.cloudwaysapps.com/category/technical-blog/)

# Using etcd for elections

By [Peter White](https://wordpress-1075849-4005834.cloudwaysapps.com/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](http://github.com/Metaswitch/calico/blob/master/calico/election.py), 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://wordpress-1075849-4005834.cloudwaysapps.com/tags/open-source/)

Add Tigera as a preferred source on Google to see more of our content in your search results.

[![Add as a preferred source on Google](https://wordpress-1075849-4005834.cloudwaysapps.com/app/themes/tigera2021/dist/img/google-preferred-source-light.png)](https://www.google.com/preferences/source?q=tigera.io)

## Related posts

[![Join Calico at KubeCon Europe 2026: AI Agents, Silent Discos, and Dutch Delights!](https://wordpress-1075849-4005834.cloudwaysapps.com/app/uploads/2026/02/Blog-Kubecon-Euro-2026-.png)](https://wordpress-1075849-4005834.cloudwaysapps.com/blog/calico-at-kubecon-cloudnativecon-europe2026/)

[Company Blog](https://wordpress-1075849-4005834.cloudwaysapps.com/category/company-blog/)

#### [Join Calico at KubeCon Europe 2026: AI Agents, Silent Discos, and Dutch Delights!](https://wordpress-1075849-4005834.cloudwaysapps.com/blog/calico-at-kubecon-cloudnativecon-europe2026/)

By [Tigera Team](https://wordpress-1075849-4005834.cloudwaysapps.com/blog/author/marketingtigera-io/)
on Feb 25, 2026

The cloud-native community is heading to the historic canals and vibrant tech scene of Amsterdam for KubeCon + CloudNativeCon Europe 2026! From March 23–26, Amsterdam will be buzzing with the latest in Kubernetes, platform engineering,...

[Read more](https://wordpress-1075849-4005834.cloudwaysapps.com/blog/calico-at-kubecon-cloudnativecon-europe2026/)

[![Project Calico 3.30+ Hackathon: Show Us What You Can Build!](https://wordpress-1075849-4005834.cloudwaysapps.com/app/uploads/2026/02/Blog-Hackathon-.png)](https://wordpress-1075849-4005834.cloudwaysapps.com/blog/project-calico-3-30-hackathon-show-us-what-you-can-build/)

#### [Project Calico 3.30+ Hackathon: Show Us What You Can Build!](https://wordpress-1075849-4005834.cloudwaysapps.com/blog/project-calico-3-30-hackathon-show-us-what-you-can-build/)

By [Tigera Team](https://wordpress-1075849-4005834.cloudwaysapps.com/blog/author/marketingtigera-io/)
on Feb 17, 2026

Build the Future of Cloud-Native Networking! 🚀 The Calico community moves fast. With the releases of Calico 3.30 and 3.31, brings improvements in scalability, network security, and visibility. Now, we want to see what YOU...

[Read more](https://wordpress-1075849-4005834.cloudwaysapps.com/blog/project-calico-3-30-hackathon-show-us-what-you-can-build/)

[![Why Kubernetes Flat Networks Fail at Scale—and Why Your Cluster Needs a Security Hierarchy](https://wordpress-1075849-4005834.cloudwaysapps.com/app/uploads/2026/01/Why-Kubernetes-Flat-Networks-Fail-at-Scale.png)](https://wordpress-1075849-4005834.cloudwaysapps.com/blog/why-kubernetes-flat-networks-fail-at-scale-and-why-your-cluster-needs-a-security-hierarchy/)

#### [Why Kubernetes Flat Networks Fail at Scale—and Why Your Cluster Needs a Security Hierarchy](https://wordpress-1075849-4005834.cloudwaysapps.com/blog/why-kubernetes-flat-networks-fail-at-scale-and-why-your-cluster-needs-a-security-hierarchy/)

By [Reza Ramezanpour](https://wordpress-1075849-4005834.cloudwaysapps.com/blog/author/rezar/)
on Jan 28, 2026

Kubernetes networking offers incredible power, but scaling that power often transforms a clean architecture into a tangled web of complexity. Managing traffic flow between hundreds of microservices across dozens of namespaces presents a challenge that...

[Read more](https://wordpress-1075849-4005834.cloudwaysapps.com/blog/why-kubernetes-flat-networks-fail-at-scale-and-why-your-cluster-needs-a-security-hierarchy/)

<!-- plugin=object-cache-pro client=phpredis metric#hits=6259 metric#misses=17 metric#hit-ratio=99.7 metric#bytes=2172798 metric#prefetches=0 metric#store-reads=416 metric#store-writes=15 metric#store-hits=426 metric#store-misses=6 metric#sql-queries=31 metric#ms-total=916.17 metric#ms-cache=59.94 metric#ms-cache-avg=0.1394 metric#ms-cache-ratio=6.5 sample#redis-hits=74209437 sample#redis-misses=9035833 sample#redis-hit-ratio=89.2 sample#redis-ops-per-sec=320 sample#redis-evicted-keys=0 sample#redis-used-memory=110570768 sample#redis-used-memory-rss=107499520 sample#redis-memory-fragmentation-ratio=1.0 sample#redis-connected-clients=1 sample#redis-tracking-clients=0 sample#redis-rejected-connections=0 sample#redis-keys=23721 -->
