Distributed lock service on top of etcd leases. Applications that must run in a single instance (cron jobs, schedulers, leader-only workers) take a lock with one HTTP request and hold it with keepalives; when the holder stops sending keepalives, the lease expires and another instance can take the lock. Clients only need an HTTP client, no etcd library or credentials.
- Quick start
- Installation
- Configuration
- HTTP API
- Metrics
- Deployment
- Examples
- Security
- Development
- License
# etcd and shared-lock on one Docker network
docker network create shared-lock
docker run -d --name etcd --network shared-lock gcr.io/etcd-development/etcd:v3.6.12 \
etcd --listen-client-urls http://0.0.0.0:2379 --advertise-client-urls http://etcd:2379
docker run -d --name shared-lock --network shared-lock -p 8080:8080 \
-e SHARED_LOCK_ETCD_ADDR_LIST=http://etcd:2379 \
ghcr.io/tentens-tech/shared-lock:latest
# Take the lock "my-job" for 30 seconds: 201 and the lease ID
curl -i -X POST http://localhost:8080/lease -H "x-lease-ttl: 30s" -d '{"key": "my-job"}'
# Hold it: send the lease ID before the TTL runs out
curl -i -X POST http://localhost:8080/keepalive -d '<lease ID>'Without etcd, run the server with the in-memory storage (one process, for development only):
make run # SHARED_LOCK_STORAGE_TYPE=mock go run . serveMulti-arch images (linux/amd64, linux/arm64) are published to the GitHub Container Registry:
docker pull ghcr.io/tentens-tech/shared-lock:latestTags: vX.Y.Z, vX.Y, vX and latest. The image is based on distroless/static, runs as UID:GID 1010:1010,
works with a read-only root filesystem and starts shared-lock serve. Images are signed with cosign and carry an SBOM
and provenance attestations, see Verifying Releases.
Download a linux/amd64 or linux/arm64 archive from the releases page, or build from source:
make build # ./shared-lock, version from git describe
./shared-lock serve
./shared-lock --versionshared-lock is configured with environment variables:
| Environment Variable | Default Value | Description |
|---|---|---|
SHARED_LOCK_SERVER_PORT |
8080 |
Port on which the server will run |
SHARED_LOCK_SERVER_READ_TIMEOUT |
10s |
Server read timeout duration |
SHARED_LOCK_SERVER_WRITE_TIMEOUT |
10s |
Server write timeout duration |
SHARED_LOCK_SERVER_IDLE_TIMEOUT |
120s |
Server idle timeout duration |
SHARED_LOCK_SERVER_SHUTDOWN_TIMEOUT |
10s |
Server shutdown timeout duration |
SHARED_LOCK_PPROF_ENABLED |
false |
Serve pprof on /debug/pprof/ |
SHARED_LOCK_STORAGE_TYPE |
etcd |
Storage type to use (etcd or mock) |
SHARED_LOCK_ETCD_ADDR_LIST |
http://localhost:2379 |
Comma-separated list of etcd endpoints |
SHARED_LOCK_ETCD_TLS |
false |
Enable TLS for etcd connections |
SHARED_LOCK_CA_CERT_PATH |
/etc/etcd/ca.crt |
Path to the CA certificate for etcd |
SHARED_LOCK_CLIENT_CERT_PATH |
/etc/etcd/client.crt |
Path to the client certificate for etcd |
SHARED_LOCK_CLIENT_KEY_PATH |
/etc/etcd/client.key |
Path to the client key for etcd |
SHARED_LOCK_CACHE_ENABLED |
false |
Enable in-memory cache for leases |
SHARED_LOCK_CACHE_SIZE |
1000 |
Maximum number of items in the cache |
SHARED_LOCK_DEBUG |
false |
Toggle for debug mode |
The mock storage keeps leases in memory, never expires them and is meant for local development and tests.
- URL:
/lease - Method:
POST - Headers:
x-lease-ttl: (Optional) The TTL of the lease as a Go duration, e.g.30sor1m. Default10s.
- Request Body: JSON object with the lease details:
key: name of the lock. Stored in etcd under/shared-lock/<key>.value: (Optional) value stored with the key.labels,timestamp: (Optional) metadata.
- Responses (the body is the lease ID):
201 Created: Lease successfully created, the caller holds the lock.202 Accepted: Lease request accepted but lease not granted, the lock is held by someone else. The body is the ID of the existing lease.400 Bad Request: Failed to read or unmarshal the request body.500 Internal Server Error: Failed to create lease.
curl -X POST http://localhost:8080/lease \
-H "Content-Type: application/json" \
-H "x-lease-ttl: 60s" \
-d '{"key": "my-job", "value": "worker-1"}'- URL:
/keepalive - Method:
POST - Request Body: the lease ID returned by
/lease, as plain text. - Responses:
200 OK: Lease successfully renewed for another TTL.204 No Content: Failed to prolong lease, e.g. it has already expired. The lock is lost.400 Bad Request: Failed to read the request body.500 Internal Server Error: Failed to parse the lease ID.
curl -X POST http://localhost:8080/keepalive -d "7587883520138092345"- Send keepalives more often than the TTL, e.g. every TTL/3, so that one lost or slow request doesn't end the lease.
- Track when the lease was last confirmed: the time a request that returned
200(or the201of/lease) was sent, not when its answer arrived. The lease is valid until that time plus the TTL. - Stop the work before that deadline whenever renewal is not confirmed: on
204, but also on timeouts, connection errors and5xx. Give every keepalive request a timeout well below the TTL. A client that keeps working while it cannot reach shared-lock may still be running after its lease expired and another client took the lock. - A lease bounds how long a holder is trusted, it doesn't stop a paused process (GC pause, VM freeze) from acting after the lease expired. When two holders at once would corrupt data, also fence the writes: pass the lease ID to the protected resource and reject writes from a lease that is no longer current.
- URL:
/health - Method:
GET - Responses:
200 OK: Server is up.
curl http://localhost:8080/healthPrometheus metrics are served on /metrics:
| Metric | Type | Labels | Description |
|---|---|---|---|
shared_lock_lease_operations_total |
counter | operation, status |
Lease operations |
shared_lock_lease_grant_duration_seconds |
histogram | operation |
Duration of lease requests |
shared_lock_cache_operations_total |
counter | operation, status |
Lease cache operations |
The Go runtime and process metrics of the Prometheus client are exported too.
shared-lock needs a running etcd. As etcd is mostly used as a part of a Kubernetes cluster, an example manifest (headless Service and Deployment, etcd over TLS) is in deployment/kubernetes-example.yaml.
- example/client.go - an application that runs in a single instance: it retries
/leaseuntil it gets the lock, then holds it with keepalives. - scripts/ - a load testing tool.
See SECURITY.md for reporting vulnerabilities, verifying release images and hardening the deployment. The HTTP API has no authentication: expose it only inside the cluster.
make help # list targets
make test # unit tests with the race detector
make lint # golangci-lint
make vuln # govulncheck
make e2e # the image against etcd v3.5, v3.6 and v3.7 in DockerSee CONTRIBUTING.md for the development workflow and CHANGELOG.md for the changes in each release.