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

Async and evented programming with Netty and Scala

Async and evented programming with Netty and Scala

In company (OfficeDrop) presentation about how to address the problems of highly scalable systems.

mauriciolinhares

April 11, 2012
Tweet

Other Decks in Programming

Transcript

  1. How? The Java NIO package was added on Java 1.4,

    ten years ago. It has been upgraded now on Java 7 with a new filesystem API.
  2. Why? !   Java needed a bare bones low level

    solution for IO operations; !   Direct to native buffers for IO operations that do not require any kind of CPU interaction to be copied over; !   Asynchronous model, so you don’t have to waste thread time sleeping;
  3. But why? !   Synchronous IO operations are expensive and

    take time to finish; !   Copying memory bounded buffers from one place to the other is a definite way to kill the Garbage Collector; !   Every single thread waiting for an IO operation counts for the thread scheduler, consumes memory and stack resources;
  4. Sync IO is not scalable !   Clients are not

    always talking to servers, most of their time they are just idle; !   Having one thread per client means this thread is parked most of the time, but still consuming resources; !   If you have thousands of clients, having thousands of threads is not an option in commodity hardware (and not even in specialized hardware);
  5. What you need is async ! Fixed amount of threads

    (per CPU thread) waiting for events to happen; ! Everyone consumes work from a (possibly bounded) queue ; ! There is time wasted in sleep calls, everyone is CPU bound as much as possible;
  6. What if I need more control? !   The NIO

    library is rather complicated to handle directly for socket servers; ! Most socket servers run in a request-response cycle with a specific message format; ! And then we have NIO frameworks to do our job;
  7. Netty comes to the rescue ! Direct child of the

    venerable Mina library; !   Builds upon the concepts defined by Mina, you have encoders, decoders, an execution model and the handler that receives messages; ! Hides most of the complexity of building a NIO client/ server from your code, you only have to care about receiving messages and working with them;