Implementing Domain Event with Akka

download Implementing Domain Event with Akka

If you can't read please download the document

Transcript of Implementing Domain Event with Akka

Implementing Domain Event with Akka

Septeni Tech Talk - Jan 2016

TungNT @ Septeni Technology

Agenda

Introduction to Domain Event

Introduction to Akka

Implementation

Brief introduction to Reactive application

Domain Event

What is Domain Event?

Captures the memory of something interesting which affects the domain- Martin Fowler -

Monolith problems

Thread-based

Tight coupling~> hard to scale, especially rich domain

If you can't split it, you can't scale it

Event-driven

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 -

Event modeling

How can we determine if something that happens in the domain is important to the Domain Experts?

When domain experts talk:When...

If that happens...

Inform me if... and Notify me if...

An occurrence of...

Event modeling

How can we determine if something that happens in the domain is important to the Domain Experts?

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

Event properties

ImmutableNo shared state

Ansynchronous

Has no action/behaviorsjust a event pushed to the application

Benefit

High performance

Distributed

Maintainable

Durable

Loose couplingadd new feature without changes to existing code

easy to scale ~> scaling up or down as needed.

Introduction

Akka is a toolkit and runtime for building highly concurrent, distributed, resilient, message-driven applications on the JVM- Akka's homepage -

Akka highlight

Tool to build reactive application

Concurrent, distribution by design

High performance50 million msg/sec on a single machine

~2.5 million actors per GB of heap.

Elastic & DecentralizedLoad balancing

Routing

Partitioning and sharding.

Written by Scala

Implementation

Infra layer diagram

Event Abstraction

Event interface

Event subcriber interface

Message Broker Provider Factory

Factory to get multi provider

Akka EventBus

Singleton object to broadcast event and register event handler

Akka Actor

Foward message to message consummer

Domain layer diagram

Domain Event Bus

Get Akka message provider

Define event

Define an domain event inteface

Event class

Event Subscriber abstract class

Event handler

Event consummer with handler

Register subcriber to event bus

Sample Structure

Implementation's Drawback

Consistency

Deadletters processor

Duplicate events in bus

Event versioning

Distributed transaction

...

But easily resolved by:

Event SourcedAkka persistence

CQRS Architecture

Akka Cluster, DeadLetter libs

Apache Kafka written by Scala, too :)

Reactive Application

Manifesto

Reponsive react to users

Resillent react to failure

Elastic react to load

Message-driven react to events

Twitter during the WorldCup 2010

143,199 New Tweets per second (TPS).

Typical day: more than 500 million Tweets sent

Average 5,700 TPS.

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

Resources

Implementing Domain-Driven Design Vaughn Vernonhttp://www.amazon.com/Implementing-Domain-Driven-Design-Vaughn-Vernon/dp/0321834577

Resources(2)

Reactive Messaging Patterns with the Actor Model: Applications and Integration in Scala and Akka Vaughn Vernonhttp://www.amazon.com/Reactive-Messaging-Patterns-Actor-Model/dp/0133846830

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-how

Dwango Niconico: http://www.typesafe.com/resources/case-studies-and-stories/how-dwango-scaled-live-streaming-with-play-and-akka

Thank you!