Event Driven Systems

EdPug - July 2018

$(whoami)

Scott Pringle

Lead Software Developer & Technical Team Lead

@ People's Postcode Lottery

Former DDDEdinburgh Organiser

why?

What I'll cover

  • What Is An Event?
  • Advantages & Disadvantages of events
  • Thinking In Events
  • Event Driven Architectures
  • Event Sourcing
  • A Practical Example

What Is An Event?

An event is an action recognized by software, that may be handled by the software

https://en.wikipedia.org/wiki/Event_(computing)#Description

An Event is the encapsulation of a recorded action within an application

Me, probably plagiarising someone...

Event structure 

<?php

namespace Acme\Demo\Event;

use Acme\Demo\Entity\User;
use Acme\Demo\Entity\Transaction;

class UserRefundRequestedEvent
{
  /**
  * @var User
  */
  private $user;
  /**
  * @var Transaction
  */
  private $transaction;
  
  public function __construct(User $user, Transaction $transaction)
  {
    $this->user = $user;
    $this->transaction = $transaction;
  }
  
  public function getUser(): User
  {
    return $this->user;
  }
  
  public function getTransaction(): Transaction
  {
    return $this->transaction;
  }
}
{
  "timestamp": "2018-07-18T19:00:00Z",
  "payload": {
    "user": {
      "forename": "Scott",
      "surname": "Pringle",
      "id": 1
    },
    "transaction": {
      "timestamp": "2018-06-01 11:55:00",
      "totalCost": "1099",
      "items": [],
      "shipping": "99"
    }
  },
  "type": "UserRefundRequested",
  "version": 1,
  "identifier": "7683ba44-bd0a-49ac-82b7-ce41b38d4a2a"
}

Cloud Events

http://cloudevents.io

{
  "eventTime": "2018-07-18T19:00:00Z",
  "data": {
    "user": {
      "forename": "Scott",
      "surname": "Pringle",
      "id": 1
    },
    "transaction": {
      "timestamp": "2018-06-01 11:55:00",
      "totalCost": "1099",
      "items": [],
      "shipping": "99"
    }
  },
  "eventType": "UserRefundRequested",
  "eventTypeVersion": 1,
  "cloudEventsVersion": "0.1",
  "source": "ProducerUri/Component",
  "eventID": "7683ba44-bd0a-49ac-82b7-ce41b38d4a2a",
  "schemaURL": "OptionalUrlForData", # Soap did something good...
  "contentType": "application/json",
  "extensions": {
    "key1": "value1",
  }
}

Advantages of using events

Decoupling

Additional flexibility

Scalability

Disadvantages of Event Driven Systems

Additional "Plumbing"

no centralised rules for system behaviour

Complexity

Thinking In Events

As Developers we...

  • Think in Commands
  • Want to know the result of a request

thinking in events is hard!

Mindset change from "will" to "has"

Think In Reactions, Not Actions

Treat events As first class citizens

MAP TO THINGS THAT REALLY HAPPEN IN YOUR ORGANISATION

Domain Event

Command

Actor

Aggregate

Event Storming

Event Driven Architecture

4 Principal Layers

  • Event Generator
  • Event Channel
  • Event Processing
  • Downstream Activity

Event Generator

  • Senses A Change
  • Represents The Change As An Event

Event Channel

  • Middleware from Event Generator
    to Event Processor
  • TCP/IP connection / Input file
  • Stores events into queues for Processors

Event Processing

  • Where the magic happens
  • 0 or more reactions to an event
  • Not necessarily under your control

Downstream Activity

Consequence of Event Processing, e.g.

  • Order confirmed emails being sent
  • Stock count being updated

Events & Messaging

Event handling becomes asynchronous

Events are meant to be immutable

Tell other parts of your application to do something

other applications may react

Event Sourcing

Events used to Build state

State becomes

re-playable

Create alternative state with new "faked" events

Complete application history

You don't think of an audit log until you're audited

Practical example Of using an event

REFUNDING A PAYMENT

User Requests Refund

 

$$$

Side effects

  • Review required
  • Approver sent an email

Email sent to reviewer

refund approved

refund sent

Recap

Events allow for a decoupled platform

thinking in events exposes side effects

using events opens many architectural doors

events don't solve all problems...just most

Questions?

Thank you

Twitter: @Luciam91

Event Driven Systems - EdPug

By Scott Pringle

Event Driven Systems - EdPug

  • 530