Circuit Breaker

The implemented Node-RED flow serves as an exemplary demonstration of the Circuit Breaker Cloud Pattern, enhancing the resilience and fault tolerance of cloud-based applications. By incorporating intelligent circuit breaker mechanisms, the flow effectively manages failures, minimizes cascading effects, and ensures the stability of the overall system. This implementation provides valuable insights for developers aiming to leverage Node-RED in designing robust and resilient cloud architectures.

The Circuit Breaker Cloud Pattern within the Node-RED flow introduces an effective fault-handling mechanism that prevents the propagation of failures, thereby reducing the impact on the system's performance and availability. By monitoring the response of external services, the circuit breaker node intelligently detects and responds to failures. Once a failure threshold is reached, the circuit breaker transitions into an open state, temporarily preventing further requests and redirecting them to fallback or alternate paths. This approach enables the system to gracefully handle failures, mitigate cascading effects, and maintain stability.

The successful integration of the Circuit Breaker Cloud Pattern in the Node-RED flow showcases its potential in enhancing the resilience of cloud-based applications. By intelligently managing failures and providing fallback mechanisms, the flow exhibits improved fault tolerance and ensures the continued operation of critical services. Developers can draw inspiration from this implementation to leverage Node-RED's flexibility and extensibility in designing robust cloud architectures capable of withstanding failures and delivering a reliable user experience.

[{"id":"05edfd1b25e5bde7","type":"subflow","name":"Circuit Breaker","info":"","category":"","in":[{"x":160,"y":160,"wires":[{"id":"0371bc4e46410910"}]}],"out":[{"x":1860,"y":160,"wires":[{"id":"c9666502c12a3d1a","port":0},{"id":"6cb44170b4084411","port":0}]}],"env":[],"meta":{},"color":"#DDAA99"},{"id":"fdf677d770906cf3","type":"group","z":"05edfd1b25e5bde7","style":{"stroke":"#999999","stroke-opacity":"1","fill":"none","fill-opacity":"1","label":true,"label-position":"nw","color":"#a4a4a4"},"nodes":["0371bc4e46410910","2f0659fd98196bfc","8c3995791a5c52c1","92bbfc3eae8195b0","0967aa68ace56359","531ec9eb5741f340","c9666502c12a3d1a"],"x":214,"y":79,"w":1472,"h":162},{"id":"e567da6ad5d10cd8","type":"group","z":"05edfd1b25e5bde7","style":{"stroke":"#999999","stroke-opacity":"1","fill":"none","fill-opacity":"1","label":true,"label-position":"nw","color":"#a4a4a4"},"nodes":["dc8f69e4fdd7f325","3cb1df645a3d72f0","aa1a23064278cd5a","bdfcb642ee07900b"],"x":234,"y":299,"w":792,"h":82},{"id":"a2c52ae6ac73a584","type":"group","z":"05edfd1b25e5bde7","style":{"stroke":"#999999","stroke-opacity":"1","fill":"none","fill-opacity":"1","label":true,"label-position":"nw","color":"#a4a4a4"},"nodes":["959267d99327fa8a","f24fbee8413cb6ba","05f5fb2d8b572c51","064a9c04b5f8e074","502823775e67bc8a","9b04f2ff333531ed","6cb44170b4084411","5fc25fa2841c02eb","68693f5b56932983"],"x":194,"y":439,"w":1672,"h":242},{"id":"0371bc4e46410910","type":"function","z":"05edfd1b25e5bde7","g":"fdf677d770906cf3","name":"set failure counter","func":"node.warn(\"FAILURE COUNTER: \" + flow.get(\"failure_counter\"));\nreturn msg;","outputs":1,"noerr":0,"initialize":"// Code added here will be run once\n// whenever the node is started.\nflow.set(\"failure_counter\", 0);","finalize":"","libs":[],"x":330,"y":160,"wires":[["2f0659fd98196bfc"]]},{"id":"2f0659fd98196bfc","type":"switch","z":"05edfd1b25e5bde7","g":"fdf677d770906cf3","name":"failure counter reached threshold","property":"failure_counter","propertyType":"flow","rules":[{"t":"gte","v":"5","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":600,"y":160,"wires":[["3cb1df645a3d72f0"],["92bbfc3eae8195b0"]]},{"id":"8c3995791a5c52c1","type":"function","z":"05edfd1b25e5bde7","g":"fdf677d770906cf3","name":"throw failure exception","func":"throw new Error(\"HTTP Request failed\");\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1550,"y":200,"wires":[[]]},{"id":"92bbfc3eae8195b0","type":"http request","z":"05edfd1b25e5bde7","g":"fdf677d770906cf3","name":"","method":"GET","ret":"txt","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":890,"y":160,"wires":[["0967aa68ace56359"]]},{"id":"0967aa68ace56359","type":"switch","z":"05edfd1b25e5bde7","g":"fdf677d770906cf3","name":"if success","property":"statusCode","propertyType":"msg","rules":[{"t":"btwn","v":"200","vt":"num","v2":"299","v2t":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":1060,"y":160,"wires":[["c9666502c12a3d1a"],["531ec9eb5741f340"]]},{"id":"531ec9eb5741f340","type":"function","z":"05edfd1b25e5bde7","g":"fdf677d770906cf3","name":"increment failure counter","func":"flow.set(\"failure_counter\", flow.get(\"failure_counter\") + 1);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1290,"y":200,"wires":[["8c3995791a5c52c1"]]},{"id":"c9666502c12a3d1a","type":"function","z":"05edfd1b25e5bde7","g":"fdf677d770906cf3","name":"reset failure counter","func":"flow.set(\"failure_counter\", 0);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1280,"y":120,"wires":[[]]},{"id":"dc8f69e4fdd7f325","type":"delay","z":"05edfd1b25e5bde7","g":"e567da6ad5d10cd8","name":"","pauseType":"delay","timeout":"5","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"allowrate":false,"outputs":1,"x":740,"y":340,"wires":[["bdfcb642ee07900b"]]},{"id":"3cb1df645a3d72f0","type":"function","z":"05edfd1b25e5bde7","g":"e567da6ad5d10cd8","name":"set is_open","func":"return msg;","outputs":1,"noerr":0,"initialize":"// Code added here will be run once\n// whenever the node is started.\nflow.set(\"is_open\", true);","finalize":"","libs":[],"x":330,"y":340,"wires":[["aa1a23064278cd5a"]]},{"id":"aa1a23064278cd5a","type":"switch","z":"05edfd1b25e5bde7","g":"e567da6ad5d10cd8","name":"circuit is open","property":"is_open","propertyType":"flow","rules":[{"t":"true"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":540,"y":340,"wires":[["dc8f69e4fdd7f325"],["959267d99327fa8a"]]},{"id":"bdfcb642ee07900b","type":"function","z":"05edfd1b25e5bde7","g":"e567da6ad5d10cd8","name":"negate is_open","func":"flow.set(\"is_open\", false);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":920,"y":340,"wires":[["959267d99327fa8a"]]},{"id":"959267d99327fa8a","type":"function","z":"05edfd1b25e5bde7","g":"a2c52ae6ac73a584","name":"set success counter","func":"node.warn(\"SUCCESS COUNTER:\" + flow.get(\"success_counter\"));\nreturn msg;","outputs":1,"noerr":0,"initialize":"// Code added here will be run once\n// whenever the node is started.\nflow.set(\"success_counter\", 0);","finalize":"","libs":[],"x":320,"y":520,"wires":[["5fc25fa2841c02eb"]]},{"id":"f24fbee8413cb6ba","type":"switch","z":"05edfd1b25e5bde7","g":"a2c52ae6ac73a584","name":"success counter reached threshold","property":"success_counter","propertyType":"flow","rules":[{"t":"gte","v":"3","vt":"str"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":750,"y":520,"wires":[["05f5fb2d8b572c51"],["064a9c04b5f8e074"]]},{"id":"05f5fb2d8b572c51","type":"function","z":"05edfd1b25e5bde7","g":"a2c52ae6ac73a584","name":"reset circuit","func":"flow.set(\"failure_counter\", 0);\nflow.set(\"is_open\", true);\nflow.set(\"success_counter\", 0);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1070,"y":480,"wires":[["0371bc4e46410910"]]},{"id":"064a9c04b5f8e074","type":"http request","z":"05edfd1b25e5bde7","g":"a2c52ae6ac73a584","name":"","method":"GET","ret":"txt","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","insecureHTTPParser":false,"authType":"","senderr":false,"headers":[],"x":1070,"y":580,"wires":[["502823775e67bc8a"]]},{"id":"502823775e67bc8a","type":"switch","z":"05edfd1b25e5bde7","g":"a2c52ae6ac73a584","name":"if success","property":"statusCode","propertyType":"msg","rules":[{"t":"btwn","v":"200","vt":"num","v2":"299","v2t":"num"},{"t":"else"}],"checkall":"true","repair":false,"outputs":2,"x":1240,"y":580,"wires":[["6cb44170b4084411"],["9b04f2ff333531ed"]]},{"id":"9b04f2ff333531ed","type":"function","z":"05edfd1b25e5bde7","g":"a2c52ae6ac73a584","name":"reset success counter","func":"flow.set(\"success_counter\", 0);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1470,"y":640,"wires":[["68693f5b56932983"]]},{"id":"6cb44170b4084411","type":"function","z":"05edfd1b25e5bde7","g":"a2c52ae6ac73a584","name":"increment success counter","func":"flow.set(\"success_counter\", flow.get(\"success_counter\") + 1);\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1480,"y":520,"wires":[[]]},{"id":"5fc25fa2841c02eb","type":"function","z":"05edfd1b25e5bde7","g":"a2c52ae6ac73a584","name":"filter","func":"if(context.get(\"counter\") === undefined) {\n    context.set(\"counter\", -1);\n}\ncontext.set(\"counter\", context.get(\"counter\") + 1);\nif(context.get(\"counter\") % 2 != 0) {\n    throw new Error(\"Circuit Breaker: Dropped request due to half-open state\");\n}\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":510,"y":520,"wires":[["f24fbee8413cb6ba"]]},{"id":"68693f5b56932983","type":"function","z":"05edfd1b25e5bde7","g":"a2c52ae6ac73a584","name":"throw failure exception","func":"throw new Error(\"HTTP Request failed\");\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":1730,"y":640,"wires":[[]]}]

Flow Info

Created 2 years, 2 months ago
Rating: not yet rated

Owner

Actions

Rate:

Node Types

Core
  • delay (x1)
  • function (x12)
  • http request (x2)
  • switch (x5)
Other
  • group (x3)
  • subflow (x1)

Tags

Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option