Loading...
Loading...
Compare original and translation side by side
Client → API Gateway → Service 1
→ Service 2
→ Service 3Client → API Gateway → Service 1
→ Service 2
→ Service 3Service A → Event Bus → Service B
→ Service CService A → Event Bus → Service B
→ Service C// Fails fast when service is unavailable
const breaker = new CircuitBreaker(async () => {
return await serviceCall();
}, {
threshold: 5, // Fail after 5 errors
timeout: 60000 // Check after 60s
});// Fails fast when service is unavailable
const breaker = new CircuitBreaker(async () => {
return await serviceCall();
}, {
threshold: 5, // Fail after 5 errors
timeout: 60000 // Check after 60s
});// Exponential backoff
const maxRetries = 3;
const baseDelay = 1000;
for (let i = 0; i < maxRetries; i++) {
try {
return await service.call();
} catch (e) {
if (i === maxRetries - 1) throw e;
await sleep(baseDelay * Math.pow(2, i));
}
}// Exponential backoff
const maxRetries = 3;
const baseDelay = 1000;
for (let i = 0; i < maxRetries; i++) {
try {
return await service.call();
} catch (e) {
if (i === maxRetries - 1) throw e;
await sleep(baseDelay * Math.pow(2, i));
}
}Transaction 1: Service A
↓ success
Transaction 2: Service B
↓ success
Transaction 3: Service C
↓ failure → Compensating transactionsTransaction 1: Service A
↓ success
Transaction 2: Service B
↓ success
Transaction 3: Service C
↓ failure → Compensating transactionsundefinedundefinedundefinedundefined| Challenge | Solution |
|---|---|
| Distributed transactions | Saga pattern, event sourcing |
| Data consistency | Eventual consistency acceptance |
| Service discovery | Service registry (Consul, Eureka) |
| Latency | Caching, async communication |
| Debugging | Distributed tracing, correlation IDs |
| Complexity | API gateway, service mesh |
| 挑战 | 解决方案 |
|---|---|
| 分布式事务 | Saga模式、事件溯源 |
| 数据一致性 | 接受最终一致性 |
| 服务发现 | 服务注册中心(Consul、Eureka) |
| 延迟问题 | 缓存、异步通信 |
| 调试困难 | 分布式追踪、关联ID |
| 复杂度提升 | API网关、服务网格 |