Netty
List("Asynchronous", "data", "transfer")
      ::: List("practical", "aspects")




                            Victor Cherkassky
                    "vcherkassky@gmail.com"
What is Netty
         IO library



        IO
cat Files > Networking
What is Netty
                   IO library
                   asynchronous

    ChannelFuture future = channel.write(message)
future.addListener(WRITE_COMPLETE_LISTENER)
What is Netty
                          IO library
                          asynchronous
                          non-blocking
      /**
        * This method blocks until input data is available
        */
           InputStream.read()
What is Netty
         IO library
         asynchronous
         non-blocking
         multi-protocol

HTTP | WebSocket | Protobuf
         | Binary
     TCP | UDP
blocking vs non-blocking
              blocking (OIO)
Ipttemi =nwFlIpttem"nu.i";
 nuSra s  e ienuSra(iptbn)
itbt =i.ed) / crettra wisfrrsl o err
 n ye  sra(; / urn hed at o eut r ro




            non-blocking (NIO)
wie(re {
 hl tu)
  slco.eet) / rqetn eet fo mlil canl
   eetrslc(; / eusig vns rm utpe hnes
  Ieao i =slco.eetdes)ieao(;
   trtr t  eetrslceKy(.trtr)
  wie(thset) {
   hl i.aNx()
    SlcoKyky=(eetoKy i.et)
    eetre e   Slcine) tnx(;
    hnlKyky;
    adee(e)
    i.eoe)
    trmv(;
  }
}
blocking vs non-blocking
   Hardware blocking write
blocking vs non-blocking
Hardware non-blocking write: DMA
blocking vs non-blocking
                     OS
select()/poll() - traditional POSIX polling O(n)
epoll() - event-based polling O(1), Linux 2.5.44+
kqueue - FreeBSD, Mac OS X
I/O Completion Ports - Windows NT 3.5+, Solaris 10+
blocking vs non-blocking
                        Java
SelectableChannel.register(selector) - blocking operation,
registers an "interest" for IO events
Selector.select - non-blocking operation, checks for
occurred events
blocking vs non-blocking

            Java                            OS
                                     select() / poll() / kqueue
Channel.register & Selector.select   I/O Completion Ports
                                     epoll() - Java 1.6+
Thread model

    Tomcat
 1 request = 1 thread
fast for ~1000 clients
  too much memory
Thread model

    Node.js
all requests = 1 thread
     super scalable
limited error handling
Thread model

       Netty
many requests = 1 thread
    flexible model
Netty web Server
Netty web Client
Channel Pipeline
All terms together
Channel (socket read/write)
channel Buffer (byte array)
channel Event (message)
channel Pipeline (message bus)
channel Handler (message handler)
  Upstream Event (from socket)
  Downstream Event (to socket)
Creating an HTTP server
        Set up thread pools
   boss pool for handling incoming connections
   worker pool for processing IO

piaefnlEeuo bsPo =Eeuosnwahdhedol)
 rvt ia xctr osol  xctr.eCceTraPo(
;
piaefnlEeuo wrePo =Eeuosnwahdhedol
 rvt ia xctr okrol  xctr.eCceTraPo
(;
 )
Creating an HTTP server
    Provide ChannelFactory
piaefnlCanlatr canlatr =
 rvt ia hneFcoy hneFcoy
   nwNoevrokthneFcoybsPo,wrePo)
   e iSreSceCanlatr(osol okrol;
Creating an HTTP server
         Set up PipelineFactory
piaefnlCanlieieatr pplnFcoy=
 rvt ia hnePplnFcoy ieieatr
   nwCanlieieatr( {
   e hnePplnFcoy)
  @vrie
  Oerd
  pbi Canlieiegtieie)trw Ecpin{
  ulc hnePpln ePpln( hos xeto

      Canlieiep=Canl.ieie)
      hnePpln    hnesppln(;
      padat"tpevroe" nwHtSreCdc);
      .dLs(htSreCdc, e tpevroe()

      padat"ade" nwCsoRqetade(lCanl);
      .dLs(hnlr, e utmeusHnlralhnes)

      rtr p
      eun ;
  }
};
Creating an HTTP server
 Put all together with Bootstrap
piaefnlSreBosrpbosrp=nwSreBosrpca
 rvt ia evrotta otta e evrotta(h
neFcoy;
 nlatr)

 ...

bosrpstieieatr(ieieatr)
 otta.ePplnFcoypplnFcoy;

/ Otosfraprn canl-i acpscnetos
 / pin o aet hne   t cet oncin
bosrpstpin"oaAdes,nwIeSceAdespr);
 otta.eOto(lcldrs" e ntoktdrs(ot)
bosrpstpin"esAdes,tu)
 otta.eOto(ruedrs" re;

/ Otosfriscide -te poesI
 / pin o t hlrn hy rcs O
bosrpstpin"hl.cNDly,tu)
 otta.eOto(cidtpoea" re;
bosrpstpin"hl.eplv" fle;
 otta.eOto(cidkeAie, as)
Creating an HTTP server
    Writing a custom handler
casCsoHnlretnsSmlCanlptemade {
 ls utmade xed ipehneUsraHnlr
   ...
  @vrie
   Oerd
  pbi vi msaeeevdCanladeCnetcx Msae
   ulc od esgRcie(hneHnlrotx t, esgE
vn e trw Ecpin{
 et ) hos xeto
    HtRqetrq=(tpeus)egtesg(;
     tpeus e   HtRqet .eMsae)
    / pouersos adwiei t acanl
     / rdc epne n rt t o hne
    HtRsos rs=nwDfuttpepneHT__,O)
     tpepne e   e ealHtRsos(TP11 K;
    Canluueftr =Canl.rt(t.eCanl) rs;
     hneFtr uue   hneswiecxgthne(, e)
    ftr.dLsee(hneFtrLsee.LS)
     uueaditnrCanluueitnrCOE;
  }
}
Creating an HTTP server
Gather channels for future closing
piaefnlCanlru alhnes=nwDfuthneGop
 rvt ia hneGop lCanl    e ealCanlru(
)
    ...
/ aanisd orcso hnlr
 / gi nie u utm ade
casCsoHnlretnsSmlCanlptemade {
 ls utmade xed ipehneUsraHnlr
    ...
    @vrie
    Oerd
  pbi vi canlpnCanladeCnetcx Canltt
   ulc od hneOe(hneHnlrotx t, hneSae
Eete trw Ecpin{
 vn ) hos xeto
    Canlcanl=egthne(;
    hne hne   .eCanl)
    alhnesadcanl;
    lCanl.d(hne)
  }
}
Creating an HTTP server
        Release resources
         close gathered channels
         release external resources

alhnescoe)aatnnerpil(;
 lCanl.ls(.wiUitrutby)
bosrprlaexenleore(;
 otta.eesEtraRsucs)
TCPnodelay, TCPkeepalive
  Remember that bootstrap code?

 / Otosfriscide -te poesI
  / pin o t hlrn hy rcs O
 bosrpstpin"hl.cNDly,tu)
  otta.eOto(cidtpoea" re;
 bosrpstpin"hl.eplv" fle;
  otta.eOto(cidkeAie, as)




   What do these options mean?
TCP keepAlive

prolongs each TCP connection
makes NAT router happy
TCP noDelay
   Nagle's algorithm

glues up small TCP packets
tries to reduce bandwidth
increases latency
TCPnodelay, TCPkeepalive

    To use,
 or not to use?
Don't ask,
measure!
Thank you
Additional resources
    netty @ github
    introductory article

Netty: asynchronous data transfer

  • 1.
    Netty List("Asynchronous", "data", "transfer") ::: List("practical", "aspects") Victor Cherkassky "vcherkassky@gmail.com"
  • 2.
    What is Netty IO library IO cat Files > Networking
  • 3.
    What is Netty IO library asynchronous ChannelFuture future = channel.write(message) future.addListener(WRITE_COMPLETE_LISTENER)
  • 4.
    What is Netty IO library asynchronous non-blocking       /**         * This method blocks until input data is available         */ InputStream.read()
  • 5.
    What is Netty IO library asynchronous non-blocking multi-protocol HTTP | WebSocket | Protobuf | Binary TCP | UDP
  • 6.
    blocking vs non-blocking blocking (OIO) Ipttemi =nwFlIpttem"nu.i"; nuSra s e ienuSra(iptbn) itbt =i.ed) / crettra wisfrrsl o err n ye sra(; / urn hed at o eut r ro non-blocking (NIO) wie(re { hl tu) slco.eet) / rqetn eet fo mlil canl eetrslc(; / eusig vns rm utpe hnes Ieao i =slco.eetdes)ieao(; trtr t eetrslceKy(.trtr) wie(thset) { hl i.aNx() SlcoKyky=(eetoKy i.et) eetre e Slcine) tnx(; hnlKyky; adee(e) i.eoe) trmv(; } }
  • 7.
    blocking vs non-blocking Hardware blocking write
  • 8.
    blocking vs non-blocking Hardwarenon-blocking write: DMA
  • 9.
    blocking vs non-blocking OS select()/poll() - traditional POSIX polling O(n) epoll() - event-based polling O(1), Linux 2.5.44+ kqueue - FreeBSD, Mac OS X I/O Completion Ports - Windows NT 3.5+, Solaris 10+
  • 10.
    blocking vs non-blocking Java SelectableChannel.register(selector) - blocking operation, registers an "interest" for IO events Selector.select - non-blocking operation, checks for occurred events
  • 11.
    blocking vs non-blocking Java OS select() / poll() / kqueue Channel.register & Selector.select I/O Completion Ports epoll() - Java 1.6+
  • 12.
    Thread model Tomcat 1 request = 1 thread fast for ~1000 clients too much memory
  • 13.
    Thread model Node.js all requests = 1 thread super scalable limited error handling
  • 14.
    Thread model Netty many requests = 1 thread flexible model
  • 15.
  • 16.
  • 17.
  • 18.
    All terms together Channel(socket read/write) channel Buffer (byte array) channel Event (message) channel Pipeline (message bus) channel Handler (message handler) Upstream Event (from socket) Downstream Event (to socket)
  • 19.
    Creating an HTTPserver Set up thread pools boss pool for handling incoming connections worker pool for processing IO piaefnlEeuo bsPo =Eeuosnwahdhedol) rvt ia xctr osol xctr.eCceTraPo( ; piaefnlEeuo wrePo =Eeuosnwahdhedol rvt ia xctr okrol xctr.eCceTraPo (; )
  • 20.
    Creating an HTTPserver Provide ChannelFactory piaefnlCanlatr canlatr = rvt ia hneFcoy hneFcoy nwNoevrokthneFcoybsPo,wrePo) e iSreSceCanlatr(osol okrol;
  • 21.
    Creating an HTTPserver Set up PipelineFactory piaefnlCanlieieatr pplnFcoy= rvt ia hnePplnFcoy ieieatr nwCanlieieatr( { e hnePplnFcoy) @vrie Oerd pbi Canlieiegtieie)trw Ecpin{ ulc hnePpln ePpln( hos xeto Canlieiep=Canl.ieie) hnePpln hnesppln(; padat"tpevroe" nwHtSreCdc); .dLs(htSreCdc, e tpevroe() padat"ade" nwCsoRqetade(lCanl); .dLs(hnlr, e utmeusHnlralhnes) rtr p eun ; } };
  • 22.
    Creating an HTTPserver Put all together with Bootstrap piaefnlSreBosrpbosrp=nwSreBosrpca rvt ia evrotta otta e evrotta(h neFcoy; nlatr) ... bosrpstieieatr(ieieatr) otta.ePplnFcoypplnFcoy; / Otosfraprn canl-i acpscnetos / pin o aet hne t cet oncin bosrpstpin"oaAdes,nwIeSceAdespr); otta.eOto(lcldrs" e ntoktdrs(ot) bosrpstpin"esAdes,tu) otta.eOto(ruedrs" re; / Otosfriscide -te poesI / pin o t hlrn hy rcs O bosrpstpin"hl.cNDly,tu) otta.eOto(cidtpoea" re; bosrpstpin"hl.eplv" fle; otta.eOto(cidkeAie, as)
  • 23.
    Creating an HTTPserver Writing a custom handler casCsoHnlretnsSmlCanlptemade { ls utmade xed ipehneUsraHnlr ... @vrie Oerd pbi vi msaeeevdCanladeCnetcx Msae ulc od esgRcie(hneHnlrotx t, esgE vn e trw Ecpin{ et ) hos xeto HtRqetrq=(tpeus)egtesg(; tpeus e HtRqet .eMsae) / pouersos adwiei t acanl / rdc epne n rt t o hne HtRsos rs=nwDfuttpepneHT__,O) tpepne e e ealHtRsos(TP11 K; Canluueftr =Canl.rt(t.eCanl) rs; hneFtr uue hneswiecxgthne(, e) ftr.dLsee(hneFtrLsee.LS) uueaditnrCanluueitnrCOE; } }
  • 24.
    Creating an HTTPserver Gather channels for future closing piaefnlCanlru alhnes=nwDfuthneGop rvt ia hneGop lCanl e ealCanlru( ) ... / aanisd orcso hnlr / gi nie u utm ade casCsoHnlretnsSmlCanlptemade { ls utmade xed ipehneUsraHnlr ... @vrie Oerd pbi vi canlpnCanladeCnetcx Canltt ulc od hneOe(hneHnlrotx t, hneSae Eete trw Ecpin{ vn ) hos xeto Canlcanl=egthne(; hne hne .eCanl) alhnesadcanl; lCanl.d(hne) } }
  • 25.
    Creating an HTTPserver Release resources close gathered channels release external resources alhnescoe)aatnnerpil(; lCanl.ls(.wiUitrutby) bosrprlaexenleore(; otta.eesEtraRsucs)
  • 26.
    TCPnodelay, TCPkeepalive Remember that bootstrap code? / Otosfriscide -te poesI / pin o t hlrn hy rcs O bosrpstpin"hl.cNDly,tu) otta.eOto(cidtpoea" re; bosrpstpin"hl.eplv" fle; otta.eOto(cidkeAie, as) What do these options mean?
  • 27.
    TCP keepAlive prolongs eachTCP connection makes NAT router happy
  • 28.
    TCP noDelay Nagle's algorithm glues up small TCP packets tries to reduce bandwidth increases latency
  • 29.
    TCPnodelay, TCPkeepalive To use, or not to use?
  • 30.
  • 31.
    Thank you Additional resources netty @ github introductory article