Concurrency
the Good, the Bad and the Ugly
Dr. Roland Kuhn
Akka Tech Lead
Lgd. Viktor Klang
Director of Engineering
Concurrency
wat?
Characteristics
Compartmentalization
Characteristics
Compartmentalization
Coordination
Characteristics
Compartmentalization
Coordination
Execution
Characteristics
Compartmentalization
ExecutionCoordination
Characteristics
Characteristics
Characteristics
Good
UglyBad
Characteristics
Good
UglyBad ☓
Threads
… and locks
The Good
The Good
✓Can be mapped onto CPUs
The Good
✓Can be mapped onto CPUs
✓"To the metal" — no overhead
The Good
✓Can be mapped onto CPUs
✓"To the metal" — no overhead
✓High degree of control
The Good
✓Can be mapped onto CPUs
✓"To the metal" — no overhead
✓High degree of control
✓Debugging
The Bad
The Bad
☣Heavyweight
The Bad
☣Heavyweight
☣Scarce
The Bad
☣Heavyweight
☣Scarce
☣Diminishing returns
The Bad
☣Heavyweight
☣Scarce
☣Diminishing returns
☣High wake-up latency
The Bad
☣Heavyweight
☣Scarce
☣Diminishing returns
☣High wake-up latency
☣Barebones / low-level
The Bad
☣Heavyweight
☣Scarce
☣Diminishing returns
☣High wake-up latency
☣Barebones / low-level
☣No means of recovery
The Ugly
The Ugly
Is it threadsafe?
The Ugly
Is it threadsafe?
— Too coarse locks?
The Ugly
Is it threadsafe?
— Too coarse locks?
— The right locks?
The Ugly
Is it threadsafe?
— Too coarse locks?
— The right locks?
— The right locking order?
The Ugly
Is it threadsafe?
— Too coarse locks?
— The right locks?
— The right locking order?
Defensive error handling
The Ugly
Is it threadsafe?
— Too coarse locks?
— The right locks?
— The right locking order?
Defensive error handling
Do you understand JSR133?
Compartmentalization
ExecutionCoordination
Conclusion
Compartmentalization
ExecutionCoordination
Conclusion
Thread + Locks
EventLoop
… or Executor
The Good
The Good
✓Configurable execution
The Good
✓Configurable execution
✓M:N task to thread ratio
The Good
✓Configurable execution
✓M:N task to thread ratio
✓Less JMM headache
The Bad
The Bad
☣How to handle errors?
The Bad
☣How to handle errors?
☣Dealing with blocking code
The Bad
☣How to handle errors?
☣Dealing with blocking code
☣Closing over mutable state
The Ugly
The Ugly
Callback Hell
The Ugly
Callback Hell
Composing results
The Ugly
Callback Hell
Composing results
Low reusability
The Ugly
Callback Hell
Composing results
Low reusability
Is it sync or async?
The Ugly
Callback Hell
Composing results
Low reusability
Is it sync or async?
Debugging
Compartmentalization
ExecutionCoordination
Conclusion
Compartmentalization
ExecutionCoordination
Conclusion
Executor
STM
Software Transactional Memory
The Good
The Good
✓Typesafe & non-blocking
The Good
✓Typesafe & non-blocking
✓Transactional
The Good
✓Typesafe & non-blocking
✓Transactional
✓Composable
The Bad
The Bad
☣Size to failure ratio
The Bad
☣Size to failure ratio
☣Retries are costly
The Ugly
The Ugly
Dealing with side-effects
The Ugly
Dealing with side-effects
Dealing with failure
The Ugly
Dealing with side-effects
Dealing with failure
Unpredictable performance
The Ugly
Dealing with side-effects
Dealing with failure
Unpredictable performance
Debugging
Compartmentalization
ExecutionCoordination
Conclusion
Compartmentalization
ExecutionCoordination
STM
Conclusion
Java Future
… and ExecutorService
The Good
The Good
✓Configurable execution
The Good
✓Configurable execution
✓Typesafe
The Bad
The Bad
☣Blocking composition
The Bad
☣Blocking composition
☣Guesstimation of deadlock
The Bad
☣Blocking composition
☣Guesstimation of deadlock
☣Futures are one-off
The Ugly
The Ugly
Cancel other's Futures?
The Ugly
Cancel other's Futures?
Non-blocking requires polling
The Ugly
Cancel other's Futures?
Non-blocking requires polling
Dealing with failure
Compartmentalization
ExecutionCoordination
Conclusion
Compartmentalization
ExecutionCoordination
Java Future
Conclusion
Scala Future
… with ExecutionContext
The Good
The Good
✓Typesafe
The Good
✓Typesafe
✓Non-blocking composition
The Good
✓Typesafe
✓Non-blocking composition
✓Monadic
The Good
✓Typesafe
✓Non-blocking composition
✓Monadic
✓Freely sharable
The Good
✓Typesafe
✓Non-blocking composition
✓Monadic
✓Freely sharable
✓Can recover from failure
The Bad
The Bad
☣Defers failures to downstream
The Bad
☣Defers failures to downstream
☣Closing over mutable state
The Bad
☣Defers failures to downstream
☣Closing over mutable state
☣No ordering of callbacks
The Bad
☣Defers failures to downstream
☣Closing over mutable state
☣No ordering of callbacks
☣Futures are one-off
The Ugly
The Ugly
Callback Hell (use for instead)
The Ugly
Callback Hell (use for instead)
Debugging
Compartmentalization
ExecutionCoordination
Conclusion
Compartmentalization
ExecutionCoordination
Conclusion
Scala Future
Akka Dataflow
The Good
The Good
✓Typesafe
The Good
✓Typesafe
✓Non-blocking composition
The Good
✓Typesafe
✓Non-blocking composition
✓Declarative using direct style
The Good
✓Typesafe
✓Non-blocking composition
✓Declarative using direct style
✓Deterministic
The Good
✓Typesafe
✓Non-blocking composition
✓Declarative using direct style
✓Deterministic
✓Non-local composition
The Bad
The Bad
☣CPS plugin error messages
The Bad
☣CPS plugin error messages
☣Limited applicability
The Bad
☣CPS plugin error messages
☣Limited applicability
— Error handling
The Bad
☣CPS plugin error messages
☣Limited applicability
— Error handling
☣Debugging
The Bad
☣CPS plugin error messages
☣Limited applicability
— Error handling
☣Debugging
☣Futures are one-off
The Ugly
The Ugly
CPS plugin error messages!!
Compartmentalization
ExecutionCoordination
Conclusion
Compartmentalization
ExecutionCoordination
Conclusion
Dataflow
Scala Async
The Good
The Good
✓Typesafe
The Good
✓Typesafe
✓Non-blocking composition
The Good
✓Typesafe
✓Non-blocking composition
✓Declarative using direct style
The Bad
The Bad
☣Limited applicability
The Bad
☣Limited applicability
☣Debugging
The Bad
☣Limited applicability
☣Debugging
☣Futures are one-off
The Ugly
The Ugly
Error handling
Compartmentalization
ExecutionCoordination
Conclusion
Compartmentalization
ExecutionCoordination
Conclusion
Async
Threads
… and Queues
The Good
The Good
✓Typesafe
The Good
✓Typesafe
✓Blocking or Non-blocking
The Good
✓Typesafe
✓Blocking or Non-blocking
✓Back pressure possible
The Bad
The Bad
☣Heavyweight
The Bad
☣Heavyweight
☣Scarce
The Bad
☣Heavyweight
☣Scarce
☣Diminishing returns
The Bad
☣Heavyweight
☣Scarce
☣Diminishing returns
☣High wake-up latency
The Bad
☣Heavyweight
☣Scarce
☣Diminishing returns
☣High wake-up latency
☣Not really high-level
The Bad
☣Heavyweight
☣Scarce
☣Diminishing returns
☣High wake-up latency
☣Not really high-level
☣No means of recovery
The Ugly
The Ugly
Propagating failure
The Ugly
Propagating failure
Unidirectional
Compartmentalization
ExecutionCoordination
Conclusion
Compartmentalization
ExecutionCoordination
Conclusion
Thread + Queue
Actors
The Good
The Good
✓Encapsulation
The Good
✓Encapsulation
✓Supervision
The Good
✓Encapsulation
✓Supervision
✓Configurable execution
The Good
✓Encapsulation
✓Supervision
✓Configurable execution
✓Location transparency
The Good
✓Encapsulation
✓Supervision
✓Configurable execution
✓Location transparency
✓Models real-world
The Good
✓Encapsulation
✓Supervision
✓Configurable execution
✓Location transparency
✓Models real-world
✓Require less guarantees
The Bad
The Bad
☣Untyped
The Bad
☣Untyped
☣Explicit lifecycle management
The Bad
☣Untyped
☣Explicit lifecycle management
☣Debugging
The Ugly
The Ugly
Easy encapsulation violation
The Ugly
Easy encapsulation violation
Composition can be awkward
Compartmentalization
ExecutionCoordination
Conclusion
Compartmentalization
ExecutionCoordination
Conclusion
Actors
Rx & friends
The Good
The Good
✓Typesafe
The Good
✓Typesafe
✓Non-blocking composition
The Good
✓Typesafe
✓Non-blocking composition
✓Reusable components
The Good
✓Typesafe
✓Non-blocking composition
✓Reusable components
✓Stream centric
The Bad
The Bad
☣Defers failures to downstream
The Bad
☣Defers failures to downstream
☣Explicit lifecycle management
The Bad
☣Defers failures to downstream
☣Explicit lifecycle management
☣"Glitches"
The Bad
☣Defers failures to downstream
☣Explicit lifecycle management
☣"Glitches"
☣Debugging
The Ugly
The Ugly
Callback Limbo
The Ugly
Callback Limbo
Inverted control flow
Compartmentalization
ExecutionCoordination
Conclusion
Compartmentalization
ExecutionCoordination
Conclusion
Rx
Functional Reactive
Programming
The Good
The Good
✓Typesafe
The Good
✓Typesafe
✓Declarative value composition
The Good
✓Typesafe
✓Declarative value composition
✓Direct style
The Good
✓Typesafe
✓Declarative value composition
✓Direct style
✓Deterministic
The Bad
The Bad
☣Single-threaded
The Ugly
The Ugly
Side-effecting can introduce
glitches
Compartmentalization
ExecutionCoordination
Conclusion
Compartmentalization
ExecutionCoordination
Conclusion
FRP
Summary
60
Compartmentalization
ExecutionCoordination
Conclusion
Compartmentalization
ExecutionCoordination
STM
Conclusion
Compartmentalization
ExecutionCoordination
STM
Conclusion
Scala Future
Compartmentalization
ExecutionCoordination
STM
Conclusion
Scala Future
FRP
Compartmentalization
ExecutionCoordination
STM
Java Future
Conclusion
Scala Future
FRP
Compartmentalization
ExecutionCoordination
STM
Java Future
Conclusion
Scala Future
FRP
Actors
Compartmentalization
ExecutionCoordination
STM
Java Future
Conclusion
Scala Future
FRP
Actors
Dataflow & Async & Rx
Compartmentalization
ExecutionCoordination
STM
Java Future
Conclusion
Scala Future
FRP
Actors
Dataflow & Async & Rx
Thread + Queue
Compartmentalization
ExecutionCoordination
STM
Java Future
Conclusion
Scala Future
FRP
Actors
Dataflow & Async & Rx
Thread + Queue
Executor
Compartmentalization
ExecutionCoordination
STM
Java Future
Conclusion
Scala Future
FRP
Actors
Dataflow & Async & Rx
Thread + Queue
Executor
Thread + Locks
Conclusion
Conclusion
Pick
Conclusion
Pick
your
Conclusion
Pick
your
☠
E0F

Concurrency: The Good, The Bad and The Ugly