Middle · Concurrency
Data races versus business races
Why is a check-then-insert still wrong with a mutex around each operation?
Try explaining this aloud before opening the answer. State your assumptions and trace a boundary case.
Reveal a strong answer
A data race is concurrent unsynchronized conflicting access to memory. A logical race can occur even when each individual access is synchronized, if a multi-step invariant is not atomic.
Protect the entire read-check-write operation under one lock, use a database conditional write/unique constraint, or transfer ownership to one goroutine. Choose the scope where the invariant actually lives.
The race detector observes executed memory accesses. It can reveal data races on covered paths but cannot prove every interleaving safe or detect every distributed lost update.
Common trap
“The map methods lock internally, so the sequence is safe” misses the gap between methods.
Try it
Two requests each see no username and both create it. What is the final authority?
Worked solution
A uniqueness constraint in the authoritative store must decide. A process-local mutex cannot coordinate several replicas, and a precheck can only improve the user experience, not enforce uniqueness.
Change the constraint
Which deterministic concurrency test would demonstrate the gap using a barrier?
How did you do?
Self-assessment, not a compiler pass or interview score. Explain the reasoning unaided before marking understood.
Not marked. Progress stays on this browser, separate from course completion.
Primary references
Checked 2026-10-20. Use documentation for your pinned runtime, driver and tool version.