BBR (Bottleneck Bandwidth and Round-trip time) is a congestion control algorithm originally developed by Google. In the context of rate limiting, BBR has been adapted to provide dynamic throttling by automatically adjusting request concurrency to balance system throughput and response times.
Core Principles of BBR Rate Limiting
The BBR algorithm operates on three fundamental principles:
- Continuous monitoring of system metrics (CPU utilization, request throughput, response latency)
- Dynamic calculation of system capacity based on these metrics
- Triggering throttling when system load approaches or exceeds capacity
Implementation in Go
Below is a analysis of BBR implementation in the Kratos framework.
Core Data Structure
type BBRController struct {
cpuMonitor performanceMonitor
throughputStats metrics.SlidingWindow
latencyStats metrics.SlidingWindow
activeRequests atomic.Int64
samplingRate int64
samplingInterval time.Duration
lastThrottleTime atomic.Value
peakThroughput atomic.Value
optimalLatency atomic.Value
config controllerOptions
}
Initialization
func NewBBRController(cfg ...ConfigOption) *BBRController {
// Initialization logic here
}
Key Methods
CalculatePeakThroughput()
func (c *BBRController) CalculatePeakThroughput() int64 {
// Implementation details
}
DetermineOptimalLatency()
func (c *BBRController) DetermineOptimalLatency() int64 {
// Implementation details
}
ComputeMaxConcurrency()
func (c *BBRController) ComputeMaxConcurrency() int64 {
return int64(math.Floor(
float64(c.CalculatePeakThroughput()*
c.DetermineOptimalLatency()*
c.samplingRate)/1000.0) + 0.5)
}
ShouldThrottle()
func (c *BBRController) ShouldThrottle() bool {
// Decision logic implementation
}
RequestCheckpoint()
func (c *BBRController) RequestCheckpoint() (CompletionCallback, error) {
// Main entry point implementation
}
System Monitoring
The BBR implementation encludes a background process that samples CPU utilization every 500ms using an Exponential Moving Average (EMA) algorithm.
BBR Workflow
- Continuous monitoring of system metrics
- Request validation through RequestCheckpoint()
- Throttle decision via ShouldThrottle()
- Request processing with concurrency control
- Metric updates upon request completion
Advantages of BBR
- Self-adapting to system conditions
- Multi-dimensional protection
- High performance under load
- Precise request control
Implementation Considerations
- Proper CPU threshold configuration
- Optimal sliding window sizing
- Continuous performance monitoring