Fundamentals of
Event-Driven Microservices
Table of Contents
Part 1: Problems, Communication, and Event-Driven basics
Part 2: Event Modelling, Schemas, & Bootstrapping Domain Data
Part 3: Service Modelling, Using the DCL, & Examples
System Architectures
It’s all tradeoffs
Modern Problems
Big-Data Scale
(Near-) Real Time updates
Coupling
How do we handle these
issues in the real world?
First, let’s look at
Communication Structures
Communication Structures
• Business
• Implementation
• Data
Business Communication Structure
What do we do to
achieve our business goals?
Implementation Communication
Structure
How do we optimize the processes
that lead to our business goals?
Data Communication Structures
How do we get the data we need
for our business processes?
The Implementation
Communication Structure
Also, The Data
Communication Structure!
Ad-hoc Data Communication Structures
Synchronous Services can be
used to help solve these problems
Synchronous Services
Fan-Out
Scaling Issues
Boundary & Implementation Issues
Where do Synchronous Services work well?
Many companies succeed
with synchronous services!
It’s all about tradeoffs
What about Event-Driven
Architectures?
Events are Business Facts
Example
E-commerce
• Stores, Merchants, Products, Users, Accounts
Shipping
• Warehouses, Orders, Shipments, Drivers, Trucks
Product A has 100 units of stock
User A has placed an order, ID=1234
Merchant 100’s minimum order amount is $50
Publish the Facts
Single-Source of Truth
The Event-Broker stores all
events and event streams
The Event Stream
(Immutable Log)
Partitions for Scalability and Data Locality
Producer Responsibilities
Partitioning
Producer Responsibilities
Consistency
Single-Writer Principle
Single-Schema Principle
Consumer Responsibilities
Offsets
Consumer Responsibilities
State
Principle
Treat domain event data
as a first-class citizen
Does this mean everything should
be written to an event-stream?
No
Event Types & Streams
1) Entity
2) Keyed Event
3) Unkeyed Event
1) Entity
VIN MAKE MODEL COLOUR
A1 Ford F150 Tan
B2 Toyota Camry Gold
Cars
ID FIRST LAST
123 Adam Bellemare
444 Guy Incognito
People
Key Value
An entity has
a unique key
VIN is Key
Materialize Entities into Each Service
Each event is the
current state of
a (keyed) entity!
Partition 0
Overwrite
“9” with “2”
2) Keyed Events
VIN INFRACTION AMOUNT DATE DRIVER_ID
A1 Speeding $150 2018-10-07 123
A1 Parking $25 2018-11-11 123
B2 Parking $25 2018-11-13 444
Tickets Issued
Key Value
Multiple events
with same VIN
2) Keyed Events
VIN INFRACTION AMOUNT DATE DRIVER_ID
A1 Speeding $150 2018-10-07 123
A1 Parking $25 2018-11-11 123
B2 Parking $25 2018-11-13 444
Tickets Issued
Key Value
Multiple events
with same VIN
“This ticket belongs to VIN ID X”
Aggregating State from Keyed Events
Keyed by Shape
Partition 0
Partition 1
Instance 1
Instance 0
eg: Total
Ticket Costs
3) Unkeyed Events
LICENSE PLATE CAMERA_ID DATETIME IMAGE_URI
ZXJ123 123 2020-07-07… s3://…
ABC123 234 2020-07-08… hdfs://…
ACBZ900 345 2020-07-08… c:/program…
Intersection Traffic Camera
Value
3) Unkeyed Events
LICENSE PLATE CAMERA_ID DATETIME IMAGE_URI
ZXJ123 123 2020-07-07… s3://…
ABC123 234 2020-07-08… hdfs://…
ACBZ900 345 2020-07-08… c:/program…
Not that Common - Usually have a key!
Value
May be found in “dumb” data pipelining
A Simple Enrichment Example
Cars
Tickets
Ticket $
per Car
1) Materialize
1) Aggregate
2) Join
3) Emit
Break
Part 2: Event Modelling, Schemas,
& Bootstrapping Domain Data
“The fundamental problem of communication is that of
reproducing at one point, either exactly or approximately,
a message selected at another point.”
- Claude Shannon, Father of Communication Theory
Modelling Events using
Domain-Driven Design
Modelling Events
What business occurrence
does this event represent?
Event Modelling Example
SALES FACTS
FK ITEM_ID
FK STORE_ID
PRICE
PAYMENT
ITEM DIMENSIONS
PK ITEM_ID
NAME
BRAND
CATEGORY
STORE DIMENSIONS
PK STORE_ID
STORE_NAME
ADDRESS
CATEGORY
Entity Stream
Store Stream
Key: Store_Id
Event Stream
Sales Stream
Key: N/A
Entity Stream
Item Stream
Key: Item_Id
Event Modelling Example - Alternative
SALES FACTS
PK SALES_ID
FK ITEM_ID
FK STORE_ID
PRICE
PAYMENT
ITEM DIMENSIONS
PK ITEM_ID
NAME
BRAND
CATEGORY
STORE DIMENSIONS
PK STORE_ID
STORE_NAME
ADDRESS
CATEGORY
Entity Stream
Store Stream
Key: Store_Id
Entity Stream
Item Stream
Key: Item_Id
Sales Stream
Key: Sales_Id
Entity Stream
Event Modelling Example - Denormalized
SALES FACTS
PK SALES_ID
FK ITEM_ID
FK STORE_ID
PRICE
PAYMENT
ITEM DIMENSIONS
PK ITEM_ID
NAME
BRAND
CATEGORY
STORE DIMENSIONS
PK STORE_ID
STORE_NAME
ADDRESS
CATEGORY
Enriched Sales Stream
Key: Sales_Id
Merged Entity Stream
Join Join
Unstructured Data
A recipe for disaster
Unstructured Big Data
Real-life bloopers
isScreenOn: “yes” “no” 1 0 true false null
productId: null 1234 “null” “<null>” “%3cnull%3e”
premium: true false 4040 null “null”
Consumer Pain
Schemas
ID (INTEGER)
COLOUR (NULL,
STRING) DEFAULT: NULL
TIME (DATETIME)
100 Red 2020-08-08…
200 NULL 2020-08-08…
Producer Responsibility:
Create events that
match the schema
Schema Options
• Apache Avro
• Google’s Protobuf
• JSONSchema (not plain JSON!)
(Almost) self-documenting
{
"type" : "record",
"name" : "User",
"namespace" : "com.example",
"fields" : [
{"name": "username", "type": “string"},
{"name": "phone", "type": [null,"int"], "default" : null},
{"name": "address", "type": "string", "default": “UNKNOWN"},
{"name": "country", "type": "enum", "symbols": ["CA", "US", "OTHER"],
"doc" : "The user's country. Select other if not in list"}
]
}
Schema Evolution
Consuming & Converting Events
Backwards Compatible
Can convert a V2 event
to V3 format at Consumer
Forwards Compatible
Can convert a V3 event
to V2 format at Consumer
Full Transitive
Serialization & Transmission
Schema Registry
Schema Registry
Enable Data Discovery
Schema Breaking Change
Breaking Changes
New Streams
New offsets
Migrating consumers
Reprocessing
Halfway-Mark: Questions?
Event-Broker Options
Criteria
• Performance & Throughput
• Indefinite retention
• Tiered storage
• Security & Access Control
• Adoption & Open Source
• 3rd-party options and tools
Stream Retention
•Indefinite
•Time-based
•Size-based
Indefinite Retention
It’s just bytes on disk!
Tiered Storage
Put those bytes in the Cloud!
Security and Access Control
• Secure / Encrypted connections
• Data Encryption (Field Level Encryption!)
• Access Control Lists (ACLs)
• Enforce single-writer principle
• Track stream ownership and subscriptions
Adoption & Open Source
• Avoid vendor lock-in
• Hire talent
• Fix own issues
• Extended network
3rd Party Options & Tools
• Schema Registry
• Stream Access & Visualization Tooling
• Change-Data Capture Tooling
• Hosting
Options
Apache Kafka
Apache Pulsar
AWS Kinesis
Azure Event Hub
Google Pub Sub
Limited Retention
(2-52 Weeks)
Small Community
Tiered Storage
Tiered Storage
(OSS & Private)
Populating Event Streams
for Data Communication
What is it that we’re willing to do
to provide reliable domain data?
Principle:
Use the event-broker
as the single source of truth
Forked-Write Anti-Pattern
Independent
Data Store
Independent
Data Store
Forked-Write Anti-Pattern
Forked-Write Anti-Pattern
Can’t write to both data stores atomically!
Forked-Write Anti-Pattern
Forked-Write Anti-Pattern
Forked-Write Anti-Pattern
Forked-Write Anti-Pattern
Forked-Write Anti-Pattern
What else can we do?
Transactional Outbox
Transactional Outbox
Transactional Outbox
Transactional Outbox
Works!
But can be challenging to set up!
Pattern: Materialize after Write
Pattern: Materialize after Write
Question
Won’t these options slow down the application?
Liberating Data from Legacy Systems
Connectors & Change-Data Capture
Connectors & CDC Pros
Flexible Scalable
Single Source of Truth
Connectors & CDC Cons
Idiosyncracies
Overhead
Performance
Impact
Coupling on Internal
Data Model
Brittle & Reactive
It’s not one or the other!
Point-to-Point or Event-Driven?
Event Streams are part of the API
Questions?
Part 3: Service Modelling,
Using Event Streams, & Examples
The Microservice Tax
Event Stream Requirements
Paying the tax lets your users
focus on building the services
How do you model your services?
Use Domain-Driven Design (again)
Modelling Services
Build services around
business functions
Consider the Modular Monolith
Modelling Services
It’s an art as
much as a science
Eg Workflows built from Services
Let’s look at a subset
Payment Processing
Payment Processing
Payment Processing
KEY VALUE
OrderId List(ItemId)
CustomerInfo
KEY VALUE
OrderId List(ItemId)
CustomerInfo
PaymentResults
KEY VALUE
OrderId PaymentFailureInfo
PaymentAPIInfo
Payment Processing
KEY VALUE
OrderId List(ItemId)
CustomerInfo
KEY VALUE
OrderId List(ItemId)
CustomerInfo
KEY VALUE
OrderId PaymentInfo
KEY VALUE
OrderId PaymentResults
Service Decides Payment Failure
Try 3 times Failed 3 times
Payment Processing: Internal State
Payment Processing: Internal State
Restoring Internal State
Idempotent Processing
Reduce Complexity with Field-Level Encryption
Reduce Complexity with Field-Level Encryption
KEY VALUE
OrderId List(ItemId)
CustomerInfo
//Encrypted
PaymentInfo
(CC, Address, etc)
Only Authorized Services
can Decrypt the Fields!
Reduce Complexity with Field-Level Encryption
KEY VALUE
OrderId List(ItemId)
CustomerInfo
//Encrypted
PaymentInfo
(CC, Address, etc)
KEY VALUE
OrderId PaymentFailureInfo
PaymentAPIInfo
//Encrypted
PaymentResults
Only Authorized Services
can Decrypt the Fields!
Fulfillment Service Example
Expanded
Schemas
KEY VALUE
OrderId List(ItemId)
CustomerInfo
KEY VALUE
ItemId Map(Location,Quantity)
KEY VALUE
OrderId Location
FulfimentInfo
List(ItemId)
CustomerInfo
Fulfillment Workflow Pt.1
(Automated)
High-Priority Materialization!
Fulfillment Workflow Pt.2
(Manual)
Unfulfilled?
Compensation
KEY VALUE
FulfimentId Location
OrderId
List(ItemId)
CustomerInfo
KEY VALUE
OrderId State
List(ItemId)
CustomerInfo
Augmented Compensation Workflow
In Conclusion…
Event streams as the DCL
Event Streams: The Data Communication Layer
Business Communications
Services Align with Business
Questions, please!

fundamentalsofeventdrivenmicroservices11728489736099.pdf