Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Asynchronous programming in Scala

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.

Asynchronous programming in Scala

The presentation show the basic introductory concept of asynchronous programming, a programming model that allow application scalability and high performance across distributed systems.

Avatar for binkabir

binkabir

July 21, 2018
Tweet

Other Decks in Programming

Transcript

  1. Nairobi JVM MeetUp Asynchronous Programming in Scala @_binkabir I never

    lose. I either win or learn – Nelson Mandela Nigerian name: Idris Muhammad Kabir Kenyan Name: Kamau
  2. Synchronous programming •  Blocking •  Logic is expressed as a

    sequence of steps. •  Calling a method which adds two number looks the same as calling a method which does I/O e.g http request, DB operations.
  3. Challenges with Synchronous? •  Code written in a blocking fashion

    doesn’t scale •  Each Business logic requires a threads and threads are expensive resources. Hence, synchronous quickly reach the thread limits
  4. Why Asynchronous •  Runs on Less Thread(Business logic are interleaved)

    •  Efficient Utilization of OS-level thread •  Performance
  5. Styles of Asynchronous Code •  Callbacks - Asynchronous methods accepts

    an additional callback parameter •  Wrappers( Containers) - objects that represent an asynchronous operation e.g Future, Task or IO
  6. •  The ability to grab a computation’s results and treat

    is a value make wrappers preferred option •  Parallel data processing (multiple I/O operations) •  Side effects are contained and I/O is clearly demarcated
  7. Asynchronous Object in Scala Futures provide a way to reason

    about performing many operations in parallel– in an efficient and non-blocking way. A Future is a placeholder object for a value that may not yet exist Return 2 possible values •  Value => Success •  Exception => Failure
  8. •  Asynchronous computations that field futures are created with Future.apply

    call and are computed using a supplied ExecutionContext, which can be backed by a Thread pool.
  9. Recover - Method •  Creates a new future that will

    handle any matching throwable that this future might contain. If there is no match.
  10. recoverWith - method Creates a new future that will handle

    any matching throwable that this future might contain by assigning it a value of another future.
  11. Demo Time •  Consider a task of writing an application

    that gets a gets yours gravatar then look up same user’s github profile asynchronously.