Tech Tock

Time is of the essence.

So many quotes — All fake

I was interviewing with an interesting company the other day and I was asked how I would achieve the maximum throughput for a data feed of financial instrument quotes (not just stock quotes, but maybe foreign exchange, or bonds, etc. — its just an example, but I like to generalize :).

So we worked through the problem a little and as always there’s a couple things I left out that I could have said, but I think I did ok. My last statement was — doing it this way will be pretty good, but probably still not good enough.   There’s something in me that just wants to optimize and admitting “not good enough” bugs me.  The next day I was doing normal stuff and an interesting solution struck me out of the blue.  Multithreaded Stacks, enough said.

So I set out to make a demo program to test the theory and lo and behold, you can easily push 50,000 quotes per second through a WPF application!  50K!  On my crappy 2 year old duo 1.66GHZ/T5450.  I couldn’t believe it. Take that AJAX.

It took a while to setup the demo program and I didn’t get to anything I consider innovative or advanced, but I don’t think I’ll be needing any Multithreaded Stacks today.  Just seeing 50,000 quotes flowing through was enough for me.

Here’s the test program:
Mind you its just a demo. The quotes just consist of a random 3 letter id and a random price. But it does have a fun interactive graph thanks to Microsoft and the quotes do update in the grid. Actually all the numbers you can see in the pictures update through the automagic of WPF databinding. I even colored the quote process count with databinding.  This isn’t your daddy’s old Windows Forms, this is W P F.  This actually does more with less effort.  MS may have gotten something right.

The app is all about allowing the user to modify different parameters and combinations that may affect the system.

ThroughputView

On the left side are all the parameter settings. I’ve really just been playing with the Calls Per Second and Quotes Per Call options.  I made 2 quote servers, one in WCF which is a little flaky at higher revs and one that runs a timer to send quotes from the ThreadPool within the application.  I did the WCF first, because it seemed to replicate the problem domain, but its not really germane and I’m moving away from it.

The timer tick, simulates a data push, that I’m calling a “Call”.  Within each data push, the number of quotes requested is sent.  Multiplying “Calls per sec” and “Quotes per call” together gives you the expected quotes that will be pushed at the interface. I say expected, because the implementation of the “quote generator” yields less than the expected amount.

The calls per second can only go so high before it stops increasing the quotes generated number which displays on the bottom statusbar. This is probably because it comes to the resolution limit of the simple System.Timers.Timer I’m using.  In the picture above, its set to 66 calls per second.  Setting it higher does not increase the number of quotes pushed.  Setting the quotes per call higher will increase the throughput and the UI for displaying quotes only gets sluggish over around 60K per second.  CPU usage at 55K quotes per second is only 30 – 40% so maybe I could even do better.

I just put the quote display in a simple ListView/Grid:

DataView

Those quotes are jumpin’ 5 times per second.  Take my word or download the code and see for yourself.

StatusBar

This statusbar shows Snapshot Quotes Per Second (TPS — I use the word Tick and Quote interchangeably — bad habit) at 59,165, Expected Pushed Quotes at 103,563 and Total Processed and Total Served (Pushed) Quotes within 1000 of each other.  Either the consumer is 1/60 of a second behind the producer or just as likely there’s a synchronization issue between those 2 numbers.

You can download the code here and run in Visual Studio or stand alone.  Just hit the Start button to see things go.  Of course the code is AS IS, NO WARRANTY and freely distributable.  See here for additional warnings.

If anybody’s interested I’ll do some more writeups on what’s going on in the code.

Advertisements

September 28, 2009 - Posted by | Uncategorized | , , , , , , , , , , ,

3 Comments »

  1. Dude – that’s awesome!

    Comment by Rick | September 28, 2009 | Reply

  2. this is awesome, but I fear that the elephant in this room, unless I am misunderstanding what you wrote, is the network.

    Are these fake quotes coming from a different machine, or all being generated on the same box?

    Comment by Eliel | September 28, 2009 | Reply

    • Yea, the network is another interesting issue. I decided it was out of scope for this demo since the idea was, given a humongous feed coming in through a callback, how do you handle it in the UI. I started with a WCF Server sending the quotes via a callback, and then made a timer within the application to send quotes internally to compare. Since both feeds look pretty much the same from the consumer point of view, I went the rest of the way with the internal server to avoid too much fiddling with WCF. Once the WCF is serving a lot of rates, it times out on incoming calls. I’m sure I could overcome that, but I’m not too interested in that today. Without another demo (maybe next week) I can’t be sure, but I bet WCF could serve 50K of quotes.

      The WCF option is still there. Just click the WCF radio button in the middle of the left panel and it will switch to the WCF quote server.

      This is all running on my laptop since my other machines are in various states of non .Net 3.5.

      A bigger issue may be that I’m not really doing much processing on these quotes. I’m only displaying 5 times a second and only storing the individual quotes with an overwrite in a dictionary. If I had to perform a real operation on every quote, it would be slower. On the other hand, this is an acceptable approach for many use cases of this kind of application. Also, if the UI were more complex it could also be slower, but WPF is really impressing me in that respect. As a matter of fact, as I added features to the demo (small though they may be) throughput wasn’t noticeably affected.

      Another thing is I’m only showing 25 instruments. I have a suspicion that it will scale well from there though with no or minimal effort.

      Upgrades I may make are to allow the update frequency and number of instruments to be adjusted. That will give a better feeling for what the limits of UI performance are.

      Comment by goldmanalpha | September 28, 2009 | Reply


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: