[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"academy-blogs-en-1-1-all-golang-the-series-ep166-circuit-breaker-pattern-all--*":3,"academy-blog-translations-awsc2imu18pmn52":92},{"data":4,"page":77,"perPage":77,"totalItems":77,"totalPages":77},[5],{"alt":6,"collectionId":7,"collectionName":8,"content":9,"cover_image":10,"cover_image_path":11,"created":12,"created_by":13,"expand":14,"id":85,"keywords":86,"locale":57,"published_at":87,"scheduled_at":73,"school_blog":81,"short_description":88,"status":79,"title":89,"updated":90,"updated_by":91,"slug":82,"views":84},"Preventing system failures with Circuit Breaker Pattern in Go using sonygobreaker","sclblg987654321","school_blog_translations","\u003Cp>Welcome back to EP.166! In EP.165, we learned how to distribute workloads across multiple AI nodes using a Load Balancer to handle massive traffic. However, in a real-world production environment, networks and external services are always unpredictable.\u003C\u002Fp>\u003Cp>Imagine this: The primary AI provider's API suffers an outage, or your on-premise network experiences a latency spike from 2 seconds to 30 seconds. The consequence isn't just a slow AI response; incoming requests pile up, Goroutines consume all application memory resources, and your entire backend system collapses in a domino effect known as a \u003Cem>Cascading Failure\u003C\u002Fem>.\u003C\u002Fp>\u003Cp>To prevent our core system from crashing along with the downstream AI, we can implement an architectural pattern called the \u003Cem>Circuit Breaker Pattern\u003C\u002Fem> to quarantine the failure!\u003C\u002Fp>\u003Ch2>Understanding the 3 States of a Circuit Breaker\u003C\u002Fh2>\u003Cp>A Circuit Breaker acts like an electrical circuit breaker in a house. When a short circuit occurs, the breaker trips immediately to prevent a fire. It operates in three main states:\u003C\u002Fp>\u003Cp>Plaintext\u003C\u002Fp>\u003Cpre>\u003Ccode>               ┌─────────────────────────────────────────────────────────┐\n               │                                                         │\n               ▼                                                         │\n         ┌──────────┐      (High Error Rate Threshold)    ┌──────────┐   │ (Test Request Success)\n         │  CLOSED  │ ───────────────────────────────────&gt; │   OPEN   │   │\n         │ (Closed) │                                     │  (Open)  │   │\n         └──────────┘                                      └──────────┘   │\n              ▲                                                  │        │\n              │                                                  │        │\n              │                                                  │ (Cooldown Period)\n              │                                                  ▼        │\n              │                (Test Request Failed)       ┌───────────┐  │\n              └─────────────────────────────────────────── │ HALF-OPEN │ ─┘\n                                                           │(Half-Open)│\n                                                           └───────────┘\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cul>\u003Cli>\u003Cp>\u003Cstrong>CLOSED (Normal State):\u003C\u002Fstrong> The switch is closed, and requests flow freely to the AI API. The system tracks the failure rate in memory.\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>\u003Cstrong>OPEN (Tripped \u002F Failure State):\u003C\u002Fstrong> When errors or timeouts exceed the threshold, the breaker trips open! Any incoming requests are rejected immediately (\u003Cem>Fail Fast\u003C\u002Fem>) without wasting time calling the failed AI API.\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>\u003Cstrong>HALF-OPEN (Testing State):\u003C\u002Fstrong> After a cooldown period, the breaker allows a small number of test requests to pass through to the AI API. If they succeed, it transitions back to CLOSED; if they fail, it trips back to OPEN.\u003C\u002Fp>\u003C\u002Fli>\u003C\u002Ful>\u003Ch2>Installing Dependencies with Sonygobreaker\u003C\u002Fh2>\u003Cp>One of the most stable, high-performance, and popular Circuit Breaker libraries in the Go ecosystem is \u003Cem>sonygobreaker\u003C\u002Fem> by Sony:\u003C\u002Fp>\u003Cp>Bash\u003C\u002Fp>\u003Cpre>\u003Ccode>go get github.com\u002Fsony\u002Fgobreaker\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Ch2>Structure and Go Code Implementation with Circuit Breaker\u003C\u002Fh2>\u003Cp>We separate the mock API call function and the circuit breaker controller for clean and readable code.\u003C\u002Fp>\u003Ch3>Part 1: External API Call Function and Failure Simulation\u003C\u002Fh3>\u003Cp>Go\u003C\u002Fp>\u003Cpre>\u003Ccode>package main\n\nimport (\n\t\"context\"\n\t\"errors\"\n\t\"fmt\"\n\t\"log\"\n\t\"time\"\n\n\t\"[github.com\u002Fsony\u002Fgobreaker](https:\u002F\u002Fgithub.com\u002Fsony\u002Fgobreaker)\"\n)\n\n\u002F\u002F CallExternalAIAPI simulates calling an external AI API\nfunc CallExternalAIAPI(ctx context.Context, question string, simulateFailure bool) (string, error) {\n\tif simulateFailure {\n\t\t\u002F\u002F Simulate AI API outage or timeout\n\t\ttime.Sleep(200 * time.Millisecond)\n\t\treturn \"\", errors.New(\"503 Service Unavailable: AI Model Overloaded\")\n\t}\n\n\t\u002F\u002F Normal operation case\n\treturn fmt.Sprintf(\"AI response for question '%s': Processing successful...\", question), nil\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Ch3>Part 2: Circuit Breaker Configuration and Main Controller: main.go\u003C\u002Fh3>\u003Cp>Go\u003C\u002Fp>\u003Cpre>\u003Ccode>func main() {\n\t\u002F\u002F 1. Configure the Circuit Breaker settings\n\tsettings := gobreaker.Settings{\n\t\tName:        \"AI-API-Breaker\",\n\t\tMaxRequests: 2,                \u002F\u002F Number of requests allowed to pass during Half-Open state\n\t\tInterval:    5 * time.Second,  \u002F\u002F Clears error statistics every 5 seconds\n\t\tTimeout:     3 * time.Second,  \u002F\u002F Cooldown period in Open state before transitioning to Half-Open\n\t\tReadyToTrip: func(counts gobreaker.Counts) bool {\n\t\t\t\u002F\u002F Trip condition: If requests &gt;= 3 and failure rate &gt;= 50%, trip immediately!\n\t\t\tfailureRatio := float64(counts.TotalFailures) \u002F float64(counts.Requests)\n\t\t\treturn counts.Requests &gt;= 3 &amp;&amp; failureRatio &gt;= 0.5\n\t\t},\n\t\tOnStateChange: func(name string, from gobreaker.State, to gobreaker.State) {\n\t\t\t\u002F\u002F Callback observer when the circuit state changes\n\t\t\tlog.Printf(\"⚠️ [Circuit Breaker: %s] State changed from %s -&gt; %s\\n\", name, from, to)\n\t\t},\n\t}\n\n\tcb := gobreaker.NewCircuitBreaker(settings)\n\tquestion := \"Please summarize the annual report.\"\n\n\t\u002F\u002F 2. Simulate 10 consecutive requests to observe trip behavior\n\tfmt.Println(\"🚀 Starting request simulation to AI API...\")\n\tfor i := 1; i &lt;= 10; i++ {\n\t\t\u002F\u002F Simulate API failure for requests 1 to 5, then recover afterwards\n\t\tsimulateFailure := i &lt;= 5\n\n\t\t\u002F\u002F Execute controls whether the request goes through to the actual API\n\t\tresult, err := cb.Execute(func() (interface{}, error) {\n\t\t\treturn CallExternalAIAPI(context.Background(), question, simulateFailure)\n\t\t})\n\n\t\tif err != nil {\n\t\t\t\u002F\u002F Check if the error is caused by the Circuit Breaker tripping\n\t\t\tif errors.Is(err, gobreaker.ErrOpenState) {\n\t\t\t\tfmt.Printf(\"🔴 [Req #%d] Circuit Breaker OPEN! (Tripped) -&gt; [Fallback]: 'AI service is currently unavailable. Please use standard search.'\\n\", i)\n\t\t\t} else {\n\t\t\t\tfmt.Printf(\"❌ [Req #%d] API Error: %v -&gt; [Fallback]: 'Sorry, unable to process response.'\\n\", i, err)\n\t\t\t}\n\t\t} else {\n\t\t\tfmt.Printf(\"🟢 [Req #%d] Success: %v\\n\", i, result)\n\t\t}\n\n\t\ttime.Sleep(800 * time.Millisecond)\n\t}\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Ch2>Fallback Mechanism Strategies\u003C\u002Fh2>\u003Cp>When the Circuit Breaker trips to \u003Cem>OPEN\u003C\u002Fem>, the system should not leave users stranded with frozen screens or cryptic error messages. Here are strategies you can implement:\u003C\u002Fp>\u003Col>\u003Cli>\u003Cp>\u003Cstrong>Degraded Service (Model Failover):\u003C\u002Fstrong> Switch to a smaller, locally-hosted model (such as Llama-3-8B) instead of the heavy cloud model.\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>\u003Cstrong>Static \u002F Cached Answer:\u003C\u002Fstrong> Fetch a basic response from Redis Cache (covered in EP.163) as an instant substitute.\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>\u003Cstrong>Graceful Degradation Message:\u003C\u002Fstrong> Display a friendly notification with alternative actions, e.g., \u003Cem>\"The AI assistant is experiencing high traffic. Your query has been saved to the queue.\"\u003C\u002Fem>\u003C\u002Fp>\u003C\u002Fli>\u003C\u002Fol>\u003Ch2>🎯 Daily Mission\u003C\u002Fh2>\u003Cp>Try running the code example above and observe the console output showing state transitions from CLOSED to OPEN and tripping immediately under failure conditions.\u003C\u002Fp>\u003Cp>\u003Cstrong>Challenge Question: \u003C\u002Fstrong>If your organization uses two primary AI APIs—OpenAI (Primary) and Gemini (Secondary)—how would you design a Go fallback function so that when OpenAI's Circuit Breaker trips (\u003Cem>OPEN\u003C\u002Fem>), the system automatically routes requests to Gemini? Try designing this architecture!\u003C\u002Fp>\u003Ch2>Frequently Asked Questions (FAQ)\u003C\u002Fh2>\u003Ch3>How does the Circuit Breaker know when the downstream AI API has recovered?\u003C\u002Fh3>\u003Cp>Once the \u003Cem>Timeout\u003C\u002Fem> duration expires while in the \u003Cem>OPEN\u003C\u002Fem> state, the switch automatically transitions to the \u003Cem>HALF-OPEN\u003C\u002Fem> state to test a limited number of requests (defined by \u003Cem>MaxRequests\u003C\u002Fem>). If successful, the system immediately switches back to \u003Cem>CLOSED\u003C\u002Fem>. If it still fails, a new cooldown timer starts.\u003C\u002Fp>\u003Ch3>What Timeout and Error Rate values should we use?\u003C\u002Fh3>\u003Cp>There are no fixed numbers; it depends on the AI provider's SLA and user behavior. Generally, it is recommended to set a \u003Cem>Timeout\u003C\u002Fem> of 3–5 seconds (to prevent users from waiting too long) and trigger \u003Cem>ReadyToTrip\u003C\u002Fem> when the error rate exceeds 50% out of at least 3–5 test requests, preventing premature tripping from transient faults.\u003C\u002Fp>\u003Ch3>How is a Circuit Breaker different from a Rate Limiter?\u003C\u002Fh3>\u003Cp>A \u003Cstrong>Rate Limiter\u003C\u002Fstrong> controls inbound request volume to protect your system from traffic spikes or quota limits. A \u003Cstrong>Circuit Breaker\u003C\u002Fstrong> protects your system outbound when calling external dependencies that are failing or lagging, preventing cascading failures.\u003C\u002Fp>\u003Cdiv data-type=\"horizontalRule\">\u003Chr>\u003C\u002Fdiv>\u003Ch3>Conclusion\u003C\u002Fh3>\u003Cp>In this article, we explored the \u003Cstrong>Circuit Breaker Pattern\u003C\u002Fstrong>, an essential tool for preventing Cascading Failures when external AI APIs experience outages or high latencies. We covered the three main states—\u003Cstrong>CLOSED\u003C\u002Fstrong>, \u003Cstrong>OPEN\u003C\u002Fstrong>, and \u003Cstrong>HALF-OPEN\u003C\u002Fstrong>—and wrote practical Go code using \u003Cem>gobreaker\u003C\u002Fem> to control executions while designing fallback mechanisms to keep our backend stable and deliver a great user experience even during partial system failures.\u003C\u002Fp>\u003Cp>\u003Cstrong>Coming up in EP.167: \u003C\u002Fstrong>Our system now features Multi-LLM, Cache, Rate Limiter, Load Balancer, and Circuit Breaker. But the question is... how do we know how well everything is performing? What is the latency of each AI provider? Next time, we will install a system health monitor with \"Monitoring AI Performance — Using Prometheus to Track Speed and Measure Latency\". Don't miss it, Gophers!\u003C\u002Fp>\u003Cp>\u003Cstrong>Follow Superdev Academy on all platforms:\u003C\u002Fstrong>\u003C\u002Fp>\u003Cul>\u003Cli>\u003Cp>\u003Cstrong>🔵 Facebook: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener\" class=\"ng-star-inserted\" href=\"https:\u002F\u002Fwww.facebook.com\u002Fsuperdev.academy.th\">\u003Cstrong>Superdev Academy Thailand\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>\u003Cstrong>🎬 YouTube: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener\" class=\"ng-star-inserted\" href=\"https:\u002F\u002Fwww.youtube.com\u002F@SuperdevAcademy\">\u003Cstrong>Superdev Academy Channel\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>\u003Cstrong>📸 Instagram: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener\" class=\"ng-star-inserted\" href=\"https:\u002F\u002Fwww.instagram.com\u002Fsuperdevacademy\u002F\">\u003Cstrong>@superdevacademy\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>\u003Cstrong>🎬 TikTok: \u003C\u002Fstrong>\u003Ca target=\"_blank\" rel=\"noopener\" class=\"ng-star-inserted\" href=\"https:\u002F\u002Fwww.tiktok.com\u002F@superdevacademy?lang=th-TH\">\u003Cstrong>@superdevacademy\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>\u003Cstrong>🌐 Website: \u003C\u002Fstrong>\u003Ca rel=\"noopener noreferrer\" href=\"https:\u002F\u002Fsuperdevacademy.com\">\u003Cstrong>superdevacademy.com\u003C\u002Fstrong>\u003C\u002Fa>\u003C\u002Fp>\u003C\u002Fli>\u003C\u002Ful>\u003Cp>\u003C\u002Fp>","52ivpikg8dwp_rjvpe6xij6.png","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclblg987654321\u002Fbkboww8dcyo4a63\u002F52ivpikg8dwp_rjvpe6xij6.png","2026-07-29 06:20:48.582Z","76qprkevbgfdps8",{"keywords":15,"locale":51,"school_blog":61},[16,23,28,32,36,41,46],{"collectionId":17,"collectionName":18,"created":19,"created_by":13,"id":20,"name":21,"updated":22,"updated_by":13},"sclkey987654321","school_keywords","2026-03-04 08:20:14.253Z","ah6lvy4x8qe08l5","Golang","2026-06-07 06:45:08.193Z",{"collectionId":17,"collectionName":18,"created":24,"created_by":13,"id":25,"name":26,"updated":27,"updated_by":13},"2026-03-04 08:44:37.056Z","erl8jxcjedbe0i0","Circuit Breaker","2026-06-07 06:46:36.230Z",{"collectionId":17,"collectionName":18,"created":29,"created_by":13,"id":30,"name":31,"updated":29,"updated_by":13},"2026-07-29 05:13:09.274Z","fyvc2s03ninvaso","Go API",{"collectionId":17,"collectionName":18,"created":33,"created_by":13,"id":34,"name":35,"updated":33,"updated_by":13},"2026-07-29 05:13:11.605Z","socfbskutcq78z4","Cascading Failure",{"collectionId":17,"collectionName":18,"created":37,"created_by":13,"id":38,"name":39,"updated":40,"updated_by":13},"2026-04-03 10:57:34.421Z","azixuoag5jisout","Backend Development","2026-06-07 06:49:02.435Z",{"collectionId":17,"collectionName":18,"created":42,"created_by":13,"id":43,"name":44,"updated":45,"updated_by":13},"2026-05-19 08:32:40.909Z","y6cwydp81xsem1f","AI API","2026-06-07 06:49:16.309Z",{"collectionId":17,"collectionName":18,"created":47,"created_by":13,"id":48,"name":49,"updated":50,"updated_by":13},"2026-03-04 08:20:11.547Z","ey3puyme01a9bsw","Go","2026-06-07 06:45:07.798Z",{"code":52,"collectionId":53,"collectionName":54,"created":55,"flag":56,"id":57,"is_default":58,"label":59,"updated":60},"en","pbc_1989393366","locales","2026-01-22 11:00:02.726Z","twemoji:flag-united-states","qv9c1llfov2d88z",false,"English","2026-04-10 15:42:46.825Z",{"category":62,"collectionId":63,"collectionName":64,"created":65,"expand":66,"id":81,"slug":82,"updated":83,"views":84},"wqxt7ag2gn7xcmk","pbc_2105096300","school_blogs","2026-07-29 05:13:30.982Z",{"category":67},{"blogIds":68,"collectionId":69,"collectionName":70,"created":71,"created_by":13,"id":62,"image":72,"image_alt":73,"image_path":74,"label":75,"name":76,"priority":77,"publish_at":78,"scheduled_at":73,"status":79,"updated":80,"updated_by":13},[],"sclcatblg987654321","school_category_blogs","2026-03-04 08:33:53.210Z","59ty92ns80w_15oc1implw.png","","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclcatblg987654321\u002Fwqxt7ag2gn7xcmk\u002F59ty92ns80w_15oc1implw.png",{"en":76,"th":76},"Golang The Series",1,"2026-03-16 04:39:38.440Z","published","2026-06-07 06:45:03.856Z","awsc2imu18pmn52","golang-the-series-ep166-circuit-breaker-pattern","2026-08-04 16:46:25.577Z",126,"bkboww8dcyo4a63",[20,25,30,34,38,43,48],"2026-08-04 04:12:27.810Z","Techniques to prevent Cascading Failures in Go backend systems when AI APIs collapse using the Circuit Breaker Pattern with gobreaker, along with Enterprise-grade Fallback Mechanism strategies.","Golang The Series EP.166: Circuit Breaker Pattern How to Handle AI API Outages","2026-08-04 04:12:27.833Z","423vhnv3ckczcyn",{"th":82,"en":82}]