promise of a better future by rahul goma phulore and pooja akshantal, thoughtworks - presented at...

20
Promise of a better future Pooja Akshantal Rahul Goma Phulore

Upload: thoughtworks

Post on 29-Nov-2014

879 views

Category:

Technology


3 download

DESCRIPTION

With the recent, vivid trend towards multicore hardware and the ever growing application requirements, concurrency is no more a niche area it used to be, and is slowly becoming a norm. In this talk, we will talk about promises/futures, one of the concurrency models that has risen to the occasion. We will look at what they are, how they're implemented and used in Java and Javascript. We will see how Scala, with its functional paradigm and greater abstraction capabilities, avoids "callback hell" typically associated with the model, allows writing of concurrent code in "direct style", and thereby greatly reduces the cognitive burden, allowing you to focus on application logic better.

TRANSCRIPT

Page 1: Promise of a better future by Rahul Goma Phulore and Pooja Akshantal, ThoughtWorks - presented at Pune Scala Symposium 2014, ThoughtWorks

Promise of a better future

Pooja AkshantalRahul Goma Phulore

Page 2: Promise of a better future by Rahul Goma Phulore and Pooja Akshantal, ThoughtWorks - presented at Pune Scala Symposium 2014, ThoughtWorks

Who are we?

Page 3: Promise of a better future by Rahul Goma Phulore and Pooja Akshantal, ThoughtWorks - presented at Pune Scala Symposium 2014, ThoughtWorks

What we are going to talk about

• How concurrency and parallelism are different ideas

• Why concurrency is important• Futures and promises • DEMO!• Pitfalls

Page 4: Promise of a better future by Rahul Goma Phulore and Pooja Akshantal, ThoughtWorks - presented at Pune Scala Symposium 2014, ThoughtWorks

Concurrency ≠ ParallelismConcurrency is a program-structuring technique in which there are multiple threads of control

A parallel program is one that uses a multiplicity of computational hardware (e.g. multiple processor cores)

Structure Execution

Dealing with lots of things at once

Doing lots of things at once

Page 5: Promise of a better future by Rahul Goma Phulore and Pooja Akshantal, ThoughtWorks - presented at Pune Scala Symposium 2014, ThoughtWorks

Orthogonality illustrated

Javascript promises, Python

generators

Java and Scala futures

Well… lots of things Parallel collections

P

C

NP

NC

Page 6: Promise of a better future by Rahul Goma Phulore and Pooja Akshantal, ThoughtWorks - presented at Pune Scala Symposium 2014, ThoughtWorks

Fall of Moore’s law

Page 7: Promise of a better future by Rahul Goma Phulore and Pooja Akshantal, ThoughtWorks - presented at Pune Scala Symposium 2014, ThoughtWorks

Age of multicore

• Multicore has become a norm!

• Need to exploit parallelism to perform

• Good concurrency abstractions a way to get there

Page 8: Promise of a better future by Rahul Goma Phulore and Pooja Akshantal, ThoughtWorks - presented at Pune Scala Symposium 2014, ThoughtWorks

Futures and promises

Page 9: Promise of a better future by Rahul Goma Phulore and Pooja Akshantal, ThoughtWorks - presented at Pune Scala Symposium 2014, ThoughtWorks

Futures and promises

• Can be thought of as a single concurrency abstraction

Page 10: Promise of a better future by Rahul Goma Phulore and Pooja Akshantal, ThoughtWorks - presented at Pune Scala Symposium 2014, ThoughtWorks

Future ≈ Cheap thread

• Futures are multiplexed dynamically onto threads as needed.

• Managed by ExecutionContext.• It’s practical to have hundreds of thousands

going at once.

• Spawn away!

Page 11: Promise of a better future by Rahul Goma Phulore and Pooja Akshantal, ThoughtWorks - presented at Pune Scala Symposium 2014, ThoughtWorks

Code Time

Page 12: Promise of a better future by Rahul Goma Phulore and Pooja Akshantal, ThoughtWorks - presented at Pune Scala Symposium 2014, ThoughtWorks

Async and non-blocking

• Goal: Do not block current thread while waiting for the result of the future.

• Callbacks:– Register callback which is invoked asynchronously

when future is completed– Async computations do not block

Page 13: Promise of a better future by Rahul Goma Phulore and Pooja Akshantal, ThoughtWorks - presented at Pune Scala Symposium 2014, ThoughtWorks

Callbacks?!

Page 14: Promise of a better future by Rahul Goma Phulore and Pooja Akshantal, ThoughtWorks - presented at Pune Scala Symposium 2014, ThoughtWorks

There is a solution!

• Higher-order functions / combinators• for-comprehensions• Other abstractions

Page 15: Promise of a better future by Rahul Goma Phulore and Pooja Akshantal, ThoughtWorks - presented at Pune Scala Symposium 2014, ThoughtWorks

Running example

• Collect statistics for a facebook page

• For every post on the page:– Collect all likes– Collect all comments

• Aggregate results into popularity score

Page 16: Promise of a better future by Rahul Goma Phulore and Pooja Akshantal, ThoughtWorks - presented at Pune Scala Symposium 2014, ThoughtWorks

Code Time

Page 17: Promise of a better future by Rahul Goma Phulore and Pooja Akshantal, ThoughtWorks - presented at Pune Scala Symposium 2014, ThoughtWorks

Other parallels

• Error recovery combinators

– recover– recoverWith

• Collection operations

– map: (Seq[A], A ⇒ B) ⇒ Seq[B]

mapF: (Seq[A], A ⇒ Future[B]) ⇒ Future[Seq[B]]

– filter: (Seq[A], A ⇒ Boolean) ⇒ Seq[A]

filterF: (Seq[A], A ⇒ Future[Boolean]) ⇒ Future[Seq[A]]

Page 18: Promise of a better future by Rahul Goma Phulore and Pooja Akshantal, ThoughtWorks - presented at Pune Scala Symposium 2014, ThoughtWorks

Pitfalls

• Interactions with thread-unsafe libraries• ThreadLocal variables• Retries, time-outs not very natural• Simpler than other concurrency models in

many ways, but still incur some cognitive cost

Page 19: Promise of a better future by Rahul Goma Phulore and Pooja Akshantal, ThoughtWorks - presented at Pune Scala Symposium 2014, ThoughtWorks

Credits

• Heather Miller, for her material on relationship between futures and promises

• Simon Marlow and Rob Pike, for their material on concurrency and parallelism

• Josh Suereth, for his Github example (Inspiration for our facebook example)

• Scala, Play, and Akka teams for all their amazing work

Page 20: Promise of a better future by Rahul Goma Phulore and Pooja Akshantal, ThoughtWorks - presented at Pune Scala Symposium 2014, ThoughtWorks

Thank You!