SlideShare ist ein Scribd-Unternehmen logo
1 von 56
Downloaden Sie, um offline zu lesen
Actors or Not
Async Event Architectures
Yaroslav Tkachenko
Senior Software Engineer at Demonware (Activision)
Background
• 10 years in the industry
• ~1 year at Demonware/Activision, 5 years at Bench Accounting
• Mostly web, back-end, platform, infrastructure and data things
• @sap1ens / sap1ens.com
• Talk to me about data pipelines, stream processing and the Premier League ;-)
Two stories
Context: sync vs async communication
Service A Service B
POST /foo
service-b.example.com
“Easy” way – HTTP (RPC) API
Context: sync vs async communication
• Destination – where to send request?
• Service discovery
• Tight coupling
• Time – expect reply right away?
• Failure – always expect success?
• Retries
• Back-pressure
• Circuit breakers
You cannot make
synchronous requests
over the network
behave like local ones
Context: async communication styles
• Point-to-Point Channel
• One sender
• One receiver
• Publish-Subscribe Channel (Broadcast)
• One publisher
• Multiple subscribers
Context: Events vs Commands
• Event
• Simply a notification that something happened in the past
• Command
• Request to invoke some functionality (“RPC over messaging”)
• 469+ million gamers
• 3.2+ million concurrent online gamers
• 100+ games
• 300,000 requests per second at peak
• Average query response time of <.02 second
• 630,000+ metrics a minute
• 132 billion+ API calls per month
Demonware by the numbers
• Core game services including:
• Auth
• Matchmaking
• Leaderboards
• Marketplace
• Loot & Rewards
• Storage
• Etc.
• Erlang for networking layer, Python for application layer
• Still have a big application monolith, but slowly migrating to independent
services (SOA)
Demonware Back-end Services
• Lots of synchronous request/response communication between the monolith
and the services using:
• HTTP
• RPC
• The requesting process:
• conceptually knows which service it wants to call into
• is aware of the action that it is requesting, and its effects
• generally needs to be notified of the request’s completion and any
associated information before proceeding with its business logic
DW Services: Synchronous communication
• Using Domain Events
• Communication model assumes the following:
• The event may need to be handled by zero or more service processes,
each with different use cases; the process that generates the event does
not need to be aware of them
• The process that generates the event does not need to be aware of
what actions will be triggered, and what their effects might be
• The process that generates the event does not need to be notified of
the handlers’ completion before proceeding with its business logic
• Seamless integration with the Data Pipeline / Warehouse
DW Services: Asynchronous communication*
Domain Driven Design
Application
Core
Event
Adapter
Events
Commands
Events
HTTP
Adapter
CLI
Adapter
Kafka
Service
Kafka
Kafka
Publish-Subscribe OR Point-to-Point is a decision made by consumers
Kafka
• Service name is used as a topic name in Kafka
• Services have to explicitly subscribe to interested topics on startup (some
extra filtering is also supported)
• All messages are typically partitioned by a user ID to preserve order
Event Dispatcher
Application
Core
Event
Dispatcher
Kafka topic
Partitions
Kafka Python
Consumer
(librdkafka)
Local
buffer
queue
queue
queue
Tornado
Queues
Event Dispatcher
1 @demonata.event.source(
2 name='events_from_service_a'
3 )
4 class ServiceAEventsDispatcher (object):
5 def __init__(self, my_app_service):
6 self._app = my_app_service
7
8 @demonata.event.schema(
9 name='service.UserUpdated' ,
10 ge_version= '1.2.3',
11 event_dto=UserUpdated
12 )
13 def on_user_updated (self, message, event):
14 assert isinstance(message, DwPublishedEvent)
15 # ...
Publishing Events
The following reliability modes are supported:
• Fire and forget, relying on Kafka producer (acks = 0, 1, all)
• At least once (guaranteed), using remote EventStore backed by a DB
• At least once (intermediate), using local EventStore
Event Publisher
Application
Core
Event
Publisher
Kafka topic
PartitionsKafka Python
Producer
(librdkafka)
Event
Store Event
Producer
Publishing Events
1 @demonata.coroutine
2 def handle_event_atomically (self, event_to_process):
3 entity_key = self. determine_entity_key (event_to_process)
4 entity = self.db. read(entity_key)
5
6 some_data = yield self.perform_some_async_io_read ()
7 new_entity, new_event = self. apply_business_logic (
8 entity, event_to_process, some_data
9 )
10
11 # single-shard MySQL transaction:
12 with self.db. trans(shard_key=entity_key):
13 db.save(new_entity)
14 self.publisher. publish(new_event)
15 commit()
Event Framework in Demonware
• Decorator-driven consumers using callbacks
• Reliable producers
• Non-blocking IO using Tornado
• Apache Kafka as a transport
But still…
Can we do better?
1 @demonata.event.source(
2 name='events_from_service_a'
3 )
4 class ServiceAEventsDispatcher (object):
5 def __init__(self, my_app_service):
6 self._app = my_app_service
7
8 @demonata.event.schema(
9 name='service.UserUpdated' ,
10 ge_version= '1.2.3',
11 event_dto=UserUpdated
12 )
13 def on_user_updated (self, message, event):
14 assert isinstance(message, DwPublishedEvent)
15 # ...
Event Dispatcher
This is just
a boilerplate
Callback that
should pass
an event to
the actual
application
Can we create
producers and
consumers that support
message-passing
natively?
Actors
• Communicate with asynchronous messages instead of method invocations
• Manage their own state
• When responding to a message, can:
• Create other (child) actors
• Send messages to other actors
• Stop (child) actors or themselves
Actors
Actors: Erlang
1 loop() ->
2 receive
3 {From, Msg} ->
4 io:format("received ~p~n" , [Msg]),
5
6 From ! "got it";
7 end.
Actors: Akka
1 class MyActor extends Actor with ActorLogging {
2 def receive = {
3 case msg => {
4 log.info(s"received $msg" )
5
6 sender() ! "got it"
7 }
8 }
9 }
Actor-to-Actor communication
• Asynchronous and non-blocking message-passing
• Doesn’t mean senders must wait indefinitely - timeouts can be used
• Location transparency
• Enterprise Integration Patterns!
Bench Accounting
Bench Accounting Online Services
• Classic SAAS application used by the customers and internal bookkeepers:
• Double-entry bookkeeping with sophisticated reconciliation engine
and reporting [no external software]
• Receipt collection and OCR
• Integrations with banks, statement providers, Stripe, Shopify, etc.
• Enterprise Java monolith transitioning to Scala microservices (with Akka)
• Legacy event-based system built for notifications
Bench Accounting Legacy Eventing
• Multiple issues:
• Designed for a few specific use-cases, schema is not extendable
• Wasn’t built for microservices
• Tight coupling
• New requirements:
• Introduce real-time messaging (web & mobile)
• Add a framework for producing and consuming Domain Events and
Commands (both point-to-point and broadcasts)
• Otherwise very similar to the Demonware’s async communication
model
Bench Accounting Eventing System
ActiveMQ
Eventing
service
Service
A
Service
B
queue
queue
or
topic
IntegrationsEvent
store
ActiveMQ
Point-to-Point Publish-Subscribe
ActiveMQ
• Service name is used as a queue or topic name in ActiveMQ, but there is a also a
topic for global events
• Services can subscribe to interested queues or topics any time a new actor is
created
• Supports 3 modes of operations:
• Point-to-Point channel using a queue (perfect for Commands)
• Publish-Subscribe channel with guaranteed delivery using a Virtual topic
• Global Publish-Subscribe channel with guaranteed delivery using a Virtual
topic
Secret sauce: Apache Camel
• Integration framework that implements Enterprise Integration Patterns
• akka-camel is an official Akka library (now deprecated, Alpakka is a modern
alternative)
• Can be used with any JVM language
• “The most unknown coolest library out there”: JM (c)
Event Listener
akka-camel
ActiveMQ
queue or
topic
ActiveMQ
Consumer
prefetch
buffer
Actor
Event Listener
1 class CustomerService extends EventingConsumer {
2 def endpointUri = "activemq:Consumer.CustomerService.VirtualTopic.events"
3
4 def receive = {
5 case e: CamelMessage if e.isEvent && e.name == “some.event.name” => {
6 self ! DeleteAccount(e.clientId, sender())
7 }
8
9 case DeleteAccount(clientId, originalSender) => {
10 // ...
11 }
12 }
13 }
Event Sender
akka-camel ActiveMQ
queue or
topic
ActiveMQ
ProducerActor
Event Sender
1 // Broadcast
2 EventingClient
3 .buildSystemEvent (Event.BankError, userId, Component.ServiceA)
4 .send(true)
5
6 // Direct
7 EventingClient
8 .buildSystemEventWithAsset (Event.BankError, userId, Component.ServiceB)
9 .buildUrlAsset("http://example.com" )
10 .sendDirect("reporting")
Eventing Service
Event
Recorder
Event
Receiver
Event
Forwarder
Event
Reader
HTTP
API
Events
DAO
Event
Store
Integrations
ActiveMQqueue
ACK
Send
Receive
Eventing Service
So, we do we need this “router” service?
• Routing is handled in one place
• Lightweight consumers and producers
• The same Event Store is used for all services
Event framework in Bench Accounting
• Actor-based consumers and producers using Apache Camel
• Producer with ACKs
• Non-blocking IO
• Apache ActiveMQ as a transport
Lessons learned
So, Actors
• Semantics is important! Natural message-passing in Actors is a huge
advantage
• Asynchronous communication and location transparency by default makes it
easy to move actors between service boundaries
• We could also talk about supervision hierarchies and “Let it crash”
philosophy, excellent concurrency, networking features, etc… next time! You
can start with basics
Recommendations
• Domain Driven Design and Enterprise Integration Patterns are great!
• Understand your Domain space and choose the concepts you need to
support: Events, Commands, Documents or all of them
• Explicitly handle all possible failures. They will happen eventually
• Event Stores can be used for so many things! Tracing and debugging,
auditing, data analytics, etc.
• Actors or not? It really depends. It’s possible to build asynchronous,
non-blocking event frameworks in Java, Python, Node.js or a lot of the other
languages, but actors are asynchronous and message-based by default
Recommendations
• Carefully choose the transport layer. Apache Kafka can handle an impressive
scale, but many messaging features are missing / support just introduced
• Understand what you need to optimize: latency or throughput. You might
need to introduce multiple channels with different characteristics
• Do you really need exactly-once semantics?
• Message formats and schemas are extremely important! Choose binary
formats (Protobuf, Avro) AND/OR make sure to use a schema registry and
design a schema evolution strategy
• Consider splitting your messages into an envelope (metadata) and a payload.
Events and Commands could use the same envelope
Challenges
• We’re too attached to the synchronous request/response paradigm. It’s
everywhere - in the libraries, frameworks, standards. It takes time to learn
how to live in the asynchronous world
• High coupling will kill you. Routing is not a problem when you have a
handful of services (producers/consumers), but things get really complicated
with 10+ services. Try to avoid coupling by using Events as much as possible
and stay away from Commands unless you really need them
• Managing a properly partitioned, replicated and monitored message broker
cluster is still a non-trivial problem. Consider using managed services if your
Ops resources are limited
Challenges
• It’s very straightforward to implement event-based communication for
writes, but harder for reads. You’ll probably end up with some sort of DB
denormalization, in-memory hash join tables, caching or all of the above
• When you have dozens of producers and consumer scattered across the
service it becomes challenging to see the full picture. State and sequence
diagrams can help with capturing business use-cases, distributed tracing
becomes almost a must-have
• When things break you won’t notice them immediately without a proper
monitoring and alerting. Considering covering all critical business use-cases
first
That signup page...
Thanks
Davide Romani (Demonware)
Pavel Rodionov (Bench Accounting)
Questions?
@sap1ens | sap1ens.com

Weitere ähnliche Inhalte

Was ist angesagt?

Achieving a 50% Reduction in Cross-AZ Network Costs from Kafka (Uday Sagar Si...
Achieving a 50% Reduction in Cross-AZ Network Costs from Kafka (Uday Sagar Si...Achieving a 50% Reduction in Cross-AZ Network Costs from Kafka (Uday Sagar Si...
Achieving a 50% Reduction in Cross-AZ Network Costs from Kafka (Uday Sagar Si...
confluent
 

Was ist angesagt? (20)

Exactly-once Data Processing with Kafka Streams - July 27, 2017
Exactly-once Data Processing with Kafka Streams - July 27, 2017Exactly-once Data Processing with Kafka Streams - July 27, 2017
Exactly-once Data Processing with Kafka Streams - July 27, 2017
 
What is the State of my Kafka Streams Application? Unleashing Metrics. | Neil...
What is the State of my Kafka Streams Application? Unleashing Metrics. | Neil...What is the State of my Kafka Streams Application? Unleashing Metrics. | Neil...
What is the State of my Kafka Streams Application? Unleashing Metrics. | Neil...
 
Kafka Summit SF 2017 - Kafka Stream Processing for Everyone with KSQL
Kafka Summit SF 2017 - Kafka Stream Processing for Everyone with KSQLKafka Summit SF 2017 - Kafka Stream Processing for Everyone with KSQL
Kafka Summit SF 2017 - Kafka Stream Processing for Everyone with KSQL
 
ksqlDB: A Stream-Relational Database System
ksqlDB: A Stream-Relational Database SystemksqlDB: A Stream-Relational Database System
ksqlDB: A Stream-Relational Database System
 
Akka Microservices Architecture And Design
Akka Microservices Architecture And DesignAkka Microservices Architecture And Design
Akka Microservices Architecture And Design
 
KSQL Intro
KSQL IntroKSQL Intro
KSQL Intro
 
A Tour of Apache Kafka
A Tour of Apache KafkaA Tour of Apache Kafka
A Tour of Apache Kafka
 
Exactly-once Stream Processing with Kafka Streams
Exactly-once Stream Processing with Kafka StreamsExactly-once Stream Processing with Kafka Streams
Exactly-once Stream Processing with Kafka Streams
 
Webinar: Deep Dive on Apache Flink State - Seth Wiesman
Webinar: Deep Dive on Apache Flink State - Seth WiesmanWebinar: Deep Dive on Apache Flink State - Seth Wiesman
Webinar: Deep Dive on Apache Flink State - Seth Wiesman
 
KSQL Performance Tuning for Fun and Profit ( Nick Dearden, Confluent) Kafka S...
KSQL Performance Tuning for Fun and Profit ( Nick Dearden, Confluent) Kafka S...KSQL Performance Tuning for Fun and Profit ( Nick Dearden, Confluent) Kafka S...
KSQL Performance Tuning for Fun and Profit ( Nick Dearden, Confluent) Kafka S...
 
Exactly-Once Made Easy: Transactional Messaging Improvement for Usability and...
Exactly-Once Made Easy: Transactional Messaging Improvement for Usability and...Exactly-Once Made Easy: Transactional Messaging Improvement for Usability and...
Exactly-Once Made Easy: Transactional Messaging Improvement for Usability and...
 
Production Ready Kafka on Kubernetes (Devandra Tagare, Lyft) Kafka Summit SF ...
Production Ready Kafka on Kubernetes (Devandra Tagare, Lyft) Kafka Summit SF ...Production Ready Kafka on Kubernetes (Devandra Tagare, Lyft) Kafka Summit SF ...
Production Ready Kafka on Kubernetes (Devandra Tagare, Lyft) Kafka Summit SF ...
 
Achieving a 50% Reduction in Cross-AZ Network Costs from Kafka (Uday Sagar Si...
Achieving a 50% Reduction in Cross-AZ Network Costs from Kafka (Uday Sagar Si...Achieving a 50% Reduction in Cross-AZ Network Costs from Kafka (Uday Sagar Si...
Achieving a 50% Reduction in Cross-AZ Network Costs from Kafka (Uday Sagar Si...
 
What's the time? ...and why? (Mattias Sax, Confluent) Kafka Summit SF 2019
What's the time? ...and why? (Mattias Sax, Confluent) Kafka Summit SF 2019What's the time? ...and why? (Mattias Sax, Confluent) Kafka Summit SF 2019
What's the time? ...and why? (Mattias Sax, Confluent) Kafka Summit SF 2019
 
Apache kafka meet_up_zurich_at_swissre_from_zero_to_hero_with_kafka_connect_2...
Apache kafka meet_up_zurich_at_swissre_from_zero_to_hero_with_kafka_connect_2...Apache kafka meet_up_zurich_at_swissre_from_zero_to_hero_with_kafka_connect_2...
Apache kafka meet_up_zurich_at_swissre_from_zero_to_hero_with_kafka_connect_2...
 
Event sourcing - what could possibly go wrong ? Devoxx PL 2021
Event sourcing  - what could possibly go wrong ? Devoxx PL 2021Event sourcing  - what could possibly go wrong ? Devoxx PL 2021
Event sourcing - what could possibly go wrong ? Devoxx PL 2021
 
Top Ten Kafka® Configs
Top Ten Kafka® ConfigsTop Ten Kafka® Configs
Top Ten Kafka® Configs
 
KSQL and Security: The Current State of Affairs (Victoria Xia, Confluent) Kaf...
KSQL and Security: The Current State of Affairs (Victoria Xia, Confluent) Kaf...KSQL and Security: The Current State of Affairs (Victoria Xia, Confluent) Kaf...
KSQL and Security: The Current State of Affairs (Victoria Xia, Confluent) Kaf...
 
Introduction to Kafka Streams
Introduction to Kafka StreamsIntroduction to Kafka Streams
Introduction to Kafka Streams
 
UDF/UDAF: the extensibility framework for KSQL (Hojjat Jafapour, Confluent) K...
UDF/UDAF: the extensibility framework for KSQL (Hojjat Jafapour, Confluent) K...UDF/UDAF: the extensibility framework for KSQL (Hojjat Jafapour, Confluent) K...
UDF/UDAF: the extensibility framework for KSQL (Hojjat Jafapour, Confluent) K...
 

Ähnlich wie Actors or Not: Async Event Architectures

Building an Event-oriented Data Platform with Kafka, Eric Sammer
Building an Event-oriented Data Platform with Kafka, Eric Sammer Building an Event-oriented Data Platform with Kafka, Eric Sammer
Building an Event-oriented Data Platform with Kafka, Eric Sammer
confluent
 
HTML5 Server Sent Events/JSF JAX 2011 Conference
HTML5 Server Sent Events/JSF  JAX 2011 ConferenceHTML5 Server Sent Events/JSF  JAX 2011 Conference
HTML5 Server Sent Events/JSF JAX 2011 Conference
Roger Kitain
 

Ähnlich wie Actors or Not: Async Event Architectures (20)

Event-Based API Patterns and Practices
Event-Based API Patterns and PracticesEvent-Based API Patterns and Practices
Event-Based API Patterns and Practices
 
Guaranteed Event Delivery with Kafka and NodeJS | Amitesh Madhur, Nutanix
Guaranteed Event Delivery with Kafka and NodeJS | Amitesh Madhur, NutanixGuaranteed Event Delivery with Kafka and NodeJS | Amitesh Madhur, Nutanix
Guaranteed Event Delivery with Kafka and NodeJS | Amitesh Madhur, Nutanix
 
transactions-advanced for automatic payment.pptx
transactions-advanced for automatic payment.pptxtransactions-advanced for automatic payment.pptx
transactions-advanced for automatic payment.pptx
 
Realtime stream processing with kafka
Realtime stream processing with kafkaRealtime stream processing with kafka
Realtime stream processing with kafka
 
Azure Event Hubs - Behind the Scenes With Kasun Indrasiri | Current 2022
Azure Event Hubs - Behind the Scenes With Kasun Indrasiri | Current 2022Azure Event Hubs - Behind the Scenes With Kasun Indrasiri | Current 2022
Azure Event Hubs - Behind the Scenes With Kasun Indrasiri | Current 2022
 
Modern Cloud-Native Streaming Platforms: Event Streaming Microservices with K...
Modern Cloud-Native Streaming Platforms: Event Streaming Microservices with K...Modern Cloud-Native Streaming Platforms: Event Streaming Microservices with K...
Modern Cloud-Native Streaming Platforms: Event Streaming Microservices with K...
 
The server side story: Parallel and Asynchronous programming in .NET - ITPro...
The server side story:  Parallel and Asynchronous programming in .NET - ITPro...The server side story:  Parallel and Asynchronous programming in .NET - ITPro...
The server side story: Parallel and Asynchronous programming in .NET - ITPro...
 
[DSC Europe 23] Pramod Immaneni - Real-time analytics at IoT scale
[DSC Europe 23] Pramod Immaneni - Real-time analytics at IoT scale[DSC Europe 23] Pramod Immaneni - Real-time analytics at IoT scale
[DSC Europe 23] Pramod Immaneni - Real-time analytics at IoT scale
 
ADF and JavaScript - AMIS SIG, July 2017
ADF and JavaScript - AMIS SIG, July 2017ADF and JavaScript - AMIS SIG, July 2017
ADF and JavaScript - AMIS SIG, July 2017
 
How bol.com makes sense of its logs, using the Elastic technology stack.
How bol.com makes sense of its logs, using the Elastic technology stack.How bol.com makes sense of its logs, using the Elastic technology stack.
How bol.com makes sense of its logs, using the Elastic technology stack.
 
Oracle Drivers configuration for High Availability
Oracle Drivers configuration for High AvailabilityOracle Drivers configuration for High Availability
Oracle Drivers configuration for High Availability
 
Building an Event-oriented Data Platform with Kafka, Eric Sammer
Building an Event-oriented Data Platform with Kafka, Eric Sammer Building an Event-oriented Data Platform with Kafka, Eric Sammer
Building an Event-oriented Data Platform with Kafka, Eric Sammer
 
Writing Asynchronous Programs with Scala & Akka
Writing Asynchronous Programs with Scala & AkkaWriting Asynchronous Programs with Scala & Akka
Writing Asynchronous Programs with Scala & Akka
 
signalr
signalrsignalr
signalr
 
Keystone - ApacheCon 2016
Keystone - ApacheCon 2016Keystone - ApacheCon 2016
Keystone - ApacheCon 2016
 
HTML5 Server Sent Events/JSF JAX 2011 Conference
HTML5 Server Sent Events/JSF  JAX 2011 ConferenceHTML5 Server Sent Events/JSF  JAX 2011 Conference
HTML5 Server Sent Events/JSF JAX 2011 Conference
 
Building a system for machine and event-oriented data - SF HUG Nov 2015
Building a system for machine and event-oriented data - SF HUG Nov 2015Building a system for machine and event-oriented data - SF HUG Nov 2015
Building a system for machine and event-oriented data - SF HUG Nov 2015
 
Building a system for machine and event-oriented data with Rocana
Building a system for machine and event-oriented data with RocanaBuilding a system for machine and event-oriented data with Rocana
Building a system for machine and event-oriented data with Rocana
 
From a kafkaesque story to The Promised Land
From a kafkaesque story to The Promised LandFrom a kafkaesque story to The Promised Land
From a kafkaesque story to The Promised Land
 
OpenWhisk: Where Did My Servers Go?
OpenWhisk: Where Did My Servers Go?OpenWhisk: Where Did My Servers Go?
OpenWhisk: Where Did My Servers Go?
 

Mehr von Yaroslav Tkachenko

Dynamic Change Data Capture with Flink CDC and Consistent Hashing
Dynamic Change Data Capture with Flink CDC and Consistent HashingDynamic Change Data Capture with Flink CDC and Consistent Hashing
Dynamic Change Data Capture with Flink CDC and Consistent Hashing
Yaroslav Tkachenko
 
Быстрая и безболезненная разработка клиентской части веб-приложений
Быстрая и безболезненная разработка клиентской части веб-приложенийБыстрая и безболезненная разработка клиентской части веб-приложений
Быстрая и безболезненная разработка клиентской части веб-приложений
Yaroslav Tkachenko
 

Mehr von Yaroslav Tkachenko (13)

Dynamic Change Data Capture with Flink CDC and Consistent Hashing
Dynamic Change Data Capture with Flink CDC and Consistent HashingDynamic Change Data Capture with Flink CDC and Consistent Hashing
Dynamic Change Data Capture with Flink CDC and Consistent Hashing
 
Streaming SQL for Data Engineers: The Next Big Thing?
Streaming SQL for Data Engineers: The Next Big Thing?Streaming SQL for Data Engineers: The Next Big Thing?
Streaming SQL for Data Engineers: The Next Big Thing?
 
Apache Flink Adoption at Shopify
Apache Flink Adoption at ShopifyApache Flink Adoption at Shopify
Apache Flink Adoption at Shopify
 
Storing State Forever: Why It Can Be Good For Your Analytics
Storing State Forever: Why It Can Be Good For Your AnalyticsStoring State Forever: Why It Can Be Good For Your Analytics
Storing State Forever: Why It Can Be Good For Your Analytics
 
It's Time To Stop Using Lambda Architecture
It's Time To Stop Using Lambda ArchitectureIt's Time To Stop Using Lambda Architecture
It's Time To Stop Using Lambda Architecture
 
Bravo Six, Going Realtime. Transitioning Activision Data Pipeline to Streaming
Bravo Six, Going Realtime. Transitioning Activision Data Pipeline to StreamingBravo Six, Going Realtime. Transitioning Activision Data Pipeline to Streaming
Bravo Six, Going Realtime. Transitioning Activision Data Pipeline to Streaming
 
10 tips for making Bash a sane programming language
10 tips for making Bash a sane programming language10 tips for making Bash a sane programming language
10 tips for making Bash a sane programming language
 
Building Stateful Microservices With Akka
Building Stateful Microservices With AkkaBuilding Stateful Microservices With Akka
Building Stateful Microservices With Akka
 
Querying Data Pipeline with AWS Athena
Querying Data Pipeline with AWS AthenaQuerying Data Pipeline with AWS Athena
Querying Data Pipeline with AWS Athena
 
Why Actor-Based Systems Are The Best For Microservices
Why Actor-Based Systems Are The Best For MicroservicesWhy Actor-Based Systems Are The Best For Microservices
Why Actor-Based Systems Are The Best For Microservices
 
Why actor-based systems are the best for microservices
Why actor-based systems are the best for microservicesWhy actor-based systems are the best for microservices
Why actor-based systems are the best for microservices
 
Building Eventing Systems for Microservice Architecture
Building Eventing Systems for Microservice Architecture  Building Eventing Systems for Microservice Architecture
Building Eventing Systems for Microservice Architecture
 
Быстрая и безболезненная разработка клиентской части веб-приложений
Быстрая и безболезненная разработка клиентской части веб-приложенийБыстрая и безболезненная разработка клиентской части веб-приложений
Быстрая и безболезненная разработка клиентской части веб-приложений
 

Kürzlich hochgeladen

introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
VishalKumarJha10
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
masabamasaba
 

Kürzlich hochgeladen (20)

introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
SHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions PresentationSHRMPro HRMS Software Solutions Presentation
SHRMPro HRMS Software Solutions Presentation
 
Generic or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsGeneric or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisions
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Vancouver Psychic Readings, Attraction spells,Br...
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand%in Midrand+277-882-255-28 abortion pills for sale in midrand
%in Midrand+277-882-255-28 abortion pills for sale in midrand
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 

Actors or Not: Async Event Architectures

  • 1. Actors or Not Async Event Architectures Yaroslav Tkachenko Senior Software Engineer at Demonware (Activision)
  • 2. Background • 10 years in the industry • ~1 year at Demonware/Activision, 5 years at Bench Accounting • Mostly web, back-end, platform, infrastructure and data things • @sap1ens / sap1ens.com • Talk to me about data pipelines, stream processing and the Premier League ;-)
  • 4. Context: sync vs async communication Service A Service B POST /foo service-b.example.com “Easy” way – HTTP (RPC) API
  • 5. Context: sync vs async communication • Destination – where to send request? • Service discovery • Tight coupling • Time – expect reply right away? • Failure – always expect success? • Retries • Back-pressure • Circuit breakers
  • 6. You cannot make synchronous requests over the network behave like local ones
  • 7. Context: async communication styles • Point-to-Point Channel • One sender • One receiver • Publish-Subscribe Channel (Broadcast) • One publisher • Multiple subscribers
  • 8. Context: Events vs Commands • Event • Simply a notification that something happened in the past • Command • Request to invoke some functionality (“RPC over messaging”)
  • 9.
  • 10.
  • 11.
  • 12. • 469+ million gamers • 3.2+ million concurrent online gamers • 100+ games • 300,000 requests per second at peak • Average query response time of <.02 second • 630,000+ metrics a minute • 132 billion+ API calls per month Demonware by the numbers
  • 13. • Core game services including: • Auth • Matchmaking • Leaderboards • Marketplace • Loot & Rewards • Storage • Etc. • Erlang for networking layer, Python for application layer • Still have a big application monolith, but slowly migrating to independent services (SOA) Demonware Back-end Services
  • 14. • Lots of synchronous request/response communication between the monolith and the services using: • HTTP • RPC • The requesting process: • conceptually knows which service it wants to call into • is aware of the action that it is requesting, and its effects • generally needs to be notified of the request’s completion and any associated information before proceeding with its business logic DW Services: Synchronous communication
  • 15. • Using Domain Events • Communication model assumes the following: • The event may need to be handled by zero or more service processes, each with different use cases; the process that generates the event does not need to be aware of them • The process that generates the event does not need to be aware of what actions will be triggered, and what their effects might be • The process that generates the event does not need to be notified of the handlers’ completion before proceeding with its business logic • Seamless integration with the Data Pipeline / Warehouse DW Services: Asynchronous communication*
  • 17. Kafka
  • 18. Kafka Publish-Subscribe OR Point-to-Point is a decision made by consumers
  • 19. Kafka • Service name is used as a topic name in Kafka • Services have to explicitly subscribe to interested topics on startup (some extra filtering is also supported) • All messages are typically partitioned by a user ID to preserve order
  • 20. Event Dispatcher Application Core Event Dispatcher Kafka topic Partitions Kafka Python Consumer (librdkafka) Local buffer queue queue queue Tornado Queues
  • 21. Event Dispatcher 1 @demonata.event.source( 2 name='events_from_service_a' 3 ) 4 class ServiceAEventsDispatcher (object): 5 def __init__(self, my_app_service): 6 self._app = my_app_service 7 8 @demonata.event.schema( 9 name='service.UserUpdated' , 10 ge_version= '1.2.3', 11 event_dto=UserUpdated 12 ) 13 def on_user_updated (self, message, event): 14 assert isinstance(message, DwPublishedEvent) 15 # ...
  • 22. Publishing Events The following reliability modes are supported: • Fire and forget, relying on Kafka producer (acks = 0, 1, all) • At least once (guaranteed), using remote EventStore backed by a DB • At least once (intermediate), using local EventStore
  • 23. Event Publisher Application Core Event Publisher Kafka topic PartitionsKafka Python Producer (librdkafka) Event Store Event Producer
  • 24. Publishing Events 1 @demonata.coroutine 2 def handle_event_atomically (self, event_to_process): 3 entity_key = self. determine_entity_key (event_to_process) 4 entity = self.db. read(entity_key) 5 6 some_data = yield self.perform_some_async_io_read () 7 new_entity, new_event = self. apply_business_logic ( 8 entity, event_to_process, some_data 9 ) 10 11 # single-shard MySQL transaction: 12 with self.db. trans(shard_key=entity_key): 13 db.save(new_entity) 14 self.publisher. publish(new_event) 15 commit()
  • 25. Event Framework in Demonware • Decorator-driven consumers using callbacks • Reliable producers • Non-blocking IO using Tornado • Apache Kafka as a transport
  • 26. But still… Can we do better?
  • 27. 1 @demonata.event.source( 2 name='events_from_service_a' 3 ) 4 class ServiceAEventsDispatcher (object): 5 def __init__(self, my_app_service): 6 self._app = my_app_service 7 8 @demonata.event.schema( 9 name='service.UserUpdated' , 10 ge_version= '1.2.3', 11 event_dto=UserUpdated 12 ) 13 def on_user_updated (self, message, event): 14 assert isinstance(message, DwPublishedEvent) 15 # ... Event Dispatcher This is just a boilerplate Callback that should pass an event to the actual application
  • 28. Can we create producers and consumers that support message-passing natively?
  • 29. Actors • Communicate with asynchronous messages instead of method invocations • Manage their own state • When responding to a message, can: • Create other (child) actors • Send messages to other actors • Stop (child) actors or themselves
  • 31. Actors: Erlang 1 loop() -> 2 receive 3 {From, Msg} -> 4 io:format("received ~p~n" , [Msg]), 5 6 From ! "got it"; 7 end.
  • 32. Actors: Akka 1 class MyActor extends Actor with ActorLogging { 2 def receive = { 3 case msg => { 4 log.info(s"received $msg" ) 5 6 sender() ! "got it" 7 } 8 } 9 }
  • 33. Actor-to-Actor communication • Asynchronous and non-blocking message-passing • Doesn’t mean senders must wait indefinitely - timeouts can be used • Location transparency • Enterprise Integration Patterns!
  • 35. Bench Accounting Online Services • Classic SAAS application used by the customers and internal bookkeepers: • Double-entry bookkeeping with sophisticated reconciliation engine and reporting [no external software] • Receipt collection and OCR • Integrations with banks, statement providers, Stripe, Shopify, etc. • Enterprise Java monolith transitioning to Scala microservices (with Akka) • Legacy event-based system built for notifications
  • 36. Bench Accounting Legacy Eventing • Multiple issues: • Designed for a few specific use-cases, schema is not extendable • Wasn’t built for microservices • Tight coupling • New requirements: • Introduce real-time messaging (web & mobile) • Add a framework for producing and consuming Domain Events and Commands (both point-to-point and broadcasts) • Otherwise very similar to the Demonware’s async communication model
  • 37. Bench Accounting Eventing System ActiveMQ Eventing service Service A Service B queue queue or topic IntegrationsEvent store
  • 39. ActiveMQ • Service name is used as a queue or topic name in ActiveMQ, but there is a also a topic for global events • Services can subscribe to interested queues or topics any time a new actor is created • Supports 3 modes of operations: • Point-to-Point channel using a queue (perfect for Commands) • Publish-Subscribe channel with guaranteed delivery using a Virtual topic • Global Publish-Subscribe channel with guaranteed delivery using a Virtual topic
  • 40. Secret sauce: Apache Camel • Integration framework that implements Enterprise Integration Patterns • akka-camel is an official Akka library (now deprecated, Alpakka is a modern alternative) • Can be used with any JVM language • “The most unknown coolest library out there”: JM (c)
  • 42. Event Listener 1 class CustomerService extends EventingConsumer { 2 def endpointUri = "activemq:Consumer.CustomerService.VirtualTopic.events" 3 4 def receive = { 5 case e: CamelMessage if e.isEvent && e.name == “some.event.name” => { 6 self ! DeleteAccount(e.clientId, sender()) 7 } 8 9 case DeleteAccount(clientId, originalSender) => { 10 // ... 11 } 12 } 13 }
  • 43. Event Sender akka-camel ActiveMQ queue or topic ActiveMQ ProducerActor
  • 44. Event Sender 1 // Broadcast 2 EventingClient 3 .buildSystemEvent (Event.BankError, userId, Component.ServiceA) 4 .send(true) 5 6 // Direct 7 EventingClient 8 .buildSystemEventWithAsset (Event.BankError, userId, Component.ServiceB) 9 .buildUrlAsset("http://example.com" ) 10 .sendDirect("reporting")
  • 46. Eventing Service So, we do we need this “router” service? • Routing is handled in one place • Lightweight consumers and producers • The same Event Store is used for all services
  • 47. Event framework in Bench Accounting • Actor-based consumers and producers using Apache Camel • Producer with ACKs • Non-blocking IO • Apache ActiveMQ as a transport
  • 49. So, Actors • Semantics is important! Natural message-passing in Actors is a huge advantage • Asynchronous communication and location transparency by default makes it easy to move actors between service boundaries • We could also talk about supervision hierarchies and “Let it crash” philosophy, excellent concurrency, networking features, etc… next time! You can start with basics
  • 50. Recommendations • Domain Driven Design and Enterprise Integration Patterns are great! • Understand your Domain space and choose the concepts you need to support: Events, Commands, Documents or all of them • Explicitly handle all possible failures. They will happen eventually • Event Stores can be used for so many things! Tracing and debugging, auditing, data analytics, etc. • Actors or not? It really depends. It’s possible to build asynchronous, non-blocking event frameworks in Java, Python, Node.js or a lot of the other languages, but actors are asynchronous and message-based by default
  • 51. Recommendations • Carefully choose the transport layer. Apache Kafka can handle an impressive scale, but many messaging features are missing / support just introduced • Understand what you need to optimize: latency or throughput. You might need to introduce multiple channels with different characteristics • Do you really need exactly-once semantics? • Message formats and schemas are extremely important! Choose binary formats (Protobuf, Avro) AND/OR make sure to use a schema registry and design a schema evolution strategy • Consider splitting your messages into an envelope (metadata) and a payload. Events and Commands could use the same envelope
  • 52. Challenges • We’re too attached to the synchronous request/response paradigm. It’s everywhere - in the libraries, frameworks, standards. It takes time to learn how to live in the asynchronous world • High coupling will kill you. Routing is not a problem when you have a handful of services (producers/consumers), but things get really complicated with 10+ services. Try to avoid coupling by using Events as much as possible and stay away from Commands unless you really need them • Managing a properly partitioned, replicated and monitored message broker cluster is still a non-trivial problem. Consider using managed services if your Ops resources are limited
  • 53. Challenges • It’s very straightforward to implement event-based communication for writes, but harder for reads. You’ll probably end up with some sort of DB denormalization, in-memory hash join tables, caching or all of the above • When you have dozens of producers and consumer scattered across the service it becomes challenging to see the full picture. State and sequence diagrams can help with capturing business use-cases, distributed tracing becomes almost a must-have • When things break you won’t notice them immediately without a proper monitoring and alerting. Considering covering all critical business use-cases first
  • 55. Thanks Davide Romani (Demonware) Pavel Rodionov (Bench Accounting)