Implementing a Custom Circuit Breaker in Distributed Systems

This article details the implementation of a custom circuit breaker to prevent system failures in distributed systems, covering design principles, Java and Python implementations, and its effectiveness during high traffic periods.

Dada Group Technology
Dada Group Technology
Dada Group Technology
Implementing a Custom Circuit Breaker in Distributed Systems

达达配送平台是复杂的分布式系统,由近百个Python服务和Java服务构成,业务复杂,服务间依赖也较多,出现了不少由于服务调用出现异常而导致的重大事故,如系统雪崩和数据库读写压力。

针对系统的痛点,我们把熔断技术引入到达达系统中,经过几个月的改进,研发出一套适合达达自己的熔断技术,从而保障了双十一期间每天支撑高达八百万订单而系统坚如磐石。

熔断就是在系统资源耗尽时,对继续访问的请求执行快速失败逻辑,不再执行正常的业务逻辑,为服务减压,待服务恢复稳定后,重新允许请求执行正常的业务逻辑。

为了保护系统,需要根据系统的特点和需求来选择合适的技术。首先,达达系统是包括Python和Java的系统,不是单一技术体系的系统;其次,达达系统是向微服务演进的系统,系统的承载压力处于时时变化中;第三,需要有合适的保护策略,简单好用;最后,我们希望能监控到引入的技术是否真正保护了系统。

根据这些特点和需求,我们整理出熔断需要遵守以下原则:

支持Java和Python系统

熔断参数可动态配置

熔断策略简单易用

熔断效果可监控

尽可能不影响用户体验

考虑这些因素,Netflix的Hystrix熔断技术比较符合我们的选型要求。虽然Hystrix只用于Java系统,但是它的设计思想非常好,可以借鉴它的设计思想,实现Python系统的熔断,也可以在Hystrix的源码基础上改进满足动态配置的效果。

Hystrix依赖的熔断隔离架构,如下图(供读者参考):

Hystrix通过将每个依赖服务分配独立的线程池进行资源隔离,从而避免服务雪崩。比如当商品评论服务不可用时,即使商品评论服务独立分配的20个线程全部处于同步等待状态,也不会影响其他依赖服务的调用。

针对Java系统的设计思想,我们对Hystrix增加动态配置和监控,即可满足上面的设计思想。对于Python系统的熔断接入,我们也根据Hystrix的思想开发了相应的熔断。

在断路器未打开状态时检查相应跟踪的参数,如超时、异常,在错误率或超时超过相应的熔断阈值时,将断路器置于半启动状态,根据半启动状态时监控到的数据,进行相应的操作,比如熔断、回退或是恢复。

在双十一期间,熔断也时时刻刻保护系统,下图的数据表示在11.11日在11:00-12:00的高峰期,每分钟API熔断次数在1000~3000次,熔断在保护系统中起到了巨大的作用。

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Distributed SystemsjavaSystem ArchitecturePythonhigh availabilitycircuit breaker
Dada Group Technology
Written by

Dada Group Technology

Sharing insights and experiences from Dada Group's R&D department on product refinement and technology advancement, connecting with fellow geeks to exchange ideas and grow together.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.