Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(tutorial): update example of multiple writes #908

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 40 additions & 26 deletions content/en/docs/v3.5/tutorials/how-to-transactional-write.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,56 @@ description: Guide to making transactional writes
weight: 500
---

`txn` to wrap multiple requests into one transaction:

![05_etcdctl_transaction_2016050501](https://storage.googleapis.com/etcd/demo/05_etcdctl_transaction_2016050501.gif)
## Prerequisites

```shell
etcdctl --endpoints=$ENDPOINTS put user1 bad
etcdctl --endpoints=$ENDPOINTS txn --interactive
* Install [`etcd` and `etcdctl`](https://etcd.io/docs/v3.5/install/)

compares:
value("user1") = "bad"
## Transactions

success requests (get, put, delete):
del user1
`txn` to process all the requests in one transaction:

failure requests (get, put, delete):
put user1 good
```bash
etcdctl txn --help
```

### Transaction for multiple writes
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.

A transaction is an atomic If/Then/Else construct over the key-value store. It provides a primitive for grouping requests together in atomic blocks (i.e., then/else) whose execution is guarded (i.e., if) based on the contents of the key-value store. Transactions can be used for protecting keys from unintended concurrent updates, building compare-and-swap operations, and developing higher-level concurrency control. However, modifications to the same key multiple times within a single transaction are forbidden.
### Example

```shell
etcdctl --endpoints=$ENDPOINTS put user1 bad
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.

etcdctl --endpoints=$ENDPOINTS txn --interactive
compares:
value("user1") = "bad"
![05_etcdctl_transaction_2024101213](https://github.com/user-attachments/assets/01320212-b824-40b0-8a33-c6d74c600248)

success requests (get, put, del):
put user test
put user2 testing3
get user1
1. **Set up initial data**: First, create a user with some initial data.

failure requests (get, put, del):
put user1 bad
```
```shell
etcdctl put /users/12345/email "[email protected]"
etcdctl put /users/12345/phone "123-456-7890"
```

2. **Perform a transaction**: Update the user's email and phone number in a single transaction.

```shell
etcdctl txn --interactive

compares:
value("/users/12345/email") = "[email protected]"

success requests (get, put, delete):
put /users/12345/email "[email protected]"
put /users/12345/phone "098-765-4321"

failure requests (get, put, delete):
get /users/12345/email
```

* **Compare**: Check if the current email is "<[email protected]>". This ensures the transaction only proceeds if the data is as expected.
* **Success**: If the comparison is true, update both the email and phone number.
* **Failure**: If the comparison fails, retrieve the current email to understand why the transaction didn't proceed.

### Important considerations

{{< figure src="/img/transaction-multiple-writes.gif" >}}
* **Atomicity**: The transaction ensures that both the email and phone number are updated together. If the initial condition (comparison) is not met, neither update is applied.
* **Consistency**: Using transactions maintains data consistency, especially when dealing with multiple related updates.
* **Avoid multiple puts on the same key**: Do not put multiple values for the same key within a single transaction, as this can lead to unexpected results. Each key should be updated only once per transaction.
66 changes: 40 additions & 26 deletions content/en/docs/v3.6/tutorials/how-to-transactional-write.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,56 @@ description: Guide to making transactional writes
weight: 500
---

`txn` to wrap multiple requests into one transaction:

![05_etcdctl_transaction_2016050501](https://storage.googleapis.com/etcd/demo/05_etcdctl_transaction_2016050501.gif)
## Prerequisites

```shell
etcdctl --endpoints=$ENDPOINTS put user1 bad
etcdctl --endpoints=$ENDPOINTS txn --interactive
* Install [`etcd` and `etcdctl`](https://etcd.io/docs/v3.6/install/)

compares:
value("user1") = "bad"
## Transactions

success requests (get, put, delete):
del user1
`txn` to process all the requests in one transaction:

failure requests (get, put, delete):
put user1 good
```bash
etcdctl txn --help
```

### Transaction for multiple writes
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.

A transaction is an atomic If/Then/Else construct over the key-value store. It provides a primitive for grouping requests together in atomic blocks (i.e., then/else) whose execution is guarded (i.e., if) based on the contents of the key-value store. Transactions can be used for protecting keys from unintended concurrent updates, building compare-and-swap operations, and developing higher-level concurrency control. However, modifications to the same key multiple times within a single transaction are forbidden.
### Example

```shell
etcdctl --endpoints=$ENDPOINTS put user1 bad
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.

etcdctl --endpoints=$ENDPOINTS txn --interactive
compares:
value("user1") = "bad"
![05_etcdctl_transaction_2024101213](https://github.com/user-attachments/assets/01320212-b824-40b0-8a33-c6d74c600248)

success requests (get, put, del):
put user test
put user2 testing3
get user1
1. **Set up initial data**: First, create a user with some initial data.

failure requests (get, put, del):
put user1 bad
```
```shell
etcdctl put /users/12345/email "[email protected]"
etcdctl put /users/12345/phone "123-456-7890"
```

2. **Perform a transaction**: Update the user's email and phone number in a single transaction.

```shell
etcdctl txn --interactive

compares:
value("/users/12345/email") = "[email protected]"

success requests (get, put, delete):
put /users/12345/email "[email protected]"
put /users/12345/phone "098-765-4321"

failure requests (get, put, delete):
get /users/12345/email
```

* **Compare**: Check if the current email is "<[email protected]>". This ensures the transaction only proceeds if the data is as expected.
* **Success**: If the comparison is true, update both the email and phone number.
* **Failure**: If the comparison fails, retrieve the current email to understand why the transaction didn't proceed.

### Important considerations

{{< figure src="/img/transaction-multiple-writes.gif" >}}
* **Atomicity**: The transaction ensures that both the email and phone number are updated together. If the initial condition (comparison) is not met, neither update is applied.
* **Consistency**: Using transactions maintains data consistency, especially when dealing with multiple related updates.
* **Avoid multiple puts on the same key**: Do not put multiple values for the same key within a single transaction, as this can lead to unexpected results. Each key should be updated only once per transaction.