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

CQRS & Event Sourcing in OLX

CQRS & Event Sourcing in OLX

Łukasz Szymański

May 16, 2016
Tweet

More Decks by Łukasz Szymański

Other Decks in Programming

Transcript

  1. CQRS
    Event Sourcing
    in OLX

    View Slide

  2. CQRS

    View Slide

  3. you can use a different
    model to update
    information than the
    model you use to read
    information
    http://martinfowler.com/bliki/CQRS.html

    View Slide

  4. Command
    Query
    Responsibility
    Segregation

    View Slide

  5. Command
    Query
    Responsibility
    Segregation

    View Slide

  6. new AddCredits($userId, $amount);
    Command

    View Slide

  7. Command
    new AddCredits($userId, $amount);
    Query
    new GetCredits($userId);

    View Slide

  8. Event
    Sourcing

    View Slide

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

    ensures that all
    changes to
    application state are
    stored as a sequence
    of events

    View Slide

  10. Not just can we query
    these events, we can
    also use the event log
    to reconstruct past
    states
    http://www.martinfowler.com/eaaDev/EventSourcing.html

    View Slide

  11. Commad
    Event Model
    Event Storage Event Bus
    Command Handler
    Listeners
    Read Storage
    Read Model
    Query

    View Slide

  12. Commad
    Event Model
    Event Storage Event Bus
    Command Handler
    Listeners
    Read Storage
    Read Model
    Query
    Critical section - use transaction

    View Slide

  13. Commad
    Event Model
    Event Storage Event Bus
    Command Handler
    Listeners
    Read Storage
    Read Model
    Query
    Critical section - use transaction
    Treat like temporary cache

    View Slide

  14. That’s all

    View Slide

  15. E-Wallet
    Credits; Invoices; Refunds

    View Slide

  16. Wallet Credits Bonus Credits Refund Credits
    EXPIRE: X DAYS EXPIRE: X DAYS EXPIRE: X DAYS
    INVOICE: YES/NO INVOICE: YES/NO INVOICE: YES/NO

    View Slide

  17. Wallet Credits Bonus Credits Refund Credits
    EXPIRE: NEVER EXPIRE: 90 DAYS EXPIRE: 90 DAYS
    INVOICE: YES INVOICE: NO INVOICE: YES

    View Slide

  18. Opis walleta w
    Polsce
    specyfikacja
    komenty
    enventy
    Commands

    View Slide

  19. Add Wallet Credits
    Add Bonus Credits
    Add Refund Credits

    View Slide

  20. Sub Wallet Credits
    Sub Bonus Credits
    Sub Refund Credits
    SubCredits

    View Slide

  21. Opis walleta w
    Polsce
    specyfikacja
    komenty
    enventy
    Events

    View Slide

  22. Wallet CreditsWasAdded
    Bonus CreditsWasAdded
    Refund CreditsWasAdded

    View Slide

  23. Wallet CreditsWasSubtracted
    Bonus CreditsWasSubtracted
    Refund CreditsWasSubtracted

    View Slide

  24. specyfikacja
    komenty
    enventy
    7 commands
    6 events

    View Slide

  25. specyfikacja
    komenty
    enventy
    What about other
    stuff?

    View Slide

  26. Commad
    Event Model
    Event Storage Event Bus
    Command Handler
    Listeners
    Read Storage
    Read Model
    Query
    ?

    View Slide

  27. Broadway
    komponenty
    Broadway
    https://github.com/qandidate-labs/broadway

    View Slide

  28. Auditing
    log whether commands were
    successful or not

    View Slide

  29. CommandHandling
    Provides the interface and a
    CommandHandler base class for
    handling events

    View Slide

  30. Domain
    domain abstractions of the Broadway
    framework including the aggregate root,
    domain messages and domain events.

    View Slide

  31. EventDispatcher
    The component provides an event
    dispatcher interface and a simple
    implementation

    View Slide

  32. EventHandling
    provides interfaces for an event bus and event
    listeners, but also an implementation of a simple
    event bus and an event bus that will record
    published events

    View Slide

  33. EventSourcing
    Provides base classes for event sourced
    aggregate roots and entities, an event sourced
    repository implementation and testing helpers.

    View Slide

  34. EventStore
    implementation based on doctrine/dbal to store
    events in a relational database and an in-memory
    implementation that is useful for using in tests

    View Slide

  35. ReadModel
    provides storage for your read models, a
    projector implementation to create read models
    from event streams and testing helpers.

    View Slide

  36. Repository
    Component providing an abstraction of the
    storage of aggregates.

    View Slide

  37. Serializers
    provides a simple serializer interface and a
    serializer implementation based on "handwritten"
    serializers

    View Slide

  38. Commad
    Event Model
    Event Storage Event Bus
    Command Handler
    Listeners
    Read Storage
    Read Model
    Query
    Broadway

    View Slide

  39. Adding
    Wallet
    Credits

    View Slide

  40. View Slide

  41. View Slide

  42. Credits
    Query

    View Slide

  43. View Slide

  44. View Slide

  45. Lessons Learned

    View Slide

  46. Small Steps
    Don’t try to CQRS/EventSource everything

    View Slide

  47. Keep It Simple
    Avoid names like

    BonusCreditsWasSubtractedBecauseAdminHitSubtractBonusCr
    editsInAdminPanelEvent

    View Slide

  48. Event Storming
    is not that easy as it sounds

    View Slide

  49. It’s not RDBM
    Remember about analysts

    View Slide

  50. Code changes
    Events handle changes to data, what about changes to code?

    View Slide

  51. Synchronize
    Remember about synchronizing read layer - especially after fail

    View Slide

  52. Commad
    Event Model
    Event Storage Event Bus
    Command Handler
    Listeners
    Read Storage
    Read Model
    Query
    Critical section - use transaction

    View Slide

  53. Performance
    is not an issue - for this case

    View Slide

  54. Timezone Agnostic
    Implement your own EventStore if you want timezone aware records

    View Slide

  55. Our Team

    View Slide

  56. Paid Features

    View Slide

  57. Next challenge
    Open source Paid Features libraries

    with Hexagonal Architecture in mind

    View Slide

  58. Team Lead
    PHP Dev
    Junior PHP Dev
    You ?

    View Slide

  59. Łukasz Szymański
    Development Team Lead at
    @szymanskilukasz
    http://szymanskilukasz.github.io/
    https://www.linkedin.com/in/szymanskilukasz
    https://twitter.com/szymanskilukasz

    View Slide