FIX Message Handling and Memory Management

FIX Message

In QWFIX, there are two types of FIX messages, alterable message and read-only message.

Both messages are derived from the FIXMessage base class. FIXMessage is the only type that is visible to the developers.

FIXMessage is always associated with a persistence. Usually application developers should call the FIXSession.CreateMessage method to create a FIXMessage. The return type is always a alterable message so that application can manipulate fields inside the message.

Message Persistence and Message Token

A typical FIXMessage life cycle is like this:

CopyFIXMessage Life Cycle
-#region Collapse it
     // Create a message
     FIXMessage message = session.CreateMessage(MsgTypes.NewOrderSingle);
     // Set Field Values and/or Repeating Instances
     FIXMessageToken token = session.SendMessage(message);
     // Later on the token can be used to retrieve the message
     FIXMessage readonlyMessage = FIXEngine.GetMessage(token);
 #endregion

After the message is sent by calling FIXEngine.SendMessage (or received by FIXSession), the message is written into the persistence on the disk. In the mean time, the message is converted into a more compact form to save memory, which is read-only copy of message, because message is not allowed to be modified after it's sent anyway.

Once persisted, the message will be assigned a message token. The FIX engine is able to retrieve persisted message (in read-only format, of course) from the persistence storage on the disk from a given message token.

Because the alterable message is used to compose the message to be sent, an exception will be thrown out if the program try to get the message token from it. The SendMessage method

Read-only Message and In-memory Message Cache

The QWFIX run-time keeps an in-memory message cache. The cache is a least-recently used (LRU) cache. New messages are placed into the top of the cache. When the cache grows past its size limit it throws away items off the bottom. Whenever a message is accessed, it's pulled back to the top.

It's obviously that all messages in the cache are of read-only format.

The GetMessageFromToken method is designed in a way that it first searches the in-memory cache and gets the message if there's a cache hit. If there's a cache miss, it will retrieve the message from the persistent storage and put the message into the cache.

The format of Read-only message is carefully designed to meet the following criteria.