Event Sourcing: Is our Projection Code fast enough?

In this part of my event sourcing series, we take a quick, pun intended, look at how fast the projector we saw in the last post actually runs. The projector gets a list of events as input and projects them into a single value, representing the latest state. This code runs a couple hundred times a second in our system, so it needs to run fast.

The benchmark result

I ran a Benchmark.Net benchmark on my machine, with the following results:

The different cases:

  • CreateOnly: only a single create event exists in the event stream
  • Many: 1 create event, followed by 500 update events in the event stream
  • WithDeletes: a create event, an update event, and a delete event
  • WithOutOfOrderEvents: a create event, 4 update events in incorrect order, and a delete event -> events need sorting
  • WithOverrides: a create event, followed by 3 update events

These cover our typical cases.

Is it fast enough?

To answer this question, we need to look at what else is typically happing around the pure projection:

  • get the events from the database (typically takes 6 to 20ms in our production system)
  • project the events (as see above, typically below a micro second)
  • compute something based on these events (nano seconds to milli seconds)

So the projection times really don’t matter much in the whole picture. Allocations are probably the bigger concern because of garbage collection runs when Gen 1 memory is used (I’m looking forward to .Net 11 and the corresponding F# version to improve this).

Deep dive: the benchmark code

I dump the whole code here. When tinkering with the projection code, I add a second [Benchmark] that calls a duplicate of the original projection code so that I can compare the old and new versions:

namespace Benchmark.CreateProjection

open System
open BenchmarkDotNet.Attributes
open Calitime
open Calitime.TimeRocket
open Calitime.TimeRocket.Projections

open Calitime.TimeRocket.Projections.ValueProjection
open Calitime.Zeit

type EventId = EventId of Guid

type Override = { Value : string }

type EventData =
    | Created
    | Override of Override
    | Delete
    | Rejected of RelatedEventId : EventId

type Event =
    {
        Id : EventId
        Application : Application
        Data : EventData
    }

type Projection = { Value : string }

type Override2 = { Value : string }


[<MemoryDiagnoser>]
type CreateProjectionBenchmark() =

    let projectAction (event : Event) =
        match event.Data with
        | Created -> CreateProjectionAction.Creates { Projection.Value = "created" }
        | Override o -> CreateProjectionAction.Updates (fun _ -> { Projection.Value = o.Value })
        | Delete -> CreateProjectionAction.Deletes
        | Rejected _ -> CreateProjectionAction.Skip

    let configuration =
        {
            ProjectionActionConfiguration = CreateUpdateDelete projectAction
            GetApplication = fun e -> e.Application
        }

    let eventsFSharpCreatesOnly =
        [
            {
                Event.Id = Guid.NewGuid () |> EventId
                Application = Application.parse "18.01.2021 08:00"
                Data = Created
            }
        ]

    let eventsFSharpWithOverrides =
        [
            {
                Event.Id = Guid.NewGuid () |> EventId
                Application = Application.parse "18.01.2021 08:00"
                Data = Created
            }
            {
                Event.Id = Guid.NewGuid () |> EventId
                Application = Application.parse "19.01.2021 08:00"
                Data = Override { Override.Value = "1" }
            }
            {
                Event.Id = Guid.NewGuid () |> EventId
                Application = Application.parse "20.01.2021 08:00"
                Data = Override { Override.Value = "2" }
            }
            {
                Event.Id = Guid.NewGuid () |> EventId
                Application = Application.parse "21.01.2021 08:00"
                Data = Override { Override.Value = "3" }
            }
        ]

    let eventsFSharpWithDeletes =
        [
            {
                Event.Id = Guid.NewGuid () |> EventId
                Application = Application.parse "18.01.2021 08:00"
                Data = Created
            }
            {
                Event.Id = Guid.NewGuid () |> EventId
                Application = Application.parse "19.01.2021 08:00"
                Data = Override { Override.Value = "1" }
            }
            {
                Event.Id = Guid.NewGuid () |> EventId
                Application = Application.parse "20.01.2021 08:00"
                Data = Delete
            }
        ]

    let eventsFSharpWithOutOfOrderEvents =
        [
            {
                Event.Id = Guid.NewGuid () |> EventId
                Application = Application.parse "18.01.2021 08:00"
                Data = Created
            }
            {
                Event.Id = Guid.NewGuid () |> EventId
                Application = Application.parse "20.01.2021 08:00"
                Data = Override { Override.Value = "2" }
            }
            {
                Event.Id = Guid.NewGuid () |> EventId
                Application = Application.parse "19.01.2021 08:00"
                Data = Override { Override.Value = "1" }
            }
            {
                Event.Id = Guid.NewGuid () |> EventId
                Application = Application.parse "22.01.2021 08:00"
                Data = Override { Override.Value = "4" }
            }
            {
                Event.Id = Guid.NewGuid () |> EventId
                Application = Application.parse "21.01.2021 08:00"
                Data = Override { Override.Value = "3" }
            }
            {
                Event.Id = Guid.NewGuid () |> EventId
                Application = Application.parse "23.01.2021 08:00"
                Data = Delete
            }
        ]

    let manyEvents =
        [
            {
                Event.Id = Guid.generateNew ()
                Application = Application.parse "18.01.2021 08:00"
                Data = Created
            }
            for i in 1 .. 500 do
                {
                    Event.Id = Guid.generateNew ()
                    Application = Application.parse "18.01.2021 08:00" |> fun (Application time) -> time.PlusTicks i |> Application
                    Data = Override { Value = i |> string } 
                }
        ]


    [<Params("CreateOnly", "WithOverrides", "WithDeletes", "WithOutOfOrderEvents", "Many")>]
    member val Case = "" with get, set

    member val FSharpEvents : Event list = [] with get, set

    [<GlobalSetup>]
    member this.GlobalSetup() =
        this.FSharpEvents <-
            match this.Case with
            | "CreateOnly" -> eventsFSharpCreatesOnly
            | "WithOverrides" -> eventsFSharpWithOverrides
            | "WithDeletes" -> eventsFSharpWithDeletes
            | "WithOutOfOrderEvents" -> eventsFSharpWithOutOfOrderEvents
            | "Many" -> manyEvents
            | _ -> failwith "unknown value for Case"

    [<Benchmark(Baseline = true)>]
    member this.FSharp() =
        let _ = ValueProjector.project configuration (this.FSharpEvents |> EventsForProjection)
        ()

About the author

Urs Enzler

Add comment

Recent Posts