Serverless Architectural Patterns
@RafalWilinski
Cloud infra engineer @ X-Team
Serverless?
= Servicefull
Serverless Architectural Patterns
FAAS
Consequences
1. Stateless
2. Infinitely Scalable
3. Perfect for short jobs
4. Chainable/Composable
5. Responsive to events
also cheap (per per use) & fully managed
ENDPOINT
Endpoint pattern
Request
API Gateway
FaaS
Endpoint pattern
?
GET /products
GET /products/:id
GET /cart
GET /user
GET /categories
nanoServices
GET /products
GET /products/:id
GET /cart
GET /user
GET /user/:id
getAllProducts
getProductById
getCartContents
getUserDetails
getUserById
MICRoServices
GET /products
GET /products/:id
GET /cart
GET /user
GET /user/:id
productsService
- getAllProducts
- getProductById
cartService
- getCartContents
userService
- getUserDetails
- getUserById
Monolith
GET /products
GET /products/:id
GET /cart
GET /user
GET /user/:id
-getAllProducts
-getProductById
-getCart Contents
-getUserDetails
-getUserById
GraphQL Endpoint
POST /graphql
{
products(category: "book") {
name
description
price
}
}
AppSync GraphQL
endpoint
DynamoDB
complexity
customizability
Nanoservices
AppSync
Microservices
Monolith
Go Nanoservices if you need flexibility
- function level configuration
- separation of concerns, single responsibility principle
- better security
- smaller code size = faster deployments, faster cold boot
- more granular unit deployment
- probably most popular
Go Microservices if you need a bit less flexibility and less configuration burden
Don't waste time for Monoliths
AppSync for the win
- even frontend devs can handle it
- uses GraphQL = easier to handle on the frontend
- awesome out of the box
- robust queries (filtering, sorting, fuzzy searching etc.)
- realtime updates & offline support
- authorizers
- resolvers work magically with multiple data sources
Authorization
GET /me
Context
Principal + Policy
Allowed
Denied 403
Auth Function
DB
Migration Router
Gateway
Load Balancer
Legacy App
New Serverless App
Internal Endpoint
Client
Function
Internal Endpoint ASYNC
Client
Function
Queue
EVENT DRIVEN
SCHEDULED ACTION
CloudWatch Event
(scheduled every X hours)
Dump Table
Move S3 Images
3rd party API
...
Event-driven Encoder
S3 Bucket
on file uploaded
SQS Queue
receive message
retrieve original file, modify and re-upload
Encoder
THumbnailer w/ DB
S3 Bucket
on file uploaded
SQS Queue
receive message
retrieve original file, create thumbnail, upload to /thumbnails/:userId
Encoder
on thumbnail uploaded
SQS Queue
User Updater
receive message
update User where id = m.userId
DB Table
Collecting huge amounts of data
Events Collector
(e.g. analytics events)
Website 1
Gateway
Website 3
Website 2
Database
Collecting huge amounts of data
Events Collector
(e.g. analytics events)
Website 1
Gateway
Website 3
Website 2
Database
...
Error: Too many connections
SOLUTION: BUffer
Events Collector
Website 1
Gateway
Website 3
Website 2
Database
Injector
(batch size = 100)
SQS
HIGH Concurrence
Consequences
1. Stateless
2. Infinitely Scalable
3. Perfect for short jobs
4. Chainable/Composable
5. Responsive to events
also cheap (per per use) & fully managed
FAN OUT
Dispatcher
Workers
...
FAN OUT
Dispatcher & Reducer
Workers
...
FAN OUT Example
user.id = 1 - 100
API Callers
...
user.id = 100-200
user.id = 200-300
user.id = ...
Every hour
Get all users from db and Dispatch API call jobs
Crawling instagram N profiles and dumping them to s3 every hour
Database
API Call
S3
images
Images
API Call
Images
API Call
Images
API Call
Images
MAP-Reduce
Splitter
N Mappers
...
SQS
Reducer
Input Data
batch_size = n
Stream Processing
IoT Device
MQTT
Kinesis Firehose
Kinesis Stream
Transformer
IoT Pipeline
Database
Notifier
SNS Topic
Dump / Reporting
S3
AppSync
Kinesis Firehose
Kinesis Stream
Deduplication
Deduplication
Database with TTL
Input Stream
Final DB
👋 ThaNKS!
@rAFALwILINSKI
Serverless Architectural Patterns
By Rafal Wiliński
Serverless Architectural Patterns
- 388