# Optimistic Locking

> A design pattern that checks a record's version when it is written and rejects the write if another writer changed it first, comparing only the writer's own span where a surface is fenced per writer.

Record: `architecture:optimistic-locking`
Kind: pattern
Layer: [Atomic Boundary](https://banes-lab.com/records/layer/atomic-boundary.md)
Severity: contextual
Scope: persistence, transaction
Aliases: Own-Span Compare-and-Swap
Canonical: https://banes-lab.com/ontology#architecture-optimistic-locking

Listed in [Architecture principles](https://banes-lab.com/api/records/architecture.md), after [Concurrency Control](https://banes-lab.com/records/architecture/concurrency-control.md) and before [Pessimistic Locking](https://banes-lab.com/records/architecture/pessimistic-locking.md).

## Repair

- Refactored by: Add Version Column, Add Compare-And-Swap
- Detected by: updates without version check
- Violated by: [lost update](https://banes-lab.com/records/architecture/lost-update.md)
- Measured by: conflict/retry rate
- Enforced by: repository rules, integration tests

## Requires

- [Version Field](https://banes-lab.com/records/lexicon/version-field.md)

## Reinforces

- [Concurrency Control](https://banes-lab.com/records/architecture/concurrency-control.md)

## Enables

- [Conflict Detection](https://banes-lab.com/records/lexicon/conflict-detection.md)

## Conflicts with

- [Blind Overwrite](https://banes-lab.com/records/lexicon/blind-overwrite.md)

## In tension with

- [Retry Complexity](https://banes-lab.com/records/lexicon/retry-complexity.md)

## Tensions

- [Optimistic Locking / Retry Complexity](https://banes-lab.com/records/tension/optimistic-locking-retry-complexity.md)

## Severity

- [contextual](https://banes-lab.com/records/vocabulary/severity-contextual.md)

## Category

- [Transactions / State / Concurrency](https://banes-lab.com/records/architecture-category/transactions-state-concurrency.md)

## Reinforced by

- [Write Barrier](https://banes-lab.com/records/architecture/write-barrier.md)

## Linked from

- [Verify the verifier](https://banes-lab.com/disciplined-methodology/verify/verify-the-verifier.md)
- [Posting and waiting are one operation](https://banes-lab.com/disciplined-methodology/collaborate/posting-and-waiting-are-one-operation.md)
- [Anti-patterns](https://banes-lab.com/ontology/principles/architecture-category-anti-patterns.md)
- [Coordination Surfaces](https://banes-lab.com/ontology/principles/architecture-category-coordination-surfaces.md)
- [Transactions / State / Concurrency](https://banes-lab.com/ontology/principles/architecture-category-transactions-state-concurrency.md)
- [Transactions / State / Concurrency](https://banes-lab.com/ontology/lexicon/lexicon-category-transactions-state-concurrency.md)
- [Severity levels](https://banes-lab.com/ontology/schema/the-vocabulary-severity.md)
- [The resolutions](https://banes-lab.com/ontology/schema/the-resolutions.md)
