Circuit breaker design pattern: Difference between revisions

Content deleted Content added
m cat
restructured text, added example implementation in PHP
Line 3:
'''Circuit breaker''' is a [[Design pattern (computer science)|design pattern]] in modern [[software development]].
 
ItCircuit breaker is aused to detect componentfailures thatand encapsulates logic of preventing a failure to reoccur constantly (during maintenance, temporary external system failure or unexpected system difficulties).
 
==Common Uses==
Example: Your application connects to a [[database]] 100 times per second and the database fails. You do not want to have the same error reoccur constantly. You also want to handle the error quickly and gracefully without waiting for [[TCP connection]] timeout.
 
Example: Your application connects to a [[database]] 100 times per second and the database fails. You do not want to have the same error reoccur constantly. You also want to handle the error quickly and gracefully without waiting for [[TCP connection]] timeout.
 
[[Circuit breaker]] is used to detect failures and prevent application from trying to perform the action that is doomed to fail (until its safe to retry).
 
==Example Implementation==
==External links==
 
===PHP===
 
The following is a [[Proof_of_concept|POC]] example implementation in PHP.
 
==== Check ====
 
The following script could be run on a set interval through [[crontab]].
 
<source lang="php">
$db = mysql_connect('localhost','root','pass');
if ($db === false) {
apc_store('dbUp', 'up');
} else {
apc_store('dbUp', 'down');
@mysql_close($db);
}
</source>
 
====Usage in an application====
 
<source lang="php">
if (apc_fetch('dbUp') === 'down') {
echo "The database server is currently not available. Please try again in a minute.";
exit;
}
$db = mysql_connect('localhost', 'root', 'pass');
$res = mysql_db_query('database', 'SELECT * FROM table');
</source>
 
==Weblinks==
*[http://artur.ejsmont.org/blog/PHP-Circuit-Breaker-initial-Zend-Framework-proposal Example of PHP implementation with diagrams]
 
[[Category:Software developmentdesign patterns]]
[[Category:Articles with example PHP code]]