Scala, Akka
Michał Tomański
Funcional Programming
- Immutable data
- Pure functions
- Based on Alonso Church's Lambda Calculus
- No side effects, no global state
- Easy to reuse, test and analyse
- Makes concurrency simple
Pure funcion
A pure function always computes the same value given the same parameters.
sin(pi/2)
class BankAccount {
private balance;
public withdraw() {
this.balance = balance > 100 ? balance - 100 : 0;
return balance
}
}
Scala
- Scalable Language
- Designed by prof. Martin Odersky from EPFL
- Combines Object Oriented Programming with Functional Programming
- Statically typed
- Oriented on immutability
- Compiles to Java byte code, runs on JVM
Types
- Scala is strongly and statically typed
- Types are checked at compile time
- Types can be inferred by compiler
- Functions are types too: A => B
- Types can be abstract and polymorphic
- Can define own types
- Can be parametrised
- Either, tuples
Basic syntax
Akka
- Actor based concurrency
- Message-based and asynchronous
- 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.
- No mutable data are shared and no synchronization primitives are used
- An actor's failure is treated as a result that has to be handled by actor's supervisor
- The way actors interact is the same whether they are on the same host or separate hosts, communicating directly or through routing facilities, running on a few threads or many threads. Such details may be altered at deployment time through a configuration mechanism, allowing a program to be scaled up and out without modification.
Thread Pools
simple-db-lookups {
fork-join-executor {parallelism-factor = 10.0}
}
expensive-db-lookups {
fork-join-executor {parallelism-max = 4}
}
db-write-operations {
fork-join-executor {parallelism-factor = 2.0}
}
expensive-cpu-operations {
fork-join-executor {parallelism-max = 2}
}
Fork-join model
Divide and conquer algorithm
Lightweight threads
Threads stealing
solve(problem):
if problem is small enough:
solve problem directly (sequential algorithm)
else:
for part in subdivide(problem)
fork subtask to solve part
join all subtasks spawned in previous loop
combine results from subtasks
Reactive manifesto
-
Thread pools
-
Futures
Text
Reactive Streams
Finite State Machine
class Buncher extends FSM[State, Data] {
startWith(Idle, Uninitialized)
when(Idle) {
case Event(SetTarget(ref), Uninitialized) =>
stay using Todo(ref, Vector.empty)
}
when(Active, stateTimeout = 1 second) {
case Event(Flush | StateTimeout, t: Todo) =>
goto(Idle) using t.copy(queue = Vector.empty)
}
initialize()
}
Clustering
- akka-cluster, akka-cluster-tools, akka-cluster-sharding
- allows actor systems to join and leave a cluster.
- Actors can listen to events like MemberUp, MemberRemove, MemberExited, UnreachableMember etc.
- UnreachableMember policy
Akka Persistence
- Actor's journal
- Snapshots
CQRS / Event Sourcing
Netflix's Failure Injection Testing
Chaotic Monkeys
-
Increase latency
-
Throw exceptions
-
Return 500 response code
-
Kill a node
-
Kill a store
References
- Programming in Scala: A Comprehensive Step-by-Step Guide, 2nd Edition, Martin Odersky
- Functional Programming in Scala, Paul Chiusano and Rúnar Bjarnason
- Akka Concurrency Paperback, Derek Wyatt
Coursera:
- Functional Programming Principles in Scala
- Principles of Reactive Programming
Conferences:
- Lambda Days, Kraków , February
- Scalar, Warsaw, April
- Confitura, Warsaw, July
Scala, Akka
By Michał Tomański
Scala, Akka
- 1,259