Fixing Performance Issues - Introducing Caching with Circuit Breaker

James Vidler -

If you've identified a bottleneck in your website which is causing performance issues, there a variety of approaches you can take to alleviate these. Often these bottlenecks revolve around data access whether that's from an external API, database, or a complex method that is resulting in intensive CPU usage.

If you haven't read our post on Accessing Volatile Resources, its a great read and will help you in understanding the problem with relying on external sources and the steps required keep your app performing well.

Introducing Circuit Breaker

In this post, we want to share an in-house library we've been using for years on our sites to easily add memory and file based caching with minimal code for each implementation.

Circuit Breaker works by specifying a CircuitId, CacheKey, WorkingDirectory (for file based cache), CacheDuration (how long you want to keep it in cache), and lastly a function delegate (your code you need to execute).

The Setup

var breaker = new CircuitBreaker(
                    circuitId: "My-circuit-key",
                    cacheKey: "My-unique-key",
                    workingDirectory: @"C:\MyCacheDirectory",
                    cacheDuration: Timespan.FromSeconds(60)
                );

 The Execution

dynamic result = breaker.Execute(() => {
                var _result = DoThing();
                return _result;
            });

How it Works

  1. Application starts and the first request to DoThing() is executed (because nothing is cached yet)
  2. Response is stored in memory cache for 60 seconds
  3. Response is also stored in a JSON file representing your serialized response
  4. All subsequent requests will have a response served from cache within the 60 seconds
  5. If the response is kicked out of cache before the 60 seconds has elapsed, the file system will be used to return the response
  6. After the 60 seconds has elapsed, DoThing() will be executed again
  7. If DoThing() throws an exception, the last file based cache response will be used as a fallback

Get the Source

The full Circuit Breaker source code is now available on GitHub along with a sample console application to test out its functionality. 

Any questions or feedback? Leave us a note in the comments.

Have more questions? Submit a request

0 Comments

Please sign in to leave a comment.