Architecting High-Performance API Gateways in Go

Hoàng Ngô Anh Đức· 28/05/2026 02:12
Architecting High-Performance API Gateways in Go Cover

An **API Gateway** is the single entry point for all incoming client requests. It handles authentication, logs traffic, applies security filters, and routes requests to corresponding upstream services.

Building this gateway in Go is a fantastic choice. Go’s concurrency model (goroutines) and high-performance network stack allow it to process hundreds of thousands of concurrent connections with almost zero memory footprint. Let's build a lightweight API gateway!

### 1. Designing the Reverse Proxy

Go’s standard library comes equipped with `net/http/httputil`, which has a built-in reverse proxy implementation. We can use it to forward requests seamlessly:

```go
package main

import (
"net/http"
"net/http/httputil"
"net/url"
)

func NewProxy(target string) (*httputil.ReverseProxy, error) {
url, err := url.Parse(target)
if err != nil {
return nil, err
}
return httputil.NewSingleHostReverseProxy(url), nil
}
```

### 2. Implementing Token Bucket Rate Limiting

To protect our backend microservices from DDoS or spam, we must apply rate limiting. The **Token Bucket** algorithm is perfect for this:

```go
package main

import (
"sync"
"time"
)

type TokenBucket struct {
mu sync.Mutex
tokens float64
maxTokens float64
refillRate float64 // tokens per second
lastRefilled time.Time
}

func (tb *TokenBucket) Allow() bool {
tb.mu.Lock()
defer tb.mu.Unlock()

now := time.Now()
elapsed := now.Sub(tb.lastRefilled).Seconds()
tb.lastRefilled = now

// Refill tokens
tb.tokens = tb.tokens + (elapsed * tb.refillRate)
if tb.tokens > tb.maxTokens {
tb.tokens = tb.maxTokens
}

if tb.tokens >= 1.0 {
tb.tokens -= 1.0
return true
}
return false
}
```

### 3. Assembling the Gateway Middleware

We can chain these components together using simple middleware functions:

```go
func RateLimiterMiddleware(bucket *TokenBucket, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !bucket.Allow() {
http.Error(w, "Too Many Requests", http.StatusTooManyRequests)
return
}
next.ServeHTTP(w, r)
})
}
```

This gives us an extremely lightweight gateway that intercepts incoming connections, checks rate limits, validates headers, and forwards requests dynamically!

// Read next

// Reader response

Comments

0 Comments

This article has no comments yet.

Name is limited to 60 characters and comment content to 1000 characters.

Hoàng Ngô Anh Đức

// Author

Hoàng Ngô Anh Đức

Senior Full-Stack Engineer & Software Architect

Tôi là một kỹ sư phần mềm giàu kinh nghiệm chuyên thiết kế và xây dựng các hệ thống web hiện đại, scalable backend sử dụng Go, Vue.js, TypeScript và kiến trúc đám mây Cloud. Đam mê chia sẻ kiến thức kỹ thuật và tối ưu hiệu năng phần mềm.