All Journal Notes

High-Concurrency Microservice Resilience in Go: Circuit Breakers, Token Buckets & Distributed Tracing

How we built go-libs to protect cloud services against cascading downtime: Three-state circuit breakers, sliding window token bucket rate limiting, and 64-bit Sonyflake ID generation.

When an upstream microservice begins to degrade—whether due to database lock contention, memory leaks, or network packet loss—the default behavior of downstream services is to keep retrying.

In high-concurrency environments processing 10,000+ requests per second, these retries cause the infamous cascading stampede: healthy services exhaust their thread pools waiting on a degraded dependency, collapsing the entire infrastructure within seconds.

To safeguard distributed systems against this failure mode, we built [go-libs](https://github.com/Abeta-dev/go-libs): a battle-hardened suite of Go primitives for circuit breaking, adaptive rate limiting, and zero-allocation tracing.

Here is an in-depth review of the core components.


1. Three-State Circuit Breakers

A circuit breaker monitors outgoing requests and acts as an automatic safety switch. It transitions between three distinct states: 1. Closed: Requests flow normally. Failures are counted within a sliding time window. 2. Open: Once error rates exceed a configurable threshold (e.g. 25% failures over 10 seconds), the circuit trips. All subsequent calls fail immediately with ErrCircuitOpen, sparing the struggling upstream service. 3. Half-Open: After a cooldown period (e.g. 5 seconds), a trial batch of requests is allowed through. If successful, the circuit resets to Closed; if failures persist, it returns to Open.

go
package resilience

type CircuitBreaker struct {
    mu          sync.RWMutex
    state       State
    failures    int64
    successes   int64
    threshold   float64
    cooldown    time.Duration
    lastTripped time.Time
}

func (cb *CircuitBreaker) Execute(fn func() error) error {
    if !cb.canExecute() {
        return ErrCircuitOpen
    }

    err := fn()
    cb.recordResult(err == nil)
    return err
}

In go-libs, state transitions use atomic bitwise operations, keeping execution overhead under 4 nanoseconds per call.


2. Token Bucket Rate Limiting with Microsecond Precision

Protecting public API endpoints requires fine-grained traffic shaping. While naive fixed-window counters allow burst spikes at window boundaries, the Token Bucket algorithm provides smooth, deterministic throttling:

go
type TokenBucket struct {
    capacity     int64
    tokens       int64
    refillRate   int64 // Tokens per second
    lastRefill   time.Time
    mu           sync.Mutex
}

func (tb *TokenBucket) Allow() bool {
    tb.mu.Lock()
    defer tb.mu.Unlock()

    now := time.Now()
    elapsed := now.Sub(tb.lastRefill).Seconds()
    tb.tokens = min(tb.capacity, tb.tokens + int64(elapsed*float64(tb.refillRate)))
    tb.lastRefill = now

    if tb.tokens >= 1 {
        tb.tokens--
        return true
    }
    return false
}

3. Distributed 64-bit Sonyflake ID Generation

Traditional UUIDv4 identifiers consume 16 bytes and suffer from non-monotonic random distribution, fragmenting B-Tree database indexes and degrading insert performance by up to 40%.

go-libs provides a distributed Sonyflake ID generator that produces 64-bit unsigned integers: - 39 bits: Timestamp with 10-millisecond precision (lifetime ~174 years). - 8 bits: Sequence number within the same 10ms window (up to 256 IDs per node per 10ms). - 16 bits: Machine / worker node ID.

Because the most significant bits represent time, generated IDs are strictly monotonically increasing. Database indexes remain compact, and sorting by primary key yields exact chronological ordering with zero additional indexing cost.


4. Summary & Repository Access

Building resilient cloud architectures requires designing for failure as a first-class property. [go-libs](https://github.com/Abeta-dev/go-libs) provides Go engineers with the essential primitives required to withstand unexpected traffic spikes and partial network failures.

Explore the source code: github.com/Abeta-dev/go-libs.

UG

Umesh Gupta

@umesh0492

Founder & Software Architect

Founder of Abeta. Software architect focusing on high-throughput distributed systems, financial math engines, and @abeta.dev/react-libs.

View all articles by Umesh Gupta