Golang / GoLang System Architecture and Testing Interview Questions
What is the API Gateway pattern and how does it complement Go microservices?
An API Gateway is a single entry point for all client requests. It handles cross-cutting concerns — auth, rate limiting, routing, SSL termination, response aggregation — so individual services don't need to implement them.
// Lightweight Go API gateway (simplified) type Gateway struct { routes map[string]RouteConfig auth *JWTValidator limit *RateLimiter log *slog.Logger } type RouteConfig struct { Target string // upstream URL RequireAuth bool RateLimit int // requests per second Methods []string // allowed HTTP methods StripPrefix string } func (g *Gateway) ServeHTTP(w http.ResponseWriter, r *http.Request) { route, ok := g.matchRoute(r.URL.Path) if !ok { http.Error(w, "not found", 404); return } // Auth (before rate limiting to save rate limit quota for auth users) if route.RequireAuth { if _, err := g.auth.Validate(r); err != nil { http.Error(w, "unauthorized", 401); return } } // Rate limiting if !g.limit.Allow(clientIP(r)) { http.Error(w, "rate limited", 429); return } // Proxy to upstream proxy := httputil.NewSingleHostReverseProxy(mustParseURL(route.Target)) if route.StripPrefix != "" { r.URL.Path = strings.TrimPrefix(r.URL.Path, route.StripPrefix) } proxy.ServeHTTP(w, r) } // In practice: use Kong, Envoy, or AWS API Gateway // Go excels at writing custom gateway logic as plugins: // - Kong plugins in Go (go-pdk) // - Envoy WASM filters in Go (tetratelabs/proxy-wasm-go-sdk) // - Custom gateway in Go using httputil.ReverseProxy
BFF (Backend for Frontend) pattern: a specialised API gateway for each type of client — mobile BFF, web BFF. Each BFF aggregates calls to multiple services and returns exactly the data its client needs (graph-like queries without GraphQL's complexity). Often written in Go for performance.
More Related questions...