These are slides of talk made at https://www.agilemovement.it/workingsoftware/schedule.html. In this talks I'll show how the concepts of DDD like aggregate, domains events, read model, command, CQRS and so on love the idea of Actor Model and how a languages like Elixir that is built on a real Actor Model VM like the BEAM, is very powerful to build software that take care of DDD/CQRS/ES.
Human Factors of XR: Using Human Factors to Design XR Systems
DDD loves Actor Model and Actor Model loves Elixir
1. DDD ❤️ Actor
Model and Actor
Model ❤️ Elixir
Everybody needs somebody to love
2. DDD ❤️ Actor ❤️ Elixir
GPad
Born to be a developer with an interest in distributed system.
I have developed with many languages like C++, C#, js and ruby. I had fallen in
love with functional programming, especially with elixir and erlang.
● Twitter: https://twitter.com/gpad619
● Github: https://github.com/gpad/
● Medium: https://medium.com/@gpad
CTO & founder of coders51
3. DDD ❤️ Actor ❤️ Elixir
Schedule
What is DDD?
What is Actor Model?
What is Elixir?
Why they love each other?
Examples
5. DDD ❤️ Actor ❤️ Elixir
What is DDD?
From wikipedia:
Domain-driven design (DDD) is an approach to software development for
complex needs by connecting the implementation to an evolving model.
The premise of domain-driven design is the following:
● placing the project's primary focus on the core domain and domain logic;
● basing complex designs on a model of the domain;
● initiating a creative collaboration between technical and domain experts
to iteratively refine a conceptual model that addresses particular domain
problems.
6. DDD ❤️ Actor ❤️ Elixir
Aggregate
Commands & Events
Process Manager/Policy
Read Model
What is DDD? - Building Blocks
7. DDD ❤️ Actor ❤️ Elixir
Aggregate
A collection of objects that are bound together by a root entity, otherwise
known as an aggregate root. The aggregate root guarantees the consistency
of changes being made within the aggregate by forbidding external objects
from holding references to its members.
Change State
Keep transaction consistency
Transaction boundary
8. DDD ❤️ Actor ❤️ Elixir
Commands
Actions that can be executed on an Aggregate.
They can fail and they produce a result.
They often (always ?!?) produce an event.
The Aggregate emits events.
9. DDD ❤️ Actor ❤️ Elixir
Events
Data emitted by Aggregate when it changes state.
It’s something that has already happened.
They can only be listened.
Who does listen the events?
10. DDD ❤️ Actor ❤️ Elixir
Process Manager (Policy)
The Process Manager listens the events and decide what to do.
It’s often where the logic resides.
It generates commands on Aggregate using its internal “policy”.
11. DDD ❤️ Actor ❤️ Elixir
Read Model
Projection of data that can be used to read data.
It listens the events and decide to build a projection of different events.
It can be always rebuild using the passed events.
It’s a projection!!!
12. DDD ❤️ Actor ❤️ Elixir
DDD - Recap
We model our domain using Aggregate that change state in a “transactional
way”.
We use the “command” to change the state of the aggregate in a transactional
way.
When the Aggregate changes the state it emits one or more events.
The read models listen the events and build some projections to return data
to the user.
The policies listen the events to take some decisions and “do something”.
15. DDD ❤️ Actor ❤️ Elixir
Concurrency
A lot of commands received, often by the same aggregate.
A lot of events emitted and processed.
A lot of events listened by read models and policies.
Events, commands (messages?!?) …
16. DDD ❤️ Actor ❤️ Elixir
Concurrency
A lot of messages (events, commands) received and emitted concurrently by
the same aggregate.
Can we use threads?
18. DDD ❤️ Actor ❤️ Elixir
What is Actor Model?
From wikipedia:
The actor model in computer science is a mathematical model of concurrent
computation that treats "actors" as the universal primitives of concurrent
computation.
In response to a message that it receives, an actor can: make local decisions,
create more actors, send more messages, and determine how to respond to the
next message received.
Actors may modify their own private state, but can only affect each other indirectly
through messaging (obviating lock-based synchronization).
19. DDD ❤️ Actor ❤️ Elixir
What is Actor Model?
An actor is a computational entity that, in response to a message it receives,
can concurrently:
● send a finite number of messages to other actors;
● create a finite number of new actors;
● designate the behavior to be used for the next message it receives.
20. DDD ❤️ Actor ❤️ Elixir
What is Actor Model?
Always from wikipedia:
22. DDD ❤️ Actor ❤️ Elixir
What is Elixir
From wikipedia:
Elixir is a functional, concurrent, general-purpose programming language that
runs on the Erlang virtual machine (BEAM).
Elixir builds on top of Erlang and shares the same abstractions for building
distributed, fault-tolerant applications.
Elixir also provides productive tooling and an extensible design. The latter is
supported by compile-time metaprogramming with macros and polymorphism
via protocols.
23. DDD ❤️ Actor ❤️ Elixir
What is Elixir
It’s a young language.
It works, very well!❤️!❤️!
It’s based on a old language.
24. DDD ❤️ Actor ❤️ Elixir
Who is using Elixir?
From wikipedia:
Elixir is used by companies such as PagerDuty, Discord, E-MetroTel, Pinterest,
Moz, Bleacher Report, The Outline, Inverse and Divvy, and for building
embedded systems.
But wait ...
25. DDD ❤️ Actor ❤️ Elixir
Who is using Elixir?
Elixir ❤️ Erlang, Who is using erlang?
WhatsApp, T-Mobile, Motorola, Ericsson, Cisco, ...
RabbitMQ, Ejabberd, Riak, CouchDB …
It’s used when stopping is not an option ...
26. DDD ❤️ Actor ❤️ Elixir
What is Elixir?
From https://elixir-lang.org/:
Elixir is a dynamic, functional language designed for building scalable and
maintainable applications.
Elixir leverages the Erlang VM, known for running low-latency, distributed and
fault-tolerant systems, while also being successfully used in web development and
the embedded software domain.
27. DDD ❤️ Actor ❤️ Elixir
What is Elixir?
Dynamic → We can’t declare new types
Functional Language → Immutable (rebinding…)
Has a lot of nice features like:
● Pattern Matching
● |>
● Metaprogramming
● Debugging
● ...
29. DDD ❤️ Actor ❤️ Elixir
Is Elixir/Erlang an implementation of Actor Model?
From erlang mailing list:
Erlang does *not* implement Actors but processes with links/monitors mailboxes
and messages, which are not equivalent to actors.
Processes: sequence of function calls interspersed with (selective) receives that pick
out something out of the mailbox.
Actor: has to handle every message immediately, the actions a message triggers
are happening concurrently, nothing longer running or sequential allowed.
30. DDD ❤️ Actor ❤️ Elixir
Is Elixir/Erlang an implementation of Actor Model?
Hewitt says himself that Erlang does not implement Actors:
Erlang imposes the overhead that messages sent between two Erlang Actors
are delivered in the order they are sent.
Instead of using exception handling, Erlang Actors rely on process failure
propagating between processes and their spawned processes.
Instead of using garbage collection to recover storage and processing of
unreachable Actors, each Erlang Actor must perform an internal termination or
be killed. However, data structures within a process are garbage collected
31. DDD ❤️ Actor ❤️ Elixir
Is Elixir/Erlang an implementation of Actor Model?
Robert Virding wrote:
We had never heard of the actor model, at least I hadn't. We had other inputs,
amongst others Eripascal which an internal Ericsson version of Pascal which had
processes and messages.
…
And of course OSes which embody a lot of the ideas we were after, but in such a
way that they were/are way to heavy for what we were after. In many ways an
Erlang system does have an OS feeling about it.
32. DDD ❤️ Actor ❤️ Elixir
Is Elixir/Erlang an implementation of Actor Model?
Robert Virding wrote:
We were trying to solve THE problem and this was the best solution we could come
with. It was purely pragmatic. We definitely took ideas from other inputs but not
from the Actor model.
33. DDD ❤️ Actor ❤️ Elixir
Is Elixir/Erlang an implementation of Actor Model?
Joe Armstrong wrote:
Q: What sources DID you draw from (other than the predecessor languages at
Ericsson), are there any that you'd consider primary influences?
A: Prolog and Smalltalk in equal measure. Pattern matching and syntax was
inspired by Prolog. Messaging from Smalltalk. We took a few ideas on guarded
commands from Dijkstra.
The message queues were largely inspired by SDL and occam (SDL has a graphic
notation very similar to selective receive) Links were invented by Mike Williams and
based on the idea of a C-wire (a form of electrical circuit breaker).
34. DDD ❤️ Actor ❤️ Elixir
Is Elixir/Erlang an implementation of Actor Model?
Joe Armstrong wrote:
It's funny that the "sending message to an object" way of describing Java and
Smalltalk, Objective-C, C++ etc. objects has persisted despite the fact that this is
manifestly not what happens (which is a function call).
In Erlang we really do send a message to an object (well a process actually) - so
Erlang is probably the only OO language there is :-)
35. DDD ❤️ Actor ❤️ Elixir
What is Actor Model? - Recap
An actor is a computational entity that, in response to a message it receives,
can concurrently:
● send a finite number of messages to other actors;
● create a finite number of new actors;
● designate the behavior to be used for the next message it receives.
36. DDD ❤️ Actor ❤️ Elixir
What is Actor Model? - Recap
Erlang is not a formal implementation of the Actor model.
Probably it’s something similar (better??).
Probably it’s the best OO language out there.
Probably it’s the best language out there.
❤️
38. DDD ❤️ Actor ❤️ Elixir
DDD ❤️ Actor ❤️ Elixir
Aggregate
Commands/Events
Policy
Read Model
Process (GenServer)
FP → Message
Process (GenStateMachine)
Process (GenServer/GenStateMachine)
41. DDD ❤️ Actor ❤️ Elixir
WARNING!!!
This is an example!!!!
It’s a “dumb” solution!!!
Don’t copy & paste this code!!!
42.
43. DDD ❤️ Actor ❤️ Elixir
Recap
Elixir fits very well with the DDD concepts.
Elixir give you the ability to focus on your problems.
You don’t need to think about lock/mutex etc …
It’s Fun and Full of Love.
44. DDD ❤️ Actor ❤️ Elixir
References
DDD, CQRS/ES (google is your friend), vlingo
Actor Models
Erlang mailing list
Elixir language, Elixir book, Erlang book, More advanced
Example