의미적 잠금 및 동시 사가
의미적 잠금, 교환 법칙이 성립하는 업데이트, 비관적 뷰 등의 대응책을 사용해 사가의 동시성을 처리하고 겹치는 트랜잭션 사이의 더티 리드와 업데이트 손실을 방지해 보세요.
의미적 잠금 및 동시 사가은(는) CoddyKit의 무료 Microservices Communication Patterns (Saga, Circuit Breaker) 강의입니다. 이것은 4개 중 4번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 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.
자주 묻는 질문
“의미적 잠금 및 동시 사가” 강의는 무료인가요?
네 — “의미적 잠금 및 동시 사가” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Microservices Communication Patterns (Saga, Circuit Breaker) 강의 전체를 잠금 해제할 수 있습니다. Microservices Communication Patterns (Saga, Circuit Breaker) 강의에는 총 4개의 강의가 포함되어 있습니다.
“의미적 잠금 및 동시 사가”에서 뭘 배우나요?
의미적 잠금, 교환 법칙이 성립하는 업데이트, 비관적 뷰 등의 대응책을 사용해 사가의 동시성을 처리하고 겹치는 트랜잭션 사이의 더티 리드와 업데이트 손실을 방지해 보세요. 브라우저에서 직접 실행하는 실습 코드로 Microservices Communication Patterns (Saga, Circuit Breaker)을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.
Microservices Communication Patterns (Saga, Circuit Breaker)을(를) 시작하는 데 경험이 필요한가요?
사전 경험은 필요하지 않습니다. CoddyKit의 Microservices Communication Patterns (Saga, Circuit Breaker)은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 4번째 강의입니다.
“의미적 잠금 및 동시 사가” 강의는 얼마나 걸리나요?
대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.
이 Microservices Communication Patterns (Saga, Circuit Breaker) 강의에서 코드를 작성하고 실행할 수 있나요?
네. 모든 Microservices Communication Patterns (Saga, Circuit Breaker) 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.
이 강의의 모든 강의
- 사가의 멱등성 보장
- 사가를 위한 재시도 전략
- 고급 보상 로직
- 의미적 잠금 및 동시 사가