Netty: asynchronous data transfer

8,359 views
7,758 views

Published on

A short presentation about practical aspects of asynchronous data transfer with Netty

HTML version: http://vcherkassky.github.com/reveal.js/netty.html

Additional resources (from last slide):
* https://github.com/netty/netty
* http://seeallhearall.blogspot.co.uk/2012/05/netty-tutorial-part-1-introduction-to.html

Done with reveal.js: https://github.com/hakimel/reveal.js/

Published in: Technology
0 Comments
31 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
8,359
On SlideShare
0
From Embeds
0
Number of Embeds
74
Actions
Shares
0
Downloads
173
Comments
0
Likes
31
Embeds 0
No embeds

No notes for slide

Netty: asynchronous data transfer

  1. 1. NettyList("Asynchronous", "data", "transfer") ::: List("practical", "aspects") Victor Cherkassky "vcherkassky@gmail.com"
  2. 2. What is Netty IO library IOcat Files > Networking
  3. 3. What is Netty IO library asynchronous ChannelFuture future = channel.write(message)future.addListener(WRITE_COMPLETE_LISTENER)
  4. 4. What is Netty IO library asynchronous non-blocking      /**        * This method blocks until input data is available        */ InputStream.read()
  5. 5. What is Netty IO library asynchronous non-blocking multi-protocolHTTP | WebSocket | Protobuf | Binary TCP | UDP
  6. 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. 7. blocking vs non-blocking Hardware blocking write
  8. 8. blocking vs non-blockingHardware non-blocking write: DMA
  9. 9. blocking vs non-blocking OSselect()/poll() - traditional POSIX polling O(n)epoll() - event-based polling O(1), Linux 2.5.44+kqueue - FreeBSD, Mac OS XI/O Completion Ports - Windows NT 3.5+, Solaris 10+
  10. 10. blocking vs non-blocking JavaSelectableChannel.register(selector) - blocking operation,registers an "interest" for IO eventsSelector.select - non-blocking operation, checks foroccurred events
  11. 11. blocking vs non-blocking Java OS select() / poll() / kqueueChannel.register & Selector.select I/O Completion Ports epoll() - Java 1.6+
  12. 12. Thread model Tomcat 1 request = 1 threadfast for ~1000 clients too much memory
  13. 13. Thread model Node.jsall requests = 1 thread super scalablelimited error handling
  14. 14. Thread model Nettymany requests = 1 thread flexible model
  15. 15. Netty web Server
  16. 16. Netty web Client
  17. 17. Channel Pipeline
  18. 18. All terms togetherChannel (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. 19. Creating an HTTP server Set up thread pools boss pool for handling incoming connections worker pool for processing IOpiaefnlEeuo bsPo =Eeuosnwahdhedol) rvt ia xctr osol xctr.eCceTraPo(;piaefnlEeuo wrePo =Eeuosnwahdhedol rvt ia xctr okrol xctr.eCceTraPo(; )
  20. 20. Creating an HTTP server Provide ChannelFactorypiaefnlCanlatr canlatr = rvt ia hneFcoy hneFcoy nwNoevrokthneFcoybsPo,wrePo) e iSreSceCanlatr(osol okrol;
  21. 21. Creating an HTTP server Set up PipelineFactorypiaefnlCanlieieatr 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. 22. Creating an HTTP server Put all together with BootstrappiaefnlSreBosrpbosrp=nwSreBosrpca rvt ia evrotta otta e evrotta(hneFcoy; nlatr) ...bosrpstieieatr(ieieatr) otta.ePplnFcoypplnFcoy;/ Otosfraprn canl-i acpscnetos / pin o aet hne t cet oncinbosrpstpin"oaAdes,nwIeSceAdespr); otta.eOto(lcldrs" e ntoktdrs(ot)bosrpstpin"esAdes,tu) otta.eOto(ruedrs" re;/ Otosfriscide -te poesI / pin o t hlrn hy rcs Obosrpstpin"hl.cNDly,tu) otta.eOto(cidtpoea" re;bosrpstpin"hl.eplv" fle; otta.eOto(cidkeAie, as)
  23. 23. Creating an HTTP server Writing a custom handlercasCsoHnlretnsSmlCanlptemade { ls utmade xed ipehneUsraHnlr ... @vrie Oerd pbi vi msaeeevdCanladeCnetcx Msae ulc od esgRcie(hneHnlrotx t, esgEvn 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. 24. Creating an HTTP serverGather channels for future closingpiaefnlCanlru alhnes=nwDfuthneGop rvt ia hneGop lCanl e ealCanlru() .../ aanisd orcso hnlr / gi nie u utm adecasCsoHnlretnsSmlCanlptemade { ls utmade xed ipehneUsraHnlr ... @vrie Oerd pbi vi canlpnCanladeCnetcx Canltt ulc od hneOe(hneHnlrotx t, hneSaeEete trw Ecpin{ vn ) hos xeto Canlcanl=egthne(; hne hne .eCanl) alhnesadcanl; lCanl.d(hne) }}
  25. 25. Creating an HTTP server Release resources close gathered channels release external resourcesalhnescoe)aatnnerpil(; lCanl.ls(.wiUitrutby)bosrprlaexenleore(; otta.eesEtraRsucs)
  26. 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. 27. TCP keepAliveprolongs each TCP connectionmakes NAT router happy
  28. 28. TCP noDelay Nagles algorithmglues up small TCP packetstries to reduce bandwidthincreases latency
  29. 29. TCPnodelay, TCPkeepalive To use, or not to use?
  30. 30. Dont ask,measure!
  31. 31. Thank youAdditional resources netty @ github introductory article

×