Click here to Skip to main content
Click here to Skip to main content

Event Stream Accounting

, 2 May 2014
Rate this:
Please Sign up or sign in to vote.
Use of the Event Source application architecture in financial software

Introduction

Event sourcing is an architecture model based on the storage of the sequence of events that have occurred in an objects lifetime rather than storage of a point-in-time state of that object.

It lends itself well to financial applications because (typically) any account balance is a result of the events leading to that balance and we want the ability both to see the underlying derivation of a figure and also to be able to play that valuation as of a given point in time.

Event sourcing allows us to perform “what if” analysis by filtering out events or by playing in alternate streams.

Quick example – a simple bank account

A bank account is probably the simplest financial stream consisting solely of deposits and withdrawals of a single currency. In this model an event is simply an amount by which the balance is adjusted – deposits being positive and withdrawals being negative.

[ … +50 … +50 … -100 …]

In this example the valuation changes only when the actual deposit or withdrawal occurs – so playing the event stream through to the end we can see that the current balance is now 0.

Second example – a shareholding account

For a shareholding account there are two streams. There is a positions stream where purchases or sales of the shareholding are recorded and there is a pricing stream where prices for the underlying share are recorded.

[ … +50 …+50 … -100 … ]
[178.1 ….. 178.0 ……..178.9 …]

These can be combined into a valuation stream which is the combination of the current holding multiplied by the most recent price. Both a holding event and/or a pricing event will trigger a valuation.

[ 08905.0017810.0017800.000.000.00 ]

Third example – holdings in a non-base currency

If your shareholdings are held in a non-base currency then the changes in the exchange rate between the holding currency and the account base currency also affect the valuation.

[ … +50 …+50 … -100 … ]
[178.1 ….. 178.0 ……..178.9 …]
[1.11.21.31.4 ] 

Events

For any event you need to know – when did it occur, what stream was it in, what was its value. From the above example you can also see that the value can be an absolute amount (for example a price or an exchange rate) or it can be a change (or delta) amount.

For an event stream that is a stream of delta amounts it can be helpful to write in periodic snapshots of the current value. This is to allow us to play back the stream to any given point by going to the snapshot prior to that point then playing in any delta events post that snapshot time.

Derived events

Streams can be source data or derived by functions of one or more source streams. For derived events a function is used to calculate the value from its constituent streams and this is triggered for any event on any of those streams.

Double-entry accounting

In double entry accounting, holding events are recorded in two (or more) streams – one representing each of the ledger accounts the event impacts (most commonly one for assets and one for liabilities). Traditionally this is done using posting rules on the input stream to post to the relevant two (or more) account streams.

Points of Interest

One of the biggest things to consider with this type of architecture is idempotency - making the system immune to error if an event is run more than one time. In practice this tends to mean replacing messages that perform a delta (such as "reduce the account by $50") with messages that perform an absolute (such as "set the account holding to $745.20")

Another challenge is sequencing - it is important that events are added to the stream in the correct chronological order. In practice an accepted timestamp will need to be used.

History

2014-05-02 Initial concept

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Duncan Edwards Jones
Software Developer (Senior)
Ireland Ireland
C# / SQL Server developer
Microsoft MVP 2006, 2007
Visual Basic .NET
Follow on   Twitter   LinkedIn

Comments and Discussions

 
QuestionMore info and/or examples Pinmembergholbrook11-May-14 15:44 
AnswerRe: More info and/or examples [modified] PinpremiumDuncan Edwards Jones13-May-14 5:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140721.1 | Last Updated 2 May 2014
Article Copyright 2014 by Duncan Edwards Jones
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid