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
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
