Microservices Communication Patterns (Saga, Circuit Breaker) · Lektion

Semantische Sperren und nebenläufige Sagas

Bewältigen Sie Nebenläufigkeit in Sagas mit Gegenmaßnahmen wie semantischen Sperren, kommutativen Aktualisierungen und pessimistischen Sichten, um Dirty Reads und verlorene Aktualisierungen bei überlappenden Transaktionen zu verhindern.

Lektion 4 von 413 Schritte

Semantische Sperren und nebenläufige Sagas ist eine kostenlose Microservices Communication Patterns (Saga, Circuit Breaker)-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Microservices Communication Patterns (Saga, Circuit Breaker)-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Microservices Communication Patterns (Saga, Circuit Breaker)-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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.

Kostenlos starten

Lerne Microservices Communication Patterns (Saga, Circuit Breaker) mit einem KI-Tutor — kostenlos

Schreibe und führe echten Code in deinem Browser aus, bekomme sofortige Hilfe von einem 24/7 KI-Tutor und setze dein Lernen im Web oder in der App fort.

Kurse
12
Lektionen
48

Häufig gestellte Fragen

Ist die Lektion „Semantische Sperren und nebenläufige Sagas“ kostenlos?

Ja — der vollständige Text von „Semantische Sperren und nebenläufige Sagas“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Microservices Communication Patterns (Saga, Circuit Breaker)-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Microservices Communication Patterns (Saga, Circuit Breaker)-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Semantische Sperren und nebenläufige Sagas“?

Bewältigen Sie Nebenläufigkeit in Sagas mit Gegenmaßnahmen wie semantischen Sperren, kommutativen Aktualisierungen und pessimistischen Sichten, um Dirty Reads und verlorene Aktualisierungen bei überl… Du übst Microservices Communication Patterns (Saga, Circuit Breaker) mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Microservices Communication Patterns (Saga, Circuit Breaker) zu starten?

Keine Vorkenntnisse erforderlich. Microservices Communication Patterns (Saga, Circuit Breaker) auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.

Wie lange dauert die Lektion „Semantische Sperren und nebenläufige Sagas“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Microservices Communication Patterns (Saga, Circuit Breaker)-Lektion Code schreiben und ausführen?

Ja. Jede Microservices Communication Patterns (Saga, Circuit Breaker)-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Idempotenz in Sagas sicherstellen
  2. Wiederholungsstrategien für Sagas
  3. Fortgeschrittene Kompensationslogik
  4. Semantische Sperren und nebenläufige Sagas
← Zurück zu Microservices Communication Patterns (Saga, Circuit Breaker)