Event sourcing

Why care?


Jakub Pilimon

ORM

STATE

STATE

new ShopItem("ordered")
item.setStatus("paid")
...
insert into shop_items (id, status) values (1, 'ordered')
update shop_items set status = 'paid' where id = 1
...
"new instance of ordered Shop Item was created"
"this shop item was marked as paid"
...
new ItemOrdered(itemID, timestamp)
new ItemPaid(itemID, timestamp)
item.setStatus("paid")
new ShopItem("ordered")
new ShopItem("ordered")
ordered.setStatus("paid")

State 1

State 2 

(State 1 + Event 2)

single ShopItem deltas

technology agnostic log, descriptive event log

@Entity
class Product {

    private float price;
    
    //...
    
    public void cutPrice(int percent) {    
        checkState(percent < 100);
        this.price = 
                price - price * percent / 100;
        DomainEvents.publish(
                new NewPriceAssigned(percent, this.id));
    
    }

}
 def 'should cut price by 10 percent'() {
        given:
            ShopItem item = newWithPrice(50.00)
        when:
            item.cutPrice(10)
        then:
            item.price == 45.00
    }
 def 'should cut price by procent'() {
        given:
            ShopItem item = newWithPrice(50)
        when:
            item.cutPrice(10)
        then:
            item.getUncommittedChanges() ==
                        [new NewPriceAssigned(price: 45)]
    }

QUERIES

message broker

Shop

UI

Horizontal asymmetric scaling

Failover

A/B testing

&

blue green deployments

Adding new service

Performance

Testing?

Pitfalls

Thank you!

@JakubPilimon

jakub.pilimon@gmail.com

github.com/pilloPl

Event sourcing WJUG

By Jakub Pilo

Event sourcing WJUG

Overview of event sourcing and CQRS benefits

  • 1,590