22
1/x Artyom Gavrichenkov HOW[NOT]TO Write TCP-based Network Applications

Dumb Ways To Die: How Not To Write TCP-based Network Applications

  • Upload
    hll

  • View
    699

  • Download
    3

Embed Size (px)

DESCRIPTION

Thanks to http://www.youtube.com/watch?v=IJNR2EpS0jw :-)

Citation preview

Page 1: Dumb Ways To Die: How Not To Write TCP-based Network Applications

1/x

Artyom Gavrichenkov

HOW[NOT]TO Write TCP-based Network Applications

Page 2: Dumb Ways To Die: How Not To Write TCP-based Network Applications

2

Based on a True Story

• NOT AN AD!• Qrator: distributed network

● Custom TCP/IP at the bottom● Custom management protocol at the top● Interacting with plenty of Web servers and Web browsers

on a daily basis● 2 years of continuous debug^W Product ImprovementTM

Page 3: Dumb Ways To Die: How Not To Write TCP-based Network Applications

Issue #1

• Message delivery is unreliable in TCP.

Page 4: Dumb Ways To Die: How Not To Write TCP-based Network Applications

Issue #1

• Message delivery is unreliable in TCP: there's no estimation on when (and if) the message will arrive at all

• Timeouts!

• Limit all resources, including time

• No action is itself an action

Page 5: Dumb Ways To Die: How Not To Write TCP-based Network Applications

Timeouts

• Between recvfrom()

• Between requests

• Request timeout

• Lifetime of a session

• Lifetime of %OBJECTNAME%

• Long polling may be a bad idea

Page 6: Dumb Ways To Die: How Not To Write TCP-based Network Applications

Ex. 1

• Slowloris (Apache): DoS● (not distributed, just denial of service)

• Slow HTTP POST● Apache, IIS, Lighttpd: DoS● Nginx: DDoS with a botnet

Page 7: Dumb Ways To Die: How Not To Write TCP-based Network Applications

Ex. 2

12 rpm AJAX page update● Backup script switched the server off

Page 8: Dumb Ways To Die: How Not To Write TCP-based Network Applications
Page 9: Dumb Ways To Die: How Not To Write TCP-based Network Applications

Content-Length

– Limit resources for all actions

– Custom protocol should define limits on the input length

Page 10: Dumb Ways To Die: How Not To Write TCP-based Network Applications

errno(3)

– The connection may be closed for no good reason

– Check errno after recvfrom(), sendto(), etc.● ENOMEM● ECONNRESET● EANYTHING

Page 11: Dumb Ways To Die: How Not To Write TCP-based Network Applications

Ex. 3

● Internet Explorer: ECONNRESET means successful connection termination

– Download status is being ignored

– Content-Length is being ignored

Page 12: Dumb Ways To Die: How Not To Write TCP-based Network Applications

Memory limits

– Resource limits:● Maximum

– ENOMEM● Minimum

– idle wait → ECONNRESET

Page 13: Dumb Ways To Die: How Not To Write TCP-based Network Applications

Ex. 4

– DNS TTL● Too big: days of downtime (continuous)● Too small: days of downtime (total)

Page 14: Dumb Ways To Die: How Not To Write TCP-based Network Applications

Latency

– 3-Way Handshake takes time– Do implement persistent connections!

● Do it from the very beginning

Page 15: Dumb Ways To Die: How Not To Write TCP-based Network Applications

They haven't listened to me!

● TCP

– T/TCP● HTTP/1.0

– HTTP/1.1

Page 16: Dumb Ways To Die: How Not To Write TCP-based Network Applications

Optimization

– Measure!– Profile!– Emulate packet loss!

Page 17: Dumb Ways To Die: How Not To Write TCP-based Network Applications

Optimization– Text-based protocols are convenient to debug

● And you will debug– Maybe even in production

– Making use of binary protocols is often a premature optimization

● BSON, Google Protocol Buffers

Page 18: Dumb Ways To Die: How Not To Write TCP-based Network Applications

Optimization

● TCP socket options:

– TCP_NODELAY: disables Nagle's algorithm● Speedup with small portions of data

– TCP_CORK (Linux): multiple portions of data in a single TCP segment

– "socket corking"

Page 19: Dumb Ways To Die: How Not To Write TCP-based Network Applications

Optimization

● TCP stack options:

– Linux: /proc/sys/net/**● net.ipv4.tcp_fin_timeout● net.ipv4.tcp_{,r,w}mem● net.core.{r,w}mem_max

– Windows: HKLM\System\CurrentControlSet\Services\Tcpip\Parameters

Page 20: Dumb Ways To Die: How Not To Write TCP-based Network Applications

IPv6

● Accidental IPv6 deployment

Page 21: Dumb Ways To Die: How Not To Write TCP-based Network Applications

21

• SO_REUSEADDR• sendfile(2)• select(2)/poll(2)/epoll(7)• {n,h}to{n,h}{s,l}()• int64_t vs long

Page 22: Dumb Ways To Die: How Not To Write TCP-based Network Applications

This is it!Artyom Gavrichenkov <[email protected]>