Building Freetrade

Event sourcing on Freetrade

Freetrade Team

September 18, 2020

Freetrade Team

Freetrade Senior Software Engineer Luke Smith talks about the nuts and bolts of our brokerage platform

Invest by Freetrade is our proprietary investment platform, which we built from the ground up. Many requirements needed to be met to ensure we have accurate data and that the platform could scale to meet the growth of the company. 


The platform also looks after your assets. This means it's the most important system we run and we have to make sure that every movement of cash and stock is accounted for.


We understood early on that having access to a stream of information about each account and order would provide us with the flexibility and insights needed to build a brokerage platform for the modern age.


Traditionally a relational database might be used, typically an SQL database, storing data normalised across multiple tables. The individual records are created, updated, or deleted, as necessary (known as CRUD operations). These records store only the current state of your data, as changes occur the history of it is lost. Looking at a record in this structure it is impossible to understand how, or more importantly why, the data reached this current state.


Using a basic table structure we can see that someone's account has a balance of 10, and this was updated on 1st January 2020. We can’t deduce any other information from this.



Event sourcing is different. Each change to your model is captured as an explicit event within what’s known as an aggregate. These events contain the data that is a side-effect of the operation, but importantly the name of each event gives an indication of why or what caused that change. Historical events are never mutated, with new events appended to the underlying store.


By storing explicit events we can see that the same account above had deposits, withdrawals, and trades made against it.


  1. DepositedFunds £100
  2. BoughtStock AAPL £30
  3. WithdrewCash £60


We end up with a balance of £10 as the current state. The current state does not need to be stored (we’ll discuss this more later), giving us a single source of truth about what happened to the account.


A benefit of this is we can also source the events to any historical point in time, meaning we can see what an account looked like at 4:22pm on Thursday 6th June 2019. This gives us more power and flexibility in investigating issues, but also helps us generate the compliance reports and statements a financial company must produce.


The number of events within a specific aggregate can grow over time. For orders, the lifetime is relatively short lived with a handful of events. But an account is a long life aggregate, which can grow to contain many thousands of events relatively quickly.


10s of millions of events overall on customer accounts
10s of thousands on our busiest individual customer accounts


In order to scale and handle this we snapshot what an aggregate looks like on a periodic basis. This snapshot contains only the information necessary to make business decisions within the aggregate. When we need to load an aggregate we first load the snapshot, and then source any unseen events from the event store.


What's in an event

The anatomy of an event contains a lot of information about the outcome of an action. These are the business facts as a result of making a change. 


Anything that was known, or calculated by the operation, at that point must be recorded on the event. If it’s not, you can assume it’s lost. This allows the event, and the information it provides, to be the truth for downstream processes to trust. For example, the stamp duty owed as a result of the order is recorded as a fact on an event.


Because Invest by Freetrade is an accounting platform at heart, it’s important that any transaction involving an asset is recorded following double-entry accounting principles. The events act as the method to record the transactions.


An event crediting an account with £100 would also contain information about the other party in the transaction (where the money is being debited from). Likewise the event recording the debit will contain details about the account being credited. Both tied together with a shared transaction reference.


When transactions occur between aggregates we don’t write the events to both at the same time. This would introduce contention in the platform. A customer account has relatively little throughput, but many of the system accounts are often involved in operations where in doing so would introduce a bottleneck that would leak its way back into the UX. As a result we delay when the transaction is recorded via a queue. This introduces the concept of eventual consistency.


We can verify the system is correct by replaying the events across the various accounts, and verifying that the system has eventually recorded both sides of the transaction. The sum of the credit + debit equals zero.


Hopefully this has given you an understanding of Invest by Freetrade, and some of the design decisions we made. Future posts will go into more detail around the challenges and solutions we’ve implemented around using event sourcing in a serverless architecture.


The views expressed above are those of community members and do not reflect the views of Freetrade. It is not investment advice and we always encourage you to do your own research.

Simple pricing plans

Choose how you'd like to pay:

Annually

Save 17%

Monthly

Annually

Save 17%

Monthly

£0.00/mo

Accounts

GIA pink
General investment account

Benefits

  • Commission-free trades (other charges may apply. See full pricing table.)
  • Trade USD & EUR stocks at the exchange rate + a 0.99% FX fee
  • Fractional US Shares
  • Access to more than 4,700 stocks, including the most popular shares and ETFs
  • 1% AER on up to £1,000 uninvested cash
£4.99/mo

£59.88 billed annually

£5.99/mo

Billed monthly

Accounts

GIA white
General investment account
ISA
Stocks and shares ISA

Benefits
Everything in Basic, plus:

  • Full range of over 6,000 US, UK and EU stocks and ETFs
  • Trade USD & EUR stocks at the exchange rate + a 0.59% FX fee
  • Automated order types, including recurring orders
  • Advanced stock fundamentals
  • 3% AER on up to £2,000 uninvested cash
£9.99/mo

£119.88 billed annually

£11.99/mo

Billed monthly

Accounts

GIA white
General investment account
ISA
Stocks and shares ISA
SIPP white
Self-invested personal pension (SIPP)

Benefits
Everything in Standard, plus:

  • Trade USD & EUR stocks at the exchange rate + a 0.39% FX fee
  • Priority customer service
  • Freetrade Web beta
  • 5% AER on up to £3,000 uninvested cash

Download the app to start investing now



When you invest your capital is at risk.