What every Java developer should know about network?
1. Things that every Java developer
should know about network
Alexey Ragozin
alexey.ragozin@gmail.com
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. 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);
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. 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();
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. 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.
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. 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. 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
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. 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. Slow morning of JDBC pool
Observation
Each morning very first request to Oracle takes 5 minutes.
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. 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. TCP Keep Alive Option
TCP has keep alive option
SO_KEEPALIVE
Heartbeat interval is OS wide
Default is 2 hours
Useless
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. 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. 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. 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. 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. Beyond TCP
UDP – User Datagram Protocol
• No handshake
• No retransmission
• No guaranties
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