etcd
and etcdctl
.etcd
cluster.Here are definations of some key terms used in the Example below.
Terms | Defination |
---|---|
etcdctl | The command line tool for interacting with the etcd server. |
txn command | txn command is an abbreviation for “transaction”. It reads multiple etcd requests from standard input and applies them as a single atomic transaction. A transaction consists of list of conditions, a list of requests to apply if all the conditions are true, and a list of requests to apply if any condition is false. View etcdctl key-value commands for more information. |
compare | The compare clause within a transaction (txn ) serves as a conditional check that determines whether the transaction’s operations should proceed. It ensures changes are only applied if the current state of the key-value store matches expected conditions, thereby maintaining data consistency and preventing conflicts in concurrent environments. To see how the command is structured, view Perform a transaction section below. |
txn
to process all the requests in one transaction:
etcdctl txn --help
Transactions in etcd allow you to execute multiple operations atomically, ensuring that either all operations are applied or none are. This is crucial for maintaining data consistency when performing related updates. Learn more about transactions in the API documentation.
Let’s consider a scenario where you want to update a user’s email and phone number in a single transaction. This ensures that both updates are applied together.
Variables |
---|
/users/{<user_id>/email : etcd key representing a user’s email address. |
/users/<user_id>/phone : etcd key representing a user’s phone number. |
Flags |
--interactive : A flag to allow inputting transaction data manually |
First, create a user with some initial data.
etcdctl put /users/12345/email "old.address@johndoe.com"
etcdctl put /users/12345/phone "123-456-7890"
Update the user’s email and phone number in a single transaction.
etcdctl txn --interactive
compares:
value("/users/12345/email") = "old.address@johndoe.com"
success requests (get, put, delete):
put /users/12345/email "new.address@johndoe.com"
put /users/12345/phone "098-765-4321"
failure requests (get, put, delete):
get /users/12345/email
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.