distributed-systems
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDistributed Systems Patterns
分布式系统模式
Comprehensive patterns for building reliable distributed systems. Each category has individual rule files in loaded on-demand.
rules/构建可靠分布式系统的综合模式。每个类别在目录下都有单独的规则文件,可按需加载。
rules/Quick Reference
快速参考
| Category | Rules | Impact | When to Use |
|---|---|---|---|
| Distributed Locks | 3 | CRITICAL | Redis/Redlock locks, PostgreSQL advisory locks, fencing tokens |
| Resilience | 3 | CRITICAL | Circuit breakers, retry with backoff, bulkhead isolation |
| Idempotency | 3 | HIGH | Idempotency keys, request dedup, database-backed idempotency |
| Rate Limiting | 3 | HIGH | Token bucket, sliding window, distributed rate limits |
| Edge Computing | 2 | HIGH | Edge workers, V8 isolates, CDN caching, geo-routing |
| Event-Driven | 2 | HIGH | Event sourcing, CQRS, transactional outbox, sagas |
Total: 16 rules across 6 categories
Quick Start
快速开始
python
undefinedpython
undefinedRedis distributed lock with Lua scripts
基于Lua脚本的Redis分布式锁
async with RedisLock(redis_client, "payment:order-123"):
await process_payment(order_id)
async with RedisLock(redis_client, "payment:order-123"):
await process_payment(order_id)
Circuit breaker for external APIs
用于外部API的断路器
@circuit_breaker(failure_threshold=5, recovery_timeout=30)
@retry(max_attempts=3, base_delay=1.0)
async def call_external_api():
...
@circuit_breaker(failure_threshold=5, recovery_timeout=30)
@retry(max_attempts=3, base_delay=1.0)
async def call_external_api():
...
Idempotent API endpoint
幂等API端点
@router.post("/payments")
async def create_payment(
data: PaymentCreate,
idempotency_key: str = Header(..., alias="Idempotency-Key"),
):
return await idempotent_execute(db, idempotency_key, "/payments", process)
@router.post("/payments")
async def create_payment(
data: PaymentCreate,
idempotency_key: str = Header(..., alias="Idempotency-Key"),
):
return await idempotent_execute(db, idempotency_key, "/payments", process)
Token bucket rate limiting
令牌桶限流
limiter = TokenBucketLimiter(redis_client, capacity=100, refill_rate=10)
if await limiter.is_allowed(f"user:{user_id}"):
await handle_request()
undefinedlimiter = TokenBucketLimiter(redis_client, capacity=100, refill_rate=10)
if await limiter.is_allowed(f"user:{user_id}"):
await handle_request()
undefinedDistributed Locks
分布式锁
Coordinate exclusive access to resources across multiple service instances.
| Rule | File | Key Pattern |
|---|---|---|
| Redis & Redlock | | Lua scripts, SET NX, multi-node quorum |
| PostgreSQL Advisory | | Session/transaction locks, lock ID strategies |
| Fencing Tokens | | Owner validation, TTL, heartbeat extension |
协调多个服务实例对资源的独占访问。
| 规则 | 文件 | 核心模式 |
|---|---|---|
| Redis & Redlock | | Lua脚本、SET NX、多节点仲裁 |
| PostgreSQL Advisory锁 | | 会话/事务锁、锁ID策略 |
| 防护令牌 | | 所有者验证、TTL、心跳续期 |
Resilience
弹性机制
Production-grade fault tolerance for distributed systems.
| Rule | File | Key Pattern |
|---|---|---|
| Circuit Breaker | | CLOSED/OPEN/HALF_OPEN states, sliding window |
| Retry & Backoff | | Exponential backoff, jitter, error classification |
| Bulkhead Isolation | | Semaphore tiers, rejection policies, queue depth |
面向生产环境的分布式系统容错机制。
| 规则 | 文件 | 核心模式 |
|---|---|---|
| 断路器 | | CLOSED/OPEN/HALF_OPEN状态、滑动窗口 |
| 重试与退避 | | 指数退避、抖动、错误分类 |
| 舱壁隔离 | | 信号量分层、拒绝策略、队列深度 |
Idempotency
幂等性
Ensure operations can be safely retried without unintended side effects.
| Rule | File | Key Pattern |
|---|---|---|
| Idempotency Keys | | Deterministic hashing, Stripe-style headers |
| Request Dedup | | Event consumer dedup, Redis + DB dual layer |
| Database-Backed | | Unique constraints, upsert, TTL cleanup |
确保操作可安全重试而不会产生意外副作用。
| 规则 | 文件 | 核心模式 |
|---|---|---|
| 幂等键 | | 确定性哈希、Stripe风格请求头 |
| 请求去重 | | 事件消费者去重、Redis + 数据库双层存储 |
| 基于数据库的实现 | | 唯一约束、Upsert操作、TTL清理 |
Rate Limiting
限流
Protect APIs with distributed rate limiting using Redis.
| Rule | File | Key Pattern |
|---|---|---|
| Token Bucket | | Redis Lua scripts, burst capacity, refill rate |
| Sliding Window | | Sorted sets, precise counting, no boundary spikes |
| Distributed Limits | | SlowAPI + Redis, tiered limits, response headers |
使用Redis实现分布式限流以保护API。
| 规则 | 文件 | 核心模式 |
|---|---|---|
| 令牌桶 | | Redis Lua脚本、突发容量、填充速率 |
| 滑动窗口 | | 有序集合、精确计数、无边界峰值 |
| 分布式限流 | | SlowAPI + Redis、分层限流、响应头标识 |
Edge Computing
边缘计算
Edge runtime patterns for Cloudflare Workers, Vercel Edge, and Deno Deploy.
| Rule | File | Key Pattern |
|---|---|---|
| Edge Workers | | V8 isolate constraints, Web APIs, geo-routing, auth at edge |
| Edge Caching | | Cache-aside at edge, CDN headers, KV storage, stale-while-revalidate |
适用于Cloudflare Workers、Vercel Edge和Deno Deploy的边缘运行时模式。
| 规则 | 文件 | 核心模式 |
|---|---|---|
| 边缘工作器 | | V8隔离环境约束、Web API、地理路由、边缘认证 |
| 边缘缓存 | | 边缘缓存旁路、CDN请求头、KV存储、stale-while-revalidate策略 |
Event-Driven
事件驱动
Event sourcing, CQRS, saga orchestration, and reliable messaging patterns.
| Rule | File | Key Pattern |
|---|---|---|
| Event Sourcing | | Event-sourced aggregates, CQRS read models, optimistic concurrency |
| Event Messaging | | Transactional outbox, saga compensation, idempotent consumers |
事件溯源、CQRS、Saga编排和可靠消息传递模式。
| 规则 | 文件 | 核心模式 |
|---|---|---|
| 事件溯源 | | 事件溯源聚合根、CQRS读模型、乐观并发控制 |
| 事件消息传递 | | 事务性发件箱、Saga补偿操作、幂等消费者 |
Key Decisions
关键决策
| Decision | Recommendation |
|---|---|
| Lock backend | Redis for speed, PostgreSQL if already using it, Redlock for HA |
| Lock TTL | 2-3x expected operation time |
| Circuit breaker recovery | Half-open probe with sliding window |
| Retry algorithm | Exponential backoff + full jitter |
| Bulkhead isolation | Semaphore-based tiers (Critical/Standard/Optional) |
| Idempotency storage | Redis (speed) + DB (durability), 24-72h TTL |
| Rate limit algorithm | Token bucket for most APIs, sliding window for strict quotas |
| Rate limit storage | Redis (distributed, atomic Lua scripts) |
| 决策项 | 推荐方案 |
|---|---|
| 锁后端 | 追求速度选Redis;若已使用PostgreSQL则选它;高可用场景选Redlock |
| 锁TTL | 设为预期操作时间的2-3倍 |
| 断路器恢复 | 半开状态下使用滑动窗口进行探测 |
| 重试算法 | 指数退避 + 全抖动 |
| 舱壁隔离 | 基于信号量的分层(核心/标准/可选) |
| 幂等性存储 | Redis(速度) + 数据库(持久性),设置24-72小时TTL |
| 限流算法 | 多数API用令牌桶;严格配额场景用滑动窗口 |
| 限流存储 | Redis(分布式、原子Lua脚本) |
Anti-Patterns (FORBIDDEN)
反模式(禁止使用)
python
undefinedpython
undefinedLOCKS: Never forget TTL (causes deadlocks)
锁:绝不要忘记设置TTL(会导致死锁)
await redis.set(f"lock:{name}", "1") # WRONG - no expiry!
await redis.set(f"lock:{name}", "1") # 错误 - 未设置过期时间!
LOCKS: Never release without owner check
锁:绝不要在不检查所有者的情况下释放锁
await redis.delete(f"lock:{name}") # WRONG - might release others' lock
await redis.delete(f"lock:{name}") # 错误 - 可能释放其他实例的锁!
RESILIENCE: Never retry non-retryable errors
弹性机制:绝不要重试不可重试的错误
@retry(max_attempts=5, retryable_exceptions={Exception}) # Retries 401!
@retry(max_attempts=5, retryable_exceptions={Exception}) # 会重试401错误!
RESILIENCE: Never put retry outside circuit breaker
弹性机制:绝不要将重试放在断路器外层
@retry # Would retry when circuit is open!
@circuit_breaker
async def call(): ...
@retry # 断路器打开时仍会重试!
@circuit_breaker
async def call(): ...
IDEMPOTENCY: Never use non-deterministic keys
幂等性:绝不要使用非确定性的键
key = str(uuid.uuid4()) # Different every time!
key = str(uuid.uuid4()) # 每次都不同!
IDEMPOTENCY: Never cache error responses
幂等性:绝不要缓存错误响应
if response.status_code >= 400:
await cache_response(key, response) # Errors should retry!
if response.status_code >= 400:
await cache_response(key, response) # 错误应该允许重试!
RATE LIMITING: Never use in-memory counters in distributed systems
限流:绝不要在分布式系统中使用内存计数器
request_counts = {} # Lost on restart, not shared across instances
undefinedrequest_counts = {} # 重启后丢失,无法在实例间共享
undefinedDetailed Documentation
详细文档
| Resource | Description |
|---|---|
| scripts/ | Templates: lock implementations, circuit breaker, rate limiter |
| checklists/ | Pre-flight checklists for each pattern category |
| references/ | Deep dives: Redlock algorithm, bulkhead tiers, token bucket |
| examples/ | Complete integration examples |
| 资源 | 描述 |
|---|---|
| scripts/ | 模板:锁实现、断路器、限流器 |
| checklists/ | 各模式类别的预启动检查清单 |
| references/ | 深度解析:Redlock算法、舱壁分层、令牌桶 |
| examples/ | 完整集成示例 |
Related Skills
相关技能
- - Redis caching patterns, cache as fallback
caching - - Job deduplication, async processing with retry
background-jobs - - Metrics and alerting for circuit breaker state changes
observability-monitoring - - Structured error responses for resilience failures
error-handling-rfc9457 - - API key management, authentication integration
auth-patterns
- - Redis缓存模式、缓存作为降级方案
caching - - 任务去重、带重试的异步处理
background-jobs - - 断路器状态变化的指标与告警
observability-monitoring - - 弹性机制失败的结构化错误响应
error-handling-rfc9457 - - API密钥管理、认证集成
auth-patterns