Golang / GoLang System Architecture and Testing Interview Questions
What is chaos engineering and how do Go teams apply it to test microservice resilience?
Chaos engineering deliberately injects failures into a running system to discover weaknesses before they cause production outages. Go services are validated against: network failures, slow dependencies, pod restarts, and resource exhaustion.
// Chaos testing in Go: inject failures in tests
// Fault injection via interface
type FaultInjector struct {
next UserRepository
failRate float64 // 0.0 to 1.0
latency time.Duration
}
func (f *FaultInjector) FindByID(ctx context.Context, id int) (*User, error) {
// Inject artificial latency
if f.latency > 0 {
select {
case <-time.After(f.latency):
case <-ctx.Done(): return nil, ctx.Err()
}
}
// Inject random failures
if rand.Float64() < f.failRate {
return nil, errors.New("injected fault: database unavailable")
}
return f.next.FindByID(ctx, id)
}
// Test service behaviour under 50% failure rate
func TestServiceUnderFaults(t *testing.T) {
repo := &fakeUserRepo{users: testUsers}
faulty := &FaultInjector{
next: repo,
failRate: 0.5,
latency: 100 * time.Millisecond,
}
svc := NewUserService(faulty)
// Test that service handles partial failures gracefully
successCount := 0
for i := 0; i < 100; i++ {
user, err := svc.GetUserWithFallback(context.Background(), 1)
if err == nil && user != nil { successCount++ }
}
// With 50% fault rate and fallback, expect at least 90% success
if float64(successCount) < 90 {
t.Errorf("success rate %d%% too low with fallback", successCount)
}
}
// Tools for chaos in production:
// - Chaos Monkey (Netflix) — terminates random pods
// - Litmus (CNCF) — k8s-native chaos experiments
// - Gremlin — cloud chaos-as-a-service
// - k6 + fault injection scenarios
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...
