Skip to content

Commit f4d25d7

Browse files
committed
Revise how to create locks section
Signed-off-by: Ronald Ngounou <[email protected]>
1 parent 082b731 commit f4d25d7

File tree

1 file changed

+57
-6
lines changed

1 file changed

+57
-6
lines changed
Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,67 @@
11
---
22
title: How to create locks
3-
description: Guide to creating distributed locks in etcd
3+
description:
44
weight: 800
55
---
66

7-
`lock` for distributed lock:
87

9-
![08_etcdctl_lock_2016050501](https://storage.googleapis.com/etcd/demo/08_etcdctl_lock_2016050501.gif)
8+
## Overview
9+
10+
A lock is a synchronization mechanism used to control access to a shared resource in distributed system. In the context of etcd, a lock ensures that only one client at a time can perform a specific task or access a critical section of code.
11+
12+
Common use cases include:
13+
14+
- Ensuring only one instance of a scheduled job runs
15+
- Leader election in distributed systems
16+
- Serializing access to shared configuration
17+
18+
etcd's locking mechanism is built on top of leases and key creation, ensuring that locks are automatically realeased if the client crashes or becomes unresponsive.
19+
20+
## Prerequisites
21+
22+
Before you can create and manage locks with etcd, make sure the following requirements are met:
23+
24+
- `etcdctl` installed: You need the `etcdctl` command-line tool installed on your system. You can install it by downloading the binaries from the etcd releases page or using a package manager.
25+
26+
- Running etcd cluster: A healthy, running etcd cluster is required. Ensure the endpoints you plan to use are reachable and the cluster is in a good state.
27+
28+
- Environment Setup:
29+
30+
- `--endpoints`: The etcd server endpoint, e.g., `--endpoints=127.0.0.1:2379`
31+
32+
- TLS-related flags (if using secure communication): `--cert`, `--key`, `--cacert`, etc.
33+
34+
## Creating a lock
35+
36+
Simple lock acquisition
1037

1138
```shell
12-
etcdctl --endpoints=$ENDPOINTS lock mutex1
39+
etcdctl --endpoints=127.0.0.1:2379 lock locks/my-lock-name echo "I have the lock"
40+
```
41+
42+
### Flags
43+
44+
- `--endpoints`: The etcd server endpoint, by default: `--endpoints=127.0.0.1:2379`
45+
- `lock`: Acquires a named lock
46+
47+
### What's Happening
48+
49+
1. etcdctl uses the concurrency API to create a lease and attaches it to a lock key ()`locks/my-lock-name/<uuid>`).
50+
51+
2. etcd uses the key's modification revision number to determine which client has acquired the lock (the lowest wins).
52+
53+
3. If the key already exists under that prefix, the client waits until the previous holder releases the lock.
1354

14-
# another client with the same name blocks
15-
etcdctl --endpoints=$ENDPOINTS lock mutex1
55+
4. Once acquired, the given command is executed (`echo "I have the lock"`).
56+
57+
5. When the command finishes or the process dies, the lease is revoked and the lock is released.
58+
59+
## Releasing a lock
60+
61+
When using the `etcdctl lock` command, the lock is released as soon as the command completes.
62+
63+
```shell
64+
etcdctl lock locks/my-lock-name echo "Task done"
1665
```
66+
67+
When echo completes, `etcdctl` exits, revoking the lease and deleting the lock key.

0 commit comments

Comments
 (0)