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

Update kv_walkthrough.md #751

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
16 changes: 11 additions & 5 deletions nats-concepts/jetstream/key-value-store/kv_walkthrough.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,26 +113,32 @@ Updates can also be used for more fine-grained concurrency control, sometimes kn
### Create (aka exclusive locking)
Create a lock/semaphore with the `create` operation.
```shell
nats kv create my-sem Semaphore1 Value1
nats kv create my-kv Semaphore1 Value1
```
Only one `create` can succeed. First come, first serve. All concurrent attempts will result in an error until the key is deleted
```shell
nats kv create my-sem Semaphore1 Value1
nats kv create my-kv Semaphore1 Value1
nats: error: nats: wrong last sequence: 1: key exists
```

### Update with CAS (aka optimistic locking)
We can also atomically `update`, sometimes known as a CAS (compare and swap) operation, a key with an additional parameter `revision`

```shell
nats kv update my-sem Semaphore1 Value2 13
nats kv update my-kv Semaphore1 Value2 0
```

A second attempt with the same revision 13, will fail

```shell
nats kv update my-sem Semaphore1 Value2 13
nats: error: nats: wrong last sequence: 14
nats kv update my-kv Semaphore1 Value2 0
nats: error: nats: wrong last sequence: 1
```

You can view the history and revision value of a key using the `history` command

```shell
nats kv history my-kv Semaphore1
```

## Watching a K/V Store
Expand Down