A small, runnable demonstration of one architectural decision that quietly decides whether a distributed system stays simple or rots:
When a service needs data it doesn't own, does it ask for it (pull), or listen for it (push)?
Both approaches are built here, side by side, over a generic entitlements/permissions domain. A four-scenario demo runs the same storyline through each and prints where they diverge. Tests cover the projection logic and the replay/recovery path under the race detector.
Standard library only. No Kafka, no Postgres, no Docker. An in-process append-only log stands in for the broker so the whole thing runs with one command.
go run . # the four-scenario demo
go test ./... # projection + replay/recovery + pull failure modes
go test -race ./... # the same, under the race detectorA service needs to answer a question it doesn't own the data for — "is this user allowed to issue refunds?" Permissions live in another service. There are exactly two ways to get that answer, and the entire design follows from which one you pick.
Pull: call the owning service, synchronously, on every request. Because a synchronous network call in your hot path is fragile, you wrap it in armor — a cache, a TTL, retries, a fallback. This is the default almost everyone reaches for, because it's the smallest change: one function that makes one call.
Push: have the owner publish an event every time something changes, keep a local copy of just the data you care about, and read that local copy. The owner can be slow or completely down and your reads keep working, because the data is already next to you.
The claim this repo makes — and demonstrates in runnable code — is that for the right kind of data, pull's armor is interest payments on a coupling you could have designed away, and push doesn't so much add resilience as remove the thing you needed resilience for. It also makes the honest counter-case: push relocates complexity rather than deleting it, and for a lot of data pull is the correct, cheaper choice.
pull.go is a consumer that calls the owner per request, with a cache, a TTL, and retries.
func (p *PullConsumer) Allowed(subject, perm string) (bool, error) {
key := subject + "|" + perm
if e, ok := p.cache[key]; ok && p.now().Sub(e.at) < p.ttl {
return e.granted, nil // may be stale — failure mode #1
}
var lastErr error
for attempt := 0; attempt <= p.maxRetry; attempt++ {
granted, err := p.owner.Lookup(subject, perm)
if err == nil {
p.cache[key] = cacheEntry{granted: granted, at: p.now()}
return granted, nil
}
lastErr = err
time.Sleep(time.Millisecond)
}
return false, lastErr // owner down on a cold key — failure mode #2
}Exactly one line — owner.Lookup — is the actual read. Everything else exists to survive the ask, and it still loses in two ways that are designed in, not bugs:
- Silently stale. On a cache hit inside the TTL window, it returns the cached answer with a
nilerror — even if the owner changed it a moment ago. No error, no log, no page. If that answer gates money movement, you're doing the wrong thing and nothing tells you. A loud outage wakes someone; this doesn't, which makes it the more dangerous failure. - Unavailable on a cold read. If the owner is down and the key was never cached, retries exhaust and you return an error. You have no answer to give, because you outsourced having one. The owner's reliability is now a hard ceiling on yours.
The TTL is the tell: turn it down to shrink staleness and you add load and outage exposure; turn it up to cut load and you widen the window where you're wrong. The cache doesn't resolve that tradeoff, it just gives you a knob to slide it along. That's the signature of treating a symptom.
push.go consumes the owner's event log into a local materialized view and reads from that view. The read path can't even return an error:
func (p *PushConsumer) Allowed(subject, perm string) bool {
p.mu.RLock()
defer p.mu.RUnlock()
return p.view[subject][perm]
}The view is built by folding events, and the fold is idempotent on the log sequence number — which is what makes replay and at-least-once delivery safe:
func (p *PushConsumer) apply(e Event) {
p.mu.Lock()
defer p.mu.Unlock()
if e.Seq <= p.cursor { // already applied — no-op. The whole ballgame.
return
}
if p.view[e.Subject] == nil {
p.view[e.Subject] = map[string]bool{}
}
p.view[e.Subject][e.Perm] = e.Granted
p.cursor = e.Seq
}On boot it replays history, then follows the live stream — stitched together with no gap by capturing the log's high-water mark at subscribe time:
func (p *PushConsumer) Start() {
ch, highWater, cancel := p.log.Subscribe()
for _, e := range p.log.Read(-1) { // replay history up to the mark...
if e.Seq > highWater {
break // anything past it arrives on the live channel
}
p.apply(e)
}
go func() { // ...then follow live
defer cancel()
for {
select {
case e := <-ch:
p.apply(e)
case <-p.done:
return
}
}
}()
}No cache (the view is the data), no TTL (nothing goes stale between calls — changes arrive as they happen), no retries (there's no call to retry). The resilience didn't get engineered; it fell out of the shape of the dependency.
go run . runs one storyline — a permission granted, revoked, re-granted, with an owner outage — through both consumers:
| Scenario | Pull (sync + cache + TTL + retry) | Push (local view from log) |
|---|---|---|
| 1. Both healthy | correct | correct |
| 2. Permission revoked | serves stale within TTL, no error | correct — applied the revoke |
| 3. Owner outage, cold read | fails — blocked on owner uptime | correct — never calls the owner |
| 4. Fresh consumer boots mid-outage | n/a | rebuilds its whole view from the log, zero owner contact |
## 2. Permission revoked — pull serves stale, push correct
pull answer: true <- STALE within its 5s TTL, and no error raised
push answer: false <- correct; it applied the revoke fact
## 3. Owner outage, cold read — pull fails, push unaffected
pull answer: error = entitlements owner: unavailable
push answer: true <- no owner call on the read path
## 4. Fresh consumer boots during the outage — rebuilds from the log
new consumer rebuilt view up to seq=3 with the owner unreachable
Scenario 2 is the one to sit with: the pull consumer isn't erroring, it's confidently wrong. Scenario 4 is the payoff: a brand-new consumer, booted while the owner is still down, rebuilds its entire view from the log and answers correctly — having never once reached the system it depends on. That's not resilience you added; it's resilience you stopped needing, because the log is the contract and the log was still there.
This is deliberately tiny so the pattern is legible. The production shapes it stands in for:
| In this repo (stdlib) | In production |
|---|---|
EventLog (append-only slice) |
a Kafka topic entitlements, keyed by subject |
EventLog.Append |
a producer send |
EventLog.Read(after) |
a consumer fetch from an offset |
EventLog.Subscribe + high-water mark |
a consumer group with a committed offset |
PushConsumer.view (in-memory map) |
a local read model in Postgres or Redis, owned by the consumer |
PushConsumer.cursor |
the consumer's committed offset / projection checkpoint |
apply idempotent on Seq |
an idempotent projection under at-least-once delivery |
Owner.Lookup |
a synchronous gRPC/HTTP call to the owning service |
Push does not delete complexity — it relocates it from read time to write time, and if you adopt it thinking it's free, the bill arrives later. Three things become your problem that weren't before:
Eventual consistency is now yours. The local view lags the owner by the propagation delay. For "can this user export reports?" that lag is milliseconds and irrelevant. For "what is this balance exactly, the instant after a write?" that lag is a correctness bug. Don't reach for events to satisfy a read-your-writes requirement that events make harder.
The projection is code you own and can get wrong. The if e.Seq <= p.cursor guard in apply is load-bearing: it's what makes a duplicate delivery a no-op and lets the replay/live seam overlap safely. Get it wrong and the view silently corrupts. In this toy it's one line; in production it's a real projection with a committed offset, consumer-lag monitoring, and somewhere for the read model to live.
The publish must be atomic with the write — and this demo doesn't model that. owner.go commits its state change and then appends to the log, as two steps. A crash in between loses the event forever, and no amount of replay recovers a fact that was never written. The real pattern is a transactional outbox (write the event in the same DB transaction as the state change, relay it afterward) or change-data-capture off the owner's write-ahead log. This is the single most important thing the stdlib version under-models, and it's called out here so nobody ships the two-step version by accident.
The pull version wears its costs on its sleeve — you can see the cache and the retries. Push hides its costs at write time and in operations, where they're easy to miss until they bite. The honest comparison isn't "armor vs. no armor"; it's "costs you can see at read time vs. costs you have to go looking for at write time." You're choosing which set of problems to own.
This repo argues for push, so it owes you the other side. Pull is correct when:
- The data is genuinely request-scoped and can't be held — a live quote, a one-time token, something only true this instant.
- The call is to something you own and deploy together, so it isn't really a cross-team boundary — a function call with extra steps.
- The coupling is cheap and the blast radius small — a quick internal lookup nobody's reliability depends on.
- You need strong read-your-writes and can't tolerate projection lag — the one place eventual consistency is a bug, a synchronous read of the source of truth is simpler and more correct.
- Volume is low and the team is small — a cache with a TTL is a few lines everyone understands; a projection is infrastructure to own and monitor. Don't build the broker before the boundary hurts.
The inversion earns its keep in the opposite conditions: data read far more than it changes, needed by more than one service, where being wrong or unavailable costs money — permissions, pricing, entitlements, account status, anything on the payment path. That's exactly where teams reach for pull by reflex and then spend quarters armoring it. The skill isn't knowing the pattern; it's knowing which column you're in.
Before you add another retry, cache, or circuit breaker to a cross-service call, ask whether the call needs to exist at all.
Senior engineers add resilience to a bad boundary. Staff engineers delete the boundary.
The best resilience code is the code you never wrote, because the data was already local when the question came in.
eventlog.go append-only log: Append / Read(after) / Subscribe (stands in for Kafka)
owner.go the service that owns the data: writes + sync Lookup + an outage toggle
pull.go synchronous consumer: cache + TTL + retry (resilience on a kept boundary)
push.go event-driven consumer: local view, replay + live follow (boundary deleted)
main.go the four-scenario demo
pull_test.go the two pull failure modes, asserted (stale-within-TTL, fail-on-cold-outage)
push_test.go projection logic + replay/recovery + the no-gap replay/live seam
MIT — see LICENSE.