Semantic Locks and Concurrent Sagas
Handle concurrency in sagas using countermeasures like semantic locks, commutative updates, and pessimistic views to prevent dirty reads and lost updates across overlapping transactions.
Semantic Locks and Concurrent Sagas is a free Microservices Communication Patterns (Saga, Circuit Breaker) lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Microservices Communication Patterns (Saga, Circuit Breaker) learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
The Concurrency Problem
Sagas relax isolation: intermediate states are visible to other transactions. When two sagas touch the same data concurrently, you risk dirty reads, lost updates, and fuzzy reads.
This lesson covers countermeasures that restore safety without full ACID isolation.
Anomaly: Lost Update
A lost update happens when one saga overwrites a change made by another that it did not see.
- Saga A reads balance 100.
- Saga B reads balance 100, subtracts 30, writes 70.
- Saga A subtracts 50 from its stale 100, writes 50.
B's deduction is lost.
Countermeasure: Semantic Lock
A semantic lock marks a record as in-progress using an application-level flag, not a database lock.
For example, set an order's status to PENDING. Other sagas see the flag and refuse to act until the saga commits or compensates.
order = {'id': 1, 'status': 'PENDING'}
def can_modify(order):
return order['status'] != 'PENDING'
print('Can modify:', can_modify(order))Releasing the Lock
The semantic lock is released by the final saga step or by the compensating transaction. The status moves to APPROVED or back to AVAILABLE.
def finalize(order, success):
order['status'] = 'APPROVED' if success else 'CANCELLED'
return order['status']
order = {'status': 'PENDING'}
print(finalize(order, True))Countermeasure: Commutative Updates
Design updates that can be applied in any order and produce the same result. Addition and subtraction on a balance are commutative; setting an absolute value is not.
Prefer balance += delta over balance = newValue.
balance = 100
# two sagas apply deltas in any order
for delta in [-30, -50]:
balance += delta
print('Final balance:', balance)Countermeasure: Pessimistic View
Reorder saga steps so that the steps most likely to fail run first, minimizing the window during which dirty data is exposed.
If a risky step succeeds early, later steps are far less likely to need compensation.
Countermeasure: Reread Value
Before writing, reread the record and verify it has not changed since you read it. If it changed, abort and retry. This is optimistic concurrency control.
def safe_write(current_version, expected_version):
if current_version != expected_version:
return 'ABORT: data changed'
return 'WRITE OK'
print(safe_write(5, 5))
print(safe_write(6, 5))Version Numbers and Optimistic Locking
Store a version column with each record. Each update increments the version and includes the expected version in the WHERE clause.
- If zero rows update, someone else changed it first.
- The saga retries with fresh data.
Countermeasure: By Value
Choose your concurrency strategy by the business risk of the data. High-value records (large payments) get strict semantic locks; low-risk records use looser, more available approaches.
Combining Countermeasures
Real systems mix several techniques: a semantic lock to mark in-progress orders, commutative updates for counters, and version checks for critical writes. The goal is to keep sagas correct while preserving availability.
Trade-offs
Every countermeasure adds complexity. Semantic locks can cause contention; rereads add round-trips. Choose the lightest mechanism that prevents the anomalies your domain actually cares about.
Quick Check
Which countermeasure marks a record as in-progress using an application-level status so other sagas refuse to act on it?
Recap
You learned countermeasures for concurrent sagas:
- Semantic locks flag in-progress records.
- Commutative updates make order-of-application irrelevant.
- Pessimistic view reorders risky steps first.
- Reread/version checks catch concurrent changes.
Together these restore safety without full ACID isolation.
Frequently asked questions
Is the “Semantic Locks and Concurrent Sagas” lesson free?
Yes — the full text of “Semantic Locks and Concurrent Sagas” is free to read here on the web, and the Microservices Communication Patterns (Saga, Circuit Breaker) course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Microservices Communication Patterns (Saga, Circuit Breaker) course, upgrade to CoddyKit PRO.
What will I learn in “Semantic Locks and Concurrent Sagas”?
Handle concurrency in sagas using countermeasures like semantic locks, commutative updates, and pessimistic views to prevent dirty reads and lost updates across overlapping transactions. You practise Microservices Communication Patterns (Saga, Circuit Breaker) with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Microservices Communication Patterns (Saga, Circuit Breaker)?
No prior experience is required. Microservices Communication Patterns (Saga, Circuit Breaker) on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Semantic Locks and Concurrent Sagas” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Microservices Communication Patterns (Saga, Circuit Breaker) lesson?
Yes. Every Microservices Communication Patterns (Saga, Circuit Breaker) lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Ensuring Idempotency in Sagas
- Retry Strategies for Sagas
- Advanced Compensation Logic
- Semantic Locks and Concurrent Sagas