SlideShare a Scribd company logo
Scalable Applications
with Scala
Nimrod Argov
Tikal Knowledge
What is a Scalable
Application?
What is a Scalable
Application?
Scalability is the ability of a system,
network, or process to handle a growing
amount of work in a capable manner or its
ability to be enlarged to accommodate that
growth.
-- wikipedia.org
What is a Scalable
Application?
Administrative - More
People using the
system, or more
programmers writing it
What is a Scalable
Application?
Functional - Adding new
functionality with minimal
effort
What is a Scalable
Application?
Geographical - Maintaining
performance, usefulness or
usability regardless of locale
specific user concentration
What is a Scalable
Application?
Load - The ability of a
system to expand and
contract to accommodate
heavier or lighter loads
Concurrency /
Parallelism
Optimizing CPU Usage Efficiency
Scala
Scala brings together the Functional and Object
Oriented worlds, where until now both sides
considered the other to be totally wrong, or the
work of the devil.
-- Martin Oderski
Fun!
Java
java.util.concurrent
Locking
Mutable Shared State
Java
When is it enough?
Java
When is it enough?
Too Little - Race Conditions
Too Much - Loss of Parallelism / Deadlocks
Immutability
Concurrency requires Immutability
Classes should be immutable unless there's a very good reason to
make them mutable....If a class cannot be made immutable, limit
its mutability as much as possible.
-- Effective
Java (Joshua Bloch)
Immutability
//java
public final class Student
private final int id;
private final String name;
public Student(int id, String name){
this.id = id;
this.name = name;
}
.. getters…
}
Immutability
//java
public final class Student
private final int id;
private final String name;
public Student(int id, String name){
this.id = id;
this.name = name;
}
.. getters…
}
Immutability
//java
List<Student> students = new ArrayList<Student>();
List<Student> immutableStudents = Collections.unmodifiableList(students);
List<Students> ohBother = new CopyOnWriteArrayList<Student>();
Immutability
//java
List<Student> students = new ArrayList<Student>();
List<Student> immutableStudents = Collections.unmodifiableList(students);
students.add(new Student(15, “Harry Potter”));
immutableStudents now shows a different list!
Scala Collections
● Mutable/Immutable variants
● Immutable variant is imported by default
● Operations are Fast
● Cost of object creation is highly exaggerated by
programmers
Scala Collections
● Set
● Map
● List
● Stream
● Vector
● Stack
● Queue
● Tree
Scala Collections
Many Operations:
● ++ (add all)
● collect
● combinations
● contains
● count
● diff
● endsWith
●
● filter
● find
● flatMap
● flatten
● forEach
● intersect
● lift
●
● min
● max
● mkString
● partition
● permutations
● reduce
● reverse
Scala Parallel
Collections
Motivation
“The hope was, and still is, that implicit parallelism behind
a collections abstraction will bring reliable parallel
execution one step closer to the workflow of mainstream
developers.”
--Scala Documentation
Scala Parallel
Collections
Example: Calculate function over list of numbers
Scala Parallel
Collections
Example: Calculate function over list of numbers
Java:
// Somehow cut the list to N parts
Scala Parallel
Collections
Example: Calculate function over list of numbers
Java:
// Somehow cut the list to N parts
// Create ForkJoinTask (or Just a Runnable if pre JDK 6)
Scala Parallel
Collections
Example: Calculate function over list of numbers
Java:
// Somehow cut the list to N parts
// Create ForkJoinTask (or Just a Runnable if pre JDK 6)
// Use ForkJoinPool to run the tasks
// Wait for all to finish (join)
Scala Parallel
Collections
public class Operation extends RecursiveAction{
private List<Integer> numbers;
private List<Integer> results;
private int start, end;
public Operation(List<Integer> nums){
numbers = nums;
start = 0;
end = numbers.size();
}
protected void doComputation(){
for(int i = start; i < end; i++)
results.set(i, calculate(number));
}
}
Scala Parallel
Collections
public class Operation extends RecursiveAction{
protected static int threshold = 1000;
...
public Operation(List<Integer> nums){ … }
protected void doComputation(){ … }
protected void compute(){
if (numbers.length < threshold)
doComputation();
else
invokeAll(new SumOperation(numbers, 0,
numbers.size() / 2),
new SumOperation(numbers, numbers.size() /
2, numbers.size());
}
Scala Parallel
Collections
val myParallelList = someList.par
myParallelList map calculate(_)
Scala Parallel
Collections
val myParallelList = someList.par
myParallelList map calculate(_)
5 8 12 7 13 86 14 2 37 23 67 41 1 77 24 95
Scala Parallel
Collections
val myParallelList = someList.par
myParallelList map calculate(_)
5 8 12 7 13 86 14 2 37 23 67 41 1 77 24 95
5 8 12 7 13 86 14 2 37 23 67 41 1 77 24 95
Scala Parallel
Collections
val myParallelList = someList.par
myParallelList map calculate(_)
5 8 12 7 13 86 14 2 37 23 67 41 1 77 24 95
5 8 12 7 13 86 14 2 37 23 67 41 1 77 24 95
11 22 66 11 55 22 66 55 99 33 88 88 55 22 77 11
Scala Parallel
Collections
val myParallelList = someList.par
myParallelList map calculate(_)
5 8 12 7 13 86 14 2 37 23 67 41 1 77 24 95
5 8 12 7 13 86 14 2 37 23 67 41 1 77 24 95
11 22 66 11 55 22 66 55 99 33 88 88 55 22 77 11
11 22 66 11 55 22 66 55 99 33 88 88 55 22 77 11
Scala Parallel
Collections
• ParArray
• ParVector
• mutable.ParHashMap
• mutable.ParHashSet
• immutable.ParHashMap
• immutable.ParHashSet
• ParRange
Scala Parallel
Collections
Each collection defines:
● Splitter
● Combiner
Default # of threads: how many cores have you got?
Underlying implementation depends on configuration
Scala Parallel
Collections
ForkJoinTaskSupport (JDK 1.6 or higher)
ThreadPoolTaskSupport
ExecutionContextTaskSupport
Scala Parallel
Collections
<<< WARNING… WARNING… >>>
Side Effects or Non-Associative operations will create
non-deterministic results!
Scala Parallel
Collections
var sum = 0;
List(1 to 10000).par.forEach(sum += _)
List(1 to 10000).par.reduce(_-_)
Scala Futures
Futures provide a nice way to reason about performing
many operations in parallel, in an efficient and non-
blocking way.
-- Scala Documentation
Scala Futures
Future Promise
Scala Futures
val p = Promise[T]
val f: Future[T] = p.future
Java Futures
val p = Promise[T]
val f: Future[T] = p.future
// Java-like variant:
doSomeWork()
f.get()
Efficient?
Efficient?
Also, Java Futures are NOT Composable
What We Want
Callbacks
Thread 1
val p = Promise[T]
val f: Future[T] = p.future
Thread 2
f onComplete {
case Success(calcResult) = println(calcResult)
case Failure(t) = println(“Error: “ + t.getMessage)
}
Callbacks
Thread 1
val p = Promise[T]
val f: Future[T] = p.future
Thread 2
f onComplete{
case Success(calcResult) = println(calcResult)
case Failure(t) = println(“Error: “ + t.getMessage)
}
Try
Try[T]
Success[T](value: T)
Failure[T](t: throwable)
Try
Try[T]
Success[T](value: T)
Failure[T](t: throwable)
Not Asynchronous
Try
val result: Try[int] = calcSomeValue
result match{
case Success(n) = println(“The result is “ + n)
case Failure(t) = println(“Error: “ + t.getMessage)
}
Monadic Combinators
val result: Int = calcSomeValue getOrElse -1
calcSomeValue.map(doSomethingWithVal).recover
(handleException)
val rateQuote = future {
service.getCurrentValue(USD)
}
val purchase = rateQuote map { quote =>
if (isProfitable(quote)) service.buy(amount, quote)
else throw new Exception("not profitable")
}
purchase onSuccess {
case _ => println("Purchased " + amount + " USD")
}
Future Composition
val usdQuote = future { service.getCurrentValue(USD) }
val chfQuote = future { service.getCurrentValue(CHF) }
val purchase = for {
usd <- usdQuote
chf <- chfQuote
if isProfitable(usd, chf)
} yield service.buy(amount, chf)
purchase onSuccess {
case _ => println("Purchased " + amount + " CHF")
}
Future Composition
def generate = future { (random, random) }
def calculate(x:(Double, Double)) = future{ pow(x._1, x._2) }
def filter(y:Any) = y.toString.contains("22222")
while (true) {
for {
i <- generate
j <- calculate(i)
if filter(j)
}
println(j)
}
Pipe Example
Scalable Applications with Scala

More Related Content

What's hot (20)

PPTX
Scala.js: Next generation front end development in Scala
Otto Chrons
 
PPTX
Speaking Scala: Refactoring for Fun and Profit (Workshop)
Tomer Gabel
 
PDF
Migrating Legacy Data (Ruby Midwest)
Patrick Crowley
 
PDF
Scarlet SmallTalk
ESUG
 
PPTX
Nd4 j slides.pptx
Adam Gibson
 
PPTX
Nd4 j slides
Adam Gibson
 
PDF
camel-scala.pdf
Hiroshi Ono
 
PPTX
Apache Camel K - Fredericia
Claus Ibsen
 
PDF
PSUG #52 Dataflow and simplified reactive programming with Akka-streams
Stephane Manciot
 
PPTX
es6
Imran shaikh
 
PPTX
Connect S3 with Kafka using Akka Streams
Seiya Mizuno
 
PDF
My Gentle Introduction to RxJS
Mattia Occhiuto
 
PDF
Experiences in ELK with D3.js for Large Log Analysis and Visualization
Surasak Sanguanpong
 
PDF
Computing recommendations at extreme scale with Apache Flink @Buzzwords 2015
Till Rohrmann
 
PPTX
Apache Camel K - Copenhagen v2
Claus Ibsen
 
PDF
Scala.js - yet another what..?
Artur Skowroński
 
PDF
JDK8 Functional API
Justin Lin
 
KEY
Scala clojure techday_2011
Thadeu Russo
 
PDF
Scylla Summit 2022: ScyllaDB Embraces Wasm
ScyllaDB
 
Scala.js: Next generation front end development in Scala
Otto Chrons
 
Speaking Scala: Refactoring for Fun and Profit (Workshop)
Tomer Gabel
 
Migrating Legacy Data (Ruby Midwest)
Patrick Crowley
 
Scarlet SmallTalk
ESUG
 
Nd4 j slides.pptx
Adam Gibson
 
Nd4 j slides
Adam Gibson
 
camel-scala.pdf
Hiroshi Ono
 
Apache Camel K - Fredericia
Claus Ibsen
 
PSUG #52 Dataflow and simplified reactive programming with Akka-streams
Stephane Manciot
 
Connect S3 with Kafka using Akka Streams
Seiya Mizuno
 
My Gentle Introduction to RxJS
Mattia Occhiuto
 
Experiences in ELK with D3.js for Large Log Analysis and Visualization
Surasak Sanguanpong
 
Computing recommendations at extreme scale with Apache Flink @Buzzwords 2015
Till Rohrmann
 
Apache Camel K - Copenhagen v2
Claus Ibsen
 
Scala.js - yet another what..?
Artur Skowroński
 
JDK8 Functional API
Justin Lin
 
Scala clojure techday_2011
Thadeu Russo
 
Scylla Summit 2022: ScyllaDB Embraces Wasm
ScyllaDB
 

Viewers also liked (20)

PDF
Play framework
Andrew Skiba
 
PDF
Cuestionario internet Hernandez Michel
jhonzmichelle
 
PPS
Web20expo Scalable Web Arch
mclee
 
PPTX
Scalable Application Development on AWS
Mikalai Alimenkou
 
PPT
Building a Scalable XML-based Dynamic Delivery Architecture: Standards and Be...
Jerry SILVER
 
PDF
Highly Scalable Java Programming for Multi-Core System
James Gan
 
PPTX
Building Highly Scalable Java Applications on Windows Azure - JavaOne S313978
David Chou
 
PPT
Diary of a Scalable Java Application
Martin Jackson
 
KEY
Writing Scalable Software in Java
Ruben Badaró
 
PDF
Java scalability considerations yogesh deshpande
IndicThreads
 
PDF
Scalable web architecture
Kaushik Paranjape
 
PPTX
Scalable Java Application Development on AWS
Mikalai Alimenkou
 
PDF
Apache Cassandra Lesson: Data Modelling and CQL3
Markus Klems
 
PPT
Scalable Web Architectures and Infrastructure
george.james
 
PDF
天猫后端技术架构优化实践
drewz lin
 
PPTX
Full stack-development with node js
Xuefeng Zhang
 
PPTX
Scalable Web Architecture and Distributed Systems
hyun soomyung
 
PPTX
浅谈电商网站数据访问层(DAL)与 ORM 之适用性
Xuefeng Zhang
 
PPT
Building a Scalable Architecture for web apps
Directi Group
 
PDF
Scalable Django Architecture
Rami Sayar
 
Play framework
Andrew Skiba
 
Cuestionario internet Hernandez Michel
jhonzmichelle
 
Web20expo Scalable Web Arch
mclee
 
Scalable Application Development on AWS
Mikalai Alimenkou
 
Building a Scalable XML-based Dynamic Delivery Architecture: Standards and Be...
Jerry SILVER
 
Highly Scalable Java Programming for Multi-Core System
James Gan
 
Building Highly Scalable Java Applications on Windows Azure - JavaOne S313978
David Chou
 
Diary of a Scalable Java Application
Martin Jackson
 
Writing Scalable Software in Java
Ruben Badaró
 
Java scalability considerations yogesh deshpande
IndicThreads
 
Scalable web architecture
Kaushik Paranjape
 
Scalable Java Application Development on AWS
Mikalai Alimenkou
 
Apache Cassandra Lesson: Data Modelling and CQL3
Markus Klems
 
Scalable Web Architectures and Infrastructure
george.james
 
天猫后端技术架构优化实践
drewz lin
 
Full stack-development with node js
Xuefeng Zhang
 
Scalable Web Architecture and Distributed Systems
hyun soomyung
 
浅谈电商网站数据访问层(DAL)与 ORM 之适用性
Xuefeng Zhang
 
Building a Scalable Architecture for web apps
Directi Group
 
Scalable Django Architecture
Rami Sayar
 
Ad

Similar to Scalable Applications with Scala (20)

PPT
Devoxx
Martin Odersky
 
PDF
Introduction to Scala | Big Data Hadoop Spark Tutorial | CloudxLab
CloudxLab
 
PDF
Scala for Java Programmers
Eric Pederson
 
PDF
Stepping Up : A Brief Intro to Scala
Derek Chen-Becker
 
PDF
Scala.pdf
ssuser155dbc1
 
ODP
Introduction to apache_cassandra_for_developers-lhg
zznate
 
PDF
What is new in java 8 concurrency
kshanth2101
 
PDF
MLlib sparkmeetup_8_6_13_final_reduced
Chao Chen
 
PDF
Martin Odersky: What's next for Scala
Marakana Inc.
 
PPTX
Scala 20140715
Roger Huang
 
PPTX
Intro to Apache Spark and Scala, Austin ACM SIGKDD, 7/9/2014
Roger Huang
 
PPTX
Taxonomy of Scala
shinolajla
 
PPTX
Xebicon2013 scala vsjava_final
Urs Peter
 
PPT
Scala Talk at FOSDEM 2009
Martin Odersky
 
PDF
Scala parallel-collections
Knoldus Inc.
 
PDF
Scala parallel-collections
Knoldus Inc.
 
PPT
Scala in a nutshell by venkat
Venkateswaran Kandasamy
 
PDF
BOF2644 Developing Java EE 7 Scala apps
Peter Pilgrim
 
PPTX
Scala final ppt vinay
Viplav Jain
 
PDF
20160520 what youneedtoknowaboutlambdas
shinolajla
 
Introduction to Scala | Big Data Hadoop Spark Tutorial | CloudxLab
CloudxLab
 
Scala for Java Programmers
Eric Pederson
 
Stepping Up : A Brief Intro to Scala
Derek Chen-Becker
 
Scala.pdf
ssuser155dbc1
 
Introduction to apache_cassandra_for_developers-lhg
zznate
 
What is new in java 8 concurrency
kshanth2101
 
MLlib sparkmeetup_8_6_13_final_reduced
Chao Chen
 
Martin Odersky: What's next for Scala
Marakana Inc.
 
Scala 20140715
Roger Huang
 
Intro to Apache Spark and Scala, Austin ACM SIGKDD, 7/9/2014
Roger Huang
 
Taxonomy of Scala
shinolajla
 
Xebicon2013 scala vsjava_final
Urs Peter
 
Scala Talk at FOSDEM 2009
Martin Odersky
 
Scala parallel-collections
Knoldus Inc.
 
Scala parallel-collections
Knoldus Inc.
 
Scala in a nutshell by venkat
Venkateswaran Kandasamy
 
BOF2644 Developing Java EE 7 Scala apps
Peter Pilgrim
 
Scala final ppt vinay
Viplav Jain
 
20160520 what youneedtoknowaboutlambdas
shinolajla
 
Ad

Recently uploaded (20)

PDF
FME as an Orchestration Tool with Principles From Data Gravity
Safe Software
 
PDF
Kubernetes - Architecture & Components.pdf
geethak285
 
PDF
Why aren't you using FME Flow's CPU Time?
Safe Software
 
PPTX
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Pitch ...
Michele Kryston
 
PDF
Java 25 and Beyond - A Roadmap of Innovations
Ana-Maria Mihalceanu
 
PDF
How to Visualize the ​Spatio-Temporal Data Using CesiumJS​
SANGHEE SHIN
 
PDF
2025_06_18 - OpenMetadata Community Meeting.pdf
OpenMetadata
 
PDF
The Future of Product Management in AI ERA.pdf
Alyona Owens
 
PPTX
reInforce 2025 Lightning Talk - Scott Francis.pptx
ScottFrancis51
 
PDF
Python Conference Singapore - 19 Jun 2025
ninefyi
 
PDF
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
yosra Saidani
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PPTX
Simplifica la seguridad en la nube y la detección de amenazas con FortiCNAPP
Cristian Garcia G.
 
PDF
Unlocking FME Flow’s Potential: Architecture Design for Modern Enterprises
Safe Software
 
PDF
5 Things to Consider When Deploying AI in Your Enterprise
Safe Software
 
PPTX
Paycifi - Programmable Trust_Breakfast_PPTXT
FinTech Belgium
 
PDF
Redefining Work in the Age of AI - What to expect? How to prepare? Why it mat...
Malinda Kapuruge
 
PDF
Cracking the Code - Unveiling Synergies Between Open Source Security and AI.pdf
Priyanka Aash
 
PDF
Hello I'm "AI" Your New _________________
Dr. Tathagat Varma
 
PPTX
Enabling the Digital Artisan – keynote at ICOCI 2025
Alan Dix
 
FME as an Orchestration Tool with Principles From Data Gravity
Safe Software
 
Kubernetes - Architecture & Components.pdf
geethak285
 
Why aren't you using FME Flow's CPU Time?
Safe Software
 
MARTSIA: A Tool for Confidential Data Exchange via Public Blockchain - Pitch ...
Michele Kryston
 
Java 25 and Beyond - A Roadmap of Innovations
Ana-Maria Mihalceanu
 
How to Visualize the ​Spatio-Temporal Data Using CesiumJS​
SANGHEE SHIN
 
2025_06_18 - OpenMetadata Community Meeting.pdf
OpenMetadata
 
The Future of Product Management in AI ERA.pdf
Alyona Owens
 
reInforce 2025 Lightning Talk - Scott Francis.pptx
ScottFrancis51
 
Python Conference Singapore - 19 Jun 2025
ninefyi
 
Salesforce Summer '25 Release Frenchgathering.pptx.pdf
yosra Saidani
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
Simplifica la seguridad en la nube y la detección de amenazas con FortiCNAPP
Cristian Garcia G.
 
Unlocking FME Flow’s Potential: Architecture Design for Modern Enterprises
Safe Software
 
5 Things to Consider When Deploying AI in Your Enterprise
Safe Software
 
Paycifi - Programmable Trust_Breakfast_PPTXT
FinTech Belgium
 
Redefining Work in the Age of AI - What to expect? How to prepare? Why it mat...
Malinda Kapuruge
 
Cracking the Code - Unveiling Synergies Between Open Source Security and AI.pdf
Priyanka Aash
 
Hello I'm "AI" Your New _________________
Dr. Tathagat Varma
 
Enabling the Digital Artisan – keynote at ICOCI 2025
Alan Dix
 

Scalable Applications with Scala