セマンティックロックと並行Saga
セマンティックロック、可換な更新、悲観的ビューなどの対策を使ってSagaの並行性を制御し、重複するトランザクション間でダーティリードや更新の消失を防ぐ方法を学びます。
「セマンティックロックと並行Saga」はCoddyKit上の無料Microservices Communication Patterns (Saga, Circuit Breaker)レッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはMicroservices Communication Patterns (Saga, Circuit Breaker)学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Microservices Communication Patterns (Saga, Circuit Breaker)コースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
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.
AI チューターと学ぶ Microservices Communication Patterns (Saga, Circuit Breaker) — 無料
ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。
- コース
- 12
- レッスン
- 48
よくある質問
「セマンティックロックと並行Saga」レッスンは無料ですか?
はい。「セマンティックロックと並行Saga」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Microservices Communication Patterns (Saga, Circuit Breaker)コースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Microservices Communication Patterns (Saga, Circuit Breaker)コースには全4レッスンが含まれています。
「セマンティックロックと並行Saga」で何を学びますか?
セマンティックロック、可換な更新、悲観的ビューなどの対策を使ってSagaの並行性を制御し、重複するトランザクション間でダーティリードや更新の消失を防ぐ方法を学びます。 ブラウザで直接実行するハンズオンコードでMicroservices Communication Patterns (Saga, Circuit Breaker)を演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Microservices Communication Patterns (Saga, Circuit Breaker)を始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのMicroservices Communication Patterns (Saga, Circuit Breaker)は初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「セマンティックロックと並行Saga」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このMicroservices Communication Patterns (Saga, Circuit Breaker)レッスンでコードを書いて実行できますか?
はい。すべてのMicroservices Communication Patterns (Saga, Circuit Breaker)レッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- Sagaにおける冪等性の確保
- Sagaの再試行戦略
- 高度な補償ロジック
- セマンティックロックと並行Saga