Home/Learn/Computer Networks/DDoS Protection

DDoS Protection

Advanced
Security

A Distributed Denial of Service (DDoS) attack overwhelms a target with traffic from thousands of sources simultaneously. Defence requires absorbing volumetric attacks at the edge, filtering protocol attacks at the network layer, and rate-limiting application-layer attacks.

Overview

DDoS attacks come in three categories: volumetric attacks (UDP flood, ICMP flood — saturate bandwidth), protocol attacks (SYN flood, fragmented packet attacks — exhaust state tables), and application-layer attacks (HTTP flood, Slowloris — target server resources). Mitigation requires a layered approach: Anycast traffic scrubbing at the edge (Cloudflare, AWS Shield), SYN cookies at the TCP layer, and rate limiting + WAF at the application layer. Understanding DDoS and its mitigations is essential for any backend or infrastructure interview.

DDoS Attack Categories

The three categories differ in the layer they target, the bandwidth they consume, and the mitigation techniques required. Volumetric attacks are the largest in scale (Tbps); application attacks are the hardest to distinguish from legitimate traffic.

DDoS attack categories and mechanics
// Category 1: Volumetric — flood the pipe
// UDP Flood: send millions of UDP packets to random ports
//   target receives → checks no listener → sends ICMP "port unreachable"
//   → exhausts outbound bandwidth replying to spoofed sources
// DNS Amplification: send small DNS query with spoofed source IP
//   open resolver replies with 50× larger response to victim
//   amplification factor: ~50x

// Category 2: Protocol — exhaust connection state
// SYN Flood:
//   Attacker sends thousands of TCP SYN packets (spoofed source IPs)
//   Server allocates half-open connection state (SYN_RCVD) and sends SYN-ACK
//   ACK never arrives → connection table fills up → new legitimate SYNs rejected
//   Mitigation: SYN cookies (server encodes state in ISN, no table needed)

// Category 3: Application — mimic legitimate requests
// HTTP Flood: thousands of bots send valid GET /search?q=... requests
//   Server must execute DB queries, render pages
//   Hard to distinguish from legitimate traffic — requires rate limiting + fingerprinting
// Slowloris: open many connections, send headers very slowly
//   Keeps server connections open → pool exhaustion

SYN Cookies

SYN cookies eliminate the need to store half-open connection state on the server. Instead of allocating a connection entry on SYN, the server encodes the connection parameters in the Initial Sequence Number (ISN) of the SYN-ACK. Only when the final ACK arrives — containing the correct sequence number — does the server create a connection.

SYN cookie mechanism
// Normal TCP: server stores half-open state
// SYN received → allocate connection in SYN_RCVD table → SYN-ACK
// (table fills up under SYN flood)

// SYN Cookies: no state stored on SYN
// SYN received from client (src=1.2.3.4:54321, dst=10.0.0.5:443)
// Server computes:
//   cookie = HMAC(secret_key, src_ip, src_port, dst_ip, dst_port, timestamp)
//   ISN = encode(cookie, MSS, timestamp) in 32 bits
// Server sends SYN-ACK with ISN = cookie (no state stored)
//
// If attacker (spoofed): ACK never arrives → no cost to server
//
// If legitimate client: sends ACK with ack_number = cookie + 1
//   Server recomputes cookie from packet fields → matches → create connection
//   Recover MSS from encoded bits → allocate socket

// Linux:
sysctl -w net.ipv4.tcp_syncookies=1
// Nginx: listen 443 ssl backlog=65535;

Anycast Scrubbing & CDN-Level Mitigation

Cloud DDoS mitigation services (AWS Shield Advanced, Cloudflare Magic Transit) use Anycast routing to attract attack traffic to globally distributed scrubbing centres. Legitimate traffic is cleaned and forwarded; attack traffic is dropped. This can absorb Tbps-scale volumetric attacks before they reach the origin.

Anycast scrubbing and CDN-level DDoS mitigation
// Anycast routing:
// Your IP block (e.g. 203.0.113.0/24) is announced via BGP from 200+ PoPs worldwide
// Attacker's traffic naturally routes to the nearest PoP (BGP shortest path)
// Each PoP can absorb 10–100 Gbps → total capacity: Tbps-scale
//
// Scrubbing pipeline (simplified):
// 1. Volumetric: rate-limit by src ASN, drop known bad IP ranges (BGP Blackhole)
// 2. Protocol:   SYN proxy — PoP terminates TCP, forwards only established conns
// 3. Application: WAF rules, CAPTCHA challenge, JS challenge for browsers
//
// AWS Shield tiers:
// Standard — always-on, free, protects against most volumetric attacks
// Advanced — $3,000/month, SRT (Shield Response Team), Layer 7 protection,
//             cost protection (absorbs AWS data transfer charges during attack)

// Cloudflare Under Attack Mode — JS challenge on every request:
// Forces browser to execute CPU-intensive JS proof-of-work
// Bots without JS engine fail; real browsers pass

Rate Limiting & Application-Layer Defence

Against HTTP-layer DDoS, rate limiting is the first line of defence. Limit by IP, by user token, or by API key. Use exponential backoff on retries and return 429 Too Many Requests. Combine with connection limits, request timeouts, and circuit breakers.

Rate limiting in Nginx and Spring Boot
// Nginx rate limiting (token bucket):
http {
  limit_req_zone $binary_remote_addr zone=api:10m rate=100r/m;

  server {
    location /api/ {
      limit_req zone=api burst=20 nodelay;
      limit_req_status 429;
    }
  }
}

// Spring Boot rate limiting (Resilience4j):
@Bean
public RateLimiter rateLimiter() {
  return RateLimiter.of("api", RateLimiterConfig.custom()
      .limitRefreshPeriod(Duration.ofSeconds(1))
      .limitForPeriod(100)            // 100 req/sec
      .timeoutDuration(Duration.ofMillis(500))
      .build());
}

// Connection limits to prevent Slowloris:
// Nginx:
worker_connections 10000;
keepalive_timeout 30s;          // close idle connections quickly
client_header_timeout 10s;      // timeout if headers arrive too slowly
client_body_timeout 10s;

Key Points to Remember

  • 1DDoS attacks are volumetric (flood bandwidth), protocol (exhaust state like SYN flood), or application-layer (HTTP flood).
  • 2SYN cookies prevent SYN flood attacks by encoding connection state in the ISN — no table allocation needed until ACK received.
  • 3Anycast scrubbing centres absorb Tbps-scale volumetric attacks by distributing traffic across global PoPs before it reaches the origin.
  • 4AWS Shield Standard is free and protects against common volumetric attacks; Advanced adds Layer 7 protection and a response team.
  • 5Application-layer DDoS defence: rate limiting (by IP, token), connection limits, WAF rules, and CAPTCHA/JS challenges.

Interview Questions

Sign in to ask Aria
1

What are the three categories of DDoS attacks and how do you mitigate each?

HardAmazon
2

How do SYN cookies prevent SYN flood attacks?

HardRazorpay
3

How does Anycast help absorb DDoS attacks?

HardFlipkart
4

What is a DNS amplification attack?

MediumThoughtWorks
5

How would you rate-limit an API to protect against application-layer DDoS?

MediumEqual Experts

Ask Aria about DDoS Protection

Your personal AI tutor — ask anything about this concept

Revision Status

Personal Notes

Sign in to save personal notes for this topic.

Discussion

Sign in to join the discussion.

Loading discussion…