Jackson Oliveira
Marcelo Serpa
About me
❏ Software Architect at ilegra
❏ AWS & GCP
❏ Google Cloud Architect Certified
❏ Microservices / DevOPS
❏ Mortal Kombat Player :D
❏ https://github.com/marceloserpa/
❏ https://www.linkedin.com/in/marceloserpa/
About me
❏ Father
❏ Software Architect at ilegra
❏ Devops Engineer Specialist
❏ Google Cloud Architect Certified
❏ SOA Specialist
❏ Football fun
❏ Github
❏ Twitter
❏ Blog
Exercise 1
● Create a TCP server using socket API. Use the template available here.
● It needs to be able to process more than one request at time (use threads)
Let's review what we did and think about It
How works the traditional way
● thread per request model
● One thread for the entire request flow
● limitations 1 thread == 1MB (around it)
Blocking and synchronous
● Sequential
● Unnecessary waiting time
● Total time is high
Expectations for modern applications
● high throughput
● scalability
● lower cost (BE CAREFUL)
Java NIO
● Java abstraction to deal with low level I/O
● Work on top of channels and selectors (scales better)
● Is much more efficient than native sockets and easier to deal with.
Java NIO - Can become complex as well - link
Networking programing
● Is inhirintanly complex
○ It’s more than programming languages or algorithms
○ Requires specific skills
○ Network programs have their own performance requirements
○ Needs to be efficient, otherwise they won't attend the current market requirements.
What is Netty
“Netty is a NIO client server framework which enables quick and easy development
of network applications such as protocol servers and clients. It greatly simplifies
and streamlines network programming such as TCP and UDP socket server.”
● A networking programming server
● open source and written in Java
● Asynchronous and event driven
● Enforces a better design. Decouples business logic from network programming details.
● Configurable
● Extensible
Companies using Netty
● Apple
● Twitter
○ using Finagle their Netty-basead framework in inter-system communication
● Facebook
○ uses Netty in Nifty - Thrift service
● Google
● Square
● Instagram
● Netflix
Companies using Netty - Netflix
Companies using Netty - Twitter
OSS project using Netty
● Infinispan
● HornetQ
● Vert.x
● Apache Cassandra
● ElasticSearch
Cassandra - More nodes, worst the performance
Cassandra - Streaming data between nodes - Before
Cassandra - Streaming data between nodes
Cassandra - Streaming data between nodes - Before
Cassandra - Streaming data between nodes - Before
Cassandra - Streaming data between nodes - Before
Cassandra - Streaming data between nodes - Now
Cassandra - Streaming data between nodes - Now
Startups using Netty
● Firebase
● Urban Airship
Non-Blocking and asynchronous
● Initialize something now
● Using Threads Efficiently
● Scalability
● More efficient (doing more with less resources)
● concurrency
Core components
● EventLoop
● Channel
● Callback
● Future
● Event Handler
Netty - High level arch
Echo Server Example
Echo Server Example
Eventloop impl in use.
There are others...
Echo Server Example
Utility that helps glue
all pieces
Echo Server Example
Transport type defined
for the channel
Echo Server Example
Setting handler on the
pipeline chain
Echo Server Example
Bind the server and
channel and wait this
process complete
Echo Server Example
Block the current
thread and wait until
the channel is closed
Echo Client - Example
Echo Client - Example
What is EventLoop? How it works?
● Threading model
● Control flow
● Dispatching events to ChannelHandlers
EventLoop -Task Scheduling - run later
scheduling task to run
after 10 seconds
EventLoop -Task Scheduling - periodically
scheduling task to run
every 10 seconds
staring in 10 seconds
What is EventLoop group?
● Allocate EventLoop for a Channel
● Uses round-robin to distribute load
● An application needs a
least 1 EventLoopGroup
Channel
● Socket
● Receive events
● Transport layer
● Notify Channel Handlers
ChannelHandler
● Decoupled business from networking
● Implements the logic
● React to event notification
● ChannelInboundHandlerAdapter
○ channelRead()
○ channelReadComplete()
○ exceptionCaught()
● ChannelHandler are invoke for different events
● Hook lifecycle
ChannelPipeline
● chain of ChannelHandler
● propagation flow
● order to execute channel handlers
● ChannelHandler is installed as follow:
○ a Channelnitializaer is registered to server bootstrap
○ when Channelnitializaer.init() is called it install the channels handlers in the pipeline
Bootstraper
for clients (Bootstrap)
● establish connection
● connect to remote peer
● requires only one EventLoopGroup
for servers (ServerBootstrap)
● listening to income connections
● requires two EventLoopGroup:
○ one for ServerChannel
○ others to registered all channel that you
want
Bytebuf
● Byte container
● Alternative for NIO ByteBuffer
● Used to data
Codecs
● Transforms streams of byte from one
format to another
Encoder Decoder
Codec Example - Bootstrap
Creating a
ServerBootstrap and
registered
EventLoopGroup
Codec Example - Bootstrap
Registered Initializer
to configure Pipeline
Codec Example
Registered codecs
and aggregator on
ChannelPipeline
Codec Example - Handler part 1
Use decoder to extract
parameters
Codec Example - Handler part 1
Call service to handle
business rules
Codec Example - Handler part 2
Solution based Netty - ServiceTalk
https://github.com/diegopacheco/java-pocs/tree/master/pocs/servicetalk-fun
Solution based Netty - ServiceTalk
● gRPC
● HTTP/2
● Long-polling
● Smart Client
○ Client side load balancing
○ retry
● Modularization
○ use just you need
Solution based Netty - Armeria
https://github.com/diegopacheco/java-pocs/tree/master/pocs/armerica-fun/src/main/java
Solution based Netty - Armeria
● HTTP/2
● Integration with gRPC and Thrift
● Metrics
● Circuit breaker
● Client-side health-check, retries and client side load balancing
● Distributed Tracing with Zipkin
● Service discovery (DNS and Zookeeper)
● Built on top of Reactive Streams and Java 8 CompletableFuture
Solution based Netty - Reactor Netty
https://github.com/diegopacheco/java-pocs/tree/master/pocs/reactor-netty-simple
Solution based Netty - Reactor Netty
● Support reactive streams specification
● HTTP, UDP and TCP
● Schedulers
● Parallel Flux
Performance/Scalability > Developer Experience
● Number of lines of code is not a problem
● IFs (if not shells) is not a problem
● You don't deal with this code everyday
● There are books and materials on the web (stack overflow, etc...)
● Less lines of code does not means is better
● Spring might be “feeling” better but has worst performances and more deps
● Developers should care more about the business
○ User Experience -> slow server
○ Cost Reduction -> doing more with less(less cpu and memory used - more toughtput)
○ Reliability -> netty is battle tested by Silicon valley biggest IT companies in the world.
Netty
Thank you!
Exercise 2
● Change the code made on exercise 1, this time using Netty as a a server
implementation and client.
● Use this template in order to get started.
Exercise 3
● For this exercise we are gonna use same problem as described on exercise
2, but this time using an http interface rather than exposing an TCP socket.
● Server should be called like this:
localhost:8080/training/netty/sum?num1=1&num2=4
● Use this template in order to get started.
Exercise 4
● Do the same implementation of exercise 3 using ServiceTalk with JaxRS
● Use this template in order to get started.

Netty training

  • 1.
  • 2.
    About me ❏ SoftwareArchitect at ilegra ❏ AWS & GCP ❏ Google Cloud Architect Certified ❏ Microservices / DevOPS ❏ Mortal Kombat Player :D ❏ https://github.com/marceloserpa/ ❏ https://www.linkedin.com/in/marceloserpa/
  • 3.
    About me ❏ Father ❏Software Architect at ilegra ❏ Devops Engineer Specialist ❏ Google Cloud Architect Certified ❏ SOA Specialist ❏ Football fun ❏ Github ❏ Twitter ❏ Blog
  • 4.
    Exercise 1 ● Createa TCP server using socket API. Use the template available here. ● It needs to be able to process more than one request at time (use threads)
  • 5.
    Let's review whatwe did and think about It
  • 6.
    How works thetraditional way ● thread per request model ● One thread for the entire request flow ● limitations 1 thread == 1MB (around it)
  • 7.
    Blocking and synchronous ●Sequential ● Unnecessary waiting time ● Total time is high
  • 8.
    Expectations for modernapplications ● high throughput ● scalability ● lower cost (BE CAREFUL)
  • 9.
    Java NIO ● Javaabstraction to deal with low level I/O ● Work on top of channels and selectors (scales better) ● Is much more efficient than native sockets and easier to deal with.
  • 10.
    Java NIO -Can become complex as well - link
  • 11.
    Networking programing ● Isinhirintanly complex ○ It’s more than programming languages or algorithms ○ Requires specific skills ○ Network programs have their own performance requirements ○ Needs to be efficient, otherwise they won't attend the current market requirements.
  • 12.
    What is Netty “Nettyis a NIO client server framework which enables quick and easy development of network applications such as protocol servers and clients. It greatly simplifies and streamlines network programming such as TCP and UDP socket server.” ● A networking programming server ● open source and written in Java ● Asynchronous and event driven ● Enforces a better design. Decouples business logic from network programming details. ● Configurable ● Extensible
  • 13.
    Companies using Netty ●Apple ● Twitter ○ using Finagle their Netty-basead framework in inter-system communication ● Facebook ○ uses Netty in Nifty - Thrift service ● Google ● Square ● Instagram ● Netflix
  • 14.
  • 15.
  • 16.
    OSS project usingNetty ● Infinispan ● HornetQ ● Vert.x ● Apache Cassandra ● ElasticSearch
  • 17.
    Cassandra - Morenodes, worst the performance
  • 18.
    Cassandra - Streamingdata between nodes - Before
  • 19.
    Cassandra - Streamingdata between nodes
  • 20.
    Cassandra - Streamingdata between nodes - Before
  • 21.
    Cassandra - Streamingdata between nodes - Before
  • 22.
    Cassandra - Streamingdata between nodes - Before
  • 23.
    Cassandra - Streamingdata between nodes - Now
  • 24.
    Cassandra - Streamingdata between nodes - Now
  • 25.
    Startups using Netty ●Firebase ● Urban Airship
  • 26.
    Non-Blocking and asynchronous ●Initialize something now ● Using Threads Efficiently ● Scalability ● More efficient (doing more with less resources) ● concurrency
  • 27.
    Core components ● EventLoop ●Channel ● Callback ● Future ● Event Handler
  • 28.
    Netty - Highlevel arch
  • 29.
  • 30.
    Echo Server Example Eventloopimpl in use. There are others...
  • 31.
    Echo Server Example Utilitythat helps glue all pieces
  • 32.
    Echo Server Example Transporttype defined for the channel
  • 33.
    Echo Server Example Settinghandler on the pipeline chain
  • 34.
    Echo Server Example Bindthe server and channel and wait this process complete
  • 35.
    Echo Server Example Blockthe current thread and wait until the channel is closed
  • 36.
  • 37.
  • 38.
    What is EventLoop?How it works? ● Threading model ● Control flow ● Dispatching events to ChannelHandlers
  • 39.
    EventLoop -Task Scheduling- run later scheduling task to run after 10 seconds
  • 40.
    EventLoop -Task Scheduling- periodically scheduling task to run every 10 seconds staring in 10 seconds
  • 41.
    What is EventLoopgroup? ● Allocate EventLoop for a Channel ● Uses round-robin to distribute load ● An application needs a least 1 EventLoopGroup
  • 42.
    Channel ● Socket ● Receiveevents ● Transport layer ● Notify Channel Handlers
  • 43.
    ChannelHandler ● Decoupled businessfrom networking ● Implements the logic ● React to event notification ● ChannelInboundHandlerAdapter ○ channelRead() ○ channelReadComplete() ○ exceptionCaught() ● ChannelHandler are invoke for different events ● Hook lifecycle
  • 44.
    ChannelPipeline ● chain ofChannelHandler ● propagation flow ● order to execute channel handlers ● ChannelHandler is installed as follow: ○ a Channelnitializaer is registered to server bootstrap ○ when Channelnitializaer.init() is called it install the channels handlers in the pipeline
  • 45.
    Bootstraper for clients (Bootstrap) ●establish connection ● connect to remote peer ● requires only one EventLoopGroup for servers (ServerBootstrap) ● listening to income connections ● requires two EventLoopGroup: ○ one for ServerChannel ○ others to registered all channel that you want
  • 46.
    Bytebuf ● Byte container ●Alternative for NIO ByteBuffer ● Used to data
  • 47.
    Codecs ● Transforms streamsof byte from one format to another Encoder Decoder
  • 48.
    Codec Example -Bootstrap Creating a ServerBootstrap and registered EventLoopGroup
  • 49.
    Codec Example -Bootstrap Registered Initializer to configure Pipeline
  • 50.
    Codec Example Registered codecs andaggregator on ChannelPipeline
  • 51.
    Codec Example -Handler part 1 Use decoder to extract parameters
  • 52.
    Codec Example -Handler part 1 Call service to handle business rules
  • 53.
    Codec Example -Handler part 2
  • 54.
    Solution based Netty- ServiceTalk https://github.com/diegopacheco/java-pocs/tree/master/pocs/servicetalk-fun
  • 55.
    Solution based Netty- ServiceTalk ● gRPC ● HTTP/2 ● Long-polling ● Smart Client ○ Client side load balancing ○ retry ● Modularization ○ use just you need
  • 56.
    Solution based Netty- Armeria https://github.com/diegopacheco/java-pocs/tree/master/pocs/armerica-fun/src/main/java
  • 57.
    Solution based Netty- Armeria ● HTTP/2 ● Integration with gRPC and Thrift ● Metrics ● Circuit breaker ● Client-side health-check, retries and client side load balancing ● Distributed Tracing with Zipkin ● Service discovery (DNS and Zookeeper) ● Built on top of Reactive Streams and Java 8 CompletableFuture
  • 58.
    Solution based Netty- Reactor Netty https://github.com/diegopacheco/java-pocs/tree/master/pocs/reactor-netty-simple
  • 59.
    Solution based Netty- Reactor Netty ● Support reactive streams specification ● HTTP, UDP and TCP ● Schedulers ● Parallel Flux
  • 60.
    Performance/Scalability > DeveloperExperience ● Number of lines of code is not a problem ● IFs (if not shells) is not a problem ● You don't deal with this code everyday ● There are books and materials on the web (stack overflow, etc...) ● Less lines of code does not means is better ● Spring might be “feeling” better but has worst performances and more deps ● Developers should care more about the business ○ User Experience -> slow server ○ Cost Reduction -> doing more with less(less cpu and memory used - more toughtput) ○ Reliability -> netty is battle tested by Silicon valley biggest IT companies in the world.
  • 61.
  • 62.
    Exercise 2 ● Changethe code made on exercise 1, this time using Netty as a a server implementation and client. ● Use this template in order to get started.
  • 63.
    Exercise 3 ● Forthis exercise we are gonna use same problem as described on exercise 2, but this time using an http interface rather than exposing an TCP socket. ● Server should be called like this: localhost:8080/training/netty/sum?num1=1&num2=4 ● Use this template in order to get started.
  • 64.
    Exercise 4 ● Dothe same implementation of exercise 3 using ServiceTalk with JaxRS ● Use this template in order to get started.