Note that there are some explanatory texts on larger screens.

plurals
  1. POMulti-Threading an Application with Extremely High Message Rate (Which method should I use?)
    text
    copied!<p>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.</p> <p>The end goal is to be able to process up to 1000 messages per second as quickly as possible.</p> <p>Here is what I am thinking so far:</p> <h2>Method I: Thread with ConcurrentQueue</h2> <p>This method creates a Thread and basically has the thread continuously take messages off of a queue (the Thread will stay alive).</p> <pre><code>public class MessageQueue { private ConcurrentQueue&lt;Message&gt; MessageQueue; private Thread QueueThread; public MessageQueue() { MessageQueue = new ConcurrentQueue&lt;Message&gt;(); 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 } } </code></pre> <p><strong>Question:</strong> 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?</p> <p><strong>Question:</strong> 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.</p> <p><strong>Question:</strong> Should I be setting any properties of the Thread like 'ApartmentState' or 'IsBackground'?</p> <h2>Method II: ThreadPool</h2> <p>This method simply invokes the ThreadPool with every new Message.</p> <pre><code>public class MessagePool { public static void OnInboundMessage(Message MarketMessage) { ThreadPool.QueueUserWorkItem(obj =&gt; ProcessMessage(MarketMessage); } public static void ProcessMessage(Message MarketMessage) { // Process Message here } } </code></pre> <p><strong>Question:</strong> Are there any settings of the ThreadPool that might make it a more appropriate solution?</p> <p><strong>Question:</strong> 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?</p> <h2>Method III: MessageTasks</h2> <p>This method uses a Task in a similar fashion as the ThreadPool.</p> <pre><code>public class MessageTasks { public static void OnInboundMessage(Message MarketMessage) { Task.Factory.StartNew(() =&gt; ProcessMessage(MarketMessage)); } public static void ProcessMessage(Message MarketMessage) { // Process Message here } } </code></pre> <p><strong>Question:</strong> Would creating my own TaskFactory and TaskScheduler improve it's performance?</p> <h2>My Performance Testing</h2> <p>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?</p> <p>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!</p> <p>William </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload