Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Model with actors and
implement with Akka
ITLC Hanoi, Oct 5 2015
Ngoc Dao
https://www.linkedin.com/in/ngocdaothanh
https://github.com/ngocdaothanh
https://github.com/xitrum-framework
https://githu...
https://www.atlassian.com/software
Favorite languages: Ruby, Erlang, Scala
My Scala style: Use Scala as if
Scala =
Java (performance, libs eco) +
Ruby (human...
Schedule
● [What] Explain about actor model:
~¼ of time
● [How] Explain about how to create actors with
Akka library, via ...
https://www.facebook.com/groups/720666194693928
net/ngocdaothanh/develop-realtime-web-
with-scala-and-xitrum
Xmas 2014
New year 2015
More photos:
Scala VN →
Photos →
Albu...
http://scalamatsuri.org/
http://www.meetup.com/scalasyd/
https://github.com/scalasyd/scalasyd
● Your workplace, your school etc.
● Your background, your favorite languages
● Do you know Scala?
● Why do you want to le...
Part 1/2:
[What] Actor model
Benefit of actors in one sentence
C/C++ vs Java:
You can use memory without having to release memory
manually.
Thread vs a...
● Actor =
states +
mailbox +
behaviors (msg handlers)
● From outside, can’t manipulate actors directly.
● To interact with...
Actor vs OOP
“The Actor model adopts the philosophy that everything is an actor. This is
similar to the everything is an o...
Actor vs Thread
● Thread: n dimensions, hard to reason about.
● Actor: 1D, one thing at a time.
var1
var2
Actor vs Thread
Thread:
● Heavy weight: Can only create not too many threads;
usually: 2000~5000
● Shared state ← Source o...
Just like JVM automatically manages memory for you, so
that you don’t have to care about releasing memory
manually:
● Acto...
Actor vs Future
Actor:
● Reactive: messaging, scheduling
● FSM (Finite State Machine)
● Monitoring
● Supervision
● Locatio...
Some actor pitfalls
Send mutable msgs between actors.
↑ May lead to bug, if actor A sends msg M to actor B, state
of B inc...
From inside actor:
anObject.foo(new Callback {
def onCallback() {
// Modify actor state directly
}
})
↑ May lead to bug, b...
Useful links
● Concurrent Programming for Scalable Web
Architectures
http://berb.github.io/diploma-thesis/index.html
● Fun...
Part 2/2:
[How] Create actors with Akka
[Demo]
Multiplayer
web
chess game
http://akka.io/
Chess game
Simple spec, focus on chess logic:
● Web, multiplayer
● Standard chess, no variations (chess 960 etc.)
● No rec...
Useful links
Akka doc:
http://akka.io/docs/
Chess:
● UCI (Universal Chess Interface) protocol:
https://en.wikipedia.org/wi...
Upcoming SlideShare
Loading in …5
×

Model with actors and implement with Akka

1,931 views

Published on

Talk at ITLC Hanoi about actor model and Akka

Published in: Software
  • Be the first to comment

Model with actors and implement with Akka

  1. 1. Model with actors and implement with Akka ITLC Hanoi, Oct 5 2015 Ngoc Dao
  2. 2. https://www.linkedin.com/in/ngocdaothanh https://github.com/ngocdaothanh https://github.com/xitrum-framework https://github.com/netty
  3. 3. https://www.atlassian.com/software
  4. 4. Favorite languages: Ruby, Erlang, Scala My Scala style: Use Scala as if Scala = Java (performance, libs eco) + Ruby (human oriented syntax) + Erlang (functional, reactive) Interests: ● Realtime and distributed systems ● Multiplayer games ● Web frameworks (wrote several for Erlang, Scala, Java) "Soon we had three kinds of Scala written at Twitter: Scala written by people who wished it was Ruby, Scala written by people who wished it was Java, and Scala written by people who wished it was Haskell." http://www.gigamonkeys.com/flowers/ https://youtu.be/sYsHK81MTHI
  5. 5. Schedule ● [What] Explain about actor model: ~¼ of time ● [How] Explain about how to create actors with Akka library, via multiplayer chess demo: ~ ¾ of time Level: Scala beginner Please ask questions/discuss whenever you want
  6. 6. https://www.facebook.com/groups/720666194693928
  7. 7. net/ngocdaothanh/develop-realtime-web- with-scala-and-xitrum Xmas 2014 New year 2015 More photos: Scala VN → Photos → Albums
  8. 8. http://scalamatsuri.org/
  9. 9. http://www.meetup.com/scalasyd/ https://github.com/scalasyd/scalasyd
  10. 10. ● Your workplace, your school etc. ● Your background, your favorite languages ● Do you know Scala? ● Why do you want to learn Scala? ● How do you know about this meetup? ● What do you expect from this meetup? ● etc. etc. Please introduce yourselves
  11. 11. Part 1/2: [What] Actor model
  12. 12. Benefit of actors in one sentence C/C++ vs Java: You can use memory without having to release memory manually. Thread vs actor: You can use concurrency without having to create threads and sync vars manually. Don't communicate by sharing memory; share memory by communicating.
  13. 13. ● Actor = states + mailbox + behaviors (msg handlers) ● From outside, can’t manipulate actors directly. ● To interact with an actor, must send msgs to it. ● Each actor has a mailbox, msgs are put to mailbox, and processed one by one. ← An actor is like a single threaded process; it doesn’t do more than one thing at a time. http://www.cs.tsukuba.ac. jp/~yas/cs/csys-2013/2013-12- 03/
  14. 14. Actor vs OOP “The Actor model adopts the philosophy that everything is an actor. This is similar to the everything is an object philosophy used by some object-oriented programming languages, but differs in that object-oriented software is typically executed sequentially, while the Actor model is inherently concurrent.” https://en.wikipedia.org/wiki/Actor_model ● An actor is somewhat similar to an object => Easy to learn/model: actor ~ object method call ~ msg sending ● Scala supports both actors and OOP objects (Erlang only supports actors)
  15. 15. Actor vs Thread ● Thread: n dimensions, hard to reason about. ● Actor: 1D, one thing at a time. var1 var2
  16. 16. Actor vs Thread Thread: ● Heavy weight: Can only create not too many threads; usually: 2000~5000 ● Shared state ← Source of bugs ● Passive: Have to call object.method() to make the object alive. Actor: ● Light weight: Can create millions of actors; usually: ~2.5 million actors/GB ● Self contained, shared nothing ● Active: Actors are alive by themselves. ← Easy to model programs that have millions of on-going things (very high level of concurrency), like MMOG games.
  17. 17. Just like JVM automatically manages memory for you, so that you don’t have to care about releasing memory manually: ● Actor is a high level logical way to think, to model programs. You don’t have to care about managing threads and syncing vars manually. ● At lower level, actors still run above a managed thread pool. Actor vs Thread
  18. 18. Actor vs Future Actor: ● Reactive: messaging, scheduling ● FSM (Finite State Machine) ● Monitoring ● Supervision ● Location transparency (actors on one server can send messages to actors on another server) => Easier to scale out to multiple servers/clustering Future: ● Syntax is easier to write ● Composable: Run future A first, then B, then C etc. ● More typesafe (Akka 2.4.0 introduced “Akka Typed” feature)
  19. 19. Some actor pitfalls Send mutable msgs between actors. ↑ May lead to bug, if actor A sends msg M to actor B, state of B incorporates M, then M is later changed by A. => Shared memory Fix: Use immutable messages.
  20. 20. From inside actor: anObject.foo(new Callback { def onCallback() { // Modify actor state directly } }) ↑ May lead to bug, because the actor’s thread and the callback’s thread may be 2 different threads. Remember: An actor is like a single threaded process, can’t do more than one thing at a time. An actor should be self contained, shared nothing. Fix: self ! msgFromCallback
  21. 21. Useful links ● Concurrent Programming for Scalable Web Architectures http://berb.github.io/diploma-thesis/index.html ● Functions + Messages + Concurrency = Erlang http://www.infoq.com/presentations/joe-armstrong- erlang-qcon08
  22. 22. Part 2/2: [How] Create actors with Akka [Demo] Multiplayer web chess game
  23. 23. http://akka.io/
  24. 24. Chess game Simple spec, focus on chess logic: ● Web, multiplayer ● Standard chess, no variations (chess 960 etc.) ● No reconnection on network disconnection ● No time ● No game watching ● No chat ● No etc.
  25. 25. Useful links Akka doc: http://akka.io/docs/ Chess: ● UCI (Universal Chess Interface) protocol: https://en.wikipedia.org/wiki/Universal_Chess_Interface ● Stockfish (world’s top open source UCI engine): https://github.com/official-stockfish/Stockfish ● JStockfish (JNI wrapper): https://github.com/ngocdaothanh/JStockfish ● Chessground (web UI): https://github.com/ornicar/chessground ● Chess.js (JavaScript logic): https://github.com/ornicar/chess.js Chinese chess: ● UCCI (Universal Chinese Chess Interface) protocol: http://www.xqbase.com/protocol/cchess_ucci.htm ● UCCI engines: https://github.com/huygithub/hoxServer/tree/master/plugins

×