I am creating a trading platform for a hedge fund and I need to be able to process market data on a separate thread(s) and I am trying to figure out which method I should be using.
The end goal is to be able to process up to 1000 messages per second as quickly as possible.
Here is what I am thinking so far:
Method I: Thread with ConcurrentQueue
This method creates a Thread and basically has the thread continuously take messages off of a queue (the Thread will stay alive).
public class MessageQueue
{
private ConcurrentQueue<Message> MessageQueue;
private Thread QueueThread;
public MessageQueue()
{
MessageQueue = new ConcurrentQueue<Message>();
QueueThread = new Thread(ProcessQueue);
QueueThread.Start();
}
public void ProcessQueue()
{
Message OrderMessage;
while (IsRunning)
{
if (MessageQueue.TryDequeue(out OrderMessage)
{
ProcessMessage(OrderMessage);
}
}
}
public static void OnInboundMessage(Message MarketMessage)
{
MessageQueue.Enqueue(DataMessage);
}
public static void ProcessMessage(Message MarketMessage)
{
// Process Message Here
}
}
Question: Why do people use the ‘ThreadStart’ method when creating a new Thread? It seems to work just as well if you don’t use it – what is it for?
Question: What is the benefit of using ConcurrentQueue? When will a regular Queue have a concurrency issue? It would seem that as long as you check to see that there is already something in the Queue before dequeing, there should not be any concurrency issues but I may not be understanding something.
Question: Should I be setting any properties of the Thread like ‘ApartmentState’ or ‘IsBackground’?
Method II: ThreadPool
This method simply invokes the ThreadPool with every new Message.
public class MessagePool
{
public static void OnInboundMessage(Message MarketMessage)
{
ThreadPool.QueueUserWorkItem(obj => ProcessMessage(MarketMessage);
}
public static void ProcessMessage(Message MarketMessage)
{
// Process Message here
}
}
Question: Are there any settings of the ThreadPool that might make it a more appropriate solution?
Question: I also don’t quite ‘get’ Lambda expressions but they seem to work – is there perhaps a better way of invoking ThreadPools? Is there any downside (performance, etc.) of using a Lambda?
Method III: MessageTasks
This method uses a Task in a similar fashion as the ThreadPool.
public class MessageTasks
{
public static void OnInboundMessage(Message MarketMessage)
{
Task.Factory.StartNew(() => ProcessMessage(MarketMessage));
}
public static void ProcessMessage(Message MarketMessage)
{
// Process Message here
}
}
Question: Would creating my own TaskFactory and TaskScheduler improve it’s performance?
My Performance Testing
It would seem that Method I is by far the fastest method of processing messages. It would seem like the ThreadPool takes about 9 Microseconds to add a new Workitem and the Task.Factory Method takes about 12 Microseconds. Has anyone had similar results?
Thanks in advance for your help – and if you think that I am going about this problem in entirely the wrong manner, don’t hesitate to let me know!
William
A public trading platform with several 1000s of requests per second has written http://code.google.com/p/disruptor/ to support that use-case. It’s written in Java and a work-alike sister project has built http://code.google.com/p/disruptor-net/ for the .NET platform.
There is a nice presentation by them at http://www.infoq.com/presentations/LMAX which explains the benefits in performance (10-100x faster than a ConcurrentQueue) and reliability and discusses necessary changes to the rest of the program architecture.