event driven-architecture from a scalability perspective

54
Building loosely coupled and scalable systems using Event-Driven Architecture Jonas Bonér Patrik Nordwall Andreas Källberg

Upload: jonas-boner

Post on 06-May-2015

10.592 views

Category:

News & Politics


0 download

DESCRIPTION

Event Driven-Architecture from a Scalability perspective

TRANSCRIPT

Page 1: Event Driven-Architecture from a Scalability perspective

Building loosely coupled and scalable systems using

Event-Driven Architecture

Jonas BonérPatrik NordwallAndreas Källberg

Page 2: Event Driven-Architecture from a Scalability perspective

Why is EDA Important for Scalability?

Page 3: Event Driven-Architecture from a Scalability perspective

What building blocks does EDA consists of?

Page 4: Event Driven-Architecture from a Scalability perspective

Outline

Concepts

Patterns

Challenges

Highly Scalable Web Sites

Page 5: Event Driven-Architecture from a Scalability perspective

Concepts

Page 6: Event Driven-Architecture from a Scalability perspective

Messaging

Publish-Subscribe

Point-to-Point

Store-forward

Request-Reply

Page 7: Event Driven-Architecture from a Scalability perspective

Publish-Subscribe

Page 8: Event Driven-Architecture from a Scalability perspective

Point-to-Point

Page 9: Event Driven-Architecture from a Scalability perspective

Store-Forward

Page 10: Event Driven-Architecture from a Scalability perspective

Request-Reply

Page 11: Event Driven-Architecture from a Scalability perspective

Standards

AMQPJMS

Page 12: Event Driven-Architecture from a Scalability perspective

Some Products

...

Page 13: Event Driven-Architecture from a Scalability perspective

Domain Events

“It's really become clear to me in the last couple of years that we need a new building block and that is the Domain Events”

-- Eric Evans, 2009

Page 14: Event Driven-Architecture from a Scalability perspective

Domain Events

“State transitions are an important part of our problem space and should be modeled within our domain.”

-- Greg Young, 2008

Page 15: Event Driven-Architecture from a Scalability perspective

Domain Events

Something that has happened in the past

CustomerRelocatedCargoShipped

InventoryLossageRecorded

Page 16: Event Driven-Architecture from a Scalability perspective

Domain Events

Uniquely identifiable

Self contained

Observable  

Time relevant

Page 17: Event Driven-Architecture from a Scalability perspective

Patterns

Page 18: Event Driven-Architecture from a Scalability perspective

Event Stream Processing

select  *  from  Withdrawal(amount>=200).win:length(5)

Page 19: Event Driven-Architecture from a Scalability perspective

Actors

• Share NOTHING• Isolated lightweight processes• Communicates through messages• Asynchronous and non-blocking• No shared state

… hence, nothing to synchronize.• Each actor has a mailbox (message queue)

Page 20: Event Driven-Architecture from a Scalability perspective

Easier to reason aboutRaised abstraction level

Easier to avoidRace conditions

DeadlocksStarvationLive locks

Actors

Page 21: Event Driven-Architecture from a Scalability perspective

Transparent remoting•Client-managed• Server-managed

Pub-Sub• Redis• ZeroMQ

Guaranteed deliveryPersistent mailbox• File-based•Network-based

Actors

Page 22: Event Driven-Architecture from a Scalability perspective

“A single model cannot be appropriate for reporting, searching and transactional behavior.”

-- Greg Young, 2008

Command and Query Responsibility Segregation

Page 23: Event Driven-Architecture from a Scalability perspective

• Aggregate roots receive Commands and publish Events

• All state changes are represented by Domain Events

• Reporting module is updated as a result of the published Events

• All Queries go directly to the Reporting, the Domain is not involved

CQRS

Page 24: Event Driven-Architecture from a Scalability perspective

The traditional way...

Client

UpdateDTO

QueryDTO

Domain

Data access

Service

DB

Page 25: Event Driven-Architecture from a Scalability perspective

The CQRS way...

Client

Command QueryDTO

Domain

Data access

Service

DB

Data access

Service

DB

publish subscribe

Page 26: Event Driven-Architecture from a Scalability perspective

The CQRS way...

Client

Domain

Data access

Service

DB Data access

Service

DB

publish Data access

Service

DB

Data access

Service

DB

Page 27: Event Driven-Architecture from a Scalability perspective

• Separation of concern

• Fully encapsulated domain that only exposes behavior

• Queries do not use the domain model

• Easy integration with external systems

• Performance and scalability

• Testability

CQRS Benefits

Page 28: Event Driven-Architecture from a Scalability perspective

Event Sourcing

• Every state change is materialized in an Event

• All events are stored in an Event Log

• System can be reset and Event Log replayed

• Many different Listeners can be added

Page 29: Event Driven-Architecture from a Scalability perspective

Storing Structure

Purchase Order

Line Item

Shipping Information

*

Page 30: Event Driven-Architecture from a Scalability perspective

Event Sourcing - Storing Deltas

Cart Created

Added 2 Socks

Added 2 Shirts

Shipping Info

Added

Page 31: Event Driven-Architecture from a Scalability perspective

Aggregates are tracking events as to what has changed within them

Current state is constructed by replaying all events

Data is not persisted in a structure but as a series of transactions

No ORM is needed

Page 32: Event Driven-Architecture from a Scalability perspective

Event Sourcing - Replaying Events

1 2 3 4 5 6 7

Page 33: Event Driven-Architecture from a Scalability perspective

Event Sourcing - Rolling Snapshot

1 2 100 101 102 103 104...

Snapshot

Page 34: Event Driven-Architecture from a Scalability perspective

• No object-relational impedance mismatch

• Bullet-proof auditing and historical tracing

• Support future ways of looking at data

• Performance and scalability

• Testability

• Reconstruct production scenarios

Event Sourcing - Benefits

Page 35: Event Driven-Architecture from a Scalability perspective

http://github.com/patriknw/sculptor-simplecqrs/

Simple CQRS Sample

Page 36: Event Driven-Architecture from a Scalability perspective

Challenges

Page 37: Event Driven-Architecture from a Scalability perspective

Clustering of Brokers

ActiveMQ

• Master-Slave

• Store and Forward Network of Brokers

RabbitMQ

• Cluster of Erlang nodes

ZeroMQ

• Brokerless - point-to-point or pub-sub

Page 38: Event Driven-Architecture from a Scalability perspective

ZeroMQ

Text

Network protocol - thin layer above TCP/IP

Transports• INPROC• IPC• MULTICAST • TCP

Page 39: Event Driven-Architecture from a Scalability perspective

ZeroMQ

Forwarding devices• QUEUE• FORWARDER• STREAMER

Page 40: Event Driven-Architecture from a Scalability perspective

ZeroMQ

PatternsREQUEST/REPLY (load-balanced)PUB/SUBUPSTREAM/DOWNSTREAM (pipelining)PAIR (exclusive)

Page 41: Event Driven-Architecture from a Scalability perspective

Wire Formats

Text

Java serialization (binary, schema, runtime)Protobuf (binary, schema, compiled)Avro (binary, schema, compiled & runtime)Thrift (binary, schema, compiled)MsgPack (binary, schema, compiled)Protostuff (binary, schema, compiled)Kryo (binary, schema-less, runtime)BERT (binary, schema-less, runtime)Hessian (binary, schema-less, compiled)XML (text, schema)JSON (text, schema-less)

Page 42: Event Driven-Architecture from a Scalability perspective

Guaranteed Delivery

Do I really need it?

Persistence increases reliability at the expense of performance  

Page 43: Event Driven-Architecture from a Scalability perspective

Competing Consumers

Pattern for solving:

• Load balancing• Concurrency• Failover

Only works with Point-to-Point Channel

Challenge

• ordering• duplicates (idempotent receiver)

Page 44: Event Driven-Architecture from a Scalability perspective

Duplicate Messages

What do I need?• Once-and-only-once• At-least-once• At-most-once

Unique message identifier

keep history of processed ids

Business semantics

QOS

Page 45: Event Driven-Architecture from a Scalability perspective

How to get back on track?

Point-to-point: no problem, just make the queue persistent

Solution: durable subscriber

Problem: only active subscribers

Problem: failover and load balancing

Pub/sub: well, not so straight forward

Page 46: Event Driven-Architecture from a Scalability perspective

Producer Flow Control

What to do when producers flood you with messages?

Running low on broker resources, slow consumers

Graceful degradation • caller run (in process only)• block• abort• discard

Page 47: Event Driven-Architecture from a Scalability perspective

Behind the Scenes of Highly Scalable Web Sites

Page 48: Event Driven-Architecture from a Scalability perspective

caching is important, but also...

Page 49: Event Driven-Architecture from a Scalability perspective

Reddit: Precompute everything and cache it

Flickr: Do The Essential Work Up-Front And Queue The Rest

Minimize latency

Amazon: ~99% of content is static

Page 50: Event Driven-Architecture from a Scalability perspective

Digg: Push on Change

Facebook: Pull on Demand

Changes - pull or push

Twitter: Push tweets

Page 51: Event Driven-Architecture from a Scalability perspective

WebSockets enable real event-driven web

Request-response doesn't fit collaborative systems

Truly event-driven web clients

Page 52: Event Driven-Architecture from a Scalability perspective

Why is EDA Important for Scalability?

• Scale out and up

• Load balance

• Parallel execution

• Non-blocking

• Loosely coupled components can scale more independent of each other

Page 53: Event Driven-Architecture from a Scalability perspective

?

Page 54: Event Driven-Architecture from a Scalability perspective

thanks for listening