Lecture 04. Concurrency, idempotency and Saga workflows
Week 7 · Syllabus and preparation from the course outline; no study date or personal lecture notes have been recorded.
Business scenario
Only one meal remains. Concurrent customers submit orders, payment requests time out, and events can be delivered more than once.
Topics to cover in this class
- Concurrent updates, atomic SQL statements, transactions and unique constraints
- Idempotency keys, deduplication records and key/payload conflict semantics
- Saga orchestration versus choreography and compensating business operations
- Inventory reservation, payment simulation, cancellation and retry behavior
- Durable state transitions, the outbox/reconciliation problem and observability
Learning objectives
- Demonstrate no overselling under concurrent requests.
- Prevent duplicate simulated payments despite retries/redelivery.
- Recover failed workflows with compensation or explicit reconciliation.
Live demonstration checklist
- Run 20 concurrent reservation requests for stock=1, verify at most one success.
- Force a payment failure and inspect compensated inventory + terminal order state.
Preparation and optional written reflection (ungraded)
Reading focus: Read a Saga article; compare compensation with a real database rollback. Optional individual reflection: Why can a local SQL transaction not atomically cover the database of two independent services?
Study materials: official docs, websites and YouTube
Official | PostgreSQL transaction isolation | https://www.postgresql.org/docs/current/transaction-iso.html Isolation levels and concurrent update behavior
Official | PostgreSQL constraints | https://www.postgresql.org/docs/current/ddl-constraints.html Uniqueness and integrity checks
Website | Saga pattern (microservices.io) | https://microservices.io/patterns/data/saga.html Choreography and orchestration
Website | Transactional outbox pattern | https://microservices.io/patterns/data/transactional-outbox.html Publishing events safely after DB changes
YouTube | Saga Pattern (ByteMonk) | https://www.youtube.com/watch?v=d2z78guUR4g Distributed transaction motivation and patterns