AlgoTraderAlgoTrader Documentation

Chapter 14. Hibernate Sessions and Caching

14.1. Hibernate Caching
14.1.1. Level-Zero Cache

AlgoTrader uses Hibernate for accessing and persisting objects to the database.

In order to prevent having to access the database on every single request, Hibernate provides two types of caches:

Both First and Second Level Cache require a Hibernate Session. Creation of a Session is usually very quick (a few milliseconds). This mechanism is therefore fine for any request-response based system. However this approach is not feasible for a trading application. A trading application typically receives several thousand market data events per second. Ideally these market data events have to be matched to the latest data stored in the database (e.g. Security related information, current Positions, executed Trades, etc.). Opening a new Hibernate Session for every market data event, to synchronize related objects (like corresponding Security), is much too expensive!

For this purpose AlgoTrader introduces a Level-Zero Cache

AlgoTrader Level-Zero Cache is an additional Caching Level on top of Hibernate First and Second Level Cache which has the following features:

By using the Level-Zero Cache it is possible to work on fully up-to-date versions of Entities without introducing any latency penalties.

Access to the Level-Zero Cache is provided by the class ch.algotrader.cache.CacheManagerImpl which is exposed as a Spring Bean named cacheManager. The CacheManagerImpl provides these public methods to access the Level-Zero Cache:

Like Hibernate First and Second Level Cache the AlgoTrader Level-Zero Cache will first check if the requested object is available inside the Cache. If not, the object will be retrieved via the ch.algotrader.hibernate.GenericDao and stored in the Cache.

The class EntityCache is responsible for caching of Entities (handled by the EntityHandler) and Entity-Collections (handled by CollectionHandler). When adding a new Entity to the cache, the EntityHandler and CollectionHandler traverse the entire object graph of initialized Entities and Collections and store all of them in separate nodes of the internal Hash Map Cache. Java reflection is used for the object graph traversal (on new objects or object updates) .

The class QueryCache is responsible for caching of Hibernate Query Results. Query Results are cached in reference to all involved tables. So whenever one of those referenced tables is modified, the corresponding Query Cache Entry is removed (detached).

Through the class ch.algotrader.wiring.server.CacheWiring net.sf.ehcache.event.CacheEventListenerAdapter are registered with each Ehcache element. These are either EntityCacheEventListener for Entity caches or CollectionCacheEventListener for Collection Caches. These EventListeners emit CacheEvictionEvents via the AlgoTrader EventDispatcher to CacheManager of the Server and Strategies (running in remote JVMs) to get notified every time an Entity / Collection is updated or the UpdateTimestampsCache (related to the Hibernate StandardQueryCache) has been updated.