Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Things that every Java developer
should know about network
Alexey Ragozin
alexey.ragozin@gmail.com
Good old TCP
Transmission Control Protocol
• TCPv4 (RFC 793) dated 1981
• Stream oriented (in order delivery)
• Reliable (...
Accepting connections
Server
Client
ServerSocket socket = new ServerSocket();
socket.bind(bindAddr);
while(true) {
Socket ...
Accepting connections
Alice Bob
connect
accept
bind
?
Accepting connections
Quiz
Server socket is bound, but Socket.accept()
is not called. What will happen on client
attemptin...
Closing TCP connection
Quiz
In this example, what will happen on server?
1. Server will receive full message.
2. Server ma...
Closing TCP connection
Alice Bob
write
close
read ?
Closing TCP connection
?
Alice Bob
write
close
read
buffer
Closing TCP connection
• FIN – graceful shutdown
 All data are delivered
• RST – connection reset
 Connection terminated...
Closing TCP connection
Normal way
• OS will keep connection after close until all buffered
data is sent.
• Connection may ...
TCP connection states
https://en.wikipedia.org/wiki/Transmission_Control_Protocol
Socket errors in Java
Socket errors
 Socket read return -1 – graceful stream shutdown
 java.net.SocketException: Connect...
Socket errors in Java
Socket errors
 Socket read return -1 – graceful stream shutdown
 java.net.SocketException: Connect...
TCP Sliding Window
Nagle’s algorithm
Wait for 200ms to send
under sized segment
Use TCP_NODELAY
to disable
TCP sliding win...
TCP Receiver Buffer Size
Default receiver buffer size – 64k
• RTT Moscow – New York 160ms
• Sending 64k every 160ms -> 400...
TCP Slow Start
TCP slow start – congestion control algorithm
• Initial window size is small
• Window size is growing gradu...
TCP Slow Start
https://en.wikipedia.org/wiki/TCP_congestion-avoidance_algorithm
Slidingwindow
T
TCP Slow Start
https://en.wikipedia.org/wiki/TCP_congestion-avoidance_algorithm
Slidingwindow
T
Quadratic
TCP Slow Start
https://en.wikipedia.org/wiki/TCP_congestion-avoidance_algorithm
Slidingwindow
T
Quadratic
Timeout
TCP Slow Start
https://en.wikipedia.org/wiki/TCP_congestion-avoidance_algorithm
Slidingwindow
T
Quadratic
Timeout
TCP Slow Start
https://en.wikipedia.org/wiki/TCP_congestion-avoidance_algorithm
Slidingwindow
T
Quadratic
Timeout
Threshold
TCP Slow Start
https://en.wikipedia.org/wiki/TCP_congestion-avoidance_algorithm
Slidingwindow
T
Quadratic
Timeout
Threshol...
TCP Slow Start
https://en.wikipedia.org/wiki/TCP_congestion-avoidance_algorithm
Slidingwindow
T
Quadratic
Timeout
Threshol...
TCP Slow Start
https://en.wikipedia.org/wiki/TCP_congestion-avoidance_algorithm
Slidingwindow
T
Quadratic
Timeout
Threshol...
TCP Slow Start
Socket options summary
Server socket
• Server socket backlog size
• SO_REUSEADDRESS
Server and client
• SO_RCVBUF 1
• SO_S...
Socket coding in Java
Quiz
How long TCP connection may stay open
without sending any packets?
1. Double round trip time.
2...
Slow morning of JDBC pool
Observation
 Each morning very first request to Oracle takes 5 minutes.
Slow morning of JDBC pool
Observation
 Each morning very first request to Oracle takes 5 minutes.
Cause
 TCP connection ...
Slow morning of JDBC pool
Solution
 Use SO_TIMEOUT for fast invalidation stale connections
 Background connection valida...
TCP Keep Alive Option
TCP has keep alive option
 SO_KEEPALIVE
 Heartbeat interval is OS wide
 Default is 2 hours
 Usel...
Testing for network hazards
Linux traffic control (TC) – simulating hazards
• Latency
• Bandwidth limitation
• Packet loss...
Loopback IP addresses
Quiz
If server socket is bound to 127.0.0.1, is it
possible to use public host’s IP to connect?
1. Y...
Loopback IP addresses
Quiz
How many loopback addresses (connection to
this address will use loopback interface) are
availa...
Loopback IP addresses
Each of 127.*.*.* is treated as individual address
routed to loopback interface.
You can bind multip...
Accidental IPv6 deployment
IPv6 is here for ages
No body cares
Until accidently switching over
Use
-Djava.net.preferIPv4St...
Beyond TCP
UDP – User Datagram Protocol
• No handshake
• No retransmission
• No guaranties
UDP
Reasons to go UDP
• Multicast support
• Packet loss is acceptable
• Latency is critical
But be beware of
• MTU sizing
...
THANK YOU
Alexey Ragozin
alexey.ragozin@gmail.com
http://blog.ragozin.info
Upcoming SlideShare
Loading in …5
×

What every Java developer should know about network?

1,272 views

Published on

Slide deck from presentation on Joker 2015

Published in: Software
  • Be the first to comment

What every Java developer should know about network?

  1. 1. Things that every Java developer should know about network Alexey Ragozin alexey.ragozin@gmail.com
  2. 2. Good old TCP Transmission Control Protocol • TCPv4 (RFC 793) dated 1981 • Stream oriented (in order delivery) • Reliable (retransmission support) • Congestion control Designed to tolerate conditions of post thermonuclear war environment.
  3. 3. Accepting connections Server Client ServerSocket socket = new ServerSocket(); socket.bind(bindAddr); while(true) { Socket conn = socket.accept(); processConnection(conn); } Socket conn = new Socket(); conn.connect(servAddr); processConnection(conn);
  4. 4. Accepting connections Alice Bob connect accept bind ?
  5. 5. Accepting connections Quiz Server socket is bound, but Socket.accept() is not called. What will happen on client attempting to connect to this socket? 1. “Connection refused” error 2. “Connection timeout” error 3. Connection will be established
  6. 6. Closing TCP connection Quiz In this example, what will happen on server? 1. Server will receive full message. 2. Server may receive partial message. 3. Server will get socket error exception. Socket conn = new Socket(); conn.connect(servAddr); byte[] msg = encodeMessage(); conn.getOutputStream().write(msg); conn.close();
  7. 7. Closing TCP connection Alice Bob write close read ?
  8. 8. Closing TCP connection ? Alice Bob write close read buffer
  9. 9. Closing TCP connection • FIN – graceful shutdown  All data are delivered • RST – connection reset  Connection terminated in unknown state What happens of Socket.close()?
  10. 10. Closing TCP connection Normal way • OS will keep connection after close until all buffered data is sent. • Connection may remain in TIME_WAIT for sometime after that. SO_LINGER enabled • OS will keep connection sending data for some timeout. If timeout is reached connection will be reset.
  11. 11. TCP connection states https://en.wikipedia.org/wiki/Transmission_Control_Protocol
  12. 12. Socket errors in Java Socket errors  Socket read return -1 – graceful stream shutdown  java.net.SocketException: Connection reset  java.net.SocketTimeoutException: Read timed out  java.net.SocketException: Connection reset by peer: socket write error  java.net.ConnectException: Connection refused: connect  java.net.ConnectException: Connection timed out: connect
  13. 13. Socket errors in Java Socket errors  Socket read return -1 – graceful stream shutdown  java.net.SocketException: Connection reset  java.net.SocketTimeoutException: Read timed out  java.net.SocketException: Connection reset by peer: socket write error  java.net.ConnectException: Connection refused: connect  java.net.ConnectException: Connection timed out: connect
  14. 14. TCP Sliding Window Nagle’s algorithm Wait for 200ms to send under sized segment Use TCP_NODELAY to disable TCP sliding window Sender window size is controlled by receiver Alice Bob ACK x Wait for ACK Resume transmission sq=x+1 n bytes sq=x+n+1 n bytes sq=x+2*n+1 n bytes Sendwindow ACK x+3*n
  15. 15. TCP Receiver Buffer Size Default receiver buffer size – 64k • RTT Moscow – New York 160ms • Sending 64k every 160ms -> 400KiB/s SO_RCVBUF – receiver buffer size • Increase buffer on receiving side
  16. 16. TCP Slow Start TCP slow start – congestion control algorithm • Initial window size is small • Window size is growing gradually • Window size is trimmed down  Packet loss event  Application stops sending data
  17. 17. TCP Slow Start https://en.wikipedia.org/wiki/TCP_congestion-avoidance_algorithm Slidingwindow T
  18. 18. TCP Slow Start https://en.wikipedia.org/wiki/TCP_congestion-avoidance_algorithm Slidingwindow T Quadratic
  19. 19. TCP Slow Start https://en.wikipedia.org/wiki/TCP_congestion-avoidance_algorithm Slidingwindow T Quadratic Timeout
  20. 20. TCP Slow Start https://en.wikipedia.org/wiki/TCP_congestion-avoidance_algorithm Slidingwindow T Quadratic Timeout
  21. 21. TCP Slow Start https://en.wikipedia.org/wiki/TCP_congestion-avoidance_algorithm Slidingwindow T Quadratic Timeout Threshold
  22. 22. TCP Slow Start https://en.wikipedia.org/wiki/TCP_congestion-avoidance_algorithm Slidingwindow T Quadratic Timeout Threshold Congestion avoidance
  23. 23. TCP Slow Start https://en.wikipedia.org/wiki/TCP_congestion-avoidance_algorithm Slidingwindow T Quadratic Timeout Threshold Congestion avoidance 3duplicate ACKs
  24. 24. TCP Slow Start https://en.wikipedia.org/wiki/TCP_congestion-avoidance_algorithm Slidingwindow T Quadratic Timeout Threshold Congestion avoidance 3duplicate ACKs
  25. 25. TCP Slow Start
  26. 26. Socket options summary Server socket • Server socket backlog size • SO_REUSEADDRESS Server and client • SO_RCVBUF 1 • SO_SNDBUF 2 • SO_LINGER • TCP_NODELAY • SO_TIMEOUT 1 - On Linux, receiver buffer size is capped on OS level. Look for net.core.rmem_max sysctrl parameters. 2 - Modern OSes (Linux 2.4, NT 6) use auto tuning for send buffer
  27. 27. Socket coding in Java Quiz How long TCP connection may stay open without sending any packets? 1. Double round trip time. 2. 30 seconds. 3. 1 Hour. 4. 99 years.
  28. 28. Slow morning of JDBC pool Observation  Each morning very first request to Oracle takes 5 minutes.
  29. 29. Slow morning of JDBC pool Observation  Each morning very first request to Oracle takes 5 minutes. Cause  TCP connection stays overnight.  Firewall in the middle cleans connection from connection table after few hours of inactivity.  Write to connection fails after TCP timeout (30 sec).  JDBC pool attempts to validate connection from pool , validation fails with write timeout  10 connections * 30 sec = 5 minutes
  30. 30. Slow morning of JDBC pool Solution  Use SO_TIMEOUT for fast invalidation stale connections  Background connection validation is also helpful  Some pools (DBCP) have special timeout for validation query
  31. 31. TCP Keep Alive Option TCP has keep alive option  SO_KEEPALIVE  Heartbeat interval is OS wide  Default is 2 hours  Useless
  32. 32. Testing for network hazards Linux traffic control (TC) – simulating hazards • Latency • Bandwidth limitation • Packet loss WANEM (web front end to TC) • http://wanem.sourceforge.net/
  33. 33. Loopback IP addresses Quiz If server socket is bound to 127.0.0.1, is it possible to use public host’s IP to connect? 1. Yes 2. No If server socket is bound to local public IP, is it possible to 127.0.0.1 to connect? 1. Yes 2. No
  34. 34. Loopback IP addresses Quiz How many loopback addresses (connection to this address will use loopback interface) are available on typical server? 1. One 2. Number of interfaces + one 3. 16 millions
  35. 35. Loopback IP addresses Each of 127.*.*.* is treated as individual address routed to loopback interface. You can bind multiple processes to same port but using different loopback addresses.
  36. 36. Accidental IPv6 deployment IPv6 is here for ages No body cares Until accidently switching over Use -Djava.net.preferIPv4Stack=true to get back to your network.
  37. 37. Beyond TCP UDP – User Datagram Protocol • No handshake • No retransmission • No guaranties
  38. 38. UDP Reasons to go UDP • Multicast support • Packet loss is acceptable • Latency is critical But be beware of • MTU sizing • Flow / congestion control Out of control UDP flow can easily disrupt TCP communications • OS tuning Monitor receiver error rates on interfaces
  39. 39. THANK YOU Alexey Ragozin alexey.ragozin@gmail.com http://blog.ragozin.info

×