3. Slides will be shared at:
http://www.slideshare.net/ngocdaothanh
This talk is long.
Please ask question immediately when you have one.
For questions/discussions after this talk, please post to:
https://www.facebook.com/groups/720666194693928
You can also post to scala-user group:
https://groups.google.com/d/forum/scala-user
4. About me
Working at Mobilus in Tokyo: http://mobilus.co.jp/
● Server side
● Android, iOS
Interests:
● Realtime and distributed systems
● Web frameworks
(have been written several for Erlang, Scala, Java)
● Big data, analytics (learning Apache Spark)
Open source:
● https://github.com/ngocdaothanh
● https://github.com/xitrum-framework
● https://github.com/sinetja
● https://github.com/netty
7. About you
I guess you already know OOP.
Functional programming:
● How many of you have used Ruby?
(somewhat functional, popular)
● Have used a more functional language?
(Lisp, Haskell, Erlang, Clojure, F# etc.)
Scala looks like Java and runs on JVM. It’s convenient if
you already know Java:
● How many of you have used Java?
● Have used Scala?
8. ● Why use Scala? Compare with Java, Ruby, Node.js etc.
● Books, sites
Getting started:
● Install Scala
● Ways to run Scala program: Hello World
Scala = OOP + functional:
● Learn Scala’s functional feature via collections
● Learn Scala’s OOP feature via the Animals exercise
Some tools:
● Development:
○ REPL (Read–Eval–Print Loop), SBT (Scala Build Tool)
○ IDEs: Eclipse, IntelliJ
● Production:
○ xitrum-package
○ Scalive
PART 1/5: Scala overview
9. ● Web app ≈ normal app + routing
● Xitrum:
○ Compare with Scalatra, Lift, Play
○ Basic usage:
■ Add routes to the previous demos
■ respondXxx
○ Swagger UI, Swagger Codegen
○ Metrics
○ i18n
PART 2/5: Use Scala for web
10. ● Compare with Node.js
● Future and future composition
● Actor model and Akka actor
↑ NEW programming paradigm
A language that doesn't change the way you
think about programming is not worth
learning.
○ Actor vs Thread
○ Actor vs Future
○ Some pitfalls
● Akka FSM
PART 3/5: Use Scala for concurrency
11. ● Realtime web
● WebSocket, Socket.IO, SockJS
● Tictactoe web game
PART 4/5: Use Scala for realtime web
12. PART 5/5: Use Scala for distributed systems
Compare with Redis
Remoting:
● Akka remoting
Clustering:
● Akka clustering
● Glokka
● Hazelcast
Scale Tictactoe to multiple servers
13.
14. ● Why use Scala? Compare with Java, Ruby, Node.js etc.
● Books, sites
Getting started:
● Install Scala
● Ways to run Scala program: Hello World
Scala = OOP + functional:
● Learn Scala’s functional feature via collections
● Learn Scala’s OOP feature via the Animals exercise
Some tools:
● Development:
○ REPL (Read–Eval–Print Loop), SBT (Scala Build Tool)
○ IDEs: Eclipse, IntelliJ
● Production:
○ xitrum-package
○ Scalive
PART 1/5: Scala overview
15. ● Trendy: Used by by Apple, Foursquare, Guardian, LinkedIn,
Netflix, Quora, Sony Pictures Entertainment, Twitter, UK Guardian
etc.
● Compare with Java, Ruby, Node.js etc.
● Language: ≈ Java + Ruby
○ Fast speed of Java + nice syntax of Ruby:
Faster than Ruby + syntax nicer than Java
○ Flexible:
■ Static + dynamic:
Compiled to Java .class or run as script like Ruby
■ OOP of Java + functional of Ruby
http://www.quora.com/Why-is-Swift-so-similar-to-Scala
● Not only a good language: Scala has toys
○ Ecosystem: Can reuse all Java libs
○ Concurrent/parallel: Future, Akka (≈ Erlang)
○ Big data: Apache Spark
Why use Scala?
20. Level A3:
Expert application programmer
● Folds, i.e. methods such as foldLeft,
foldRight
● Streams and other lazy data structures
● Actors
● Combinator parsers
26. Ways to run Scala program
● println("Hello World")
● Run as script with scala
● Compile with scalac and run with scala
● Compile with scalac and run with java
27.
28. SBT (Scala Build Tool)
http://www.scala-sbt.org/
● Dependency library manager + build tool
● Like “gem” + “rake” of Ruby
● Like Maven, Gradle
● Has plugins to generate project files for IDEs,
like Eclipse and IntelliJ
● Has plugins to collect and package .jar files,
ready to deploy to production environment
(Make, Ant are just build tools, not dependency library
managers)
32. $ sbt tasks
console Starts the Scala interpreter
compile Compiles sources
clean Deletes files produced by the build
run Runs a main class
package Produces project jar file
publish Publishes project jar file to a repository
publish-local Copies project jar file to ~/.ivy2
test Executes all tests
test-only Executes the tests provided as arguments
test-quick Executes the tests that either failed before
34. Hello World demo
● sbt console
● sbt run
● sbt eclipse
● Run in Eclipse
● Debug in Eclipse
● sbt xitrum-package
● Run with java
● Add dependency library
http://search.maven.org/
● Scalive
35. Rules of thumb
Use dependency manager:
● Do not download dependency libraries
manually
● Do not commit dependency libraries
IDEs:
● Generate project files for IDEs
● Do not commit project files
38. Functional language
● Pass a function as an argument to another
function
=> Convenient syntax
f(123, g)
f(123, {
// ...
})
● Immutable data structures
=> Thread-safe
○ Tail recursion
39. Learn Scala’s functional feature via
collections
● From a collection, create another collection of same size
=> map
● From a collection, create another collection of larger size
=> flatMap
● From a collection, create another collection of smaller size
=> filter
● From a collection, create one element
=> foldLeft, foldRight, reduce
http://www.scala-lang.org/api/current/index.html#scala.collection.Seq
44. val a = 1
var b = 1
b = 2
val → immutable:
val c = Seq(1, 2, 3)
var → immutable:
var d = Seq(1, 2, 3)
d = Seq(4, 5, 6)
val → mutable:
val e = Array(1, 2, 3)
e(0) = 4
var → mutable:
var f = Array(1, 2, 3)
f = Array(4, 5, 6)
f(0) = 4
45. val VS var
immutable VS mutable collection
Order of preference (best practice):
● val
● var
● val → immutable: strictly functional
● var → immutable
● val → mutable
● var → mutable: do not use
Some languages only allow val → immutable
Scala is flexible:
var and mutable are OK to use in small, closed scope
46. For your curiosity: How to write
programs with only constants?
Example problem:
Without var, mutable:
● Generate a collection of positive numbers
that are <= n, and are multiple of 3 or 5.
gen(n: Int): Seq[Int]
● Calculate sum of all numbers in a collection.
sum(nums: Seq[Int]): Int
47. With var and mutable
import scala.collection.mutable.ArrayBuffer
def gen(n: Int): Seq[Int] = {
val buf = ArrayBuffer[Int]()
var i = 1
while (i <= n) {
if (i % 3 == 0 || i % 5 == 0)
buf.append(i)
i += 1
}
buf
}
48. def gen(n: Int): Seq[Int] = {
val nums = 1 to n
nums.filter { i =>
i % 3 == 0 || i % 5 == 0
}
}
def gen(n: Int): Seq[Int] = {
for {
i <- 1 to n
if (i % 3 == 0 || i % 5 == 0)
} yield i
}
49. Without var and mutable
● Use recursion to loop
● Optimize: Use tail recursion
○ Use accumulator
50. def gen(n: Int): Seq[Int] = {
// Stop condition
if (n < 3) return Seq()
// Recursion
if (n % 3 == 0 || n % 5 == 0)
gen(n - 1) :+ n
else
gen(n - 1)
}
Stackoverflow
if n is big enough
51. def genAcc(
n: Int, acc: Seq[Int]
): Seq[Int] = {
if (n < 3) return acc
// Tail recursion; Scala compiler will
// convert to “while” loop
if (n % 3 == 0 || n % 5 == 0)
genAcc(n - 1, n +: acc)
else
genAcc(n - 1, acc)
}
def gen(n: Int) = genAcc(n, Seq())
52. def genAcc(
n: Int, acc: Seq[Int]
): Seq[Int] = {
if (n < 3) return acc
if (n % 3 == 0 || n % 5 == 0)
genAcc(n - 1, n +: acc)
else
genAcc(n - 1, acc)
}
How to write programs with only
constants?
State is stored
at function arguments
State is set/changed
at function call
53.
54. How I learn an OOP language:
● Learn basic syntax
● Learn collections
● Animals exercise:
abstraction, encapsulation,
inheritance, polymorphism
Learn Scala’s OOP feature via the
Animals exercise
55. Animals exercise
trait (interface) Animal
● name: String
● talk()
class Cat extends Animal
class Dog extends Animal
class Zoo
● addAnimal(a: Animal)
● talkAll()
58. ● Web app ≈ normal app + routing
● Xitrum:
○ Compare with Scalatra, Lift, Play
○ Basic usage:
■ Add routes to the previous demos
■ respondXxx
○ Swagger UI, Swagger Codegen
○ i18n
PART 2/5: Use Scala for web
60. Web ≈ Routing
A web library/framework is not worth using if its routing feature sucks.
function
Request Response
function
ResponseRequest
Request
Request
Router
64. ● Compare with Node.js
● Future and future composition
● Actor model and Akka actor
↑ NEW programming paradigm
A language that doesn't change the way you
think about programming is not worth
learning.
○ Actor vs Thread
○ Actor vs Future
○ Some pitfalls
● Akka FSM
PART 3/5: Use Scala for concurrency
65. Compare with Node.js
Node.js:
● Single thread => Single core
● Complicate to scale to multiple cores or multiple
servers; except for shared-nothing (non-realtime)
systems
● Callback hell
Scala:
● Multiple threads; immutable data structures, future, and
actor provide more safety for multithread programming
● Easy to scale
● No callback hell
71. ↑ NEW programming paradigm
A language that doesn't change the way you
think about programming is not worth
learning.
Alan Perlis
(the first recipient of the Turing Award)
Actor model and Akka actor
72. Benefit of actor in one sentence
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 manually.
Don't communicate by sharing memory; share memory by
communicating.
73. Actor model
● Actor =
states +
mailbox +
behaviors (msg handlers)
● From outside, can’t manipulate actors directly.
● To interact with an actor, must send messages to it.
● Each actor has a mailbox, messages are put to mailbox,
and processed one by one. ← An actor is like a single
threaded process (each actor is like a Node.js 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/
74. 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.
75. Actor vs Thread
● Thread: n dimensions, hard to reason about.
● Actor: 1D, one thing at a time.
var1
var1
76. Actor vs Thread
Just like JVM automatically manages memory
for you:
● Actor is a high level logical way to think, to
model programs.
● At lower level, actors run above a thread
pool.
77. Actor vs Future
Actor:
● Messaging
● Scheduling
● FSM
● Monitoring and 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
● More typesafe
79. Demo: Counter
// First, define messages
object Counter {
case class Inc(amount: Int)
case class Dec(amount: Int)
}
80. import akka.actor.Actor
// Define actor state and message handlers
class Counter(name: String) extends Actor {
// Actor state
private var value = 0
...
81. ...
import Counter._
// Message handlers
def receive = {
case Inc(amount) =>
value += amount
println(s"Value: $value")
case Dec(amount) =>
value -= amount
println(s"Value: $value")
}
}
82. import akka.actor.{ActorSystem, Props}
// Think of this as a thread pool
val system = ActorSystem("mysystem")
// Create actor reference (actor instance)
val c1 = system.actorOf(
Props(new Counter("mylock"))
)
// Send messages to the actor
c1 ! Counter.Inc(1)
c1 ! Counter.Dec(2)
83. Some 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.
84. Some pitfalls
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
85. Good read
● 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
● Akka doc
http://doc.akka.io/docs/akka/2.3.8/scala.html
86.
87. Akka FSM
● FSM: Finite State Machine
● Implemented based on actors
● Very good for realtime multiplayer games, to
manage states of game sessions and inter-
player interactions
Example:
Code Lock
https://github.com/ngocdaothanh/code-lock-fsm-akka
88.
89. ● Realtime web
● WebSocket, Socket.IO, SockJS
● Tictactoe web game
PART 4/5: Use Scala for realtime web
90. HTTP problem:
Server can send data to clients only one time.
Some years ago:
Flash (open TCP socket connection to server)
Now:
WebSocket
https://developer.mozilla.org/en-
US/docs/WebSockets/Writing_WebSocket_clie
nt_applications
Realtime web ≈ server push
91. var sock = new WebSocket('ws://domain/path');
sock.onopen = function() {
console.log('onopen');
};
sock.onmessage = function(e) {
console.log('onmessage: ' + e.data);
sock.send(e.data);
};
sock.onclose = function() {
console.log('onclose');
};
Server can push data to
clients multiple times
92. WebSocket problem
New browsers are required:
● Internet Explorer: 10
● Firefox: 6
● Chrome: 4
● Safari: 5
● Opera: 12.10
93. WebSocket emulators
SockJS: WebSocket-like API
● JavaScript client:
https://github.com/sockjs/sockjs-client
● Protocol:
https://github.com/sockjs/sockjs-protocol
http://sockjs.github.io/sockjs-protocol/sockjs-protocol-0.3.3.html
● Closer to WebSocket, much simpler than Socket.IO
=> Server side support for many languages
Socket.IO: More complicate built-in features, like rooms
● http://socket.io/
● Node.js
94. SockJS
● Provides WebSocket-like API
● Supports all browsers, e.g. IE 6
● Transports: websocket xhr-streaming xdr-
streaming iframe-eventsource iframe-
htmlfile xhr-polling xdr-polling iframe-xhr-
polling jsonp-polling
● Requires
○ Client side: sockjs.js
○ Server side: SockJS protocol
implementation
95. var sock = new WebSocket('ws:
//domain/path');
sock.onopen = function() {
};
sock.onmessage = function(e) {
sock.send(e.data);
};
sock.onclose = function() {
};
var sock = new SockJS('http:
//domain/path');
sock.onopen = function() {
};
sock.onmessage = function(e) {
sock.send(e.data);
};
sock.onclose = function() {
};
WebSocket vs SockJS
97. Tictactoe web game
Exercises:
● Instead of letting player actors send messages directly
to each other, use an intermediate game actor for each
game.
● Let users other than the players watch games.
● Use Scala.js to convert game logic code written in Scala
to JavaScript, to share game logic code between server
side and client side.
http://www.scala-js.org/
● Use Akka FSM instead of become.
98.
99. Compare with Redis
Remoting:
● Akka remoting
Clustering:
● Akka clustering
● Glokka
● Hazelcast
Scale Tictactoe to multiple servers
PART 5/5: Use Scala for distributed systems
100. Compare with Redis
Redis:
● Separate process
● Single point of failure
Akka, Hazelcast:
● In-process
=> Faster, easier to use
● Redundancy, failover