# Check-and-set

> A write names the version it expects to replace, so two users changing one secret never silently overwrite each other.

When two users or two tools change the same secret at once, the later write would normally wipe out the earlier one
without either noticing. With check-and-set, a write says which version it was based on. If someone else wrote first,
the write fails instead, and the writer reads again and decides.

## Before you begin

- **Who:** anyone who may write the secret. Requiring check-and-set for one path is for anyone with every right in its
  vault; for the whole realm, a realm administrator (see [Realm settings](/secrets-store/realm-settings/)).
- **Where:** the API. There are no screens yet.
- **First:** know the latest version: read the secret, or its [metadata](/secrets-store/secrets/metadata/).

## How it works

A write or a change carries `"options": { "cas": <n> }`:

| `cas` sent | The write goes through when |
|---|---|
| none | always, unless check-and-set is required |
| `0` | the path doesn't exist yet |
| `<n>` | version `<n>` is the latest |

A write whose `cas` doesn't match answers `400`, and says what the latest version is. Two writes that arrive together
are applied one after the other, each checked against the version the other left, so exactly one of them succeeds.

## Set it up

Write `deploy/key` in the `engineering` group's vault, based on version 3 ([Call the API](/develop/api/) shows how to
get `$CVYUH_SESSION`):

```sh
curl https://foobar.cvyuh.com/api/v0/vault/managed/v1/group/engineering/data/deploy/key \
  -H "Cookie: x-cvyuh-token=$CVYUH_SESSION" \
  -H "Content-Type: application/json" \
  -d '{ "options": { "cas": 3 }, "data": { "key": "<deploy key>" } }'
```

To make every write name its version:

- **For one path**, as its owner: `POST` `{ "cas_required": true }` to the path's
  [metadata](/secrets-store/secrets/metadata/).
- **For the whole realm**, as a realm administrator: set `managed.casRequired` to `true`. See
  [Realm settings](/secrets-store/realm-settings/).

## When it fails

| Answer | When | What to do |
|---|---|---|
| `400`, "check-and-set parameter did not match the current version" | Someone wrote since you read; the answer's detail names the latest version | Read the latest, decide, and write again with its version |
| `400`, "check-and-set parameter required for this call" | The path or the realm requires check-and-set and the write sent none | Send `options.cas` |
| `400` with `cas: 0` | The path already exists | Read it, and write with its version instead |

## Limits and defaults

| Limit | Default | Range |
|---|---|---|
| Check-and-set required, realm-wide | `false` | `true` or `false` |
| Check-and-set required, one path | `false` | `true` or `false`; can add the requirement, never lift the realm's |

## Troubleshooting

| Symptom | Cause | Fix |
|---|---|---|
| A scheduled job's writes all fail with `400` | It writes without `cas` and the realm now requires it | Have the job read the version first and send it |
| Two writers keep failing each other | Both retry with the version they read before the other's write | Read again after each failure before retrying |

## Audit record

A write that goes through is an `UPDATED` change record, as any write; a refused one changes nothing and is recorded
only as an `ACCESS` record with its `400`. Setting `cas_required` on a path is an `UPDATED` change record. See
[Secrets audit trail](/secrets-store/keys-and-protection/audit-trail/).

## Good to know

- **A path can add the requirement, never lift it.** A path's `cas_required: false` doesn't override the realm's
  `managed.casRequired`.
- **Changes count too.** A `PATCH` to the path's `data` takes `cas` the same way.
- **`cas: 0` creates only.** Use it to write a secret only if no one has written it yet.

## Related

- [Change a secret safely](/secrets-store/guides/change-a-secret-safely/): the task, start to finish.
- [Versions](/secrets-store/secrets/versions/): what each write makes.
