AboutBlogContact
DevOps & InfrastructureApril 15, 2016 2 min read 273Updated: June 22, 2026

Distributed Locking: etcd vs. Consul (2016)

AunimedaAunimeda
📋 Table of Contents

Distributed Locking: etcd vs. Consul

It's 2016, and we're all moving towards microservices. One of the biggest challenges is coordination: how do you ensure that only one instance of a service performs a specific task (like a database migration or a scheduled report)? You need a Distributed Lock.

The Raft Consensus Algorithm

Both etcd (the backbone of Kubernetes) and Consul (from HashiCorp) are built on the Raft consensus algorithm. This ensures that even if a node fails, the cluster can still agree on who holds the lock.

Locking with etcd v3

The newly released etcd v3 uses a gRPC API and the concept of "Leases."

// Go example using etcd v3
cli, _ := clientv3.New(clientv3.Config{Endpoints: []string{"localhost:2379"}})
s, _ := concurrency.NewSession(cli, concurrency.WithTTL(10))
defer s.Close()

m := concurrency.NewMutex(s, "/my-service-lock")

// Acquire the lock
if err := m.Lock(context.TODO()); err != nil {
    log.Fatal(err)
}

// Perform critical section...
fmt.Println("I have the lock!")

m.Unlock(context.TODO())

Locking with Consul

Consul uses a simpler HTTP API and "Sessions." A session is tied to a health check; if the node becomes unhealthy, the lock is automatically released.

# Acquire a lock via curl
curl -X PUT -d 'my-lock-data' http://localhost:8500/v1/kv/my-service/lock?acquire=<session_id>

Comparison: Which one to choose?

  • etcd is the better choice if you are already in the Kubernetes ecosystem. Its v3 API is extremely powerful but has a steeper learning curve.
  • Consul is more than just a KV store; it's a full service-discovery solution. Its DNS interface and built-in health checks make it a more "all-in-one" tool for traditional deployments.

In 2016, the choice between them often comes down to your existing infrastructure. But regardless of the tool, the goal is the same: absolute consistency in an uncertain network.


Aunimeda provides DevOps engineering and infrastructure services - CI/CD pipelines, containerization, cloud deployments, and monitoring setups.

Contact us to discuss your infrastructure needs. See also: DevOps Services, Custom Software Development

Read Also

Docker Multi-Stage Builds: Slimming Down Your Production Images (2019)aunimeda
DevOps & Infrastructure

Docker Multi-Stage Builds: Slimming Down Your Production Images (2019)

Shipping a 1GB Node.js image is so 2017. In 2019, we use multi-stage builds to separate our build environment from our runtime environment, resulting in tiny, secure images.

The Rise of Containerization: Why We are Moving Our Production to Dockeraunimeda
DevOps & Infrastructure

The Rise of Containerization: Why We are Moving Our Production to Docker

The 'it works on my machine' era is over. In 2018, we are standardizing our development and production environments using Docker to solve the environment parity problem once and for all.

Dockerizing Your Legacy Rails App: No More 'Works on My Machine' (2013)aunimeda
DevOps & Infrastructure

Dockerizing Your Legacy Rails App: No More 'Works on My Machine' (2013)

The Docker revolution is just beginning. In 2013, we're using it to containerize legacy Ruby on Rails apps and solve the dependency nightmare.

Need IT development for your business?

We build websites, mobile apps and AI solutions. Free consultation.

DevOps Services

Get Consultation All articles