Sunday, December 21, 2008

Realtime Market Data with WCF NetTcpBinding and Observer Pattern

Recently i needed a communication protocol in order to develop a .Net 3.5 windows forms Market Data Hybrid Client/Server system model, where using the Publish/Subscribe model (Observer pattern) the client would subscribe to real time feeds on the server and the server would update the client with these feeds real time-no delay which should travel back to the client with rapid speed.

For this i ended up using for the communication model WCF with NetTcpBinding,
  • session-full (based on the TCP's session, if the session drops then subscription is aborted, no need to retain a virtual session though pooling),
  • multi threaded (no build in synchronization so that the server response would have light speed),
  • not Order (problematic for distributed environments),
  • not Secured (didn't need that)
and for the client i used Janus Systems Controls (the feeds was projected into a Janus GridEx for windows forms).

Starting this project i had many qualms regarding the use of Wcf NetTcpBinding over Internet as from what microsoft says:

"In Windows only intranet environment and when all the service clients are WCF clients, the service can be deployed with the NetTCPBinding and Transport level security for achieving maximum performance. NetTCPBinding by default uses transport level security over a TCP channel along with binary message encoding. In a Windows only intranet environment, ClientCredentialType can be set to Windows to enable Windows authentication."

"For a WCF service that has to be exposed over internet or deployed in a heterogeneous environment and needs to potentially interact with the clients on non-windows platforms, BasicHttpBinding or WSHttpBinding can be used depending upon the level of conformance required with the commonly used security standards."

WSHttpBinding couldn't satisfy my needs for performance and NetTcpBinding couldn't satisfy my needs for stability.

Other thoughts i had was:
Xmpp was a good and mature solution and although it is very well used in communication systems it is just trying to enter the group of financial applications. The XML perspective it has also would be a big problem. One other problem would raise from my "Hybrid" need, that is although the system basically would be "Client/Server" sometimes i needed Client Clustering so that groups of clients would be server by an auto configured local server ( hierarchical structure) and this leads to clients being servers too. In that case i would use a different central xmpp server and different sub-servers (it would be impossible to use openfire as a middle server).

Fix protocol would be a good solution too but extensibility was one concern of mine as also the "Hybrid" needs i mentioned before.

Proprietary protocols are very good as long as you have a good stomach and mine couldn't stand overnight drinking coffee and smoking.

Finally i gave a chance to NetTcpBinding with my fingers crossed.

After some moths of the system being released i have to admit that NetTcpBinding is working just fine with no drops and very good transfer rates (real time updates).

Currently my only problem is Janus GridEx performance as for real time updates their Grid is using the most of the processor time and there is no way you can avoid that, except if timing the updates.

Sql Server Compact 3.5 Sp1 with Entity Framework

Writing for C# and .Net Framework 3.5, recently i needed a local database for a Desktop Application.

I tried to figure out which Database system is best for my case.
My needs was a small footprint database system with good interaction with Visual Studio or with good Tools which should have a .Net Sql provider and it would be Free or cost minimum.
There was a good article (i will update here when i find it again) referring
Sql Server Compact (CE),
Sql Server Express,
Vista DB,
Firebird,
SqlClient and more.

I ended to the 2 Microsoft's database systems, Sql Server Compact and Sql Express.
There is a good article (MS Word Document) from MSDN comparing these two database systems.

After reading that too i ended up to the Sql Server Compact as it runs in process with my desktop app, it uses a small memory footprint, it does not need an installation procedure (X-Copy) and it seems fast enough for my needs.
One think it doesn't have is stored procedures and views.
As i needed a factor of abstraction into my database system i searched for some DAL (OR/M) components to sit on top of my Database.
Currently there are these three choices:
NHibernate seems to be a good solution and compared to Entity Framework it is more mature, but for me being a Microsoft lover (Why use the Entity Framework?), i preferred to give a chance to Entity Framework with the add on of the Linq queries i like very much.
Linq to Sql seems more like a "Queries simplifier" to me than a real OR/M system.

So, i ended up using Sql Server Compact with Entity Framework and Linq queries.
Trying to harness these beasts the following links seemed most useful to me:

Regarding Sql Server Compact
Regarding Entity Framework: