64
GOING SERVERLESS WITH CQRS ON AWS JavaDay Kyiv 10.2016 Anton Udovychenko

Going Serverless with CQRS on AWS

Embed Size (px)

Citation preview

Page 1: Going Serverless with CQRS on AWS

GOING SERVERLESS WITH CQRS ON AWSJavaDay Kyiv 10.2016

Anton Udovychenko

Page 2: Going Serverless with CQRS on AWS

ABOUT ME Software Architect @ Levi9 8+ years of Java experience AWS Certified Architect Passionate about agile methodology and clean code

https://www.linkedin.com/in/antonudovychenko

http://www.slideshare.net/antonudovychenko

Page 3: Going Serverless with CQRS on AWS

AGENDA•What is Serverless?• Event Sourcing• CQRS•Demo•Downsides of Serverless•Q&A

Page 4: Going Serverless with CQRS on AWS

MAIN SERVERLESS VENDORS

Page 5: Going Serverless with CQRS on AWS

EVERYTHING WAS A LIE

Serverless architecture has servers

Page 6: Going Serverless with CQRS on AWS

WHAT IS SERVERLESS Backend as a Service (rich frontend client)

Cognito DynamoDB SES

S3 Kinesis

Page 7: Going Serverless with CQRS on AWS

WHAT IS SERVERLESS Function as a Service

Cognito

DynamoDB

SES

S3

Kinesis

Lambda

Page 8: Going Serverless with CQRS on AWS

AWS LAMBDA AWS Lambda lets you run code without provisioning or managing servers. You pay only for the compute time you consume - there is no

charge when your code is not running.

Lambda

Page 9: Going Serverless with CQRS on AWS

AWS LAMBDA AWS Lambda lets you run code without provisioning or managing servers. You pay only for the compute time you consume - there is no

charge when your code is not running.

Lambda

Function as a unit of scale

Page 10: Going Serverless with CQRS on AWS

AWS LAMBDA AWS Lambda lets you run code without provisioning or managing servers. You pay only for the compute time you consume - there is no

charge when your code is not running.

Lambda

Function as a unit of scale Abstracts the language runtime

Page 11: Going Serverless with CQRS on AWS

AWS LAMBDA AWS Lambda lets you run code without provisioning or managing servers. You pay only for the compute time you consume - there is no

charge when your code is not running.

Lambda

Function as a unit of scale Abstracts the language runtime

Never pay for idle

Page 12: Going Serverless with CQRS on AWS

AWS LAMBDA AWS Lambda lets you run code without provisioning or managing servers. You pay only for the compute time you consume - there is no

charge when your code is not running.

≠ No OpsLambda

Page 13: Going Serverless with CQRS on AWS

STANDARD MONOLITHIC APPLICATION

Application Database

Page 14: Going Serverless with CQRS on AWS

SERVERLESS APPLICATION

Authentication service Database

API Gateway

Function 1

Function 2

Function N

Page 15: Going Serverless with CQRS on AWS

SERVERLESS APPLICATION WITH AWS

Cognito

API Gateway Database

Function 1

Function 2

Function N

Page 16: Going Serverless with CQRS on AWS

SERVERLESS APPLICATION WITH AWS

Cognito API Gateway

Database

Function 1

Function 2

Function N

Page 17: Going Serverless with CQRS on AWS

SERVERLESS APPLICATION WITH AWS

Cognito API Gateway

Lambda

Lambda…

Lambda

Database

Page 18: Going Serverless with CQRS on AWS

SERVERLESS APPLICATION WITH AWS

Lambda

Lambda

Lambda

…API GatewayCognito RDS

Page 19: Going Serverless with CQRS on AWS

AWS SERVERLESS FLAVOURS• Serverless backend

one function per REST API endpoint

• Event handlersone function per event type (scheduled, S3, Kinesis, DynamoDB,

SNS, etc)

• Mobile Logicone function per SDK call

Page 20: Going Serverless with CQRS on AWS

AWS LAMBDA EXAMPLE USE CASES• Scheduled infrastructure tasks (e. g. turn on/off EC2 on schedule)• Backup creation, validation• Log analysis on the fly• Processing of uploaded files• Regular calculation on the database• Filtering and transforming data on the fly

Page 21: Going Serverless with CQRS on AWS

MICROSERVICES

I HEARD YOU LIKE

Page 22: Going Serverless with CQRS on AWS

MICROSERVICES

I HEARD YOU LIKE

SERVERLESS IS THE NEXT LEVEL OF MICROSERVICES

Page 23: Going Serverless with CQRS on AWS

BENEFITS OF SERVERLESS• No server management

Page 24: Going Serverless with CQRS on AWS

BENEFITS OF SERVERLESS• No server management• Simplicity

Page 25: Going Serverless with CQRS on AWS

BENEFITS OF SERVERLESS• No server management• Simplicity• Time to market

Page 26: Going Serverless with CQRS on AWS

BENEFITS OF SERVERLESS• No server management• Simplicity• Time to market• Pay only for the compute you need

Page 27: Going Serverless with CQRS on AWS

BENEFITS OF SERVERLESS• No server management• Simplicity• Time to market• Pay only for the compute you need• Puts focus on code performance

Page 28: Going Serverless with CQRS on AWS

BENEFITS OF SERVERLESS• No server management• Simplicity• Time to market• Pay only for the compute you need• Puts focus on code performance• Good ecosystem

Page 29: Going Serverless with CQRS on AWS

BENEFITS OF SERVERLESS• No server management• Simplicity• Time to market• Pay only for the compute you need• Puts focus on code performance• Good ecosystem• Automatic horizontal scaling

Page 30: Going Serverless with CQRS on AWS
Page 31: Going Serverless with CQRS on AWS

EVENT SOURCING

Event Sourcing ensures that all changes to application state are stored as a sequence of events

Martin Fowler

Page 32: Going Serverless with CQRS on AWS

EVENT SOURCING

State is transient

We only store facts

Page 33: Going Serverless with CQRS on AWS

EVENT SOURCING

State is transient

We only store facts

State = (Events)

Page 34: Going Serverless with CQRS on AWS

EVENT SOURCING

State is transient

We only store facts

State = (Events)

Events (facts) are immutable:• No updates, no

deletes• Only appends• Non blocking• Store all information

Page 35: Going Serverless with CQRS on AWS

EVENT SOURCING EXAMPLE

id Events Time States1 - 13:01 20 available laptops

Warehouse event store

Page 36: Going Serverless with CQRS on AWS

EVENT SOURCING EXAMPLE

id Events Time States1 - 13:01 20 available laptops

2 Created order #33 by Order #1

(3 laptops)13:29 17 available and

3 reserved laptops

Warehouse event store

Page 37: Going Serverless with CQRS on AWS

EVENT SOURCING EXAMPLE

id Events Time States1 - 13:01 20 available laptops

2 Created order #33 by Order #1

(3 laptops)13:29 17 available and

3 reserved laptops

3 Created order #34 by Order #2

(2 laptops)13:31 15 available and

5 reserved laptops

Warehouse event store

Page 38: Going Serverless with CQRS on AWS

EVENT SOURCING EXAMPLE

id Events Time States1 - 13:01 20 available laptops

2 Created order #33 by Order #1

(3 laptops)13:29 17 available and

3 reserved laptops

3 Created order #34 by Order #2

(2 laptops)13:31 15 available and

5 reserved laptops

4 Order #33 confirmed 13:38 15 available and2 reserved and 3 bought laptops

Warehouse event store

Page 39: Going Serverless with CQRS on AWS

EVENT SOURCING EXAMPLE

id Events Time States1 - 13:01 20 available laptops

2 Created order #33 by Order #1

(3 laptops)13:29 17 available and

3 reserved laptops

3 Created order #34 by Order #2

(2 laptops)13:31 15 available and

5 reserved laptops

4 Order #33 confirmed 13:38 15 available and2 reserved and 3 bought laptops

5 Order #34 cancelled 13:39 17 available and 3 bought laptops

Warehouse event store

Page 40: Going Serverless with CQRS on AWS
Page 41: Going Serverless with CQRS on AWS

CQSCommand-Query Separation is a division of write functions and read functions

Page 42: Going Serverless with CQRS on AWS

CQSCommand-Query Separation is a division of write functions and read functions

interface OrderService { //commands void createOrder(Order order); void updateOrder(Order order); void deleteOrder(Long id);

//queries Order getOrder(Long id); List<Order> getOrders();}

Page 43: Going Serverless with CQRS on AWS

CQSCommand-Query Separation is a division of write functions and read functions

interface OrderService { //commands void createOrder(Order order); void updateOrder(Order order); void deleteOrder(Long id);

//queries Order getOrder(Long id); List<Order> getOrders();}

Write functions (Commands) must not return a value

Page 44: Going Serverless with CQRS on AWS

CQSCommand-Query Separation is a division of write functions and read functions

interface OrderService { //commands void createOrder(Order order); void updateOrder(Order order); void deleteOrder(Long id);

//queries Order getOrder(Long id); List<Order> getOrders();}

Write functions (Commands) must not return a value

Read functions (Queries) must have no side effects

Page 45: Going Serverless with CQRS on AWS

CQRS

CQRS (Command Query Responsibility Segregation) is simply the creation of two objects where there was previously only one

Greg Young

Page 46: Going Serverless with CQRS on AWS

CQRS CQRS is simply the creation of two objects where there was previously only one

Greg Young

interface OrderWriteService { void createOrder(Order order); void updateOrder(Order order); void deleteOrder(Long id);}

interface OrderReadService { Order getOrder(Long id); List<Order> getOrders();}

Page 47: Going Serverless with CQRS on AWS

CQRS BENEFITS• Separate Domain Models for Query and for Command• Eventually Consistent• Polyglot Persistence• Optimized for Reads!

Page 48: Going Serverless with CQRS on AWS

Query serviceQuery service

CQRS

UI

Query service

Command service

Messaging

Event Store

State StoreState StoreState Store

Command/Validation Error

Query / Result(DTO)

Event

Request

Page 49: Going Serverless with CQRS on AWS

Query serviceQuery service

CQRS DEMO

UI

Query service

Command service

Messaging

Event Store

State StoreState StoreState Store

Command/Validation Error

Query / Result(DTO)

Event

Request

Page 50: Going Serverless with CQRS on AWS

SERVERLESS CQRS DEMO ON AWS

S3 Event

Query / Result(DTO)

Event

Request

Lambda - Command

Lambda - Query

SQS – Message queue Lambda - Connector

S3

API Gateway

RDS – Event Store

DynamoDB – State Store

Page 51: Going Serverless with CQRS on AWS

DEMO TIME

Page 52: Going Serverless with CQRS on AWS

DRAWBACKS OF SERVERLESS

NO

Page 53: Going Serverless with CQRS on AWS

CURRENT DRAWBACKS OF SERVERLESS • Very young

Page 54: Going Serverless with CQRS on AWS

CURRENT DRAWBACKS OF SERVERLESS • Very young• Functional and Integrational testing

Page 55: Going Serverless with CQRS on AWS

CURRENT DRAWBACKS OF SERVERLESS • Very young• Functional and Integrational testing• Debugging

Page 56: Going Serverless with CQRS on AWS

CURRENT DRAWBACKS OF SERVERLESS • Very young• Functional and Integrational testing• Debugging• Stateless

Page 57: Going Serverless with CQRS on AWS

CURRENT DRAWBACKS OF SERVERLESS • Very young• Functional and Integrational testing• Debugging• Stateless• Startup latency

Page 58: Going Serverless with CQRS on AWS

CURRENT DRAWBACKS OF SERVERLESS • Very young• Functional and Integrational testing• Debugging• Stateless• Startup latency• Deployment versioning

Page 59: Going Serverless with CQRS on AWS

CURRENT DRAWBACKS OF SERVERLESS • Very young• Functional and Integrational testing• Debugging• Stateless• Startup latency• Deployment versioning• Logic split

Page 60: Going Serverless with CQRS on AWS

CURRENT DRAWBACKS OF SERVERLESS • Very young• Functional and Integrational testing• Debugging• Stateless• Startup latency• Deployment versioning• Logic split• Discovery

Page 61: Going Serverless with CQRS on AWS

CURRENT DRAWBACKS OF SERVERLESS • Very young• Functional and Integrational testing• Debugging• Stateless• Startup latency• Deployment versioning• Logic split• Discovery• Frameworks

Page 62: Going Serverless with CQRS on AWS

CURRENT DRAWBACKS OF SERVERLESS • Very young• Functional and Integrational testing• Debugging• Stateless• Startup latency• Deployment versioning• Logic split• Discovery• Frameworks

Page 64: Going Serverless with CQRS on AWS

THANK YOU

github.com/terrafant/aws-lambda-cqrs www.slideshare.net/antonudovychenko