{"id":319,"date":"2020-05-12T23:44:52","date_gmt":"2020-05-12T22:44:52","guid":{"rendered":"https:\/\/blog.wnohang.net\/?p=319"},"modified":"2020-05-12T23:52:17","modified_gmt":"2020-05-12T22:52:17","slug":"circuit-breakers-stock-markets-and-distributed-systems","status":"publish","type":"post","link":"https:\/\/blog.wnohang.net\/index.php\/2020\/05\/12\/circuit-breakers-stock-markets-and-distributed-systems\/","title":{"rendered":"Circuit Breakers:  Stock Markets and Distributed Systems"},"content":{"rendered":"<span class=\"span-reading-time rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">Reading Time: <\/span> <span class=\"rt-time\"> 4<\/span> <span class=\"rt-label rt-postfix\">minutes<\/span><\/span>\n<p>                                                                              <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"640\" height=\"480\" src=\"https:\/\/blog.wnohang.net\/wp-content\/uploads\/2020\/05\/Circuit_Breaker_115_kV.jpg\" alt=\"\" class=\"wp-image-328\" srcset=\"https:\/\/blog.wnohang.net\/wp-content\/uploads\/2020\/05\/Circuit_Breaker_115_kV.jpg 640w, https:\/\/blog.wnohang.net\/wp-content\/uploads\/2020\/05\/Circuit_Breaker_115_kV-300x225.jpg 300w\" sizes=\"auto, (max-width: 640px) 100vw, 640px\" \/><\/figure>\n\n\n\n<p class=\"has-drop-cap\">There are many parallels between the stock markets and the distributed systems in computer science. This post, in particular, is about circuit breakers prevalent in them for better resilience against&nbsp;<a rel=\"noreferrer noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/Irrational_exuberance\">irrational exuberance<\/a>&nbsp;and upstream service errors respectively. In particular, this is about exploring breakers in stock markets from a distributed systems perspective.&nbsp;<\/p>\n\n\n\n<p>Of late, the US stock market (and the non-US ones too) has been quite volatile, to say the least. <a href=\"https:\/\/en.wikipedia.org\/wiki\/VIX\">CBOE VIX index<\/a>&nbsp; (also known as the \u2018fear index\u2019) which measures <a href=\"https:\/\/www.investopedia.com\/terms\/i\/iv.asp\">implied volatility<\/a> in <a href=\"https:\/\/en.wikipedia.org\/wiki\/S%26P_500_Index\">the S&amp;P 500<\/a> index, has traversed a wide range between highs and lows, fluctuating several points on many of the days in March and April, peaking at 82.69 on 16th March (stock market bottomed out on 23 March, both as of May 10, 2020). &nbsp;<\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"433\" src=\"https:\/\/lh5.googleusercontent.com\/GfLQTbBz1QshSJV0cZX0M7eB6UqUBOq8UOoNk4EZP4rmc0YjXWVbVSywj88rUF3v2qnlhU4Ms24aBwa-5DgoJ2Xzw2YOKtE7E2HKCbW6fkYjI8psQ90SB8TtQCsmJT8ShWt_2wac\"\/><\/p>\n\n\n\n<!--more-->\n\n\n\n<p>In recent times, similar levels of volatility were last observed during the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Financial_crisis_of_2007%E2%80%9308\">Global Financial Crisis<\/a> (GFC) of 2008. Markets are driven by emotions and speculation as much they are driven by algorithms and numbers.<\/p>\n\n\n\n<p>During periods of high volatility, a \u201c<a href=\"https:\/\/en.wikipedia.org\/wiki\/Thundering_herd_problem\">thundering herd<\/a>\u201d of panic sell-off can be potentially triggered. To put a stop to this herd, a <strong>circuit breaker<\/strong> or a <a href=\"https:\/\/en.wikipedia.org\/wiki\/Trading_curb\">trading curb<\/a> kicks in. Many would have experienced <a href=\"https:\/\/en.wikipedia.org\/wiki\/Circuit_breaker\">electrical circuit breakers<\/a> in daily life when dealing with not-so-pleasant electrical issues wherein an overload would have triggered the breaker, requiring a manual reset. Similarly, these circuit breakers are triggered corresponding to a fall of 7%, 13%, and 20% in levels of the S&amp;P 500 index, and markets are halted for 15 minutes each for the first two and the entirety of the trading day for a 20% drop. The goal of each of these curbs is to prevent the need for the next one. <a href=\"https:\/\/www.weforum.org\/agenda\/2020\/03\/stock-market-volatility-coronavirus\/\">World Economic Forum<\/a> and <a href=\"https:\/\/graphics.reuters.com\/USA-MARKETS\/0100B5L144C\/index.html\">Reuters<\/a> have crisp short reads on the recent circuit breaker events.&nbsp;<\/p>\n\n\n\n<p>In March alone, these circuit breakers were triggered four times. This is very significant since, before these clustered breaker events, the circuit breakers were triggered only once in 1997 after being instituted to avoid a repeat of the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Black_Monday_(1987)\">1987 Black Monday crash<\/a>.<\/p>\n\n\n\n<p>On one of these days in March, with these breakers triggered, other than dread at the roller coaster-like volatility, this reminded me of circuit breakers in distributed systems. In these systems, these breakers are a design pattern (popularized by Michael Nygard in the book <a href=\"https:\/\/www.amazon.co.uk\/Release-Production-Ready-Software-Pragmatic-Programmers\/dp\/0978739213\">Release It!<\/a>) used often by services\/clients when their dependent service is unavailable. It works by isolating the unavailable\/erroring service from calling clients and periodically monitoring the service before allowing some (half-open) or all the traffic (open) states.&nbsp; The overarching goal here is to avoid escalation of an error state to a crash and cascading failures in general.<\/p>\n\n\n\n<p>Following diagram from Azure <a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/architecture\/patterns\/circuit-breaker\">Circuit Breaker pattern &#8211; Cloud Design Patterns<\/a> illustrates the state transitions better:&nbsp;<\/p>\n\n\n\n<p><img loading=\"lazy\" decoding=\"async\" width=\"624\" height=\"516\" alt=\"Circuit Breaker states\" src=\"https:\/\/lh5.googleusercontent.com\/pB_uQPlMCX1aBzGdKVM8h9sT56Wb2ML89druElaxvEg-9Hju8SOD-qWvbmr7SvmrM2N6oi1LyyfCKbPsneMNuqG7y35G8FQL41RPVf8czT6IIppxT16RF_dqKi06KEJ-YfF5OZqK\"\/><\/p>\n\n\n\n<p>Cascading failures are those wherein a failure in one service avalanches into larger problems in multiple dependent services. One hapless git commit somewhere deep in the stack ends up breaking your photo-sharing service. It would be hard to miss this class of failures from postmortems of popular website\/service incidents. This clip from Ice Age sort of exemplifies a cascading failure in my mind:<\/p>\n\n\n\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"Ice Age (1\/5) Movie CLIP - Acorn Troubles (2002) HD\" width=\"525\" height=\"295\" src=\"https:\/\/www.youtube.com\/embed\/_LKe8V-h7h8?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>Getting back to the stock crash, the parallels here are clear. The &#8220;error&#8221;, in this case, is a precipitous fall in the Index exacerbated by real-life unexpected out-of-the-blue conditions (stock markets hate uncertainty more than an actual downturn on a slow burner). The &#8220;clients&#8221; here are the traders who, in response to this panic, try to de-risk their portfolio by liquidating it. This further stokes panic creating a self-perpetuating rapidly escalating feedback loop, causing more &#8220;errors&#8221;. Rightfully, the circuit breakers kick in and create a time gap (much like the insulating gap in electrical breakers) between traders and the market, so that short-term gut reaction is (hopefully) supplanted by more non-short-term calculated approaches. Should these breakers fail to kick in, the cascading failures, in this case, would be the potentially catastrophic damage to the market cap of listed companies and even the economy in the worst case, if left unabated. This whole flow gets complex when &#8220;clients&#8221; (can be automated\/misconfigured algos) and &#8220;errors&#8221; (<a rel=\"noreferrer noopener\" target=\"_blank\" href=\"https:\/\/en.wikipedia.org\/wiki\/Flash_crash\">flash crashes<\/a>) are not as straightforward.<\/p>\n\n\n\n<p>In <a href=\"https:\/\/www.investopedia.com\/ask\/answers\/042115\/what-caused-black-monday-stock-market-crash-1987.asp\">1987<\/a>, when these circuit breakers were not in place, Dow Jones (DJIA) fell by about 22% in a single day and billions were wiped out of many funds (including some of the cr\u00e8me de la cr\u00e8me like <a href=\"https:\/\/www.youtube.com\/watch?v=tJNDkcASC_k\">Fidelity Magellan<\/a> managed by <a href=\"https:\/\/en.wikipedia.org\/wiki\/Peter_Lynch\">Peter Lynch<\/a>). Such market crashes, in general, are not healthy since recovery is not symmetrical, in other words, it is much harder to gain than lose.&nbsp; So much so that there is a well-worn adage which goes like:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote is-style-large is-layout-flow wp-block-quote-is-layout-flow\"><p>&#8220;The market takes the stairs up and the elevator down&#8221;&nbsp;<\/p><\/blockquote>\n\n\n\n<h3 class=\"wp-block-heading\">Photo Credits:<\/h3>\n\n\n\n<ol class=\"wp-block-list\"><li>https:\/\/en.wikipedia.org\/wiki\/File:Circuit_Breaker_115_kV.jpg<\/li><li>Google Search Stock Summaries for the VIX index.<\/li><li><a href=\"https:\/\/docs.microsoft.com\/en-us\/azure\/architecture\/patterns\/circuit-breaker\">https:\/\/docs.microsoft.com\/en-us\/azure\/architecture\/patterns\/circuit-breaker<\/a><\/li><\/ol>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p><span class=\"span-reading-time rt-reading-time\" style=\"display: block;\"><span class=\"rt-label rt-prefix\">Reading Time: <\/span> <span class=\"rt-time\"> 4<\/span> <span class=\"rt-label rt-postfix\">minutes<\/span><\/span>There are many parallels between the stock markets and the distributed systems in computer science. This post, in particular, is about circuit breakers prevalent in them for better resilience against&nbsp;irrational exuberance&nbsp;and upstream service errors respectively. In particular, this is about exploring breakers in stock markets from a distributed systems perspective.&nbsp; Of late, the US stock &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/blog.wnohang.net\/index.php\/2020\/05\/12\/circuit-breakers-stock-markets-and-distributed-systems\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Circuit Breakers:  Stock Markets and Distributed Systems&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[62],"tags":[66,65,68,67],"class_list":["post-319","post","type-post","status-publish","format-standard","hentry","category-musings","tag-circuit-breakers","tag-distributed-systems","tag-resilience-engineering","tag-stock-market"],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3AlYV-59","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":334,"url":"https:\/\/blog.wnohang.net\/index.php\/2020\/05\/22\/gossips-in-distributed-systems-physalia\/","url_meta":{"origin":319,"position":0},"title":"Gossips in Distributed Systems:  Physalia","author":"Raghavendra","date":"May 22, 2020","format":false,"excerpt":"I often take notes and jot down observations when I read academic\/industry papers. \u00a0 Thinking of a name for this series \u2018Gossips in Distributed Systems\u2019 seemed apt to me, inspired by the gossip protocol with which peers in these systems communicate with each other which mimics the spread of ideas\u2026","rel":"","context":"In \"availability\"","block_context":{"text":"availability","link":"https:\/\/blog.wnohang.net\/index.php\/tag\/availability\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/blog.wnohang.net\/wp-content\/uploads\/2020\/05\/Screen-Shot-2020-05-22-at-4.40.40-PM.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/blog.wnohang.net\/wp-content\/uploads\/2020\/05\/Screen-Shot-2020-05-22-at-4.40.40-PM.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/blog.wnohang.net\/wp-content\/uploads\/2020\/05\/Screen-Shot-2020-05-22-at-4.40.40-PM.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/blog.wnohang.net\/wp-content\/uploads\/2020\/05\/Screen-Shot-2020-05-22-at-4.40.40-PM.png?resize=700%2C400&ssl=1 2x, https:\/\/i0.wp.com\/blog.wnohang.net\/wp-content\/uploads\/2020\/05\/Screen-Shot-2020-05-22-at-4.40.40-PM.png?resize=1050%2C600&ssl=1 3x"},"classes":[]},{"id":146,"url":"https:\/\/blog.wnohang.net\/index.php\/2014\/05\/04\/slides-plmce-2014-breakout-session\/","url_meta":{"origin":319,"position":1},"title":"Slides from PLMCE 2014 breakout session","author":"Raghavendra","date":"May 4, 2014","format":false,"excerpt":"As many of you already know, PLMCE is an annual MySQL community conference and Expo organized by Percona in the month of April (usually). It is a great conference, not only to meet new and eminent people in MySQL and related database fields, but also to attend interesting talks, and\u2026","rel":"","context":"In \"ACID\"","block_context":{"text":"ACID","link":"https:\/\/blog.wnohang.net\/index.php\/tag\/acid\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":59,"url":"https:\/\/blog.wnohang.net\/index.php\/2014\/04\/30\/saving-form-data\/","url_meta":{"origin":319,"position":2},"title":"Saving form data in firefox","author":"Raghavendra","date":"April 30, 2014","format":false,"excerpt":"When commenting on sites, I have sometimes, seen that the commenting system just swallows the comment, or there is a browser crash, or a system one. In these cases it would be great if you can recover it somehow, particularly when you typed quite a bit. There are plugins for\u2026","rel":"","context":"Similar post","block_context":{"text":"Similar post","link":""},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"_links":{"self":[{"href":"https:\/\/blog.wnohang.net\/index.php\/wp-json\/wp\/v2\/posts\/319","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.wnohang.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.wnohang.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.wnohang.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.wnohang.net\/index.php\/wp-json\/wp\/v2\/comments?post=319"}],"version-history":[{"count":8,"href":"https:\/\/blog.wnohang.net\/index.php\/wp-json\/wp\/v2\/posts\/319\/revisions"}],"predecessor-version":[{"id":331,"href":"https:\/\/blog.wnohang.net\/index.php\/wp-json\/wp\/v2\/posts\/319\/revisions\/331"}],"wp:attachment":[{"href":"https:\/\/blog.wnohang.net\/index.php\/wp-json\/wp\/v2\/media?parent=319"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.wnohang.net\/index.php\/wp-json\/wp\/v2\/categories?post=319"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.wnohang.net\/index.php\/wp-json\/wp\/v2\/tags?post=319"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}