Using Event-Driven Architectures with Cassandra

30
@petabridge Petabridge.com Event-Driven Architectures with Cassandra Aaron Stannard, CTO & Cofounder Petabridge DataStax MVP 2015

Transcript of Using Event-Driven Architectures with Cassandra

Page 1: Using Event-Driven Architectures with Cassandra

@petabridge Petabridge.com

Event-Driven Architectures with Cassandra

Aaron Stannard, CTO & Cofounder PetabridgeDataStax MVP 2015

Page 2: Using Event-Driven Architectures with Cassandra

@petabridge Petabridge.com

Crash Course in EDA

Page 3: Using Event-Driven Architectures with Cassandra

@petabridge Petabridge.com

Successful Product

30,000,000devices permonth

500 mbs of eventdata per second

100,000,000db transactionsper day

30 servers

Page 4: Using Event-Driven Architectures with Cassandra

@petabridge Petabridge.com

Struggling Business

12 months ofrunway

3 engineers

0engineersw/prior distributedsystems experience

Page 5: Using Event-Driven Architectures with Cassandra

@petabridge Petabridge.com

A Radical New Product1. IntegrateMarkedUpinto your application.

Your Installer

Notification Client

Your ApplicationAnalytics SDK

2. Users install your appandMarkedUp collects events.

3. Create campaigns basedon events and user-data.

4.MarkedUp delivers personalized, brandedmessages to users instantly via push,based on their in-app activity.

Page 6: Using Event-Driven Architectures with Cassandra

@petabridge Petabridge.com

Allow our customers to start conversations with specific types of users.

Page 7: Using Event-Driven Architectures with Cassandra

@petabridge Petabridge.com

Had to be done in real-time for best results.

Page 8: Using Event-Driven Architectures with Cassandra

@petabridge Petabridge.com

Prototype: HTTP + Database

Page 9: Using Event-Driven Architectures with Cassandra

@petabridge Petabridge.com

(Read after Write)

Page 10: Using Event-Driven Architectures with Cassandra

@petabridge Petabridge.com

TheoryUSER

HTTP LOAD BALANCER

ASP.NETMVC(IIS)

CASSANDRACLUSTER

Events 0...3

0

0

1

0 10,1

2

20,1,2

3

30,1,2,3

MESSAGE

Page 11: Using Event-Driven Architectures with Cassandra

@petabridge Petabridge.com

Reality

HTTP LOAD BALANCER

ASP.NETMVC(IIS)

CASSANDRACLUSTER

USER

Events 0...3

0 3

0 ??3??

1 2

1??2 ??

Page 12: Using Event-Driven Architectures with Cassandra

@petabridge Petabridge.com

Breakthrough!

USER

HTTP LOAD BALANCER

Events 0...3

0 12 3

MESSAGE

Page 13: Using Event-Driven Architectures with Cassandra

@petabridge Petabridge.com

I had STATEFUL, REACTIVE, STREAM PROCESSING

problem!

Page 14: Using Event-Driven Architectures with Cassandra

@petabridge Petabridge.com

Implementation: HTTP -> EDA -> C*

C* ClusterPrivateVPCZone

HTTP Load Balancer

HTTP Load Balancer

API

TargetingSystem

Win32 MailboxService

CQL Writes

CQL R/W

CQL Reads

Akka.NETremoting(TCP +protobufs)

Page 15: Using Event-Driven Architectures with Cassandra

@petabridge Petabridge.com

Why Do We Care About EDA?

Page 16: Using Event-Driven Architectures with Cassandra

@petabridge Petabridge.com

Databases Aren’t Magical

HTTP LOAD BALANCER

ASP.NETMVC(IIS)

CASSANDRACLUSTER

USER

Events 0...3

0 3

0 ??3??

1 2

1??2 ??

Page 17: Using Event-Driven Architectures with Cassandra

@petabridge Petabridge.com

Event-Driven Architecture Concepts

Page 18: Using Event-Driven Architectures with Cassandra

@petabridge Petabridge.com

Key Terms• Event – a significant change in

“state”• Message – a notification of an event• Emitter – detect, gather, transfer

events• Sinks – react to events immediately• Channels – conduit between

emitters and sinks

Page 19: Using Event-Driven Architectures with Cassandra

@petabridge Petabridge.com

Goals & Benefits• Extreme decoupling

• Easily distributed

• Inherently asynchronous and concurrent

Page 20: Using Event-Driven Architectures with Cassandra

@petabridge Petabridge.com

EDA Styles• Simple Event Processing

• Event Stream Processing

• Complex Event Processing (CEP)

Page 21: Using Event-Driven Architectures with Cassandra

@petabridge Petabridge.com

Event Stream Processing w/ FSMs

Page 22: Using Event-Driven Architectures with Cassandra

@petabridge Petabridge.com

State A

State B

State C

Event 0

Stay

Event 1

Transition

Event 2

Transition

Event 3

REACT

Page 23: Using Event-Driven Architectures with Cassandra

@petabridge Petabridge.com

FSM0

C*

C*

C*

C* C*

Events 0..2

Transition

Flush

FSM 0 State

FSM 0 State FSM 0 State

Page 24: Using Event-Driven Architectures with Cassandra

@petabridge Petabridge.com

C*

C*

C*

C* C*

FSM 0 State

FSM 0 State FSM 0 State

FSM0

Crash

FSM0

RecreateFSMRequestState

ReplayState

Page 25: Using Event-Driven Architectures with Cassandra

@petabridge Petabridge.com

CQL SchemaCREATE TABLE IF NOT EXISTS FSMState ( persistence_id text, state_id int, state_data blob, PRIMARY KEY (persistence_id, state_id))

Page 26: Using Event-Driven Architectures with Cassandra

@petabridge Petabridge.com

Event Sourcing with C*

Page 27: Using Event-Driven Architectures with Cassandra

@petabridge Petabridge.com

C*

RoomMessageHistoryActor

Message 1

Message 2

Message 3

Chat History forUser X

1. Build history,persist to store

History

Message 1Message 2Message 3

2. Requesthistory

3. Deliver historyas materialized view

4. Present to user

Page 28: Using Event-Driven Architectures with Cassandra

@petabridge Petabridge.com

C*

RoomMessageHistoryActor

1. Startsafter application

restart

2. Replays storedmessages from

C*Message 1

Message 2

Message 3

History

Message 1Message 2Message 3

3. Rebuildsin-memory

history

Page 29: Using Event-Driven Architectures with Cassandra

@petabridge Petabridge.com

EDA + C* Tips and Tricks• Large number of small sinks works

best• Define simple, reusable journals and

snapshots• C* == durable backup, App = single

source of truth • TTL everywhere your business

domain supports it

Page 30: Using Event-Driven Architectures with Cassandra

@petabridge Petabridge.com

Have questions? Ask us!

http://petabridge.com/