Posts Tagged ‘NIO’

Netty: Using Handlers

Tuesday, April 21st, 2009

Before I start, let me say that I am in no way affiliated with JBoss or Netty (although I may choose to submit future code as part of the open source community).  Therefore, all comments, code samples, etc are mine or a derivative of examples provided by Netty.   I apologize up front for any possible discrepancies or errors (please let me know though, so I can fix and update them).  I believe this documentation to be completely accurate, though.

In Netty, the main injection point into your code or business logic is through the use of handlers.  Handlers follow the interceptor pattern similar to filters in a traditional servlet-based web application.  For more information, see Netty documentation.  Handlers provide an event model that allows an application to monitor incoming/outgoing data, modify the data, convert the data, act upon the data, etc.  In essence, they allow you to completely abstract separate concerns into separate classes without needing to have class A know about class B.  As an example, you can add a log handler to listen for incoming data and print it out.  This handler can be added and removed without modifying any other class.  Handlers, however, are more often used to build protocol stacks in which one filter acts as a codec to decode/encode a stream of bytes into higher level objects which are then acted upon by another handler such as a business logic handler.

(more…)

Scalable NIO Servers - Part 4 - Protocol

Monday, April 20th, 2009

Now that I have narrowed by server down to using JBoss Netty, it’s time to define protocols. This involves two things. First, we need to select the actual protocol itself and second, we need to define how to construct the handlers. Let’s start by defining the handlers. Netty is built upon the principle of handlers, which are more or less the same idea as filters in web applications. This allows you to define functional handlers that perform specific tasks such as compression, security, and business logic. In this manner, one handler does not need to concern itself with another handlers and handlers can be added/removed without affecting the system. However, order and dependency does matter in certain cases. For example, if you have a handler that depends on a translation of bytes into a POJO, then the handler performing the translation must be in the pipeline and occur prior to the application logic. Generally, a semi-complex, yet real world application, for a protocol stack would consist of:

  • Encryption Handlers via SSL processing, handshakes, etc. These handlers take the data stream, decrypt it, and pass the decrypted bytes down the stream
  • Compression Handlers via GZip processing for instance. These handlers take the data stream, uncompress it, and pass the uncompressed bytes down the stream
  • Transport Protocol Handlers for converting transport protocol data in protocol messages such as HTTP. Subsequent handlers use the protocol-specific messages (ie: HTTP message) for interacting with the protoocol data
  • Security Handler for performing security in terms of authentication and authorization checks. These handlers read a header or portions of the data stream and validate the data as being both a valid, authentication user as well as being permissible or authorized or view/modify the associated data
  • Application Protocol Handlers for controlling an actual underlying application/business protocol. These differs from the transport protocols in that they specify the actual business data and commands to perform on the business data. Often times, these handlers parse out a given business command, convert to a business message or POJO, and invoke a business delegate object or handler to process the data
  • Application/Business Handlers for processing business logic related to a specific protocol message.

This type of stack allows easy extensibility and customization. Future handlers can be added in as needed and future application messages can be added in through custom delegates. It also allows the business logic code to only contain business logic without concern for security, compression, etc. The business logic deals solely with business objects and the business protocol. Further, because of this abstraction and because of the abstraction of the transport layers in Netty, supporting other transport protocols such as UDP, serial, USB, etc is an easy task.

(more…)

Scalable NIO Servers - Part 3 - Features

Monday, April 13th, 2009

We have now analyzed various open source NIO servers for performance and memory consumption.  Per my quick, initial testing, only Grizzly, Mina, and Netty were comparable. Now, let’s analyze features and how each of these frameworks use them.  For my purposes, I am going to be looking into the following features that I personally value most important for my project:

  • Intercepting Pattern (ie: Filters)
  • Access to high level, yet effcient, buffers rather than lower level byte buffers
  • Protocol independence and abstraction
  • Socket independence and abstraction
  • Custom protocol support
  • POJO support for encoding/decoding
  • Custom thread model support
  • HTTP support
  • User documentation (user guide, javadoc, source code, examples

(more…)

Scalable NIO Servers - Part 2 - Memory

Thursday, April 9th, 2009

As my ongoing investigation into picking a suitable and highly scalable server for my future applications, today I will be talking about memory.  Yesterday, we discussed performance and saw in general how Netty and Grizzly compared to one another.  Today, we will further extend that by comparing memory usage footprint at start, over time, and how the memory usage compares as a trend with respect to garbage collection.  As GC is one of the biggest culprits of performance, the more efficient the server at conserving memory, the better the performance.  So, without further ado, let’s get to the testing.

(more…)

Scalable NIO Servers - Part 1 - Performance

Tuesday, April 7th, 2009

As a continuation of evaluating an NIO server for my iPhone game, I started with looking at pure performance. First, the following links provide already existant benchmarks:

I took my own samples and quickly ran some basic tests as well to just get a really rough idea.  Note that these metrics should be taken with a large grain of salt as they do not attempt to optimize any of the test libraries and the clients are all run in traditional threads on non-server class machines alongside the actual servers.  Thus, these tests are quickly CPU bound on the client.  The idea is to just get a very rough estimate to compare alongside the above tests.

That point aside, my test basically creates a echo server and echo client.  It then starts up X simultaneous threads that push a simple ‘testing’ string back and forth to the server as fast as it can.  The end result is the time it takes to send a message and receive the echo in milliseconds.

(more…)

Building Scalable Servers

Thursday, April 2nd, 2009

As part of my new iPhone project to build a new game, I need to have a backend server to communicate with all the members in order to sync and dynamically update on the fly.  Knowing the iPhone and its possibilities, I want to plan for a highly optimistic future in which thousands of concurrent connections take place.  The transactions will be minimal and compressed to conserve bandwidth, but will occur frequently to monitor player actions and provide alerts to other players as they are interacted with.  So, my immediate question was where do I start.  I obviously have two primary choices:  HTTP or proprietary.  Let’s look into each option a little more in depth.

(more…)