FizzBee
GitHub Discord Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Optimistic Locking Pattern

Version-based concurrency control

Demonstrates: optimistic locking, version numbers, CAS

Run in playground
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# Example 0069: Optimistic Locking Pattern
# Version-based concurrency control
# Demonstrates: optimistic locking, version numbers, CAS

action Init:
    value = 0
    version = 0
    txn1_read_version = None
    txn1_new_value = None
    txn2_read_version = None
    txn2_new_value = None
    commits = 0

atomic action Txn1Read:
    require txn1_read_version == None

    txn1_read_version = version
    txn1_new_value = value + 1

atomic action Txn1Commit:
    require txn1_read_version != None
    require commits < 2

    # CAS: commit only if version unchanged
    if version == txn1_read_version:
        value = txn1_new_value
        version = version + 1
        commits = commits + 1

atomic action Txn2Read:
    require txn2_read_version == None

    txn2_read_version = version
    txn2_new_value = value + 10

atomic action Txn2Commit:
    require txn2_read_version != None
    require commits < 2

    # CAS: commit only if version unchanged
    if version == txn2_read_version:
        value = txn2_new_value
        version = version + 1
        commits = commits + 1

# Retry failed transaction
atomic action Txn2Retry:
    require txn2_read_version != None

    # Retry if version changed (CAS would fail)
    if version != txn2_read_version:
        txn2_read_version = version
        txn2_new_value = value + 10

# Safety: version increments with each commit
always assertion VersionMatches:
    return version == commits

# Safety: no lost updates (both can't commit if concurrent)
always assertion NoLostUpdates:
    if commits == 2:
        # Version should be 2, meaning sequential commits
        return version == 2
    return True

atomic action NoOp:
    if commits >= 2:
        pass

Expected result (from fizz):

StateSpaceOptions: options:{max_actions:100 max_concurrent_actions:2}
Nodes: 26, queued: 0
Valid Nodes: 26 Unique states: 26
IsLive: true
PASSED: Model checker completed successfully

Source on GitHub

Language features used

Assertions, Flow Modifiers