Stock Exchange Bid Matching Engine

During spring cleaning my backups this week-end, I came across an old proof-of-concept that I had written, circa 2005-2006, in Java. Its a core engine for matching bids. I had created it to understand the stock exchange bid matching dynamics. I have added the LGPL licensing and I am attaching it to this blog page. 

Earlier this week-end I had also been playing with eXist DB and experimenting with Amzon Elastic Beanstalk. I uploaded the Exist DB .war package onto Elastic Beanstalk and it just ran out-of-the-box ! Elastic Beanstalk is the Amazon's answer to Google's App Engine. Both are based on Apache Tomcat.

However, while Google App Engine places restrictions on File I/O Amazon Elastic Beanstalk done nothing of this sort. But the down side is that your data may go down with your instance if you don't persist it in a cloud store, like SDB (Simple DB)  or RDS (Relational Data Service) or S3 (Simple Storage Service).  

Exist DB supports clustering on its own and it may be good exercise to create a cluster in Amazon Cloud. This architecture would ensure that you can run your application in a private cloud implemented on Open Source Ubuntu Enterprise Cloud based on Eucalyptus  or Oracle's Exadata and Exalogic Cloud in-a-box.

If you are a large Enterprise or a very rich person loaded with money with passion for boys toys like yacht, F16 and Ferrari, and you want a full control of your privacy, data and infrastructure then extreme engineered systems like Exalogic and Exadata would make an immense sense. For the rest of us, who rely on public utility and public transport system, Amazon  Cloud is the way to go.

The Exist DB cluster and easily be replaced by Exadata as Oracle Database 11g supports XML natively. And Weblogic 11g is a highly scalable and cluster enabled and can beat the wind out of Tomcat. But then, the extreme performance doesn't come for Free (as in free beer).  

Let me sketch out the Architecture of a Scalable Stock Exchange Engine based on a Cloud Computing  platform. Lets assume its Amazon cloud for a moment.

Cloud Scalable Stock Exchange Engine

Here is a logical representation of the architecture:

Each of the Stock Exchange Members (Brokers) communicate with the Exchange through a dedicate Message Queue, Q(b-in). There is one Q(b-in) queue per broker. For Amazon Cloud Queue would be implemented in Amazon Simple Queue Service (SQS) . And, for Oracle stack it will be the Java EE standard Java Message Service (JMS) Queue implemented as a part of the Weblogic Server.  

A message router component, R(b2s) listens to R(b-in) queue and routes the bid or offer order to the respective queues, one each for a Listed Stock, denoted in the diagram as R(b2s). There are presently around 4900 stocks listed in BSE. The core matching engine, similar to the example Java code attached with this page, listens to the R(b2s) queue; one instance per queue.
Again for Amazon it will be SQS and for Enterprise Java World it would be JMS. The Router Java code will be implemented as a POJO (Plain Old Java Object) for Amazon and Stateless Session Bean for Enterprise Java. 

The core engine persists its state in a database, which could be RDS for Amazon and Oracle Database 11g RAC (Oracle Real Application Cluster) for Enterprise cloud computing.

When a match is found a deal is made, this is intimated by the Core Engine to the respective brokers by sending it the out queue, R(s-out). Which is then routed the right brokers involved in the deal by a Message Router, R(s2b) which listens to the R(s-out) queue. The Core Engine also publishes the event to an Event engine, for Amazon its Simple Notification Service (SNS) and for Oracle stack its EDA (Event Driven Architecture) Suite. Oracle Fusion Middle Ware (OFM) supports natively events.  

These matched deals events published by the Core matching Engine is subscribed by a BI/DW (Business Intelligence & data Warehousing) system. The BI/DW then feeds the stock quote systems like Bloomberg. Also, the watchdog agencies, like SEBI would access the system for fraud detection and monitoring for insider trading activities.  The stock exchange would also use it for triggering circuit breaker, to check a steep rise or fall of stocks. The BI/DW system would be Elastic Map-Reduce for Amazon; while for Enterprise private cloud it would be Oracle Business Intelligence Enterprise Edition 11g (OBIEE).   
Subpages (1): Coherence Implementation
Ashish Banerjee,
Feb 12, 2011, 9:22 PM