Slide 1

Slide 1 text

Scalable async and evented programming with Netty and Scala Maurício Linhares

Slide 2

Slide 2 text

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.

Slide 3

Slide 3 text

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;

Slide 4

Slide 4 text

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;

Slide 5

Slide 5 text

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);

Slide 6

Slide 6 text

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;

Slide 7

Slide 7 text

High level example – Websockets with Jetty Project done last year with @rcarvalho

Slide 8

Slide 8 text

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;

Slide 9

Slide 9 text

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;

Slide 10

Slide 10 text

The channel pipeline Decoder Encoder Execution Model Messaging handler

Slide 11

Slide 11 text

Low level example PostgreSQL async driver