1 of 24

event_sourced_record

and other things

2 of 24

Hi

I’m Garrow

I’m Post-Post-Technical

Fashion

fameandpartners.com

3 of 24

4 of 24

Problem

“We want to tell customers when their Order is finished.”

Add finished_date to Order.

5 of 24

...sometime later

“We want to tell the customer the state of their Order”

Add state to Order, and update it when changes happen.

6 of 24

… “per Item on the Order”

OK

Add `state` to each Item

Update each Item’s state when changes occur.

7 of 24

… a long time later

“We want a report

  • on how long certain items were in `QC`
  • correlated with the returns
  • for quality issues on that style
  • broken down by colour and size

8 of 24

Solution

?

9 of 24

Time Travel!

10 of 24

Event Sourcing

“Time-based immutable events”

“every change to the state of an object is recorded as an immutable event in a replayable sequence”

http://fhwang.net/2015/01/29/Event-Sourcing-with-Rails-A-Case-Study

http://martinfowler.com/eaaDev/EventSourcing.html

11 of 24

event_sourced_record

Francis Hwang

github.com/fhwang/event_sourced_record

Go watch the presentation!

https://youtu.be/_pbO0yAxfH0

12 of 24

Concepts

  • Events
    • Immutable
  • Calculator
    • Idempotent
    • No Side Effects
  • Projection
    • A cache of the calculated Value

13 of 24

14 of 24

Benefits

  • Trail of events
  • Free audit log
  • Can time travel
  • Logic can be updated, and rerun against existing data.

15 of 24

Shampoo Bottle Subscription

Events describe a change to number of bottles on the subscription.

Calculator adjusts the Subscription by amount

Subscription bottles_remaining is the cached value.

16 of 24

Our use cases

Dresses, manufactured on demand

Fabrication

Order Returns

17 of 24

Fabrication Events

  • Creation
  • Purchase Order Placed
  • State Changed*
  • Shipped

* really should be split up

18 of 24

Fabrication

19 of 24

DEMO TIME

(uh oh)

20 of 24

Issues & Problems

  • The create event is a little painful
  • Where does business logic go? (Service Objects)?
  • Where do integrations (emails/notifications) go?

21 of 24

What now?

Go watch Francis’ presentation

Try out event_sourced_record

22 of 24

Presentation.events

.start_talking

.create!(

prepared: false

)

23 of 24

Links

24 of 24

FIN

Thanks!

Presentation https://gist.github.com/garrow/f0c7feb17eff7c489ead

Garrow

@grokfail