Circuit breaker design pattern

Circuit breaker design pattern

Circuit breaker is a design pattern in modern software development.

Circuit breaker is used to detect failures and encapsulates logic of preventing a failure to reoccur constantly (during maintenance, temporary external system failure or unexpected system difficulties).

Contents

Common Uses

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.

Generally Circuit Breaker can be used to check the availability of an external service. An external can be a database server or a web service used by the application.

Circuit breaker detects failures and prevents the application from trying to perform the action that is doomed to fail (until its safe to retry).

Implementation

The Circuit Breaker Design Pattern should be implemented asynchronously. The reason is to offload the logic to detect failures from the actual request.

This requires Circuit Breaker to use a persistent storage layer, e.g. a network cache such as Memcached or Redis, or local cache (disk or memory based) to record the availability of a, to the application, external service.

Circuit Breaker records the state of the external service on a given interval.

Before the external service is used from the application, the storage layer is queried to retrieve the current state.

Performance Implication

While it's safe to say that the benefits outweigh the consequences, implementing Circuit Breaker will of course affect the performance.

By how much depends on the storage layer used and generally available resources. The largest factors in this regard are the type of cache, for example, disk-based vs. memory-based and local vs. network.

Example Implementation

PHP

The following is a POC example implementation in PHP. The POC stores the status of a MySQL server into a shared memory cache (APC).

Check

The following script could be run on a set interval through crontab.

$db = mysql_connect('localhost','root','pass');
if ($db === false) {
    apc_store('dbUp', 'up');
} else {
    apc_store('dbUp', 'down');
    @mysql_close($db);
}

Usage in an application

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');

External links


Wikimedia Foundation. 2010.

Игры ⚽ Нужна курсовая?

Look at other dictionaries:

  • Circuit breaker (disambiguation) — Circuit breaker or Circuit breakers may refer to: Circuit breaker, an automatic electrical switch Circuit Breaker (Transformers), a Marvel Comics character Trading curb, a stock market term An alias of Richie Hawtin, an electronic musician and DJ …   Wikipedia

  • building construction — Techniques and industry involved in the assembly and erection of structures. Early humans built primarily for shelter, using simple methods. Building materials came from the land, and fabrication was dictated by the limits of the materials and… …   Universalium

  • AC power plugs and sockets — See also: Industrial and multiphase power plugs and sockets Plugs and sockets may sometimes combine male and female contacts, but t …   Wikipedia

  • List of electronics topics — Alphabetization has been neglected in some parts of this article (the b section in particular). You can help by editing it. This is a list of communications, computers, electronic circuits, fiberoptics, microelectronics, medical electronics,… …   Wikipedia

  • gasoline engine — Most widely used form of internal combustion engine, found in most automobiles and many other vehicles. Gasoline engines vary significantly in size, weight per unit of power generated, and arrangement of components. The principal type is the… …   Universalium

  • List of Canada's Worst Handyman episodes — This is a list of episodes for the first season of the Canadian television series Canada s Worst Handyman , where the worst contestant, as determined by 25 challenges over 12 days, is given the dubious title. Format Each of the first five… …   Wikipedia

  • Copper wire and cable — Copper has been used in electric wiring since the invention of the electromagnet and the telegraph in the 1820s.[1][2] The invention of the telephone in 1876 proved to be another early boon for copper wire.[3] Today, despite competition from… …   Wikipedia

  • Ford Fiesta — Not to be confused with the Ford Festiva. Ford Fiesta Manufacturer Ford Motor Company Produ …   Wikipedia

  • Northeast Blackout of 2003 — The Northeast Blackout of 2003 was a massive widespread power outage that occurred throughout parts of the Northeastern and Midwestern United States, and Ontario, Canada on Thursday, August 14, 2003, at approximately 4:15 pm EDT (20:15 UTC), with …   Wikipedia

  • Ultramatic — For the Voigtländer SLR camera, see Voigtländer Ultramatic CS Ultramatic was the trademarked name of the Packard Motor Car Company s automatic transmission introduced in 1949 and produced until 1956 at Packard s Detroit, Michigan factory. 1935… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”