Building Freetrade

Event sourcing on Freetrade

David Kimberley

September 18, 2020

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.


Building Freetrade

Brand new ETFs on Plus

Including fixed income, investment-grade and government bonds.

21/10/2020

|

Sam Poullain

Building Freetrade

How your product vision could put a human on Mars

Freetrade VP Product Duncan Leslie on vision, strategy and measuring success.

21/10/2020

|

Duncan Leslie

Building Freetrade

Stock fundamentals are now on your app

Market cap, dividend yield, and P/E ratio are here.

20/10/2020

|

David Kimberley

Building Freetrade

Introducing the time-weighted rate of return

Compare your performance against a global benchmark

16/10/2020

|

David Kimberley

Building Freetrade

It’s raining stocks: 500 new investment opportunities on your app now

16/10/2020

|

Viktor Nebehaj

Building Freetrade

The Freetrade Christmas List 2020

Everything we plan to add to your app before the holidays.

22/10/2020

|

Sam Poullain

Building Freetrade

Optimising cold-starts with Google Cloud Functions

Freetrade engineer Simon Poole talks about overcoming some serverless infrastructure challenges.

16/10/2020

|

David Kimberley

Building Freetrade

Over 450 brand new stocks

From Papa John's to Zambian cattle farmers, we've added a wide array of new stocks to the Freetrade universe

14/10/2020

|

David Kimberley

Building Freetrade

Celebrating Black History Month at Freetrade

Freetrade Talent Sourcer, Isabelle Atunrase, shares why we should all celebrate Black History Month, and some of the ways we’re getting involved here at Freetrade.

5/10/2020

|

Sam Poullain

Building Freetrade

100 new ETFs from iShares, Vanguard, VanEck, and more!

Our biggest addition of ETFs yet.

7/10/2020

|

Alex Campbell

Building Freetrade

User Story Mapping - How we keep a focus on value

Freetrade Senior Product Managers Anant Sangar and Glenn Drawbridge have been busy working on limit orders and SIPPs. Here, they chat through how use User Story Mapping.

13/10/2020

|

David Kimberley

Building Freetrade

400 brand new stocks, including 200 exclusively for Plus

More of what you want.

1/10/2020

|

Alex Campbell

Building Freetrade

New stocks coming to your free plan and Plus subscription

Introducing your expanded stock universe.

1/10/2020

|

Alex Campbell

Building Freetrade

Money-weighted rate of return

More ways to measure your portfolio performance

1/10/2020

|

Sam Poullain

Building Freetrade

Our first Open Banking integration is rolling out

It’s now even easier to add money to your Freetrade account

1/10/2020

|

Sam Poullain

Building Freetrade

How to land a role in Product Management

Freetrade Senior Product Manager Glenn Drawbridge shares his story.

1/10/2020

|

Sam Poullain

Building Freetrade

Announcing the Tesla free share winner

1/10/2020

|

Sam Poullain

Building Freetrade

Introducing Freetrade Plus

Find out what's inside, and request your invite.

6/10/2020

|

Duncan Leslie

Building Freetrade

Brand new stocks: fashion brands, fast food, biotech innovators, and another SPAC

Over 100 new stocks, including Kodak, La-Z-Boy, Tiffany & Co, and Crocs.

1/10/2020

|

David Kimberley

Building Freetrade

Meet our new Head of People, Amy Gilman

Amy joins Freetrade as our first Head of People.

1/10/2020

|

Sam Poullain

Building Freetrade

Brand new stocks: fashion brands, gold miners, and SPACs

100 stocks inc. Avis, Tripadvisor, Goodyear, AMC Entertainment, Denny’s

1/10/2020

|

David Kimberley

Building Freetrade

Your new order experience is here

The first of many additions to order types.

1/10/2020

|

Sam Poullain

Building Freetrade

Brand new stocks: fresh IPOs, Latin American stocks, and investment trusts

75 new stocks including Ericsson, Yelp, Gfinity, Youdao

1/10/2020

|

Sam Poullain

Building Freetrade

28 brand new ETFs and 70 new stocks

Country-specific ETFs, and stocks from Wendy’s to Columbia

1/10/2020

|

Sam Poullain

Building Freetrade

Brand new: ETFs, Korean telcos, Japanese app and Chinese airlines

100 brand new stocks and ETFs are here

1/10/2020

|

Sam Poullain

Building Freetrade

Cloud computing, ETFs, UK stocks and brands from AB Inbev to Dominos

This week's 100 new stocks and ETFs might be the best batch yet.

1/10/2020

|

Sam Poullain

Building Freetrade

Brand new stocks: Banks, planes, trains and automobiles

Ferrari, Honda, Canadian Railway, United Airlines, Canadian banks, ETFs, and more

1/10/2020

|

Sam Poullain

Building Freetrade

90 brand new stocks have landed

You can now own a piece of Ed Sheeran

1/10/2020

|

Sam Poullain

Building Freetrade

Buy weed (stocks) on Freetrade

Cannabis companies have arrived

1/10/2020

|

Sam Poullain

Building Freetrade

Perry Blacher, serial entrepreneur turned VC, is joining Freetrade’s board

The former entrepreneur will be Freetrade’s non-executive director

1/10/2020

|

David Kimberley

Building Freetrade

Meet the team: Renata Labude, Senior Growth Manager

Find out more about how Free Share works

1/10/2020

|

David Kimberley

Building Freetrade

Introducing more stocks on Freetrade

250 new US stocks have landed

1/10/2020

|

Viktor Nebehaj

Building Freetrade

Fractional shares: the rollout has started

You can now invest in a slice of US companies

1/10/2020

|

Viktor Nebehaj

Building Freetrade

Meet the team: Caitlin Rich, Principal Product Designer

Meet the person responsible for making Freetrade look cool

1/10/2020

|

David Kimberley

Sign up for our newsletter

Download the app and start
investing now.