Go Engineer System Course 017

Getting Started with Rate Limiting, Circuit Breaking, and Degradation (Including Sentinel Hands-on)

Based on the key video points from Chapter 3 (3-1 ~ 3-9) of the courseware, this guide compiles a service protection guide for beginners, helping to understand "why rate limiting, circuit breaking, and degradation are needed," and how to quickly get started with Sentinel.

Quick Overview of Learning Path

  • 3-1 Understand the background of service avalanche, rate limiting, circuit breaking, and degradation
  • 3-2 Sentinel vs. Hystrix comparison, clarifying technology selection
  • 3-3 Sentinel QPS Rate Limiting Basics
  • 3-4 Predictive Rate Limiting and Cold Start
  • 3-5 Throttling Configuration Demo
  • 3-6 Sentinel Circuit Breaking and Degradation Strategies
  • 3-7 Circuit Breaking Strategy - Based on Error Ratio
  • 3-8 Degradation Strategy - Based on Response Time
  • 3-9 Gin + Sentinel Hands-on Rate Limiting

Causes of Service Avalanche

When sudden traffic exceeds system capacity (e.g., system capacity is 1k QPS but suddenly 2k QPS floods in), it will cause slow responses and soaring error rates, which then drags down more dependencies, forming an "avalanche." Common triggers:

  • Sudden Traffic Surge: Major promotions, trending news, web crawlers, etc., cause a surge in access volume.
  • Dependency Failure: Downstream services delay or crash, leading to call accumulation.
  • Resource Exhaustion: Threads/connections/CPU are fully occupied, causing more requests to queue up.

The three-piece set of coping strategies: Rate Limiting to block the flood, Circuit Breaking to pull down the circuit breaker, and Degradation to provide a fallback experience.

graph LR
    A[突发流量] --> B(限流:削峰填谷)
    B --> C{依赖正常?}
    C -->|是| D[服务稳定运行]
    C -->|否| E(熔断:暂时切断调用)
    E --> F(降级:返回备用方案)
    F --> D

Rate Limiting: Adding a "Gate" to the Entrance

Rate Limiting controls the number of requests or concurrency within a unit of time, allowing the system to operate within a controllable range. Common strategies:

Strategy Scenario Advantages Potential Side Effects
Fixed Window QPS Interface requires simple speed limiting Simple to implement Instantaneous bursts at boundaries
Sliding Window API Gateway, unified egress Smoother control Slightly more complex calculation
Leaky Bucket Replay requests, peak shaving and valley filling Stable output Burst traffic is queued
Token Bucket Allows some bursts Flexible metrics (QPS/Concurrency) Requires monitoring bucket status

Rate limiting messages should be friendly: "There are many visitors currently, please try again later or pay attention to notifications."

graph TD
    subgraph 限流闸门
        Tokens[令牌桶] -->|取令牌| Request[请求放行]
        Request --> Service[核心服务]
    end
    Tokens -.超限.-> Fallback[返回 429 或排队提示]

Circuit Breaking: Making the System "Rejuvenated"

"Circuit breaking" is similar to an electrical circuit breaker—when abnormal metrics such as error rates or response times are detected, it temporarily cuts off calls to prevent fault propagation. Because after the breaker is pulled, the system can catch its breath and recover, it's jokingly referred to in class as making the system "rejuvenated."

Sentinel supports three types of circuit breaking rules:

  • Slow Call Ratio: Triggered when the proportion of requests exceeding the set time-consuming threshold is too high.
  • Exception Ratio: Circuit breaks when the proportion of exceptions within the statistical window exceeds the limit.
  • Exception Count: Circuit breaks when the total number of exceptions within a unit of time reaches the threshold.

After circuit breaking, it usually goes through three stages:
Open(Full Block) -> Half-Open(Limited Probing) -> Closed(Normal Recovery).

Degradation: Providing a Fallback Experience

When the core process is under too much pressure or dependencies are circuit-broken, degradation strategies provide users with "simplified but usable" results:

  • Return cached or fallback data (e.g., display yesterday's inventory).
  • Prompt for later processing, asynchronous callback (order queuing, work order processing).
  • Temporarily disable high-cost features (e.g., close recommendation lists, only retain basic search).

The key to degradation is advance preparation: copy, fallback interfaces, and frontend placeholders must all be ready.

Sentinel Quick Overview

Sentinel is an open-source high-availability protection framework from Alibaba, with core values:

  • Unified console for managing rules such as rate limiting, circuit breaking, degradation, and system protection.
  • Supports Java and Go, providing rich ecosystem adaptations (Dubbo, Spring Cloud, Gin, etc.).
  • Possesses capabilities such as real-time monitoring, link aggregation, and rule pushing.

Sentinel Architecture Diagram:

graph LR
    subgraph 控制台
        Dashboard[Dashboard 控制台]
    end
    Dashboard -- 推送规则 --> DataSource[数据源/配置中心]
    DataSource -- 动态规则 --> Sentinel[Sentinel 客户端]
    Sentinel -- 上报指标 --> Dashboard
    Sentinel --> App[业务应用]

Go Language Quick Start (Taking a Gin Project as an Example)

  1. Install Dependencies

bash
go get github.com/alibaba/sentinel-golang@latest
go get github.com/alibaba/sentinel-golang/pkg/adapters/gin

  1. Initialize Sentinel

```go
package main

import (
"log"
"github.com/alibaba/sentinel-golang/api"
"github.com/alibaba/sentinel-golang/core/flow"
)

func initSentinel() {
if err := api.InitDefault(); err != nil {
log.Fatalf("init sentinel: %v", err)
}
_, err := flow.LoadRules([]*flow.Rule{
{
Resource: "GET:/api/orders",
MetricType: flow.QPS,
Count: 200,
ControlBehavior: flow.WarmUp,
WarmUpPeriodSec: 10,
WarmUpColdFactor: 3,
},
})
if err != nil {
log.Fatalf("load flow rules: %v", err)
}
}
```

  1. Integrate Gin Middleware

```go
r := gin.Default()
r.Use(sgin.SentinelMiddleware())

r.GET("/api/orders", func(c *gin.Context) {
if entry, blockErr := api.Entry("GET:/api/orders"); blockErr != nil {
c.JSON(429, gin.H{"code": 429, "msg": "Congested, please try again later"})
return
} else {
defer entry.Exit()
}
c.JSON(200, gin.H{"orders": []string{"#1201", "#1202"}})
})
```

  1. Local Debugging Suggestions
  2. Use ab/wrk for stress testing to verify if rate limiting takes effect.
  3. Observe real-time QPS and Block metrics in logs and Dashboard.

Key Points for Java Environment Integration

<!-- Maven -->
<dependency>
  <groupId>com.alibaba.csp</groupId>
  <artifactId>sentinel-core</artifactId>
  <version>1.8.6</version>
</dependency>
@Configuration
public class SentinelConfig {
    @PostConstruct
    public void init() throws Exception {
        List<FlowRule> rules = new ArrayList<>();
        FlowRule rule = new FlowRule("product_list");
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        rule.setCount(1000);
        rule.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER);
        rule.setMaxQueueingTimeMs(500);
        rules.add(rule);
        FlowRuleManager.loadRules(rules);
    }
}

Combined with Spring Cloud Alibaba, degradation callback methods can be annotated with @SentinelResource.

Dashboard Startup Guide

  1. Download the console:

bash
wget https://github.com/alibaba/Sentinel/releases/download/1.8.6/sentinel-dashboard-1.8.6.jar

  1. Start:

bash
java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 \
-Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.8.6.jar

  1. Client parameters (Java example):

bash
-Dcsp.sentinel.dashboard.server=localhost:8080 \
-Dcsp.sentinel.api.port=8719 \
-Dproject.name=demo-service

  1. Log in to the console (default username and password are both sentinel), create flow control/circuit breaking/system protection rules in the interface and push them in real-time.

Formulate Your Own Protection "Battle Plan"

  • Observation Metrics: QPS, average response time, error rate, downstream dependency health.
  • Threshold Setting: Based on capacity evaluation and stress test data; set differentiated rules for different interfaces.
  • Circuit Breaker Recovery: Configure Half-Open probing logic to ensure automatic re-closure after service recovery.
  • Degradation Copy: Confirm fallback pages and prompts with product/frontend in advance.
  • Post-mortem Review: Record reasons, adjust thresholds, or scale up after each rate limiting / circuit breaking trigger.
sequenceDiagram
    participant User
    participant Gateway as API 网关
    participant Service as 核心服务
    participant Fallback as 降级/缓存

    User->>Gateway: 请求下单
    Gateway->>Service: 令牌校验
    alt QPS 超限
        Gateway-->>User: 429 拥堵提示
    else 调用异常
        Service-->>Gateway: 熔断触发
        Gateway->>Fallback: 查询兜底方案
        Fallback-->>User: 排队中,请稍后
    end

Quick Review Checklist

  • Rate limiting controls inbound traffic within the system's capacity, prioritizing the protection of core capabilities.
  • Circuit breaking actively cuts off calls when dependencies are abnormal, allowing the system to "recover" quickly.
  • Degradation is a fallback solution, allowing users to perceive that the service is "still alive" even during failures.
  • Sentinel covers rate limiting, circuit breaking, degradation, and system protection, with visual configuration and real-time monitoring via the console.
  • Prepare thresholds, copy, monitoring, and drills in advance to "rejuvenatedly" cope before a real avalanche strikes.

主题测试文章,只做测试使用。发布者:Walker,转转请注明出处:https://walker-learn.xyz/archives/6764

(0)
Walker的头像Walker
上一篇 2 hours ago
下一篇 4 hours ago

Related Posts

EN
简体中文 繁體中文 English