4 Fundamentals of Networking For Effective Backends v5
4 Fundamentals of Networking For Effective Backends v5
Fundamentals of
Networking for Effective
Backend Applications
Understanding the first principles of networking to
build low latency and high throughput backends
husseinnasser
Introduction
husseinnasser
Introduction
● Welcome
● Who this course is for?
● Course Outline
husseinnasser
Fundamentals of
Networking
The first principles of computer networking
Client-Server Architecture
A revolution in networking
Client-Server Architecture
OSI Model
Open Systems Interconnection model
husseinnasser
● Agnostic applications
○ Without a standard model, your application must have knowledge of the
underlying network medium
○ Imagine if you have to author different version of your apps so that it
works on wifi vs ethernet vs LTE vs fiber
● Network Equipment Management
○ Without a standard model, upgrading network equipments becomes difficult
● Decoupled Innovation
○ Innovations can be done in each layer separately without affecting the rest of the models
husseinnasser
Client Server
Application Application
Presentation Presentation
Session Session
Physical Physical
Client Server
Client Server
Application Application
Presentation Presentation
Session Session
● OSI Model has too many layers which can be hard to comprehend
● Hard to argue about which layer does what
● Simpler to deal with Layers 5-6-7 as just one layer, application
● TCP/IP Model does just that
husseinnasser
TCP/IP Model
A B
A B
00:00:5e:00:53:aa 00:00:3a:12:31:0b
husseinnasser
D
husseinnasser
A A
B B
C C
N1 N2
Host 192.168.1.3 wants to talk to 192.168.2.2
192.168.1.3 192.168.2.3
192.168.1.2 192.168.2.2
192.168.1.1 192.168.2.1
192.168.1.0/24 192.168.2.0/24
husseinnasser
1.2.3.4
IP Address
● Layer 3 property
● Can be set automatically or statically
● Network and Host portion
● 4 bytes in IPv4 - 32 bits
husseinnasser
Network vs Host
● a.b.c.d/x (a.b.c.d are integers) x is the network bits and remains are host
● Example 192.168.254.0/24
● The first 24 bits (3 bytes) are network the rest 8 are for host
● This means we can have 2^24 (16777216) networks and each network has
2^8 (255) hosts
● Also called a subnet
husseinnasser
Subnet Mask
Default Gateway
192.168.1.3 =
192.168.1.0
● 255.255.255.0 & 192.168.1.2 192.168.2.2
192.168.1.2 =
192.168.1.0
● Same subnet ! no need to
192.168.1.1 192.168.2.1
route
192.168.1.0/24 192.168.2.0/24
E.g. Host 192.168.1.3 wants to talk to 192.168.2.2
192.168.1.3 =
192.168.1.0
● 255.255.255.0 & 192.168.1.2 192.168.1.100 192.168.2.2
192.168.2.2 =
192.168.2.0
● Not the subnet ! The packet
192.168.1.1 192.168.2.1
is sent to the Default
Gateway 192.168.1.100
192.168.1.0/24 192.168.2.0/24
husseinnasser
Summary
● IP Address
● Network vs Host
● Subnet and subnet mask
● Default Gateway
husseinnasser
The IP Packet
Anatomy of the IP Packet
husseinnasser
IP Packet
Actual IP Packet
Offsets Octet 0 1 2 3
Octet Bit 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
0 0 Version IHL DSCP ECN Total Length
4 32 Identification Flags Fragment Offset
8 64 Time To Live Protocol Header Checksum
12 96 Source IP Address
16 128 Destination IP Address
20 160 Options (if IHL > 5)
⋮ ⋮
56 448
Data
https://datatracker.ietf.org/doc/html/rfc791
https://en.wikipedia.org/wiki/IPv4
husseinnasser
Offsets Octet 0 1 2 3
Octet Bit 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
0 0 Version IHL DSCP ECN Total Length
4 32 Identification Flags Fragment Offset
8 64 Time To Live Protocol Header Checksum
12 96 Source IP Address
16 128 Destination IP Address
20 160 Options (if IHL > 5)
⋮ ⋮
56 448
Data
husseinnasser
Offsets Octet 0 1 2 3
Octet Bit 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
0 0 Version IHL DSCP ECN Total Length
4 32 Identification Flags Fragment Offset
8 64 Time To Live Protocol Header Checksum
12 96 Source IP Address
16 128 Destination IP Address
20 160 Options (if IHL > 5)
⋮ ⋮
56 448
Data
husseinnasser
Offsets Octet 0 1 2 3
Octet Bit 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
0 0 Version IHL DSCP ECN Total Length
4 32 Identification Flags Fragment Offset
8 64 Time To Live Protocol Header Checksum
12 96 Source IP Address
16 128 Destination IP Address
20 160 Options (if IHL > 5)
⋮ ⋮
56 448
Data
husseinnasser
Offsets Octet 0 1 2 3
Octet Bit 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
0 0 Version IHL DSCP ECN Total Length
4 32 Identification Flags Fragment Offset
8 64 Time To Live Protocol Header Checksum
12 96 Source IP Address
16 128 Destination IP Address
20 160 Options (if IHL > 5)
⋮ ⋮
56 448
Data
husseinnasser
Offsets Octet 0 1 2 3
Octet Bit 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
0 0 Version IHL DSCP ECN Total Length
4 32 Identification Flags Fragment Offset
8 64 Time To Live Protocol Header Checksum
12 96 Source IP Address
16 128 Destination IP Address
20 160 Options (if IHL > 5)
⋮ ⋮
56 448
Data
husseinnasser
Offsets Octet 0 1 2 3
Octet Bit 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
0 0 Version IHL DSCP ECN Total Length
4 32 Identification Flags Fragment Offset
8 64 Time To Live Protocol Header Checksum
12 96 Source IP Address
16 128 Destination IP Address
20 160 Options (if IHL > 5)
⋮ ⋮
56 448
Data
husseinnasser
Offsets Octet 0 1 2 3
Octet Bit 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
0 0 Version IHL DSCP ECN Total Length
4 32 Identification Flags Fragment Offset
8 64 Time To Live Protocol Header Checksum
12 96 Source IP Address
16 128 Destination IP Address
20 160 Options (if IHL > 5)
⋮ ⋮
56 448
Data
husseinnasser
Offsets Octet 0 1 2 3
Octet Bit 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
0 0 Version IHL DSCP ECN Total Length
4 32 Identification Flags Fragment Offset
8 64 Time To Live Protocol Header Checksum
12 96 Source IP Address
16 128 Destination IP Address
20 160 Options (if IHL > 5)
⋮ ⋮
56 448
Data
husseinnasser
Summary
ICMP
Internet Control Message Protocol
husseinnasser
ICMP
ICMP header
Offsets Octet 0 1 2 3
Octet Bit 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
0 0 Type Code Checksum
4 32 Rest of header
https://en.wikipedia.org/wiki/Internet_Control_Message_Protocol
https://datatracker.ietf.org/doc/html/rfc792
husseinnasser
ICMP
Ping
192.168.10.100
192.168.5.100 192.168.10.3
Ping - unreachable
192.168.10.100
192.168.5.100 192.168.10.3
TTL0 ICMP echo
192.168.1.3 192.168.10.3
request
TTL100 ICMP dest
192.168.5.100 192.168.1.3
unreachable
192.168.1.3
192.168.1.100
TTL3 ICMP echo
192.168.1.3 192.168.10.3
request
TraceRoute
Traceroute
192.168.10.100
192.168.5.100 192.168.10.3
192.168.1.3
192.168.1.100
Summary
ARP
Address Resolution Protocol
Why ARP?
aa 2 GET / 3 bb
IP : 10.0.0.2 IP : 10.0.0.3
MAC: aa:bc:32:7f:c0:07 MAC: bb:ab:dd:11:22:33
Port: 8080
● IP 10.0.0.2 (2) wants to connect to IP 10.0.0.5 (5)
● Host 2 checks if host 5 is within its subnet, it is.
● Host 2 needs the MAC address of host 5
● Host 2 checks its ARP tables and its not there
EXIP : 122.1.2.4
aa 2 GET / 5 ?? IP : 10.0.0.1 (1)
MAC: ff
ip mc ip mc ip mc ip mc
2 aa 3 bb 4 cc 5 dd
IP : 2 IP : 3 IP : 4 IP : 5
GW : 1 GW : 1 GW : 1 GW : 1
MAC: aa MAC: bb MAC: cc MAC: dd
● Host 2 sends an ARP request broadcast to all machines in its network
● Who has IP address 10.0.0.5?
● Host 5 replies with dd
● Host 2 updates its ARP Table
EXIP : 122.1.2.4
aa 2 GET / 5 dd IP : 10.0.0.1 (1)
MAC: ff
ip mc ip mc ip mc ip mc
2 aa 3 bb 4 cc 5 dd
5 dd
IP : 2 IP : 3 IP : 4 IP : 5
GW : 1 GW : 1 GW : 1 GW : 1
MAC: aa MAC: bb MAC: cc MAC: dd
● IP 10.0.0.2 (2) wants to connect to IP 1.2.3.4 (x)
● Host 2 checks if 1.2.3.4 is within its subnet, it is NOT!
● Host 2 needs to talk to its gatway
● Host 2 needs the MAC address of the gateway
EXIP : 122.1.2.4
IP : 10.0.0.1 (1) 1.2.3.4 (x)
MAC: ff
aa 2 GET / x ??
ip mc ip mc ip mc ip mc
2 aa 3 bb 4 cc 5 dd
5 dd
IP : 2 IP : 3 IP : 4 IP : 5
GW : 1 GW : 1 GW : 1 GW : 1
MAC: aa MAC: bb MAC: cc MAC: dd
● Host 2 checks its local ARP table, 10.0.0.1 is not it in
● Host 2 sends an ARP request to everybody in the network
● Who has 10.0.0.1? (A DANGEROUS QUESTION)
● Gateway reply with ff
● NAT than kicks in. EXIP : 122.1.2.4
IP : 10.0.0.1 (1) 1.2.3.4
MAC: ff
aa 2 GET / x ff
ip mc ip mc ip mc ip mc
2 aa 3 bb 4 cc 5 dd
5 dd
IP : 2 IP : 3 IP : 4 IP : 5
1 ff GW : 1 GW : 1 GW : 1 GW : 1
MAC: aa MAC: bb MAC: cc MAC: dd
husseinnasser
Summary
Routing Example
How IP Packets are routed in Switches and Routers
A B
Switch (S)
10.0.0.2 10.0.0.4
Internet
8.8.8.8 (G)
husseinnasser
UDP
User Datagram Protocol
husseinnasser
UDP
● Video streaming
● VPN
● DNS
● WebRTC
A B
husseinnasser
10.0.0.1 10.0.0.2
husseinnasser
Summary
UDP Datagram
The anatomy of the UDP datagram
husseinnasser
UDP Datagram
Offsets Octe 0 1 2 3
t
Octet Bit 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
0 1 2 3 4 5 6 7 8 9
0 0 Source port Destination port
4 32 Length Checksum
Data
https://www.ietf.org/rfc/rfc768.txt
https://en.wikipedia.org/wiki/User_Datagram_Protocol
husseinnasser
Offsets Octe 0 1 2 3
t
Octet Bit 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
0 1 2 3 4 5 6 7 8 9
0 0 Source port Destination port
4 32 Length Checksum
Data
husseinnasser
Offsets Octe 0 1 2 3
t
Octet Bit 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
0 1 2 3 4 5 6 7 8 9
0 0 Source port Destination port
4 32 Length Checksum
Data
husseinnasser
UDP Pros
● Simple protocol
● Header size is small so datagrams are small
● Uses less bandwidth
● Stateless
● Consumes less memory (no state stored in the server/client)
● Low latency - no handshake , order, retransmission or guaranteed delivery
husseinnasser
UDP Cons
● No acknowledgement
● No guarantee delivery
● Connection-less - anyone can send data without prior knowledge
● No flow control
● No congestion control
● No ordered packets
● Security - can be easily spoofed
husseinnasser
TCP
Transmission Control Protocol
husseinnasser
TCP
● Reliable communication
● Remote shell
● Database connections
● Web communications
● Any bidirectional communication
A B
husseinnasser
TCP Connection
TCP Connection
10.0.0.1 10.0.0.2
husseinnasser
Connection Establishment
Sending data
Acknowledgment
● App1 sends segment 1,2 and 3 to AppX
● AppX acknowledge all of them with a single ACK 3
AppX-port 22
App1-port 5555 AppY-port 443
10.0.0.1 5555 seq2 22 10.0.0.2
App2-port 7712 AppZ-port 80
App3-port 2222
10.0.0.1 5555 seq3 22 10.0.0.2
10.0.0.1
10.0.0.2 10.0.0.1:5555:
10.0.0.1:5555: 10.0.0.2:22
10.0.0.2:22
File descriptor
File descriptor
10.0.0.2 22 ACK3 5555 10.0.0.1
husseinnasser
Lost data
● App1 sends segment 1,2 and 3 to AppX
● Seg 3 is lost, AppX acknowledge 3
● App1 resend Seq 3
AppX-port 22
App1-port 5555 AppY-port 443
10.0.0.1 5555 seq2 22 10.0.0.2
App2-port 7712 AppZ-port 80
App3-port 2222
10.0.0.1 5555 seq3 22 10.0.0.2
10.0.0.1
10.0.0.2 10.0.0.1:5555:
10.0.0.1:5555: 10.0.0.2:22
10.0.0.2:22
Closing Connection
Summary
TCP Segment
The anatomy of the TCP Segment
husseinnasser
TCP Segment
TCP Segment
Offsets Octe 0 1 2 3
t
Octet Bit 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
0 0 Source port Destination port
4 32 Sequence number
8 64 Acknowledgment number (if ACK set)
12 96 Data offset Reserved N C E U A P R S FI Window Size
000 S W C R C S S Y N
R E G K H T N
16 128 Checksum Urgent pointer (if URG set)
20 160 Options (if data offset > 5. Padded at the end with "0" bits if necessary.)
⋮ ⋮
60 480
https://en.wikipedia.org/wiki/Transmission_Control_Protocol
https://datatracker.ietf.org/doc/html/rfc793
husseinnasser
Ports
Offsets Octe 0 1 2 3
t
Octet Bit 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
0 0 Source port Destination port
4 32 Sequence number
8 64 Acknowledgment number (if ACK set)
12 96 Data offset Reserved N C E U A P R S FI Window Size
000 S W C R C S S Y N
R E G K H T N
16 128 Checksum Urgent pointer (if URG set)
20 160 Options (if data offset > 5. Padded at the end with "0" bits if necessary.)
⋮ ⋮
60 480
husseinnasser
9 bit flags
Offsets Octe 0 1 2 3
t
Octet Bit 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
0 0 Source port Destination port
4 32 Sequence number
8 64 Acknowledgment number (if ACK set)
12 96 Data offset Reserved N C E U A P R S F Window Size
000 S W C R C S S Y I
R E G K H T N N
16 128 Checksum Urgent pointer (if URG set)
20 160 Options (if data offset > 5. Padded at the end with "0" bits if necessary.)
⋮ ⋮
60 480
husseinnasser
Flow Control
How much the receiver can handle?
husseinnasser
Flow Control
ACK 1
A B
SEG 2
ACK 2
SEG 3
ACK 3
husseinnasser
Flow Control
ACK 3
A B
husseinnasser
Flow Control
A B
husseinnasser
Offsets Octe 0 1 2 3
t
Octet Bit 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0 7 6 5 4 3 2 1 0
0 0 Source port Destination port
4 32 Sequence number
8 64 Acknowledgment number (if ACK set)
12 96 Data offset Reserved N C E U A P R S FI Window Size
000 S W C R C S S Y N
R E G K H T N
16 128 Checksum Urgent pointer (if URG set)
20 160 Options (if data offset > 5. Padded at the end with "0" bits if necessary.)
⋮ ⋮
60 480
husseinnasser
2 3 4
ACK4
5 6 7
ACK7
husseinnasser
Sliding Window
● Can’t keep waiting for receiver to acknowledge all segments
● Whatever gets acknowledge moves
● We “slide” the window
● Sender maintains the sliding window for the receiver
A B
1 2 3 4 5 6 1 2 3
1 2 3
Can be ACK2 3
1 2 3 4 5 6
dropped
4 5 4 5 3
1 2 3 4 5 6
ACK3 4 5
1 2 3 4 5 6
6
1 2 3 4 5 6 4 5 6
husseinnasser
Window Scaling
B
● 64 KB is too small
● We can’t increase the bits on the segment 1 2 3
Summary
Congestion Control
How much the network can handle?
husseinnasser
Congestion Control
● The receiver might handle the load but the middle boxes might not
● The routers in the middle have limit
● We don’t want to congest the network with data
● We need to avoid congestion
● A new window: Congestion Window (CWND)
A B
husseinnasser
A B
RWND
How large can this get?
husseinnasser
Slow Start
● CWND starts with 1 MSS (or more)
● Send 1 Segment and waits for ACK
● With EACH ACK received CWND is incremented by 1 MSS
● Until we reach slow start threshold (ssthresh) we switch to congestion avoidance algorithm
A B
1 2 3 4 5 6 7 1 1
CWND + 1 1 2 3 4 5 6 7 ACK1
2 3
1 2 3 4 5 6 7 2 3
ACK2 ACK3
CWND + 2 1 2 3 4 5 6 7
1 2 3 4 5 6 7 4 5 6 7
4 5 6 7
husseinnasser
Congestion Avoidance
● Send CWND worth of Segments and waits for ACK
● Only when ALL segments are ACKed add UP to one MSS to CWND
● Precisely CWND = CWND + MSS*MSS/CWND
A B
1 2 3 4 5 6 7 1 1
CWND + 1 1 2 3 4 5 6 7 ACK1
2 3
1 2 3 4 5 6 7 2 3
ACK2 ACK3
CWND + 1 1 2 3 4 5 6 7
1 2 3 4 5 6 7 4 5 6
4 5 6
husseinnasser
Congestion Detection
● The moment we get timeouts, dup ACKs or packet drops
● The slow start threshold reduced to the half of whatever unacknowledged data is sent (roughly CWND/2 if all CWND worth of
data is unacknowledged)
● The CWND is reset to 1 and we start over.
● Min slow start threshold is 2*MSS
A B
1 2 3 4 5 6 7 1 2 3 4 1
1
CWND 1 2 3 4 5 6 7
husseinnasser
Congestion Notification
● We don’t want routers dropping packets
● Can Routers let us know when congestion hit?
● Meet ECN (Explicit Congestion Notification)
● Routers and middle boxes can tag IP packets with ECN
● The receiver will copy this bit back to the sender
● ECN is IP Header bit
● So Routers don’t drop packets just let me know you are reaching your limit
husseinnasser
Summary
● While the receiver may handle large data middle boxes might not
● Middle routers buffers may fill up
● Need to control the congestion in the network
● Sender can send segments up to CWND or RWND without ACK
● Isn’t normally a problem in hosts connected directly (LAN)
husseinnasser
Congestion Detection
Slow Start vs Congestion Avoidance
husseinnasser
A B
RWND
How large can this get?
husseinnasser
Congestion Detection
● The moment we get timeouts, dup ACKs or packet drops
● The slow start threshold reduced to the half of whatever unacknowledged data is sent (roughly CWND/2 if all
CWND worth of data is unacknowledged)
● The CWND is reset to 1 and we start over.
● Min slow start threshold is 2*MSS
A B
1 2 3 4 5 6 7 1 2 3 4 1
1
CWND 1 2 3 4 5 6 7
husseinnasser
2*SMSS
(can’t go
lower)
1SMSS
Network Address
Translation
How the WAN sees your internal devices
husseinnasser
NAT
● IPv4 is limited only 4 billion
● Private vs Public IP Address
● E.g. 192.168.x.x , 10.0.0.x is private not routable in the Internet
● Internal hosts can be assigned private addresses
● Only your router need public IP address
● Router need to translate requests
192.168.1.1
Local Network DDD
8992 192.168.1.2
44.11.5.17 7777
NAT Applications
● Private to Public translations
○ So we don't run out IPv4
● Port forwarding
○ Add a NAT entry in the router to forward packets to 80 to a machine in your LAN
○ No need to have root access to listen on port 80 on your device
○ Expose your local web server publically
● Layer 4 Load Balancing
○ HAProxy NAT Mode - Your load balancer is your gateway
○ Clients send a request to a bogus service IP
○ Router intercepts that packet and replaces the service IP with a destination server
○ Layer 4 reverse proxying
husseinnasser
Summary
● IPv4 is limited only 4 billion
● Need to translate private to public
● Port forward/load balancing
husseinnasser
ESTABLISHED ESTABLISHED
FIN_WAIT_1
FIN
CLOSE_WAIT
ACK
LAST_ACK
FIN_WAIT_2 FIN
TIME_WAIT
ACK
….
4 minutes (2MSL)
CLOSED
CLOSED
husseinnasser
TCP Pros
● Guarantee delivery
● No one can send data without prior knowledge
● Flow Control and Congestion Control
● Ordered Packets no corruption or app level work
● Secure and can’t be easily spoofed
husseinnasser
TCP Cons
DNS
Domain Name System
husseinnasser
DNS
Google.com
(142.251.40.46)
husseinnasser
TLD
server Resolver
ROOT
husseinnasser
Resolver
s
W
nd
he TLD1
ha
re
se is t
P
TC
rve he
rs .C
He (2 OM
)
re
is
TL a TL
D1 D
(3 serv
) e r
Google.com
(142.251.40.46) ROOT
DNS Packet
Source: https://www.usenix.org/system/files/sec20-zheng.pdf
RFC: https://datatracker.ietf.org/doc/html/rfc1035
husseinnasser
Example
TLS
Transport Layer Security
TLS
● Vanilla HTTP
● HTTPS
● TLS 1.2 Handshake
● Diffie Hellman
● TLS 1.3 Improvements
HTTP
open 80
GET /
+
e a ders l
H
x.htm
inde
l>...
<htm
close ….
HTTPS open
443
Handshake
GET /
+
e a ders l
H .htm
d e x
in
h tm l>...
<
….
close
Why TLS
Change cip
her, fin
GET /
s+
Header l
tm
index.h
..
close <html>.
….
Diffie Hellman
Private x
+
Public g,n
= Symmetric key
+
Private y
Diffie Hellman
Public/
Unbreakable
+
/can be shared
g^x % n
Public/
Unbreakable +
/can be shared
g^y % n
her/ fin
server hello/ change cip
GET /
s+
Header l
tm
index.h
..
<html>.
….
● Vanilla HTTP
● HTTPS
● TLS 1.2 Handshake (two round trips)
● Diffie Hellman
● TLS 1.3 Improvements (one round trip can be zero)
husseinnasser
HTTP
Hypertext Transfer Protocol
husseinnasser
SSH
Secure Shell
Networking Concepts for Effective Backend
Applications
husseinnasser
Overview
Hardware MTU
MSS
Credit Cisco
https://learningnetwork.cisco.com/s/question/0D53i00000Kt7CXCAZ/mtu-vs-pdu
husseinnasser
● MTU is network interface property each host can have different value
● You really need to use the smallest MTU in the network
● Path MTU help determine the MTU in the network path
● Client sends a IP packet with its MTU with a DF flag
● The host that their MTU is smaller will have to fragment but can’t
● The host sends back an ICMP message fragmentation needed which will
lower the MTU
MTU : 1500
MTU : 1500
MTU : 9000
MTU : 512
husseinnasser
Summary
Nagle's algorithm
Delay in the client side
husseinnasser
Nigel Algorithm
Nagle's algorithm
A B
Delay
2500
1460
2 1460
husseinnasser
A
1460 1460 1460
Delay
2620 B
ACK
2620
husseinnasser
Delayed Acknowledgement
Less packets are good but performance is better
husseinnasser
2 1
A B
Delay
4 3
ACK 5
ACK all
at once
husseinnasser
● Causes delays in some clients that may lead to timeout and retransmission
● Noticeable performance degradation
● Combined with Nagle's algorithm can lead to 400ms delays!
● Each party is waiting on each other
A
1460 1460 1460
Delay
Delay
2620 B
ACK
2620
husseinnasser
Connection Pooling
Handshake is Slow
SYN+TFO+Data
SYN/ACK+Response
ACK
husseinnasser
Listening Server
Understanding what to listen on
husseinnasser
Listening
Listening
● There is a configuration that allows more than one process to listen on the
same port
● SO_PORTREUSE
● Operating systems balance segments among processes
● OS creates a hash source ip/source port/dest ip/ dest port
● Guarantees always go to the same process if the pair match
Goes to AppX
10.0.0.1 10.0.0.2
husseinnasser
TCP HOL
Head of line blocking
husseinnasser
1 2 3 4
2 3 4
1 2 3 4
1 2 3 4
husseinnasser
1 2 3 4
Request 1 Request 2
2 3 4
● Layer 4 vs Layer 7
● Load Balancer
● Layer 4 Load Balancer (pros and cons)
● Layer 7 Load Balancer (pros and cons)
Layer 7 Application Application
Backend server 1
Load Balancer
Backend server 2
Layer 4 Load Balancer
n
ne ctio
Con
P
TC N
Backend server 1
SY CK
N/A
SY CK 44.1.1.3
A
TC
PC
onn
44.1.1.1 ect
ion
S
SY YN
N/A
L4 Load Balancer AC CK
K
44.1.1.2
Backend server 2
44.1.1.4
Layer 4 Load Balancer
Backend server 1
44.1.1.3
TCP Connection
SYN
SYN/ACK
44.1.1.1 ACK
L4 Load Balancer
44.1.1.2
44.1.1.1
L4 Load Balancer
44.1.1.2
Backend server 2
44.1.1.4
Layer 4 Load Balancer
2
.1.1.
44
S
RE
3
.1.1.
44
Backend server 1
44.1.1.2 RES 44.1.1.1 44.1.1.3
44.1.1.1
L4 Load Balancer
44.1.1.2
Backend server 2
44.1.1.4
Layer 4 Load Balancer
3
2
1
Backend server 1
44.1.1.3
HTTP GET /1
1 2 3
44.1.1.1
L4 Load Balancer
44.1.1.2
Backend server 2
44.1.1.4
Layer 4 Load Balancer
7
6
5
Backend server 1
44.1.1.3
HTTP GET /2
5 6 7
44.1.1.1
L4 Load Balancer
44.1.1.2
Backend server 2
44.1.1.4
Layer 4 Load Balancer
Backend server 1
44.1.1.3
1
44.1.1.1 2
1 2 3 3
L4 Load Balancer
(New connection) 44.1.1.2
HTTP GET /3
Backend server 2
44.1.1.4
Layer 4 Load Balancer (Pros and Cons)
Pros Cons
TC
PC
onn
44.1.1.1 ect
ion
S
SY YN
N/A
L7 Load Balancer AC CK
K
44.1.1.2
Backend server 2
44.1.1.4
Layer 7 Load Balancer
Backend server 1
44.1.1.3
TCP Connection
SYN
SYN/ACK
44.1.1.1 ACK
L7 Load Balancer
44.1.1.2
44.1.1.1
L7 Load Balancer
44.1.1.2
Backend server 2
44.1.1.4
Layer 7 Load Balancer
2
.1.1.
44
S
RE
3
.1.1.
44
Backend server 1
44.1.1.2 RES 44.1.1.1 44.1.1.3
44.1.1.1
L7 Load Balancer
44.1.1.2
Backend server 2
44.1.1.4
Layer 7 Load Balancer
LB parsers and
understand the
3
segments 2
1
1 2 3
Backend server 1
44.1.1.3
HTTP GET /1
1 2 3
44.1.1.1
L7 Load Balancer
44.1.1.2
Backend server 2
44.1.1.4
Layer 7 Load Balancer
Backend server 1
44.1.1.3
HTTP GET /2
5 6 7
44.1.1.1 5
L7 Load Balancer 7
44.1.1.2
Backend server 2
44.1.1.4
Layer 7 Load Balancer
Backend server 1
44.1.1.3
44.1.1.1
1 2 3
L7 Load Balancer
(New connection) 44.1.1.2
HTTP GET /3
Backend server 2
44.1.1.4
Layer 7 Load Balancer (Pros and Cons)
Pros Cons
● Layer 4 vs Layer 7
● Load Balancer
● Layer 4 Load Balancer (pros and cons)
● Layer 7 Load Balancer (pros and cons)