What is the Go memory model and what does the happens-before relationship mean?
Understand the Go memory model and happens-before: how synchronization guarantees visibility across goroutines, with examples and data-race pitfalls.
Expected Interview Answer
The Go memory model specifies the conditions under which a read of a variable in one goroutine is guaranteed to observe a write from another goroutine, defined through a partial ordering of events called the happens-before relationship.
If event A happens-before event B, then A's effects are visible to B. Within a single goroutine, program order establishes happens-before. Across goroutines, you need explicit synchronization — channel sends/receives, mutex Lock/Unlock, sync.Once, or sync/atomic — to create happens-before edges. Without such an edge, the compiler and CPU are free to reorder or cache operations, so a read may see a stale value or no value at all; that is a data race, and race-free programs behave as if operations execute in the specified order.
- Defines exactly when a write is guaranteed visible to another goroutine
- Lets you reason about correctness of concurrent code
- Explains why unsynchronized shared access is undefined
- Grounds the guarantees of channels, mutexes and atomics
- Enables safe lock-free patterns using sync/atomic
AI Mentor Explanation
The happens-before relationship is like the umpire's signal that a run is completed before the next ball is bowled: only once the striker has grounded the bat behind the crease is the run official and visible on the scoreboard. Without that confirmed signal, a fielder and the scorer might disagree about the total. Synchronization in Go is that umpire's call — it establishes the fixed order in which one goroutine's work becomes officially visible to the next.
Step-by-Step Explanation
Step 1
Understand program order
Within one goroutine, statements happen-before later statements as written.
Step 2
Identify shared access
Find variables written by one goroutine and read by another.
Step 3
Add a synchronization edge
Use a channel, mutex, sync.Once, or atomic to create a happens-before relationship across goroutines.
Step 4
Rely on the guarantee
A channel receive happens-after the corresponding send, so the reader sees the writer's earlier writes.
Step 5
Detect races
Run with -race to catch any access pairs lacking a happens-before edge.
What Interviewer Expects
- Definition of the memory model as visibility rules
- Happens-before as a partial order of events
- Program order within a goroutine
- Synchronization primitives that create happens-before edges
- Understanding that data races are undefined behavior
Common Mistakes
- Assuming writes are instantly visible across goroutines
- Believing a data race just produces a wrong value, not undefined behavior
- Thinking a single Lock without a matching Unlock ordering helps
- Using plain reads/writes for flags instead of atomics or channels
- Confusing happens-before with wall-clock time ordering
Best Answer (HR Friendly)
“The Go memory model is the set of rules that says when a value written by one part of a program is guaranteed to be seen by another part running at the same time. Happens-before is the ordering that makes that guarantee — you create it with tools like channels and locks, and without it different goroutines may see stale data.”
Code Example
var msg string
done := make(chan bool)
go func() {
msg = "hello" // write
done <- true // send happens-before receive
}()
<-done // receive
fmt.Println(msg) // guaranteed to see "hello"var ready bool
var data int
go func() {
data = 42
ready = true // no happens-before edge
}()
for !ready { // may never see ready==true, or see data==0
}
fmt.Println(data) // undefined: run with -race to detectFollow-up Questions
- How does a channel send create a happens-before relationship?
- What guarantees does sync.Mutex provide in the memory model?
- Why is a data race undefined behavior rather than just a stale read?
- How does sync/atomic fit into the happens-before rules?
- What ordering does sync.Once.Do guarantee?
MCQ Practice
1. What does the happens-before relationship guarantee in Go?
If A happens-before B, the memory effects of A are guaranteed visible to B; it is about visibility and ordering, not real time.
2. Which of these creates a happens-before edge between goroutines?
A send on a channel happens-before the corresponding receive completes, making prior writes in the sender visible to the receiver.
3. What is a data race in the Go memory model?
A data race is unsynchronized concurrent access where at least one access is a write; it is undefined behavior in Go.
Flash Cards
What is the Go memory model? — Rules defining when a read is guaranteed to observe a particular write across goroutines.
What is happens-before? — A partial ordering of events; if A happens-before B, A's effects are visible to B.
How is happens-before created across goroutines? — Via synchronization: channels, mutexes, sync.Once, or sync/atomic operations.
What is a data race? — Unsynchronized concurrent access with at least one write — undefined behavior in Go.
Continue Learning
Related Interview Questions
What is a Mutex in Go and how does it differ from a channel for synchronization?
medium
What are channels in Go and how do they enable communication between goroutines?
medium
What is the difference between buffered and unbuffered channels in Go?
medium
How do you detect and avoid race conditions in Go?
medium