+ - 0:00:00
Notes for current slide
Notes for next slide

Event Sourcing

1

Event Sourcing

  1. What is it?
  2. Demystify how it could work in practice
2

Event Sourcing

  1. What is it?
  2. Demystify how it could work in practice

*still working out the specifics

3

Databases

We're familar with CRUD based design. Create, Read, Update, and Delete.

  • Databases typically store the current state.
  • Update/Delete mutates the state.
4

Databases

We're familar with CRUD based design. Create, Read, Update, and Delete.

  • Databases typically store the current state.
  • Update/Delete mutates the state.


With the event sourcing pattern pattern...

Instead of mutating state directly, we append every state change as an Event object to an event log.

5

Databases

We're familar with CRUD based design. Create, Read, Update, and Delete.

  • Databases typically store the current state.
  • Update/Delete mutates the state.


With the event sourcing pattern pattern...

Instead of mutating state directly, we append every state change as an Event object to an event log.

⭐ The application state at any point in time can be rebuilt by processing the stored events.

6

Shopping Cart Example

classDiagram Cart -- Item class Cart{ cart_id: int when: datetime address: text } class Item{ cart_id: int item_code: text }
7

Events

  1. Cart created
  2. Item 1 added
  3. Item 2 added
  4. Item 1 removed
  5. Shipping information added
  6. Make payment
8

These events would have payloads. eg. time, item code, shipping details, payment method, etc

Events

  1. Cart created
  2. Item 1 added
  3. Item 2 added
  4. Item 1 removed
  5. Shipping information added
  6. Make payment

State:

{
"cart_id": 1,
"datetime": "2020-11-01T21:34+10:30"
}
9

Events

  1. Cart created
  2. Item 1 added
  3. Item 2 added
  4. Item 1 removed
  5. Shipping information added
  6. Make payment

State:

{
"cart_id": 1,
"datetime": "2020-11-01T21:34+10:30",
"items": [
{ "item_code": "item 1" }
]
}
10

Events

  1. Cart created
  2. Item 1 added
  3. Item 2 added
  4. Item 1 removed
  5. Shipping information added
  6. Make payment

State:

{
"cart_id": 1,
"datetime": "2020-11-01T21:34+10:30",
"items": [
{ "item_code": "item 1" },
{ "item_code": "item 2" }
]
}
11

Events

  1. Cart created
  2. Item 1 added
  3. Item 2 added
  4. Item 1 removed
  5. Shipping information added
  6. Make payment

State:

{
"cart_id": 1,
"datetime": "2020-11-01T21:34+10:30",
"items": [
{ "item_code": "item 2" }
]
}
12

Events

  1. Cart created
  2. Item 1 added
  3. Item 2 added
  4. Item 1 removed
  5. Shipping information added
  6. Make payment

State:

{
"cart_id": 1,
"datetime": "2020-11-01T21:34+10:30",
"items": [
{ "item_code": "item 2" }
],
"address": "298 Payneham Rd, Payneham SA",
}
13

Events

  1. Cart created
  2. Item 1 added
  3. Item 2 added
  4. Item 1 removed
  5. Shipping information added
  6. Make payment

State:

{
"cart_id": 1,
"datetime": "2020-11-01T21:34+10:30",
"items": [
{ "item_code": "item 2" }
],
"address": "298 Payneham Rd, Payneham SA",
"paid": true
}
14

Events

  1. Cart created
  2. Item 1 added
  3. Item 2 added
  4. Item 1 removed
  5. Shipping information added
  6. Make payment

State:

{
"cart_id": 1,
"datetime": "2020-11-01T21:34+10:30",
"items": [
{ "item_code": "item 2" }
],
"address": "298 Payneham Rd, Payneham SA",
"paid": true
}
15

Stores

  • "Entity store"
  • "Read model"
  • "Cache"
  • "Materialised View"
  • "Projection"
16

Stores

  • "Entity store"
  • "Read model"
  • "Cache"
  • "Materialised View"
  • "Projection"

Examples

  • An embedded cache. eg. python dict
  • Relational database.
  • Document database. eg JSONB
  • ElasticSearch
17

Stores

  • "Entity store"
  • "Read model"
  • "Cache"
  • "Materialised View"
  • "Projection"

Examples

  • An embedded cache. eg. python dict
  • Relational database.
  • Document database. eg JSONB
  • ElasticSearch

All of the above. Depends on your application needs.

18

Stores

  • "Entity store"
  • "Read model"
  • "Cache"
  • "Materialised View"
  • "Projection"

Examples

  • An embedded cache. eg. python dict
  • Relational database.
  • Document database. eg JSONB
  • ElasticSearch

All of the above. Depends on your application needs.

The key difference with an event sourcing design is that entity stores no longer act as the system of record – they could easily get replaced or rebuilt as needed by reprocessing the event log.

19

Denormalise data to your heart’s content

In databases, it is often considered best practice to normalise data, because if something changes, you then only have to change it one place.

Normalisation makes writes fast and simple, but means you have to do more work (joins) at read time.

https://www.confluent.io/blog/making-sense-of-stream-processing/

20

Rebuilding state from event log slow. Snapshots?

Large organisations use stream processing software to implement event sourcing in production:

  • Apache Kafka
  • Amazon Kinesis
21

Large organisations use stream processing software to implement event sourcing in production:

  • Apache Kafka
  • Amazon Kinesis

https://www.confluent.io/blog/event-sourcing-cqrs-stream-processing-apache-kafka-whats-connection/

22

Why

  • Loose coupling
  • Read and write performance. CQRS
  • Flexibility and agility
  • Auditing
23

Loose coupling
If you write data to the database in the same schema as you use for reading, you have tight coupling

Read and write performance
Write = append to event log
Read = read latest state from cache
Command-Query Responsibility Segregation
Separate Writes and Reads. So you can scale them independently.

Flexibility and agility
"schema migration" doesn't make sense.
Replay the events with the new logic.

Auditing
Reconstruct exactly what happened.
Appending events only. Not destructive database updates and deletes

https://eventstore.com/#benefits-of-event-sourcing

Event Sourcing

  1. What is it?
  2. Demystify how it could work in practice
2
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow