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

Preview:

Citation preview

Kiki CarterEnterprise Architect

May 2017

Event Sourcing and CQRS in 20

minutes with Akka & 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

About me

Some things I like Some things I don’t like

IG: @thatskiki

Heights

Wild wildlife

Unintentional Sharing

These things

Why are we talking about this

anyway?

Not really *new* buzzwords

Legacy App Modernization(is a thing again)

Distributed Systems(for real this

time)

A system of

autonomous

collaborative distributed

services

AutonomyFrom Greek Auto-nomos:

auto meaning self

nomos meaning law

Leading the

way:

Promise

Theory

Think in Promises

not Obligations

“Autonomy makes information

local, leading to greater certainty

and stability”

- In Search for Certainty by Mark Burgess

No one can tell my story better than

I can…

Obligations diverge into unpredictable outcomes from definite beginnings⇒ decreased certainty

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

Decreased Certainty IRL

An Autonomous

Service can only

Promise its own

Behavior

How do we get there? (irl)

Data Center A

eCommerceserver

eCommerceserver

eCommerceserver

Database Database

Data Center B

eCommerceserver

eCommerceserver

eCommerceserver

Database Database

Heritage

Distributed

Systems

Enter Distributed Scaling

User

order

cart

Shop

data

User

order

cart

Shop

data

Scale by “stateless services”

Eat your broccoli!

I’m eating it!

I’m eating it!

I’m eating it!

User

order

cart

Shop

data

User

order

cart

Shop

data

data

data

data

But wait, there’s more!

User

order

cart

Shop

data

User

order

cart

Shop

data

Shar

ed d

om

ain

ob

js

User

order

cart

Shop

data

User

order

cart

Shop

data

data

data

dataShar

ed d

om

ain

ob

js

User

order

cart

Shop

data

User

order

cart

Shop

data

data

data

data

May not be accurate – probably isn’t

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

that happen

#facts

Define with events

Example: Instead of a shop

service, events that happen within

the shopping context

What happens to our state?

First: A true story

Enter Event Sourcing

Shopping context

data data

eventsevents

events

Stateless Gateway

entity entity entity

state state state

Akka

Persistence commands

In Akka Persistence: key structures

Command

Events

State

EventsEvents

Inside your persistent Actor:

Anatomy:

Anatomy:

Recovery, a first class concept:

Why bring cqrs along?

The world revolves around events:

command

command

command

eventevent

eventeventevent

event

state Snapshots | EventsRead-side

representation

queries

Read-side processing

Read-only command

And if you need to share facts

outside of your context boundary…

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

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

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

Questions?

An Open Source framework for building micro service systems

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

Shopping context

data data

eventsevents

events

Stateless Gateway

entity entity entity

state state state

Akka

Persistence

is focused

in the entity

commands

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

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

Too Many Choices

Analysis Paralysis

Challenge

Lagom makes reasonable choices for

you.

It’s built on message-driven technology

that’s highly responsive, elastic and fault-

tolerant.

It’s Reactive.

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

Difficult to Consistently Scale

Architectural Integrity

Challenge

Lagom consistently applies

patterns for Reactive System

Building

Architectural Principles Enforced by Lagom

Single Responsibility

Principle

Service Oriented

Architecture

EncapsulationHexagonal

Architecture

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

Example: Event Sourcing

Experts Required

Challenge

Experts Required

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

willing to apply Lagom semantics.

Must Protect Developer

Productivity

Challenge

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

Unplanned Complexity or Chaos

Challenge

Monolith

with many

moving

parts.

You cannot simply

break the seal and

expect everything

to function correctly

Lagom minimizes chaos through

isolation, autonomous services

Lagom Encourages and Enables

You to Contain Mutable State &

Publish Facts

Example:

State contained

Publish Facts

Lagom Allows You to Untangle

Read & Write Models with CQRS &

Event Sourcing

Lagom uses asynchronous

messaging, asynchronous IO &

distributed persistence patterns

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

All or Nothing

This and Everything

Challenge

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

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

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?

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:

• kiki@lightbend.com

• Twitter: @kikisworldrace

Questions?

Recommended