Golang / GoLang System Architecture and Testing Interview Questions
How do you implement feature flags and canary deployments in a Go microservice?
Feature flags decouple deployment from release — code is deployed to all servers but activated for a subset of users or traffic. Canary deployments route a small percentage of traffic to a new version, monitoring for errors before full rollout.
// Feature flag implementation type FeatureFlags struct { mu sync.RWMutex flags map[string]FlagConfig } type FlagConfig struct { Enabled bool Percentage int // 0-100: % of users who see the feature Allowlist []string // specific user IDs } func (f *FeatureFlags) IsEnabled(flagName, userID string) bool { f.mu.RLock() cfg, ok := f.flags[flagName] f.mu.RUnlock() if !ok || !cfg.Enabled { return false } // Always-on for allowlisted users (internal testing) for _, id := range cfg.Allowlist { if id == userID { return true } } // Percentage rollout: deterministic based on user ID // Same user always gets same experience h := fnv32(userID) % 100 return int(h) < cfg.Percentage } // In handler func userHandler(w http.ResponseWriter, r *http.Request) { claims, _ := claimsFromCtx(r.Context()) if flags.IsEnabled("new-profile-ui", claims.UserID) { renderNewProfile(w, r) return } renderLegacyProfile(w, r) } // Kubernetes canary via Argo Rollouts // spec.strategy.canary: // steps: // - setWeight: 10 # 10% traffic to new version // - pause: {duration: 5m} // - analysis: # check error rate // templates: [{templateName: error-rate-analysis}] // - setWeight: 50 # 50% if analysis passed // - pause: {duration: 10m} // - setWeight: 100 # full rollout
More Related questions...