Digital Ruby Software – Home

Greetings. I’m Jeff Johnson and I founded Digital Ruby in 2009 in order to learn how to run a software company, teach myself mobile programming and expand my skills and knowledge. I craft iOS apps, websites and server software.

My latest blog post:

The Power of Batching

Aug 5, 2014

Recently I coded up a very naive message handler in C# that read in received messages from a certain service in an ASP.NET MVC controller method and then inserted each message into a SQL Server table for processing. When hundreds of calls flooded my system per second, it quickly fell to it’s knees.

Why?

Inserting one object at a time to SQL server required a separate network call to the SQL Server database, and did not take advantage of any bulk insert capabilities in SQL Server. I quickly realized that I needed to do something fast as this was a production system. I decided that I would batch up all of the messages every few seconds and then bulk insert them into SQL Server.

Imagine this truck carrying one package at a time to it’s destination and returning. Not very efficient.
truck

Since all web services can have many concurrent connections, I needed a data structure that could add and remove items in a thread safe way. The Microsoft .NET developers have done a great job with System.Concurrent.Collections. Specifically, System.Concurrent.Collections.BlockingCollection is a great way to represent a thread safe queue. You can add items to the back and take them to the front.

In my new implementation, when I get a call to my controller method, I simply insert a message into a static instance of a BlockingCollection and that is all. I then have a timer that fires every few seconds, gets the count of the blocking collection, takes that many items off and then bulk inserts the messages into SQL Server. This new implementation is orders of magnitude faster than the one at a time implementation.

In short, if you have any system that handles messages and puts them into any kind of database, be sure to do batching. Even a batching interval of 1 second will give you vast improvements in performance.

0 Comments

Recent articles…


Digital Ruby Logo
I'm Jeff JohnsonJeff Johnson (jjxtra) and have been learning how to do software development since age 7 (and still learning). I enjoy playing and making games, along with websites and server software.

Twitter

Stackoverflow