Data Feed Failover Using Reactive Extensions for .NET


Reactive Framework Labs

This article is one from the “Processing Financial Data with Microsoft Reactive Framework (Reactive Extensions for .NET)” series of articles that demonstrate some basic applications of Rx framework to financial data streams. This article demonstrates automatic and transparent failover from primary data feed to a backup one implemented in terms of Microsoft Reactive Framework. Other articles in the series are:

Visual Studio 2008 solution containing full source code for all examples in this article can be downloaded here.

Failover Implementation

The effect of automatic failover is implemented using the “Catch” operator built into the Rx framework.  The “Catch” operator simply repeats the first stream until it fails. When the first stream fails, exception is swallowed and the second stream is subscribed to and repeated. You can chain any number of streams this way. Here is a code sample that uses “Catch” to switch from some primary feed to a backup:

// simulate market data feed
var rnd = new Random();
var feedSimulator = Observable.Defer(() =>
    Observable.Return(Math.Round(30.0 + rnd.NextDouble(), 2))
    .Delay(TimeSpan.FromSeconds(rnd.NextDouble())))
    .Repeat();

// Simulate fatal problem with feed 5 seconds down the road
var feedProblem =
    Observable.Throw<double>(new Exception("Fatal Error!"))
    .Delay(TimeSpan.FromSeconds(5));

// Bloomberg feed: merging simulator with error generator
var bloombergFeed = from p in feedSimulator.Merge(feedProblem)
                    select new { Feed = "Bloomberg", Price = p };
// Reuters feed
var reutersFeed = from p in feedSimulator
                  select new { Feed = "Reuters", Price = p };

// Automatic, transparent failover feed
var failoverFeed = bloombergFeed.Catch(reutersFeed);

// Subscribe and print results to console
bloombergFeed.Subscribe(
    p => { },
    x => Console.WriteLine("Bloomberg feed: {0}", x.Message));
failoverFeed.Subscribe(
    price => Console.WriteLine(price));

Console.ReadKey(true);

The first code block is a simple market data simulator implemented using Reactive Framework.

The second code block simulates a feed problem. It never produces any values and, when subscribed to, throws an exception after 5 seconds. We will combine market data simulator with problem simulator to simulate a feed that breaks down the road.

The third and forth code blocks create two feeds: Bloomberg and Reuters. Bloomberg feed is constructed to “break” after 5 seconds and Reuters is simply based on our market simulator.

Next code block is “var failoverFeed = bloombergFeed.Catch(reutersFeed);”. This statement creates a wrapper feed that starts with Bloomberg and when it fails automatically and transparently switches to Reuters. A subscriber will not notice an interruption. Notice that Reuters feed is not going to be subscribed to until failover happens.

Advertisements
7 comments
  1. TonyW said:

    This code need a small fix since feedProblem will fire the first event (the Exception) immediately without waiting for 5 seconds. Unless I’m mistaken, the 5 seconds delay only applies to the 2nd event and beyond .

    • Ashish Singh said:

      we could use this as a fix..var feedProblem =
      Observable.Never().Timeout(TimeSpan.FromSeconds(5),
      Observable.Throw(new Exception(“Fatal Error!”)));

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: