Serverless architecture allows us to build and run applications with nearly no infrastructure configuration. Isn’t it a dream of a developer to concentrate on writing software and not be distracted by infrastructure duties?
CQRS and Event Sourcing are patterns which allow us to benefit from this approach.
In this talk we will dive into the world of Serverless computing for Java developers using Amazon Web Services and of course we will take a look at some existing pitfalls.
2. 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
3. AGENDA
• What is Serverless?
• Event Sourcing
• CQRS
• Demo
• Downsides of Serverless
• Q&A
6. WHAT IS SERVERLESS
Backend as a Service (rich frontend client)
Cognito DynamoDB SES
S3 Kinesis
7. WHAT IS SERVERLESS
Function as a Service
Cognito
DynamoDB
SES
S3
Kinesis
Lambda
8. 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
9. 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
10. 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
11. 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
12. 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 Ops
Lambda
19. AWS SERVERLESS FLAVOURS
• Serverless backend
one function per REST API endpoint
• Event handlers
one function per event type (scheduled, S3, Kinesis, DynamoDB, SNS, etc)
• Mobile Logic
one function per SDK call
20. 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
26. BENEFITS OF SERVERLESS
• No server management
• Simplicity
• Time to market
• Pay only for the compute you need
27. BENEFITS OF SERVERLESS
• No server management
• Simplicity
• Time to market
• Pay only for the compute you need
• Puts focus on code performance
28. BENEFITS OF SERVERLESS
• No server management
• Simplicity
• Time to market
• Pay only for the compute you need
• Puts focus on code performance
• Good ecosystem
29. 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
30.
31. EVENT SOURCING
Event Sourcing ensures that all changes to
application state are stored as a sequence of events
Martin Fowler
34. 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
36. EVENT SOURCING EXAMPLE
id Events Time States
1 - 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
37. EVENT SOURCING EXAMPLE
id Events Time States
1 - 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
38. EVENT SOURCING EXAMPLE
id Events Time States
1 - 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 and
2 reserved and 3 bought
laptops
Warehouse event store
39. EVENT SOURCING EXAMPLE
id Events Time States
1 - 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 and
2 reserved and 3 bought
laptops
5 Order #34 cancelled
13:39
17 available and 3 bought
laptops
Warehouse event store
42. CQS
Command-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();
}
43. CQS
Command-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
44. CQS
Command-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
45. CQRS
CQRS (Command Query Responsibility Segregation)
is simply the creation of two objects where there was
previously only one
Greg Young
46. 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();
}
47. CQRS BENEFITS
• Separate Domain Models for Query and for Command
• Eventually Consistent
• Polyglot Persistence
• Optimized for Reads!
54. CURRENT DRAWBACKS OF SERVERLESS
• Very young
• Functional and Integrational testing
55. CURRENT DRAWBACKS OF SERVERLESS
• Very young
• Functional and Integrational testing
• Debugging
56. CURRENT DRAWBACKS OF SERVERLESS
• Very young
• Functional and Integrational testing
• Debugging
• Stateless
57. CURRENT DRAWBACKS OF SERVERLESS
• Very young
• Functional and Integrational testing
• Debugging
• Stateless
• Startup latency
58. CURRENT DRAWBACKS OF SERVERLESS
• Very young
• Functional and Integrational testing
• Debugging
• Stateless
• Startup latency
• Deployment versioning
59. CURRENT DRAWBACKS OF SERVERLESS
• Very young
• Functional and Integrational testing
• Debugging
• Stateless
• Startup latency
• Deployment versioning
• Logic split
60. CURRENT DRAWBACKS OF SERVERLESS
• Very young
• Functional and Integrational testing
• Debugging
• Stateless
• Startup latency
• Deployment versioning
• Logic split
• Discovery
61. CURRENT DRAWBACKS OF SERVERLESS
• Very young
• Functional and Integrational testing
• Debugging
• Stateless
• Startup latency
• Deployment versioning
• Logic split
• Discovery
• Frameworks
62. CURRENT DRAWBACKS OF SERVERLESS
• Very young
• Functional and Integrational testing
• Debugging
• Stateless
• Startup latency
• Deployment versioning
• Logic split
• Discovery
• Frameworks
A serverless platform for building event-based microservices
horizontal scaling is completely automatic, elastic, and managed by the provider‘
horizontal scaling is completely automatic, elastic, and managed by the provider‘
horizontal scaling is completely automatic, elastic, and managed by the provider‘
horizontal scaling is completely automatic, elastic, and managed by the provider‘
horizontal scaling is completely automatic, elastic, and managed by the provider‘
horizontal scaling is completely automatic, elastic, and managed by the provider‘
horizontal scaling is completely automatic, elastic, and managed by the provider‘
Event is something that happened in the past
Event is something that happened in the past
Event is something that happened in the past
// CQS (Command-Query Separation) // Bertrand Meyer devised the CQS principle // It states that every method should either be a command that performs an action, or a // query that returns data to the caller, but not both. In other words, asking a question // should not change the answer. More formally, methods should return a value only if they // are referentially transparent and hence possess no side effects. //
// CQS (Command-Query Separation) // Bertrand Meyer devised the CQS principle // It states that every method should either be a command that performs an action, or a // query that returns data to the caller, but not both. In other words, asking a question // should not change the answer. More formally, methods should return a value only if they // are referentially transparent and hence possess no side effects. //
// CQS (Command-Query Separation) // Bertrand Meyer devised the CQS principle // It states that every method should either be a command that performs an action, or a // query that returns data to the caller, but not both. In other words, asking a question // should not change the answer. More formally, methods should return a value only if they // are referentially transparent and hence possess no side effects. //
// CQS (Command-Query Separation) // Bertrand Meyer devised the CQS principle // It states that every method should either be a command that performs an action, or a // query that returns data to the caller, but not both. In other words, asking a question // should not change the answer. More formally, methods should return a value only if they // are referentially transparent and hence possess no side effects. //
Lambdas cannot use functional/integration tests because internal methods cannot be called from external systems;
Lambdas cannot use functional/integration tests because internal methods cannot be called from external systems;
Lambdas cannot use functional/integration tests because internal methods cannot be called from external systems;
Lambdas cannot use functional/integration tests because internal methods cannot be called from external systems;
Lambdas cannot use functional/integration tests because internal methods cannot be called from external systems;
Lambdas cannot use functional/integration tests because internal methods cannot be called from external systems;
Lambdas cannot use functional/integration tests because internal methods cannot be called from external systems;
Lambdas cannot use functional/integration tests because internal methods cannot be called from external systems;
Lambdas cannot use functional/integration tests because internal methods cannot be called from external systems;
Lambdas cannot use functional/integration tests because internal methods cannot be called from external systems;