Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CQRS and Event Sourcing

CQRS and Event Sourcing

CQRS and Event Sourcing are becoming buzzwords nowadays. This presentation tries to clarify both concepts and present a simple example application, written with those powerful architectures

Christian

June 12, 2014
Tweet

More Decks by Christian

Other Decks in Programming

Transcript

  1. CQRS
    EVENT SOURCING

    View full-size slide

  2. Christian Soronellas
    Web Enthusiast
    @theUniC
    theUniC

    View full-size slide

  3. Exploration of concepts (CQRS and ES)
    Practical use case
    Conclusions

    View full-size slide

  4. #phpbcn
    https://github.com/theUniC/blog-cqrs

    View full-size slide

  5. ¿What is CQRS?

    View full-size slide

  6. It’s an architectural pattern

    View full-size slide

  7. Asking a question
    should not change the answer

    View full-size slide

  8. Methods that return state information should
    not trigger side effects

    View full-size slide

  9. !
    namespace An\Awesome\Namespace;
    !
    class AnAmazingService
    {
    /** @var An\Awesome\Dependency */
    private $aDependency;
    !
    public function getDependency()
    {
    if (null === $this->aDependency) {
    $this->aDependency = new \An\Awesome\Dependency();
    }
    !
    return $this->aDependency;
    }
    !
    public function anIncredibleAction()
    {
    $aDependency = $this->getDependency();
    $aDependency->performSomethingNeeded();
    // ...
    }
    }

    View full-size slide

  10. !
    namespace An\Awesome\Namespace;
    !
    class AnAmazingService
    {
    /** @var An\Awesome\Dependency */
    private $aDependency;
    !
    public function getDependency()
    {
    if (null === $this->aDependency) {
    $this->aDependency = new \An\Awesome\Dependency();
    }
    !
    return $this->aDependency;
    }
    !
    public function anIncredibleAction()
    {
    $aDependency = $this->aDependency;
    $aDependency->performSomethingNeeded();
    // ...
    }
    }

    View full-size slide

  11. CQRS challenges the assumption that
    reading and writing are supposed
    to share the same abstractions

    View full-size slide

  12. !
    namespace AcmeShop\Billing\DomainModel\Order;
    !
    interface OrderRepository
    {
    /** return Order */
    public function get(OrderId $anId);
    }

    View full-size slide

  13. !
    namespace AcmeShop\Billing\DomainModel\Order;
    !
    interface OrderRepository
    {
    /** @return Order */
    public function get(OrderId $anId);
    !
    /** @return Order[] */
    public function getByUser(UserId $aUserId);
    }

    View full-size slide

  14. !
    namespace AcmeShop\Billing\DomainModel\Order;
    !
    interface OrderRepository
    {
    /** @return Order */
    public function get(OrderId $anId);
    !
    /** @return Order[] */
    public function byUser(UserId $aUserId);
    !
    /** @return OrderWithAge[] */
    public function allGroupedByAge();
    }

    View full-size slide

  15. !
    namespace AcmeShop\Billing\DomainModel\Order;
    !
    interface OrderRepository
    {
    /** @return Order */
    public function get(OrderId $anId);
    !
    /** @return Order[] */
    public function byUser(UserId $aUserId);
    !
    /** @return OrderWithAge[] */
    public function allGroupedByAge();
    !
    /** @return OrderWithCarrier[] */
    public function allGroupedByCarrier();
    }

    View full-size slide

  16. Command
    sends
    Command Handler
    is handled by
    Write
    Model
    uses
    Event Subscriber
    triggers
    writes
    reads from
    Read
    Model
    queries
    User Interface
    synchronizes with
    returns

    View full-size slide

  17. Command
    sends
    Command Handler
    is handled by
    Write
    Model
    uses
    Event Subscriber
    triggers
    writes
    reads from
    Read
    Model
    queries
    Eventual Consistency™
    User Interface
    synchronizes with
    returns

    View full-size slide

  18. Write
    Model
    Event
    Event
    Event
    Event

    View full-size slide

  19. Events are fundamental part in DDD

    View full-size slide

  20. We can express the system state as a sequence !
    of events that have occurred

    View full-size slide

  21. POST
    COMMENT
    has many
    belongs to
    AUTHOR
    is written by an
    writes many

    View full-size slide

  22. [
    {
    "type":"PostWasCreated",
    "created_at": "2014-06-11 18:00:00",
    "data":"{'post_id': 1, 'title': '...', 'content': '...'}"
    }
    ]

    View full-size slide

  23. [
    {
    "type":"PostWasCreated",
    "created_at": "2014-06-11 18:00:15",
    "data": "{'post_id': 1, 'title': '...', 'content': '...'}"
    },
    {
    "type": "PostWasPublished",
    "created_at": "2014-06-11 18:05:35",
    "data": "{'post_id': 1}"
    }
    ]

    View full-size slide

  24. [
    {
    "type":"PostWasCreated",
    "created_at": "2014-06-11 18:00:15",
    "data": "{'post_id': 1, 'title': '...', 'content': '...'}"
    },
    {
    "type": "PostWasPublished",
    "created_at": "2014-06-11 18:05:35",
    "data": "{'post_id': 1}"
    },
    {
    "type": "CommentWasCreated",
    "created_at": "2014-06-11 18:15:20",
    "data": "{'post_id': 1, 'comment_id': 1, 'comment': '...'}"
    }
    ]

    View full-size slide

  25. Event
    Event
    Event
    Event Store™

    View full-size slide

  26. PostWasCreated
    CommentWasAdded
    PostWasPublished
    Event Store™

    View full-size slide

  27. Event Sourcing

    View full-size slide

  28. Practical use case

    View full-size slide

  29. CQRS and ES combined provide a simple solution
    for complex problems

    View full-size slide

  30. CQRS allow reads and writes to scale independently

    View full-size slide

  31. Event Sourcing allow to perform a more efficient and more
    exhaustive debugging

    View full-size slide

  32. Event Sourcing grants not to be tightly coupled with any
    structure nor any implementation

    View full-size slide

  33. They’re not the silver bullet
    Use it wisely

    View full-size slide