Asynchronicity in Microservices
Three Main Forms of Asynchronicity – Understanding the Patterns
O’Reilly SACon, London, October 2018
Irakli Nadareishvili
Sr. Director, Shared Technology, Capital One.
Irakli Nadareishvili
Senior Director, SharedTech, Capital One
• API & Microservices Governance
• Capital One’s Developer Tooling
• Co-author of “Microservice Architecture”
inadarei
11
“Speed and Safety at Scale and in Harmony”
Need for coordination is why we constantly
compromise between speed and safety.
Microservices Way:
inadarei
Microservicesclosethealignmentloop!
AgileProcess:
Operations:
Product:
DevOps & CI/CID
Lean Product Dev
Pattern: Thinking In Small Batches
Architecture: Microservices
inadarei
Data:Batch-sizeIsaStrongPredictorofPerformance
Source: Accelerate, by Nicole Forsgren, PhD, Jez Humble, and Gene Kim
6Confidential
Async + #Microservices =
Internet’s favorite pastime
7
There is actually more than
one type of asynchronicity,
and the proper usage patterns
are significantly different,
when and if applied properly.
Asynchronous Flows
Data Streams
Event-Sourcing (and CQRS)
AGENDA
9
1. Asynchronous Flows
1010Confidential
Any type of asynchronous job execution, where
caller doesn’t have to wait for a response in a
blocking way, increases scalability and resiliency
of the system, granted: typically at the expense
of increased complexity.
Common solutions:
• Promises, async/await & coroutines
• Publish/Subscribe Queues
• Webhooks
• Reactive Programming
• Akka Actor Model
Promises (Node.js)
11Example code from: http://promises123.com.
Async/Await (Python)
12Example code from: http://promises123.com.
Goroutines (Go)
13Example code from: http://promises123.com.
Reactive Programming
14
Higher-level abstraction over messaging using Observables and functional programming.
Publish/Subscribe Queues
15
Senders generate events, receivers are workers able and interested in processing those
Graphics source: https://docs.microsoft.com/en-us/previous-versions/msp-n-p/dn589781(v=pandp.10)
Pub/Sub provides significantly superior level of flexibility, in
that: multiple kinds of receivers, implemented with
heterogenous tech stack can subscribe to the same events
using a uniform approach and implementation.
Example:
1. Message: loan application submitted
2. Received by fraud-check worker
3. Received by credit-check worker
4. Received by AML-check worker
5. Received by OFAC-check worker
6. …
Webhooks – HTTP Callbacks with Payload
16
W3C Standard: WebSub https://www.w3.org/TR/websub/
Graphics source: https://www.w3.org/TR/websub/#high-level-protocol-flow
WebSub allows abstracting Pub/Sub model away from
specific, non-web protocols used by concrete message
broker systems and projecting the interactions onto the
standard HTTP layer, with main benefit being ubiquitous
familiarity with the protocol, built-in caching and internet-
level scalability features.
Akka Actor Model
17
Actor Model-based abstraction for concurrent and distributed systems, primarily in Scala and Java
Diagram source: https://doc.akka.io/docs/akka/2.5/guide/actors-intro.html
“Everything is an Actor” model to concurrency by
Carl Hewitt (1973).
Actor is an entity that, in response to a
message, can:
1. Send messages to other actors
2. Create new actors
3. Define message-processing
behavior.
Akka Actor Model - Clustering
18
Akka provides gossip-based HA cluster impl. with advanced features like sharding & persistence.
Diagram source: https://doc.akka.io/docs/akka/2.5/common/cluster.html#intro
19
2. Data Streams
Data Analytics & Machine Learning Applications
20
Could be powered by Kafka Streams configured/optimized differently from their pub/sub usage
Diagram source: https://www.confluent.io/blog/build-deploy-scalable-machine-learning-production-apache-kafka/
Data Lake
21
Business-relevant data streamed into a Data Lake from all data/event sources
Diagram source: https://aws.amazon.com/big-data/datalakes-and-analytics/what-is-a-data-lake/
22
3. Event Sourcing (and CQRS)
Command Query Responsibility Segregation (CQRS)
23
Separating actions (state changes) from perceptions (reads, notification) in system architecture
EventStore
Alexa
VoiceSkill
BFFAPI
…
Microservice 1
…
…
Microservice 2
…
…
Microservice 4
…
MobileWebApp
…
Microservice 3
…
BFFAPIBFFAPI
Topic 1
Topic 2.1
Topic 2.2
Topic 4.1
Topic 4.2
Topic 4.3
Subscribe
publish
publish
publish
Subscribe
Subscribe
Subscribe
Query
Query
Query
Query
24
Event-Sourcing Explained
25
“Current state”-oriented, relational model – favors data-sharing, toxic for microservices
Customer Demographics
Credit-Worthiness Data
Deposits (10)
Accounts (3)
Withdrawals (10)
Documents (7)
Event-Sourcing Explained
26
Events-Based Data Model – solves data-sharing issues in Microservices
Credit Data
Received
Customer
Created
Checking
Account
Added
Credit Card
Acc. Added
Mortgage Acc.
Added
Money
Deposited
Money
Deposited
Money
Withdrawn
Money
Withdrawn
Money
Withdrawn
Document
Added
Money
Deposited
Money
Deposited
Document
Generated
Money
Withdrawn
Document
Added
Money
Withdrawn
Event “Shape”
27
Source: https://eventstore.org/docs/introduction/
Projections
28
Give us current state based on events, and are as simple as: function (state, event)
“In Event Sourcing, current state is a left-fold of previous behaviors” – Greg Young
Tooling
29
Event Store:
1. EventStore https://eventstore.org/
2. Cassandra
3. Any DB capable of storing many
rows of data .
Read Model:
1. ElasticSearch
2. PostgreSQL
3. Anything optimized for the kind of queries you want to run.
Command Query Responsibility Segregation (CQRS)
30
Separating actions (state changes) from perceptions (reads, notification) in system architecture
EventStore
Alexa
VoiceSkill
BFFAPI
…
Microservice 1
…
…
Microservice 2
…
…
Microservice 4
…
MobileWebApp
…
Microservice 3
…
BFFAPIBFFAPI
Topic 1
Topic 2.1
Topic 2.2
Topic 4.1
Topic 4.2
Topic 4.3
Subscribe
publish
publish
publish
Subscribe
Subscribe
Subscribe
Query
Query
Query
Query
Asynchronous Flows
Data Streams
Event-Sourcing (and CQRS)
ASYNC TYPES
inadarei
11
Questions?
Thank you!
inadarei

Irakli Nadareishvili - O'Reilly SACon 2018, London

  • 1.
    Asynchronicity in Microservices ThreeMain Forms of Asynchronicity – Understanding the Patterns O’Reilly SACon, London, October 2018 Irakli Nadareishvili Sr. Director, Shared Technology, Capital One.
  • 2.
    Irakli Nadareishvili Senior Director,SharedTech, Capital One • API & Microservices Governance • Capital One’s Developer Tooling • Co-author of “Microservice Architecture” inadarei
  • 3.
    11 “Speed and Safetyat Scale and in Harmony” Need for coordination is why we constantly compromise between speed and safety. Microservices Way: inadarei
  • 4.
    Microservicesclosethealignmentloop! AgileProcess: Operations: Product: DevOps & CI/CID LeanProduct Dev Pattern: Thinking In Small Batches Architecture: Microservices inadarei
  • 5.
    Data:Batch-sizeIsaStrongPredictorofPerformance Source: Accelerate, byNicole Forsgren, PhD, Jez Humble, and Gene Kim
  • 6.
    6Confidential Async + #Microservices= Internet’s favorite pastime
  • 7.
    7 There is actuallymore than one type of asynchronicity, and the proper usage patterns are significantly different, when and if applied properly.
  • 8.
  • 9.
  • 10.
    1010Confidential Any type ofasynchronous job execution, where caller doesn’t have to wait for a response in a blocking way, increases scalability and resiliency of the system, granted: typically at the expense of increased complexity. Common solutions: • Promises, async/await & coroutines • Publish/Subscribe Queues • Webhooks • Reactive Programming • Akka Actor Model
  • 11.
    Promises (Node.js) 11Example codefrom: http://promises123.com.
  • 12.
    Async/Await (Python) 12Example codefrom: http://promises123.com.
  • 13.
    Goroutines (Go) 13Example codefrom: http://promises123.com.
  • 14.
    Reactive Programming 14 Higher-level abstractionover messaging using Observables and functional programming.
  • 15.
    Publish/Subscribe Queues 15 Senders generateevents, receivers are workers able and interested in processing those Graphics source: https://docs.microsoft.com/en-us/previous-versions/msp-n-p/dn589781(v=pandp.10) Pub/Sub provides significantly superior level of flexibility, in that: multiple kinds of receivers, implemented with heterogenous tech stack can subscribe to the same events using a uniform approach and implementation. Example: 1. Message: loan application submitted 2. Received by fraud-check worker 3. Received by credit-check worker 4. Received by AML-check worker 5. Received by OFAC-check worker 6. …
  • 16.
    Webhooks – HTTPCallbacks with Payload 16 W3C Standard: WebSub https://www.w3.org/TR/websub/ Graphics source: https://www.w3.org/TR/websub/#high-level-protocol-flow WebSub allows abstracting Pub/Sub model away from specific, non-web protocols used by concrete message broker systems and projecting the interactions onto the standard HTTP layer, with main benefit being ubiquitous familiarity with the protocol, built-in caching and internet- level scalability features.
  • 17.
    Akka Actor Model 17 ActorModel-based abstraction for concurrent and distributed systems, primarily in Scala and Java Diagram source: https://doc.akka.io/docs/akka/2.5/guide/actors-intro.html “Everything is an Actor” model to concurrency by Carl Hewitt (1973). Actor is an entity that, in response to a message, can: 1. Send messages to other actors 2. Create new actors 3. Define message-processing behavior.
  • 18.
    Akka Actor Model- Clustering 18 Akka provides gossip-based HA cluster impl. with advanced features like sharding & persistence. Diagram source: https://doc.akka.io/docs/akka/2.5/common/cluster.html#intro
  • 19.
  • 20.
    Data Analytics &Machine Learning Applications 20 Could be powered by Kafka Streams configured/optimized differently from their pub/sub usage Diagram source: https://www.confluent.io/blog/build-deploy-scalable-machine-learning-production-apache-kafka/
  • 21.
    Data Lake 21 Business-relevant datastreamed into a Data Lake from all data/event sources Diagram source: https://aws.amazon.com/big-data/datalakes-and-analytics/what-is-a-data-lake/
  • 22.
  • 23.
    Command Query ResponsibilitySegregation (CQRS) 23 Separating actions (state changes) from perceptions (reads, notification) in system architecture EventStore Alexa VoiceSkill BFFAPI … Microservice 1 … … Microservice 2 … … Microservice 4 … MobileWebApp … Microservice 3 … BFFAPIBFFAPI Topic 1 Topic 2.1 Topic 2.2 Topic 4.1 Topic 4.2 Topic 4.3 Subscribe publish publish publish Subscribe Subscribe Subscribe Query Query Query Query
  • 24.
  • 25.
    Event-Sourcing Explained 25 “Current state”-oriented,relational model – favors data-sharing, toxic for microservices Customer Demographics Credit-Worthiness Data Deposits (10) Accounts (3) Withdrawals (10) Documents (7)
  • 26.
    Event-Sourcing Explained 26 Events-Based DataModel – solves data-sharing issues in Microservices Credit Data Received Customer Created Checking Account Added Credit Card Acc. Added Mortgage Acc. Added Money Deposited Money Deposited Money Withdrawn Money Withdrawn Money Withdrawn Document Added Money Deposited Money Deposited Document Generated Money Withdrawn Document Added Money Withdrawn
  • 27.
  • 28.
    Projections 28 Give us currentstate based on events, and are as simple as: function (state, event) “In Event Sourcing, current state is a left-fold of previous behaviors” – Greg Young
  • 29.
    Tooling 29 Event Store: 1. EventStorehttps://eventstore.org/ 2. Cassandra 3. Any DB capable of storing many rows of data . Read Model: 1. ElasticSearch 2. PostgreSQL 3. Anything optimized for the kind of queries you want to run.
  • 30.
    Command Query ResponsibilitySegregation (CQRS) 30 Separating actions (state changes) from perceptions (reads, notification) in system architecture EventStore Alexa VoiceSkill BFFAPI … Microservice 1 … … Microservice 2 … … Microservice 4 … MobileWebApp … Microservice 3 … BFFAPIBFFAPI Topic 1 Topic 2.1 Topic 2.2 Topic 4.1 Topic 4.2 Topic 4.3 Subscribe publish publish publish Subscribe Subscribe Subscribe Query Query Query Query
  • 31.
    Asynchronous Flows Data Streams Event-Sourcing(and CQRS) ASYNC TYPES inadarei
  • 32.