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.ReverseProxyBFF (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.
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...
