Event Sourcing in less than 20 minutes - With Akka and Java 8

82
Kiki Carter Enterprise Architect May 2017 Event Sourcing and CQRS in 20 minutes with Akka & Java 8

Transcript of Event Sourcing in less than 20 minutes - With Akka and Java 8

Page 1: Event Sourcing in less than 20 minutes - With Akka and Java 8

Kiki CarterEnterprise Architect

May 2017

Event Sourcing and CQRS in 20

minutes with Akka & Java 8

Page 2: Event Sourcing in less than 20 minutes - With Akka and Java 8

Lightbend

Headquarters in

San Francisco &

Lausanne,

Switzerland

>2 million

downloads

Lightbend

Reactive

Platform Originators of the

Reactive Manifesto

>17,000 signers

Reactive Experts

in 20 Countries

around the globe

Page 3: Event Sourcing in less than 20 minutes - With Akka and Java 8

About me

Some things I like Some things I don’t like

IG: @thatskiki

Heights

Wild wildlife

Unintentional Sharing

These things

Page 4: Event Sourcing in less than 20 minutes - With Akka and Java 8

Why are we talking about this

anyway?

Page 5: Event Sourcing in less than 20 minutes - With Akka and Java 8

Not really *new* buzzwords

Page 6: Event Sourcing in less than 20 minutes - With Akka and Java 8

Legacy App Modernization(is a thing again)

Page 7: Event Sourcing in less than 20 minutes - With Akka and Java 8

Distributed Systems(for real this

time)

Page 8: Event Sourcing in less than 20 minutes - With Akka and Java 8

A system of

autonomous

collaborative distributed

services

Page 9: Event Sourcing in less than 20 minutes - With Akka and Java 8

AutonomyFrom Greek Auto-nomos:

auto meaning self

nomos meaning law

Page 10: Event Sourcing in less than 20 minutes - With Akka and Java 8

Leading the

way:

Promise

Theory

Page 11: Event Sourcing in less than 20 minutes - With Akka and Java 8

Think in Promises

not Obligations

Page 12: Event Sourcing in less than 20 minutes - With Akka and Java 8

“Autonomy makes information

local, leading to greater certainty

and stability”

- In Search for Certainty by Mark Burgess

Page 13: Event Sourcing in less than 20 minutes - With Akka and Java 8

No one can tell my story better than

I can…

Page 14: Event Sourcing in less than 20 minutes - With Akka and Java 8

Obligations diverge into unpredictable outcomes from definite beginnings⇒ decreased certainty

Promises converge towards a definite outcome from unpredictable beginnings⇒ improved certainty

Page 15: Event Sourcing in less than 20 minutes - With Akka and Java 8

Decreased Certainty IRL

Page 16: Event Sourcing in less than 20 minutes - With Akka and Java 8

An Autonomous

Service can only

Promise its own

Behavior

Page 17: Event Sourcing in less than 20 minutes - With Akka and Java 8

How do we get there? (irl)

Page 18: Event Sourcing in less than 20 minutes - With Akka and Java 8

Data Center A

eCommerceserver

eCommerceserver

eCommerceserver

Database Database

Data Center B

eCommerceserver

eCommerceserver

eCommerceserver

Database Database

Heritage

Distributed

Systems

Page 19: Event Sourcing in less than 20 minutes - With Akka and Java 8

Enter Distributed Scaling

Page 20: Event Sourcing in less than 20 minutes - With Akka and Java 8

User

order

cart

Shop

data

User

order

cart

Shop

data

Scale by “stateless services”

Page 21: Event Sourcing in less than 20 minutes - With Akka and Java 8

Eat your broccoli!

Page 22: Event Sourcing in less than 20 minutes - With Akka and Java 8

I’m eating it!

Page 23: Event Sourcing in less than 20 minutes - With Akka and Java 8

I’m eating it!

Page 24: Event Sourcing in less than 20 minutes - With Akka and Java 8

I’m eating it!

Page 25: Event Sourcing in less than 20 minutes - With Akka and Java 8

User

order

cart

Shop

data

User

order

cart

Shop

data

data

data

data

Page 26: Event Sourcing in less than 20 minutes - With Akka and Java 8

But wait, there’s more!

Page 27: Event Sourcing in less than 20 minutes - With Akka and Java 8

User

order

cart

Shop

data

User

order

cart

Shop

data

Shar

ed d

om

ain

ob

js

Page 28: Event Sourcing in less than 20 minutes - With Akka and Java 8

User

order

cart

Shop

data

User

order

cart

Shop

data

data

data

dataShar

ed d

om

ain

ob

js

Page 29: Event Sourcing in less than 20 minutes - With Akka and Java 8

User

order

cart

Shop

data

User

order

cart

Shop

data

data

data

data

May not be accurate – probably isn’t

Page 30: Event Sourcing in less than 20 minutes - With Akka and Java 8

It’s not the things, it’s the things

that happen

#facts

Page 31: Event Sourcing in less than 20 minutes - With Akka and Java 8

Define with events

Page 32: Event Sourcing in less than 20 minutes - With Akka and Java 8

Example: Instead of a shop

service, events that happen within

the shopping context

Page 33: Event Sourcing in less than 20 minutes - With Akka and Java 8

What happens to our state?

Page 34: Event Sourcing in less than 20 minutes - With Akka and Java 8

First: A true story

Page 35: Event Sourcing in less than 20 minutes - With Akka and Java 8

Enter Event Sourcing

Page 36: Event Sourcing in less than 20 minutes - With Akka and Java 8

Shopping context

data data

eventsevents

events

Stateless Gateway

entity entity entity

state state state

Akka

Persistence commands

Page 37: Event Sourcing in less than 20 minutes - With Akka and Java 8

In Akka Persistence: key structures

Command

Events

State

EventsEvents

Page 38: Event Sourcing in less than 20 minutes - With Akka and Java 8

Inside your persistent Actor:

Page 39: Event Sourcing in less than 20 minutes - With Akka and Java 8

Anatomy:

Page 40: Event Sourcing in less than 20 minutes - With Akka and Java 8

Anatomy:

Page 41: Event Sourcing in less than 20 minutes - With Akka and Java 8

Recovery, a first class concept:

Page 42: Event Sourcing in less than 20 minutes - With Akka and Java 8

Why bring cqrs along?

Page 43: Event Sourcing in less than 20 minutes - With Akka and Java 8

The world revolves around events:

Page 44: Event Sourcing in less than 20 minutes - With Akka and Java 8

command

command

command

eventevent

eventeventevent

event

state Snapshots | EventsRead-side

representation

queries

Read-side processing

Read-only command

Page 45: Event Sourcing in less than 20 minutes - With Akka and Java 8

And if you need to share facts

outside of your context boundary…

Page 46: Event Sourcing in less than 20 minutes - With Akka and Java 8

command

command

command

eventevent

eventeventevent

event

state Snapshots | Events

distributed

pub-sub Read-only command

Read-side representation

queries

Read-side processing

Service A Service B

Page 47: Event Sourcing in less than 20 minutes - With Akka and Java 8

Technology Implications

• The storage system becomes an additive only architecture

• Append-only architectures distribute

• Far fewer locks to deal with

• Horizontal Partitioning is difficult for a relational model

Page 48: Event Sourcing in less than 20 minutes - With Akka and Java 8

Business Implications

• Criteria is tracked from inception as an event stream

• You can answer questions form the origin of the system

• You can answer questions not asked yet!

• Natural audit log

Page 49: Event Sourcing in less than 20 minutes - With Akka and Java 8

Questions?

Page 50: Event Sourcing in less than 20 minutes - With Akka and Java 8

An Open Source framework for building micro service systems

Page 51: Event Sourcing in less than 20 minutes - With Akka and Java 8

Rate of change with levels of

abstraction

Low level languages

High level languages

Libraries / packages

Application Frameworks

Application Networking

System Building Frameworks

Rate of change Levels of abstraction

Page 52: Event Sourcing in less than 20 minutes - With Akka and Java 8

Shopping context

data data

eventsevents

events

Stateless Gateway

entity entity entity

state state state

Akka

Persistence

is focused

in the entity

commands

Page 53: Event Sourcing in less than 20 minutes - With Akka and Java 8

Web Application Framework

Concurrency Toolkit \ Runtime

Thin, powerful abstraction over

Microservice system framework

Other curated tools,

architectural

principles and

patterns used for

building reactive

microservice systems

Page 54: Event Sourcing in less than 20 minutes - With Akka and Java 8

Enterprise Challenge

Build faster

Maintain architectural integrity as you move fast

Enterprise software development is a high stakes game

Success is high reward, failure is high risk

Page 55: Event Sourcing in less than 20 minutes - With Akka and Java 8

Too Many Choices

Analysis Paralysis

Challenge

Page 56: Event Sourcing in less than 20 minutes - With Akka and Java 8

Lagom makes reasonable choices for

you.

It’s built on message-driven technology

that’s highly responsive, elastic and fault-

tolerant.

It’s Reactive.

Page 57: Event Sourcing in less than 20 minutes - With Akka and Java 8

Lagom Components

Akka – Persistence, PubSub, Cluster, Streaming Async Services

Play Framework – Web framework

Cassandra – managing data persistence

Guice – dependency injection

SLF4J & Logback – Logging

Typesafe Config – configuration

JSON Serialization – Play JSON (Scala) Jackson (Java)

Service Gateway

Message Broker - Kafka

Page 58: Event Sourcing in less than 20 minutes - With Akka and Java 8

Difficult to Consistently Scale

Architectural Integrity

Challenge

Page 59: Event Sourcing in less than 20 minutes - With Akka and Java 8

Lagom consistently applies

patterns for Reactive System

Building

Page 60: Event Sourcing in less than 20 minutes - With Akka and Java 8

Architectural Principles Enforced by Lagom

Single Responsibility

Principle

Service Oriented

Architecture

EncapsulationHexagonal

Architecture

Page 61: Event Sourcing in less than 20 minutes - With Akka and Java 8

Asynchronous

& Non

Blocking

Event

Sourcing

Command

query

responsibility

segregation

Eventual

Consistency

Domain

Driven Design(Bounded Contexts,

Context Maps, Aggregate

Entities)

Service

Locator

Discovery

Edge Service

Lagom Design Patterns

Key Patterns

Required

when building

distributed

reactive

microservice

systems

Circuit

Breaker

Page 62: Event Sourcing in less than 20 minutes - With Akka and Java 8

Example: Event Sourcing

Page 63: Event Sourcing in less than 20 minutes - With Akka and Java 8

Experts Required

Challenge

Page 64: Event Sourcing in less than 20 minutes - With Akka and Java 8

Experts Required

You don’t need “Lagom developers” just Scala or Java developers

willing to apply Lagom semantics.

Page 65: Event Sourcing in less than 20 minutes - With Akka and Java 8

Must Protect Developer

Productivity

Challenge

Page 66: Event Sourcing in less than 20 minutes - With Akka and Java 8

Practical Productivity

Scala

&

Java API

Use Any IDE

Quick Start

w/ Batteries

Included

Hot Reload

& Single

Command

to Runall

Developer

productivity

is a core

feature of

the

Framework

Page 67: Event Sourcing in less than 20 minutes - With Akka and Java 8

Unplanned Complexity or Chaos

Challenge

Page 68: Event Sourcing in less than 20 minutes - With Akka and Java 8

Monolith

with many

moving

parts.

Page 69: Event Sourcing in less than 20 minutes - With Akka and Java 8

You cannot simply

break the seal and

expect everything

to function correctly

Page 70: Event Sourcing in less than 20 minutes - With Akka and Java 8

Lagom minimizes chaos through

isolation, autonomous services

Page 71: Event Sourcing in less than 20 minutes - With Akka and Java 8

Lagom Encourages and Enables

You to Contain Mutable State &

Publish Facts

Page 72: Event Sourcing in less than 20 minutes - With Akka and Java 8

Example:

State contained

Publish Facts

Page 73: Event Sourcing in less than 20 minutes - With Akka and Java 8

Lagom Allows You to Untangle

Read & Write Models with CQRS &

Event Sourcing

Page 74: Event Sourcing in less than 20 minutes - With Akka and Java 8

Lagom uses asynchronous

messaging, asynchronous IO &

distributed persistence patterns

Page 75: Event Sourcing in less than 20 minutes - With Akka and Java 8

client

co

mm

and

co

mm

and

node

nodenode

node

Reactive programmingRES

Tmessaging streaming

Stateless Behavior

node

Reactive programming

Stateful Entity

Write model

event log

DS

Event

(fact)Event

(fact)

Read model

query db

DS

Event

(fact)

Event

(fact)

Service

discovery

Service

gateway

Authentication

command

command

command

Reactive S

yste

m

Page 76: Event Sourcing in less than 20 minutes - With Akka and Java 8

All or Nothing

This and Everything

Challenge

Page 77: Event Sourcing in less than 20 minutes - With Akka and Java 8
Page 78: Event Sourcing in less than 20 minutes - With Akka and Java 8

Lagom & your Legacy

• Language agnostic communication with external services

• Safe Interaction with legacy systems via circuit breakers

• No need to tear everything down and start over

• Embraces polyglot systems

Page 79: Event Sourcing in less than 20 minutes - With Akka and Java 8

With Withoutor

Governance, Templates, Ref Apps,

Documentation, scaling architecture

expertise, etc.

Boot

Reactor

Kafka

CassandraNetflix

Patternsasynchronous REST

streaming CQRS

Event Sourcing

Eventual consistency

DDD

Service locator

Java

Eventuate

Axon

Spring Cloud …

Play

AkkaKafka

CassandraScala

Patternsasynchronous REST

streaming CQRS

Event Sourcing

Eventual consistency

DDD

Service locator

Java

Page 80: Event Sourcing in less than 20 minutes - With Akka and Java 8

In Summary

In order to keep up with the pace of change and

maintain architectural integrity as you move, try

using a framework that provides an abstraction

above the application level – at the system level.

….and what if you don’t?

Page 81: Event Sourcing in less than 20 minutes - With Akka and Java 8

Try Lagom• www.lagomframework.com , https://github.com/lagom

• Basic example “Hello World” -

https://www.lagomframework.com/get-started-scala.html

• More robust example “Online Auction” -

https://github.com/lagom/online-auction-scala

• Ask me for a demo:

[email protected]

• Twitter: @kikisworldrace

Page 82: Event Sourcing in less than 20 minutes - With Akka and Java 8

Questions?