際際滷

際際滷Share a Scribd company logo
Implementing Domain Event with
Akka
Septeni Tech Talk - Jan 2016
TungNT @ Septeni Technology
tung_nt@septeni-technology.jp
Agenda
 Introduction to Domain Event
 Introduction to Akka
 Implementation
 Brief introduction to Reactive application
tung_nt@septeni-technology.jp
Domain Event
tung_nt@septeni-technology.jp
What is Domain Event?
Captures the memory of something interesting
which affects the domain
- Martin Fowler -
tung_nt@septeni-technology.jp
Monolith problems
 Thread-based
 Tight coupling
~> hard to scale, especially rich domain
tung_nt@septeni-technology.jp
If you can't split it, you can't scale it
tung_nt@septeni-technology.jp
Event-driven
tung_nt@septeni-technology.jp
It really became clear to me in the last couple of
years that we need a new building block and that
is the Domain Event.
- Eric Evans -
tung_nt@septeni-technology.jp
tung_nt@septeni-technology.jp
Event modeling
How can we determine if something that
happens in the domain is important to the
Domain Experts?
tung_nt@septeni-technology.jp
 When domain experts talk:
 When...
 If that happens...
 Inform me if... and Notify me if...
 An occurrence of...
tung_nt@septeni-technology.jp
Event modeling
How can we determine if something that
happens in the domain is important to the
Domain Experts?
tung_nt@septeni-technology.jp
Event name modeling
 Name and their properties depend on Ubiquitous
Language in Bounded Context
 Event usually derived from the command that was
executed:
Domain Event name = Aggregate name + Command name + past tense
 Ex:
 UserRegistered
 OrderCompleted
 ProjectStarted
tung_nt@septeni-technology.jp
Event properties
 Immutable
 No shared state
 Ansynchronous
 Has no action/behaviors
 just a event pushed to the application
tung_nt@septeni-technology.jp
Benefit
 High performance
 Distributed
 Maintainable
 Durable
 Loose coupling
 add new feature without changes to
existing code
 easy to scale ~> scaling up or down as
needed.
tung_nt@septeni-technology.jp
tung_nt@septeni-technology.jp
Introduction
Akka is a toolkit and runtime for building highly
concurrent, distributed, resilient, message-
driven applications on the JVM
- Akka's homepage -
tung_nt@septeni-technology.jp
Akka highlight
 Tool to build reactive application
 Concurrent, distribution by design
 High performance
 50 million msg/sec on a single machine
 ~2.5 million actors per GB of heap.
 Elastic & Decentralized
 Load balancing
 Routing
 Partitioning and sharding.
 Written by Scala
tung_nt@septeni-technology.jp
Implementation
tung_nt@septeni-technology.jp
Infra layer diagram
tung_nt@septeni-technology.jp
Event Abstraction
Event interface
Event subcriber interface
tung_nt@septeni-technology.jp
Message Broker Provider Factory
Factory to get multi provider
tung_nt@septeni-technology.jp
Akka EventBus
Singleton object to broadcast event and register
event handler
tung_nt@septeni-technology.jp
Akka Actor
Foward message to message consummer
tung_nt@septeni-technology.jp
Domain layer diagram
tung_nt@septeni-technology.jp
Domain Event Bus
Get Akka message provider
tung_nt@septeni-technology.jp
Define event
Define an domain event inteface
Event class
Event Subscriber abstract class
tung_nt@septeni-technology.jp
Event handler
Event consummer with handler
Register subcriber to event bus
tung_nt@septeni-technology.jp
Sample Structure
tung_nt@septeni-technology.jp
tung_nt@septeni-technology.jp
Implementation's Drawback
 Consistency
 Deadletters processor
 Duplicate events in bus
 Event versioning
 Distributed transaction
 ...
But easily resolved by:
 Event Sourced
 Akka persistence
 CQRS Architecture
 Akka Cluster, DeadLetter libs
 Apache Kafka  written by Scala, too :)
tung_nt@septeni-technology.jp
Reactive Application
tung_nt@septeni-technology.jp
Manifesto
 Reponsive  react to users
 Resillent  react to failure
 Elastic  react to load
 Message-driven  react to
events
tung_nt@septeni-technology.jp
Twitter during the WorldCup 2010
tung_nt@septeni-technology.jp
 143,199 New Tweets per second (TPS).
 Typical day: more than 500 million Tweets sent
 Average 5,700 TPS.
tung_nt@septeni-technology.jp
10x Performance
 Monolith (Ruby on Rails): 200  300 req / sec / host
 Reactive (Scala): 10 - 20K req / sec / host.
Few more examples
https://www.typesafe.com/resources/case-studies-and-stories
tung_nt@septeni-technology.jp
Resources
 Implementing Domain-Driven Design 
Vaughn Vernon
http://www.amazon.com/Implementing-Domain-Driven-Design-Vaughn-Vernon/dp/03218345
tung_nt@septeni-technology.jp
Resources(2)
 Reactive Messaging Patterns with the
Actor Model: Applications and Integration
in Scala and Akka  Vaughn Vernon
http://www.amazon.com/Reactive-Messaging-Patterns-Actor-Model/dp/0133846830
tung_nt@septeni-technology.jp
References
 Akka: http://akka.io/
 Reactive Manifesto: http://www.reactivemanifesto.org/
 Implementation:
https://github.com/NgTung/domain_event_sample.git
 Others:
 Twitter:
https://blog.twitter.com/2013/new-tweets-per-second-record-and-
 Dwango Niconico:
http://www.typesafe.com/resources/case-studies-and-stories/how
Thank you!

More Related Content

Implementing Domain Event with Akka