micro--go-micro
4311b73361
* docs: compare Go Micro with Google ADK in the comparison guide Adds a 'vs Agent Frameworks (Google ADK)' section: ADK builds an agent, Go Micro builds the distributed system the agent lives in (agents are services in the mesh). Covers the category difference, a feature table, when to choose each, and MCP/A2A interoperability. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CmdEY7pYmV5zzwCjNJ4ykL * docs: replace ADK comparison slogan with concrete explanation State plainly what each tool provides (ADK builds an agent process; Go Micro builds the surrounding service mesh) instead of marketing phrasing. * lint: apply golangci-lint autofixes; exclude ST1003 and demo errcheck Mechanical, behaviour-preserving fixes applied by 'golangci-lint run --fix': gofmt, misspell (US spelling), usestdlibvars (http.Method*/Status*), unconvert, and the auto-fixable staticcheck simplifications (QF*, S1017/S1019/S1023/S1039). Config: exclude ST1003 (remaining offenders are exported API renames, e.g. web.Id, which would break compatibility) and skip errcheck for examples/ and internal/harness/ (demo code where fire-and-forget is intentional). Build and test compilation verified. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CmdEY7pYmV5zzwCjNJ4ykL * lint: WIP cleanup checkpoint (errcheck config + partial fixes) Checkpoint of an in-progress golangci-lint cleanup (background pass). Builds cleanly; lint is not yet zero. Follow-up commit will complete the cleanup and switch CI to a blocking full-tree lint. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CmdEY7pYmV5zzwCjNJ4ykL --------- Co-authored-by: Claude <noreply@anthropic.com>
Registry Cache
Cache is a library that provides a caching layer for the go-micro registry.
If you're looking for caching in your microservices use the selector.
Features
- Caching: Caches registry lookups with configurable TTL
- Stale Cache Fallback: Returns stale cached data when registry is unavailable
- Singleflight Protection: Deduplicates concurrent requests for the same service
- Adaptive Throttling: Rate limits failed lookups to prevent cache penetration (new in v5)
Interface
// Cache is the registry cache interface
type Cache interface {
// embed the registry interface
registry.Registry
// stop the cache watcher
Stop()
}
Usage
Basic Usage
import (
"github.com/micro/go-micro/registry"
"github.com/micro/go-micro/registry/cache"
)
r := registry.NewRegistry()
cache := cache.New(r)
services, _ := cache.GetService("my.service")
Advanced Configuration
import (
"time"
"github.com/micro/go-micro/registry"
"github.com/micro/go-micro/registry/cache"
)
r := registry.NewRegistry()
// Configure cache with custom options
cache := cache.New(r,
cache.WithTTL(2*time.Minute), // Cache TTL
cache.WithMinimumRetryInterval(10*time.Second), // Throttle failed lookups
)
services, _ := cache.GetService("my.service")
Adaptive Throttling
The cache implements rate limiting on ALL cache refresh attempts (not just errors) to prevent overwhelming the registry. This protects against multiple scenarios:
- Registry failures: When etcd is down/overloaded
- Rolling deployments: When all caches expire simultaneously under high QPS
- Cache expiration storms: When many services expire at once
How It Works
- Rate limiting: Refresh attempts are throttled per-service using
MinimumRetryInterval(default 5s) - Stale cache preference: If stale cache exists (even if expired), return it instead of calling registry
- No cache fallback: If no cache exists, return
ErrNotFoundand rely on gRPC retry - Singleflight deduplication: Concurrent requests are still deduplicated
- Recovery: Throttling is reset on successful registry lookup
Example Scenarios
Scenario 1: Registry Failure with Stale Cache
cache := cache.New(etcdRegistry, cache.WithMinimumRetryInterval(10*time.Second))
// Initial lookup populates cache
services, _ := cache.GetService("api") // → Calls etcd, caches result
// Cache expires after TTL
time.Sleep(2 * time.Minute)
// Etcd fails, but we have stale cache
services, err := cache.GetService("api") // → Returns stale cache WITHOUT calling etcd
// err == nil, services contains stale data
Scenario 2: Rolling Deployment Cache Storm
// Scenario: All 1000 upstream pods watch downstream service
// Downstream does rolling deployment - last pod updated
// All 1000 upstream caches expire simultaneously
// High QPS hits the system at this moment
// First request after cache expiration
services, _ := cache.GetService("downstream") // → Calls etcd, updates lastRefreshAttempt
// Next 999 requests arrive within MinimumRetryInterval
services, _ := cache.GetService("downstream") // → Returns stale cache, NO etcd call
// Rate limiting prevents 999 stampede requests to etcd
Scenario 3: No Cache Available
// First lookup when etcd is down (no cache exists yet)
_, err := cache.GetService("new-service") // → Calls etcd, fails, records attempt time
// err != nil
// Immediate retry (< 10s later, still no cache)
_, err = cache.GetService("new-service") // → Throttled, returns ErrNotFound immediately
// err == ErrNotFound
// After MinimumRetryInterval
time.Sleep(10 * time.Second)
_, err = cache.GetService("new-service") // → Allowed to retry, calls etcd again
This prevents cache penetration scenarios where thousands of concurrent requests hammer a failing or overloaded registry.