{"id":65278,"date":"2022-06-23T09:00:54","date_gmt":"2022-06-23T09:00:54","guid":{"rendered":"https:\/\/www.cryptocabaret.com\/?p=65278"},"modified":"2022-06-23T09:00:54","modified_gmt":"2022-06-23T09:00:54","slug":"how-to-use-a-circuit-breaker-pattern-for-site-reliability-engineering","status":"publish","type":"post","link":"https:\/\/www.cryptocabaret.com\/?p=65278","title":{"rendered":"How to use a circuit breaker pattern for site reliability engineering"},"content":{"rendered":"<p><span class=\"field field--name-title field--type-string field--label-hidden\">How to use a circuit breaker pattern for site reliability engineering<\/span><br \/>\n<span class=\"field field--name-uid field--type-entity-reference field--label-hidden\"><a title=\"View user profile.\" href=\"https:\/\/opensource.com\/users\/robert-charles\" class=\"username\">Robert Kimani<\/a><\/span><br \/>\n<span class=\"field field--name-created field--type-created field--label-hidden\">Thu, 06\/23\/2022 &#8211; 03:00<\/span><\/p>\n<div data-drupal-selector=\"rate-node-70057\" class=\"rate-widget-thumbs-up\" title=\"Register or Login to like.\">\n<div class=\"rate-thumbs-up-btn-up vote-pending\"><a href=\"https:\/\/opensource.com\/user\/register\">Register<\/a> or <a href=\"https:\/\/opensource.com\/user\/login?current=\/rss.xml\">Login<\/a> to like<\/div>\n<div class=\"rate-score\"><a href=\"https:\/\/opensource.com\/user\/register\">Register<\/a> or <a href=\"https:\/\/opensource.com\/user\/login?current=\/rss.xml\">Login<\/a> to like<\/div>\n<\/div>\n<div class=\"clearfix text-formatted field field--name-body field--type-text-with-summary field--label-hidden field__item\">\n<p>Distributed systems have unique requirements for the site reliability engineer (SRE). One of the best ways to maintain site reliability is to enforce a certain set of best practices. These act as guidelines for configuring infrastructure and policies.<\/p>\n<h2><strong>What is a circuit breaker pattern?<\/strong><\/h2>\n<p>A circuit breaker pattern saves your service from halting or crashing when another service is not responding.<\/p>\n<article class=\"align-center media media--type-image media--view-mode-default\">\n<div class=\"field field--name-field-media-image field--type-image field--label-hidden field__item\">  <img decoding=\"async\" src=\"https:\/\/www.cryptocabaret.com\/wp-content\/uploads\/2022\/06\/Fig_5B1.05D_LetE28099s_take_a_closer_look_at_a_typical_distributed_architecture.png\" width=\"837\" height=\"405\" alt=\"Image of a typical distributed architecture\" loading=\"lazy\"><\/div>\n<div class=\"field field--name-field-caption field--type-text-long field--label-hidden caption field__item\"><span class=\"caption__byline\">Image by: <\/span><\/p>\n<p>(Robert Kimani, CC BY-SA 40)<\/p>\n<\/div>\n<\/article>\n<p>For instance, in the example image, the business logic microservices talk to the User Auth service, Account service, and Gateway service.<\/p>\n<p>As you can see, the &#8220;engine&#8221; of this business application is the business logic microservice. There&#8217;s a pitfall, though. Suppose the account service breaks. Maybe it runs out of memory, or maybe a back end database goes down. When this happens, the calling microservice starts to have back pressure until it eventually breaks. This results in poor performance of your web services, which causes other components to break. A circuit breaker pattern must be implemented to rescue your distributed systems from this issue.<\/p>\n<h2><strong>Implementing a circuit breaker pattern<\/strong><\/h2>\n<p>When a remote service fails, the ideal scenario is for it to fail fast. Instead of bringing down the entire application, run your application with reduced functionality when it loses contact with something it relies upon. For example, keep your application online, but sacrifice the availability of its account service. Making a design like a circuit breaker helps avoids cascading failure.<\/p>\n<p>Here&#8217;s the recipe for a circuit breaker design:<\/p>\n<ol>\n<li>\n<p>Track the number of failures encountered while calling a remote service.<\/p>\n<\/li>\n<li>\n<p>Fail (open the circuit) when a pre-defined count or timeout is reached.<\/p>\n<\/li>\n<li>\n<p>Wait for a pre-defined time again and retry connecting to the remote service.<\/p>\n<\/li>\n<li>\n<p>Upon successful connection, close the circuit (meaning you can re-establish connectivity to the remote service.) If the service keeps failing, however, restart the counter.<\/p>\n<\/li>\n<\/ol>\n<p>Instead of hanging on to a poorly performing remote service, you fail fast so you can move on with other operations of your application.<\/p>\n<\/p>\n<div class=\"embedded-resource-list callout-float-right\">\n<div class=\"field field--name-title field--type-string field--label-hidden field__item\">More DevOps resources<\/div>\n<div class=\"field field--name-links field--type-link field--label-hidden field__items\">\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/resources\/devops?intcmp=7013a00000263HlAAI\">What is DevOps?<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/downloads\/devops-hiring-guide?intcmp=7013a00000263HlAAI\">The ultimate DevOps hiring guide<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/downloads\/devops-monitoring-guide?intcmp=7013a00000263HlAAI\">DevOps monitoring tools guide<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/downloads\/guide-implementing-devsecops?intcmp=7013a00000263HlAAI\">A guide to implementing DevSecOps<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/enterprisersproject.com\/cheat-sheet-devops-glossary?intcmp=7013a00000263HlAAI\">Download the DevOps glossary<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/www.ansible.com\/resources\/ebooks\/ansible-for-devops?intcmp=7013a00000263HlAAI\">eBook: Ansible for DevOps<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/tags\/devops?intcmp=7013a00000263HlAAI\">Latest DevOps articles<\/a><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<h2><strong>Open source circuit breaker pattern<\/strong><\/h2>\n<p>There are very specific components provided by open source to enable circuit breaker logic in your infrastructure. First, use a proxy layer, such as <a href=\"https:\/\/opensource.com\/article\/18\/9\/what-istio\" target=\"_blank\" rel=\"noopener\">Istio<\/a>. Istio is a technology independent solution and uses a\u00a0 &#8220;Blackbox&#8221; approach (meaning that it sits outside your application).<\/p>\n<p>Alternately, you can use Hystrix, an open source library from Netflix that&#8217;s widely and successfully used by many application teams. Hystrix gets built into your code. It&#8217;s generally accepted that Hystrix can provide more functionality and features than Istio, but it must be &#8220;hard coded&#8221; into your application. Whether that matters to your application depends on how you manage your code base and what features you actually require.<\/p>\n<h2><strong>Implementing effective load balancing<\/strong><\/h2>\n<p><a href=\"https:\/\/opensource.com\/article\/21\/4\/load-balancing\" target=\"_blank\" rel=\"noopener\">Load balancing<\/a> is the process of distributing a set of tasks over a set of resources (computing units), with the aim of making their overall processing as efficient as possible. Load balancing can optimize response time, and avoid unevenly overloading some computer nodes while other computer nodes are left idle.<\/p>\n<article class=\"align-center media media--type-image media--view-mode-default\">\n<div class=\"field field--name-field-media-image field--type-image field--label-hidden field__item\">  <img decoding=\"async\" src=\"https:\/\/www.cryptocabaret.com\/wp-content\/uploads\/2022\/06\/Fig_5B2.05D_Generic_overview_of_a_Load_Balancer.png\" width=\"735\" height=\"463\" alt=\"Image of a generic load balancer\" loading=\"lazy\"><\/div>\n<div class=\"field field--name-field-caption field--type-text-long field--label-hidden caption field__item\"><span class=\"caption__byline\">Image by: <\/span><\/p>\n<p>(Robert Kimani, CC BY-SA 40)<\/p>\n<\/div>\n<\/article>\n<p>Users on the left side talk to a virtual IP (vIP) on a load balancer. The load balancer distributes tasks across a pool of systems. Users are not aware of the back-end system, and only interact with the system through the load balancer.<\/p>\n<p>Load balancing helps to ensure high availability (HA) and good performance. High availability means that failures within the pool of servers can be tolerated, such that your services remain available a high percentage of the time. By adding more servers to your pool, effective performance is increased by horizontally spreading the load (this is called <em>horizontal scaling<\/em>.)<\/p>\n<h2><strong>\u00a03 ways to do load balancing<\/strong><\/h2>\n<ol>\n<li><strong>DNS Load Balancing:<\/strong> This is the most straight forward and is free. You do not have to purchase any specialized hardware because it&#8217;s the basic functionality of DNS protocol, you&#8217;d simply return multiple A-records An A-record maps a hostname to an IP address. When the DNS server returns multiple IP addresses for a single hostname, the client will choose one IP address at random, in other words the load balancing is automatically done for you from the client.<\/li>\n<li>\n<p><strong>Dedicated Hardware-Based Load Balancer: <\/strong>This is what is typically used within a data center, it&#8217;s feature-rich and highly performant.<\/p>\n<\/li>\n<li>\n<p>When you have a <strong>Software Load Balancer<\/strong>, you do not need to have a dedicated hardware for this type of load balancing, you can simply install the load balancer software on commodity hardware and use it for your load balancing use cases.<\/p>\n<\/li>\n<\/ol>\n<p>Nginx and HAProxy are mostly used as software load balancers and can be very useful in development environments where you can quickly spin up their functionality.<\/p>\n<h2><strong>DNS load balancing<\/strong><\/h2>\n<p>A DNS server may return multiple IP addresses when a client looks up a hostname. This is because in a large-scale deployment, a network target isn&#8217;t just running on one server or even just on one cluster. Generally, the client software chooses an IP address at random from the addresses returned. It&#8217;s not easy to control which IP a client chooses.<\/p>\n<p>Unless you have custom software built to determine the health of a server, pure DNS load balancing doesn&#8217;t know whether a server is up or down, so a client could be sent to a server that is down.<\/p>\n<p>Clients also cache DNS entries by design, and it&#8217;s not always easy to clear them. Many vendors offer the ability to choose which datacenter a client gets routed to based on geographic location. Ultimately, an important way to influence how clients reach your services is through load balancing.<\/p>\n<h2><strong>Dedicated load balancers<\/strong><\/h2>\n<p>Load balancing can happen at Layer3 (Network), Layer4 (Transport), or Layer7 (Application) of the OSI (Open Systems Interconnection) model.<\/p>\n<ul><\/ul>\n<ul>\n<li>An L3 load balancer operates on the source and destination IP addresses.<\/li>\n<li>An L4 load balancer works with IP and port numbers (using the TCP protocol).<\/li>\n<li>An L7 load balancer operates at the final layer, the application layer, by making use of the entire payload. It makes use of the entire application data, including the HTTP URL, cookies, headers, and so on. It&#8217;s able to provide rich functionality for routing. For instance, you can send a request to a particular service based on the type of data that&#8217;s coming in. For example, when a video file is requested, an L7 load balancer can send that request to a streaming appliance.<\/li>\n<\/ul>\n<p>There are 5 common load balancing methods in use today:<\/p>\n<ul><\/ul>\n<ul>\n<li><strong>Round Robin:<\/strong> This is the simplest and most common. The targets in a load balancing pool are rotated in a loop.<\/li>\n<li><strong>Weighted Round Robin:<\/strong> Like a round robin, but with manually-assigned importance for the back-end targets.<\/li>\n<li><strong>Least Loaded or Least Connections:<\/strong> Requests are routed based on the load of the targets reported by the target back-end.<\/li>\n<li><strong>Least Loaded with Slow Start<\/strong> or <strong>Least Response Time:<\/strong> Targets are chosen based on its load, and requests are gradually increased over time to prevent flooding the least loaded back-end.<\/li>\n<li><strong>Utilization Limit:<\/strong> Based on queries per second (QPS), or events per second (EPS), reported by the back-end.<\/li>\n<\/ul>\n<p>There are software-based load balancers. This can be software running on commodity hardware.You can pick up any Linux box and install <a href=\"https:\/\/opensource.com\/article\/20\/11\/load-balancing-haproxy\" target=\"_blank\" rel=\"noopener\">HAProxy<\/a> or <a href=\"https:\/\/www.redhat.com\/sysadmin\/setting-reverse-proxies-nginx\" target=\"_blank\" rel=\"noopener\">Nginx<\/a> on it. Software load balancers tend to evolve quickly, as software often does, and is relatively inexpensive.<\/p>\n<p>You can also use hardware-based, purpose-built load balancer appliances. These can be expensive, but they may offer unique features for specialized markets.<\/p>\n<h2>Transitioning to Canary-based Deployment<\/h2>\n<p>In my <a href=\"https:\/\/opensource.com\/article\/22\/6\/change-management-site-reliability-engineers\" target=\"_blank\" rel=\"noopener\">previous article<\/a>, I explained how Canary-based deployments help the SRE ensure smooth upgrades and transitions. The exact method you use for a Canary-based rollout depends on your requirements and environment.<\/p>\n<p>Generally, you release changes on one server first, just to see how the deployed package fares. What you are looking for at this point is to ensure that there are no catastrophic failures, for example, a server doesn&#8217;t come up or the server crashes after it comes up.<\/p>\n<p>Once that one server deployment is successful, you want to proceed to install it in up to 1% of the servers. Again this 1% is a generic guideline but it really depends on the number of servers you have and your environment. You can then proceed to release for early adaptors if it&#8217;s applicable in your environment.<\/p>\n<p>Finally, once canary testing is complete, you&#8217;d want to release for all users at that point.<\/p>\n<p>Keep in mind that a failed canary is a serious issue. Analyze and implement robust testing processes so that issues like this can be caught during testing phase and not during canary testing.<\/p>\n<h2>Reliability matters<\/h2>\n<p>Reliability is key, so utilize a circuit breaker pattern to fail fast in a distributed system. You can use Hystrix libraries or Istio.<\/p>\n<p>Design load balancing with a mix of DNS and dedicated load balancers, especially if your application is web-scale. DNS load balancing may not be fully reliable, primarily because the DNS servers may not know which back-end servers are healthy or even up and running.<\/p>\n<p>Finally, you must use canary releases, but remember that a canary is a not a replacement for testing.<\/p>\n<\/div>\n<div class=\"clearfix text-formatted field field--name-field-article-subhead field--type-text-long field--label-hidden field__item\">\n<p>Learn what a circuit breaker pattern is, how to implement it, and how to do load balancing.<\/p>\n<\/div>\n<div class=\"field field--name-field-lead-image field--type-entity-reference field--label-hidden field__item\">\n<article class=\"media media--type-image media--view-mode-caption\">\n<div class=\"field field--name-field-media-image field--type-image field--label-hidden field__item\">  <img decoding=\"async\" src=\"https:\/\/www.cryptocabaret.com\/wp-content\/uploads\/2022\/06\/programming_keyboard_coding-1.png\" width=\"520\" height=\"292\" alt=\"Programming keyboard.\" title=\"Programming keyboard.\" loading=\"lazy\"><\/div>\n<div class=\"field field--name-field-caption field--type-text-long field--label-hidden caption field__item\"><span class=\"caption__byline\">Image by: <\/span><\/p>\n<p>Opensource.com<\/p>\n<\/div>\n<\/article>\n<\/div>\n<div class=\"field field--name-field-listicle-title field--type-string field--label-hidden field__item\">What to read next<\/div>\n<div class=\"field field--name-field-listicles field--type-entity-reference field--label-hidden field__items\">\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/article\/22\/6\/introduction-site-reliability-engineering\" hreflang=\"en\">What you need to know about site reliability engineering<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/article\/22\/6\/effective-incident-response-site-reliability-engineers\" hreflang=\"en\">How SREs can achieve effective incident response<\/a><\/div>\n<div class=\"field__item\"><a href=\"https:\/\/opensource.com\/article\/22\/6\/change-management-site-reliability-engineers\" hreflang=\"en\">A site reliability engineer&#8217;s guide to change management<\/a><\/div>\n<\/p><\/div>\n<div class=\"field field--name-field-default-license field--type-list-string field--label-hidden field__item\"><a rel=\"license\" href=\"http:\/\/creativecommons.org\/licenses\/by-sa\/4.0\/\"><br \/>\n        <img decoding=\"async\" alt=\"Creative Commons License\" src=\"https:\/\/www.cryptocabaret.com\/wp-content\/uploads\/2022\/06\/cc-by-sa--29.png\" title=\"This work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.\"><\/a>This work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.<\/div>\n<section class=\"field field--name-field-comments field--type-comment field--label-hidden comment-wrapper\">\n<div class=\"comments__count\">\n<div class=\"login\"><a href=\"https:\/\/opensource.com\/user\/register?absolute=1\">Register<\/a> or <a href=\"https:\/\/opensource.com\/user\/login?current=\/rss.xml&amp;absolute=1\">Login<\/a> to post a comment.<\/div>\n<\/p><\/div>\n<\/section>\n<p class=\"wpematico_credit\"><small>Powered by <a href=\"http:\/\/www.wpematico.com\" target=\"_blank\" rel=\"noopener\">WPeMatico<\/a><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to use a circuit breaker pattern for site reliability engineering Robert Kimani Thu, 06\/23\/2022 &#8211; 03:00 Register or Login to like Register or Login to like Distributed systems have unique requirements for the site reliability engineer (SRE). One of the best ways to maintain site reliability is to enforce a certain set of best [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":65279,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[307],"tags":[],"class_list":["post-65278","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-open-source"],"_links":{"self":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/posts\/65278","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=65278"}],"version-history":[{"count":0,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/posts\/65278\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=\/wp\/v2\/media\/65279"}],"wp:attachment":[{"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=65278"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=65278"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cryptocabaret.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=65278"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}