the event - azure saturday• “event-driven architecture (eda), is a software architecture pattern...

43
The Event The secret to building large scale distributed cloud apps.

Upload: others

Post on 25-May-2020

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The Event - Azure Saturday• “Event-driven architecture (EDA), is a software architecture pattern promoting the production, detection, consumption of, and reaction to events.”

The Event

The secret to building large scale

distributed cloud apps.

Page 2: The Event - Azure Saturday• “Event-driven architecture (EDA), is a software architecture pattern promoting the production, detection, consumption of, and reaction to events.”

Yves GoelevenSolution Architect - Particular Software

• Shipping software since 2001

• Azure MVP since 2010

• Co-founder & board member AZUG

• NServiceBus & MessageHandler

Page 3: The Event - Azure Saturday• “Event-driven architecture (EDA), is a software architecture pattern promoting the production, detection, consumption of, and reaction to events.”

Who attended my azure storage talk?

Page 4: The Event - Azure Saturday• “Event-driven architecture (EDA), is a software architecture pattern promoting the production, detection, consumption of, and reaction to events.”

A log of what happened

The secret

Memory Data

Persistent Data

Commit Log StreamCommit Log Stream

Metadata Log Stream

Row Data Stream

Blob Data Stream

MemoryTable

RowCache

IndexCache

BloomFilters

Load Metrics

Memory Data

Persistent Data

Commit Log StreamCommit Log Stream

Metadata Log Stream

Row Data Stream

Blob Data Stream

MemoryTable

RowCache

IndexCache

BloomFilters

Load Metrics

Row Data Stream

Page 5: The Event - Azure Saturday• “Event-driven architecture (EDA), is a software architecture pattern promoting the production, detection, consumption of, and reaction to events.”

How can I apply this concept

to my own app?

Page 6: The Event - Azure Saturday• “Event-driven architecture (EDA), is a software architecture pattern promoting the production, detection, consumption of, and reaction to events.”

Commit Log Entry => Event

Commit Log => Event Store

Page 7: The Event - Azure Saturday• “Event-driven architecture (EDA), is a software architecture pattern promoting the production, detection, consumption of, and reaction to events.”

Agenda

Event Driven Architecture

• Definition

• Event

• Event Driven Architecture

• Where it can fit

• Benefits

• Patterns

• Event Generators

• Event Store

• Event Sourcing

• Event Channel

• Event Processing

• Event Projections

• Event Transformations

Page 8: The Event - Azure Saturday• “Event-driven architecture (EDA), is a software architecture pattern promoting the production, detection, consumption of, and reaction to events.”

What is an event?

Page 9: The Event - Azure Saturday• “Event-driven architecture (EDA), is a software architecture pattern promoting the production, detection, consumption of, and reaction to events.”

Definition of an event

What is an event

A structured record

• Something happened

• Rooted in business domain

Well known structure

• Event Data Payload

• Context Metadata

• When

• What

• Where

• Who...

Immutable

• Cannot change

• Safe to

• broadcast

• Store

• Replicate

Self descriptive

• Described in terms of the domain model

• All relevant state is encapsulated

• Challenge: relevant inside vs outside

Page 10: The Event - Azure Saturday• “Event-driven architecture (EDA), is a software architecture pattern promoting the production, detection, consumption of, and reaction to events.”

Defining Events

Rooted in domain

Organization

OrganizationId

Name

Group

GroupId

Name

Participant

GroupId

PersonId

Person

<<Enumeration>>

ParticipantRole

Player

Coach

ContactDetails

Email

Website

Address

Street

Number

<<Enumeration>>

GroupType

Team

Administrative Department

Partnership

PartnershipId

PartnerId

Membership

MembershipId

OrganizationId

<<Enumeration>>

PartnershipType

PlayerExchangeProgram

Sponsorship

Role: ParticipantRole

Assistent

Address: Address

BusZipCodeCityCountry

Telephone PersonId

PersonIdFromTo

ParticipantId

CounterPartnerId

PartOf: Partnership

Table Official

Sports DirectorFinancial Director

Club Judge

FromTo

FromTo

Page 11: The Event - Azure Saturday• “Event-driven architecture (EDA), is a software architecture pattern promoting the production, detection, consumption of, and reaction to events.”

Defining Events

Derived from domain

RegisterOrganization

Organization Service

Organization Registered

Organization Registration

Refused

XOR

Sign PlayerOrganization

Service

Membership Added

Signup Refused

XOR

V

Participant Added

Page 12: The Event - Azure Saturday• “Event-driven architecture (EDA), is a software architecture pattern promoting the production, detection, consumption of, and reaction to events.”

Defining Event Structure

Well known structure{

context: {id: '36b6fc55-7809-4a38-b5d5-ae3e7faf615f',what: 'OrganizationRegistered',when: '9/1/2017 12:11:04 PM',who: 'Yves',why: {

id: 'fcc050fc-fdef-42b4-a131-0f33dcfbcd91',what: 'RegisterOrganization',when: '9/1/2017 12:11:02 PM',who: 'Yves'

}},organization: {

id: 'a7623ed2-038d-49f7-84aa-18cb883c2930',name: 'Basket Lummen',contactDetails: {

email: '[email protected]',address: {

street: 'Kerkstraat',number: 9,zipcode: 3560,city: 'Lummen’

}}}}

Page 13: The Event - Azure Saturday• “Event-driven architecture (EDA), is a software architecture pattern promoting the production, detection, consumption of, and reaction to events.”

Event Driven Architecture Definition

What is an event driven architecture?

• “Event-driven architecture (EDA), is a software architecture pattern promoting the production, detection,

consumption of, and reaction to events.” – Wikipedia

• It’s a mindset! Not a predefined architecture or set of technologies.

• EDA patterns can be applied at various levels

• Implementation level: Event driven design patterns used inside a service

• System level: As top level architecture

Page 14: The Event - Azure Saturday• “Event-driven architecture (EDA), is a software architecture pattern promoting the production, detection, consumption of, and reaction to events.”

Fitting EDA to UX

The 7 stages of action *

• Events originate in the ‘world’

• In response to user commands

• From entities in the world

* Donald Norman: The Design Of Everyday Things

Goal

World

Compare

Interpret

Perceive

Plan

Specify

Execute

Gu

lf of Execu

tion

Gu

lf o

f Ev

alu

atio

n

EDA

Page 15: The Event - Azure Saturday• “Event-driven architecture (EDA), is a software architecture pattern promoting the production, detection, consumption of, and reaction to events.”

Service

Filling the UX Gap

CQRS

• Command Query Responsibility Seggregation

• Different stack for

• Execution (Command)

• Evaluation (Query)

• EDA Patterns can bridge both stacks, inside a

service

Co

mm

and

UI

EDA

No

tifi

cati

on

Qu

ery

Pro

ject

ion

Domain Model

ProjectionTransformationEvents

Store

Page 16: The Event - Azure Saturday• “Event-driven architecture (EDA), is a software architecture pattern promoting the production, detection, consumption of, and reaction to events.”

EDA as Top Level Architecture

Different flavors of SOA

Service

Co

mm

and

UI

EDA

No

tifi

cati

on

Qu

ery

Pro

ject

ion

Domain Model

(Generat

or)

Projectio

n

(Processin

g)

Transforma

tion

(Processing)

Events

Store

Point to point Orchestration Event Driven

Service

Co

mm

and

UI

EDA

No

tifi

cati

on

Qu

ery

Pro

ject

ion

Domain Model

(Generat

or)

Projectio

n

(Processin

g)

Transforma

tion

(Processing)

Events

Store

Service

Co

mm

and

UI

EDA

No

tifi

cati

on

Qu

ery

Pro

ject

ion

Domain Model

(Generat

or)

Projectio

n

(Processin

g)

Transforma

tion

(Processing)

Events

Store

Events

Service

Req

uest

UI

Res

po

nse

Domain Model

DB

Service

Req

uest

UI

Res

po

nse

Domain Model

DB

Service

Req

uest

UI

Res

po

nse

Domain Model

DB

Workflow

Service

Req

uest

UI

Res

po

nse

Domain Model

DB

Service

Req

uest

UI

Res

po

nse

Domain Model

DB

Service

Req

uest

UI

Res

po

nse

Domain Model

DB

Service

Req

uest

UI

Res

po

nse

Domain Model

DB

Service

Req

uest

UI

Res

po

nse

Domain Model

DB

Page 17: The Event - Azure Saturday• “Event-driven architecture (EDA), is a software architecture pattern promoting the production, detection, consumption of, and reaction to events.”

Benefits of applying EDA

For large scale distributed cloud apps

Benefits

Auditing

Compliance

Service Autonomy

Loose coupling

Data Replication

Polyglot Persistence

Disaster Recovery

Real Time

Page 18: The Event - Azure Saturday• “Event-driven architecture (EDA), is a software architecture pattern promoting the production, detection, consumption of, and reaction to events.”

Design Patterns

Page 19: The Event - Azure Saturday• “Event-driven architecture (EDA), is a software architecture pattern promoting the production, detection, consumption of, and reaction to events.”

Overview

Event flow layers

Event generator Event Channel Event Processing Engine Downstream activity

Event Processing Engine

Event Processing Actions

Publish

Notify

Invoke service

Business process

Capture

StandingQuery

Store

StandingQuery

Page 20: The Event - Azure Saturday• “Event-driven architecture (EDA), is a software architecture pattern promoting the production, detection, consumption of, and reaction to events.”

Mapping to CQRS

Patterns applied

• Event Generators

• Domain Model

• Other services (& IoT)

• Event Processing

• Projection logic

• Notification logic

• Downstream activity

• Event Store

• Notification

• DB update

• Event Channels ServiceEvents

SensorEvents

ServiceC

om

man

d

UI

EDA

No

tifi

cati

on

Qu

ery

Pro

ject

ion

Domain Model

(Generator)

Projection(Processing)

Transformation(Processing)

Events

Store

Page 21: The Event - Azure Saturday• “Event-driven architecture (EDA), is a software architecture pattern promoting the production, detection, consumption of, and reaction to events.”

Store

Page 22: The Event - Azure Saturday• “Event-driven architecture (EDA), is a software architecture pattern promoting the production, detection, consumption of, and reaction to events.”

Event Store

Store events for future use

• Partition by streams (e.g. type)

• Maintain order (append only)

• Store metadata & body

• Popular media

• Files (e.g. GetEventStore, Eventhubs Archive, Kafka)

• Tables (e.g. Azure Table Storage)

• Auditing & compliance

If you don’t store your events you will have a hopefully consistent system, not an eventually consistent one.

Even

t 1

Even

t 2

Even

t 3

Even

t 4

Even

t 5

Even

t 1

Even

t 2

Even

t 3

Even

t 4

Even

t 5

Even

t 1

Even

t 2

Even

t 3

Even

t 4

Even

t 5

Page 23: The Event - Azure Saturday• “Event-driven architecture (EDA), is a software architecture pattern promoting the production, detection, consumption of, and reaction to events.”

Event Store

Example

PartitionKey RowKey Timestamp Body EventType SourceId Version

Organization 1_000001 2017-08-31T19:25:04 { context: { …. OrganizationRegistered 1 1

Organization 1_000002 2017-08-31T19:26:43 { context: { …. OrganizationRenamed 1 2

Organization 1_000003 2017-09-01T09:45:16 { context: { …. MemberAdded 1 3

Person 1_000001 2017-08-31T19:25:04 { context: { …. PersonRegistered 1 1

Person 1_000002 2017-08-31T19:25:5 { context: { …. ContactDetailsAdded 1 2

Page 24: The Event - Azure Saturday• “Event-driven architecture (EDA), is a software architecture pattern promoting the production, detection, consumption of, and reaction to events.”

Replay stored events

To solve the following problems

• Disaster recovery

• Data replication

• Polyglot persistence

• Data versioning

Projection

Projection

Projection

Page 25: The Event - Azure Saturday• “Event-driven architecture (EDA), is a software architecture pattern promoting the production, detection, consumption of, and reaction to events.”

Projection

Turns events into database records

public class MyProjection : IProjection<SearchEntry, OrganizationRegistered>{

public void Project(SearchEntry record, OrganizationRegistered msg){

record.Id = msg.Organization.Id;record.Title = msg.Organization.Name;record.Description = msg.Organization.ContactDetails?.Address?.ToString();record.Type = "Organization";

}}

Page 26: The Event - Azure Saturday• “Event-driven architecture (EDA), is a software architecture pattern promoting the production, detection, consumption of, and reaction to events.”

Replay

Stored Events

var configuration = new EventsourcingConfiguration();configuration.UseEventSource(new AzureTableStorageEventSource(connectionString, table));configuration.EnableProjections(typeof(MyProjection));var projection = configuration.CreateProjector();

var records = await projection.Restore<SearchEntry>("Organization");

await search.Upsert(index, records);

Page 27: The Event - Azure Saturday• “Event-driven architecture (EDA), is a software architecture pattern promoting the production, detection, consumption of, and reaction to events.”

Generators

Page 28: The Event - Azure Saturday• “Event-driven architecture (EDA), is a software architecture pattern promoting the production, detection, consumption of, and reaction to events.”

Generators

Any object that generates events

Events

Agent Sensor Gateway Aggregate Scheduler Replay Emulator

Page 29: The Event - Azure Saturday• “Event-driven architecture (EDA), is a software architecture pattern promoting the production, detection, consumption of, and reaction to events.”

Aggregates

Transactional Boundary

• “A DDD aggregate is a cluster of domain objects that can be treated as a single unit. Aggregates are the basic element of transfer of data storage - you request to load or save whole aggregates. Transactions should not cross aggregate boundaries.” – Martin Fowler

Organization

OrganizationId

Name

Group

GroupId

Name

Participant

GroupId

PersonId

Person

<<Enumeration>>

ParticipantRole

Player

Coach

ContactDetails

Email

Website

Address

Street

Number

<<Enumeration>>

GroupType

Team

Administrative Department

Partnership

PartnershipId

PartnerId

Membership

MembershipId

OrganizationId

<<Enumeration>>

PartnershipType

PlayerExchangeProgram

Sponsorship

Role: ParticipantRole

Assistent

Address: Address

BusZipCodeCityCountry

Telephone PersonId

PersonIdFromTo

ParticipantId

CounterPartnerId

PartOf: Partnership

Table Official

Sports DirectorFinancial Director

Club Judge

FromTo

FromTo

Sign Player

Page 30: The Event - Azure Saturday• “Event-driven architecture (EDA), is a software architecture pattern promoting the production, detection, consumption of, and reaction to events.”

Aggregate as event generator

Events emitted as part of the transaction

Organization

OrganizationId

Name

Group

GroupId

Name

Participant

GroupId

PersonId

Person

<<Enumeration>>

ParticipantRole

Player

Coach

ContactDetails

Email

Website

Address

Street

Number

<<Enumeration>>

GroupType

Team

Administrative Department

Partnership

PartnershipId

PartnerId

Membership

MembershipId

OrganizationId

<<Enumeration>>

PartnershipType

PlayerExchangeProgram

Sponsorship

Role: ParticipantRole

Assistent

Address: Address

BusZipCodeCityCountry

Telephone PersonId

PersonIdFromTo

ParticipantId

CounterPartnerId

PartOf: Partnership

Table Official

Sports DirectorFinancial Director

Club Judge

FromTo

FromTo

Sign Player

Membership Added

Signup Refused

XOR

V

Participant AddedService

Page 31: The Event - Azure Saturday• “Event-driven architecture (EDA), is a software architecture pattern promoting the production, detection, consumption of, and reaction to events.”

Event Sourced aggregate

Aggregate state restored from the emitted events

Organization

OrganizationId

Name

Group

GroupId

Name

Participant

GroupId

PersonId

Person

<<Enumeration>>

ParticipantRole

Player

Coach

ContactDetails

Email

Website

Address

Street

Number

<<Enumeration>>

GroupType

Team

Administrative Department

Partnership

PartnershipId

PartnerId

Membership

MembershipId

OrganizationId

<<Enumeration>>

PartnershipType

PlayerExchangeProgram

Sponsorship

Role: ParticipantRole

Assistent

Address: Address

BusZipCodeCityCountry

Telephone PersonId

PersonIdFromTo

ParticipantId

CounterPartnerId

PartOf: Partnership

Table Official

Sports DirectorFinancial Director

Club Judge

FromTo

FromTo

Sign Player

Membership Added

Signup Refused

XOR

V

Participant AddedService

Page 32: The Event - Azure Saturday• “Event-driven architecture (EDA), is a software architecture pattern promoting the production, detection, consumption of, and reaction to events.”

Event Sourced Aggregate

Code Sample

var repository = configuration.CreateAggregateRepository();

var org = await repository.Get<Organization>(orgid);

org.Register(new { Name = "Basket Lummen" });

org.Rename(new { Name = "Basket Lummen 2" });

await repository.Flush();

Page 33: The Event - Azure Saturday• “Event-driven architecture (EDA), is a software architecture pattern promoting the production, detection, consumption of, and reaction to events.”

Event Sourced Aggregate

Code Sample public class Organization : EventSourced,IApply<OrganizationRegistered>,IApply<OrganizationRenamed>

{public string Name { get; private set; }

public void Register(dynamic msg){

// todo validate if command can be executed

Emit(new OrganizationRegistered { Context = new Context { Id = Id, What = "OrganizationRegistered", Who = "Yves“ },Organization = new Organization { Id = Id, Name = msg.Name }

});}

public void Rename(dynamic msg) {// todo validate if command can be executed

Emit(new OrganizationRenamed { Context = new Context { Id = Id, What = "OrganizationRenamed", Who = "Yves“ },OldName = Name,NewName = msg.Name

});}

public void Apply(OrganizationRegistered msg) {Name = msg.Organization.Name;

}

public void Apply(OrganizationRenamed msg) {Name = msg.NewName;

}}

Page 34: The Event - Azure Saturday• “Event-driven architecture (EDA), is a software architecture pattern promoting the production, detection, consumption of, and reaction to events.”

Channel

Page 35: The Event - Azure Saturday• “Event-driven architecture (EDA), is a software architecture pattern promoting the production, detection, consumption of, and reaction to events.”

Channels

Any technology used to transport events between producers and processors

Transactional channels

• Durable

• Every event is consumed and confirmed individually

• Pull

• At least once processing

• Failure to process an event leads to server side retry

• E.g. Queues & Topics

Streaming channels

• Durable

• Events are consumed in batch and confirmed in batch (moving cursor)

• Pull

• At least once processing

• No individual retry, entire batch need to be retried

• E.g. EventHubs & IoTHubs

Volatile channels

• Volatile

• Every event is consumed individually

• Push

• At most once processing

• Failure to process leads to loss of event

• Requires auto scaleout

• Requires client side retry logic to guarantee delivery

• E.g. Eventgrid, SignalR, Webhooks

Page 36: The Event - Azure Saturday• “Event-driven architecture (EDA), is a software architecture pattern promoting the production, detection, consumption of, and reaction to events.”

Pushing Events to Channel

From the eventsourced aggregate

var config = new HandlerRuntimeConfiguration();var dispatcher = config.EventProcessingPipeline()

.EnableDispatching().AddDirectRoute("mytopic", SupportedTransportTypes.Topic, serviceBusConnectionString)

.GetDispatcher();

var runtime = await HandlerRuntime.Create(config);await runtime.Start();

var configuration = new EventsourcingConfiguration();configuration.UseEventSource(new AzureTableStorageEventSource(storageConnectionstring, "EventSource"));configuration.UseChannel(msgs => dispatcher.Dispatch(msgs));

Page 37: The Event - Azure Saturday• “Event-driven architecture (EDA), is a software architecture pattern promoting the production, detection, consumption of, and reaction to events.”

Processing

Page 38: The Event - Azure Saturday• “Event-driven architecture (EDA), is a software architecture pattern promoting the production, detection, consumption of, and reaction to events.”

Event Processing

3 styles

Simple Event Processing

• Real Time

• Individual event processing

• Action per event

Stream Event Processing

• Real Time

• Assumed ordered in time

• All messages in a given time window are processed at once

• Aggregations, transformations, outlier detection, etc...

• Action per outcome

Complex Event Processing

• Historical analysis

• Multiple event stores

• Unordered

• Event correlation & causality detection

• Fault detection

Page 39: The Event - Azure Saturday• “Event-driven architecture (EDA), is a software architecture pattern promoting the production, detection, consumption of, and reaction to events.”

Real Time Projection

Example of simple event processing

var entry = await search.Get<SearchEntry>(settings.Index, m.Context.Id) ?? new SearchEntry();

projection.Apply(entry, Cast(context.Message, GetType(m.Context.What)));

await search.Upsert(settings.Index, new[] { entry });

Page 40: The Event - Azure Saturday• “Event-driven architecture (EDA), is a software architecture pattern promoting the production, detection, consumption of, and reaction to events.”

Transformation

Example of stream event processing

public class Transformation : IStandingQueryBuilder{

public IObservable<ActionableContext> Build(IObservable<IProcessingContext> stream){

return transformationQuery = from c in stream select Transform(c);}

private ActionableContext Transform(IProcessingContext c){

dynamic m = c.Message;return new ActionableContext(c){

Message = new { What = m.Context.What, Id = m.Context.Id,Name = m.Organization != null ? m.Organization.Name : m.NewName

},AutoComplete = true

};}

}

Page 41: The Event - Azure Saturday• “Event-driven architecture (EDA), is a software architecture pattern promoting the production, detection, consumption of, and reaction to events.”

Wrapup

Page 42: The Event - Azure Saturday• “Event-driven architecture (EDA), is a software architecture pattern promoting the production, detection, consumption of, and reaction to events.”

Resources

On GitHub & MyGet

• Sample code: https://github.com/MessageHandler/AzureSaturday

• Libraries: https://www.myget.org/F/messagehandler/api/v3/index.json

Page 43: The Event - Azure Saturday• “Event-driven architecture (EDA), is a software architecture pattern promoting the production, detection, consumption of, and reaction to events.”

Q&A