Topic Overview
DHCP Flow: Concepts, Internals & Interview Use Cases
Learn how DHCP (Dynamic Host Configuration Protocol) automatically assigns IP addresses, subnet masks, gateways, and DNS servers to network devices.
DHCP Flow
Why This Matters
Think of DHCP like a hotel check-in system. When you arrive, you don't manually find an available room—the front desk assigns you one automatically. DHCP does the same for network devices—when a device connects, DHCP automatically assigns it an IP address, subnet mask, gateway, and DNS servers. No manual configuration needed.
This matters because manual IP configuration is error-prone and doesn't scale. If you have 1000 devices, manually configuring each one is impractical. DHCP automates this, reducing errors and saving time. Also, DHCP manages IP address leases—devices get temporary IPs that can be reused when they disconnect.
In interviews, when someone asks "How does a device get an IP address?", they're testing whether you understand DHCP. Do you know the DORA process? Do you understand lease management? Most engineers don't. They just connect to WiFi and assume it works.
What Engineers Usually Get Wrong
Most engineers think "DHCP just assigns IP addresses." But DHCP does more—it assigns IP addresses, subnet masks, gateways, DNS servers, and other network configuration. Also, DHCP uses leases—IP addresses are temporary and must be renewed. If a device doesn't renew its lease, the IP is reclaimed and can be assigned to another device.
Engineers also don't understand that DHCP uses broadcasts. When a device first connects, it doesn't know the DHCP server's address, so it broadcasts a DISCOVER message. This means DHCP only works on the same network segment (broadcast domain). For devices on different networks, you need DHCP relay agents.
How This Breaks Systems in the Real World
A service was running in a cloud environment. Instances were getting IP addresses via DHCP. But the DHCP server had a small address pool (only 50 IPs). When the service scaled to 100 instances, some instances couldn't get IP addresses. They failed to start. The fix? Increase the DHCP address pool, or use static IP assignment for servers. DHCP is great for dynamic devices, but servers often need static IPs.
Another story: A service was using DHCP but didn't understand lease times. The default lease time was 24 hours. When instances were terminated and recreated quickly, they sometimes got the same IP (if the lease hadn't expired). This caused confusion—the new instance had the old instance's IP. The fix? Use shorter lease times for dynamic environments, or use static IPs for servers.
DHCP Process (DORA)
DHCP uses a four-step process called DORA:
- DISCOVER: Client broadcasts to find DHCP servers
- OFFER: Server offers an IP address
- REQUEST: Client requests the offered IP
- ACK: Server acknowledges and assigns the IP
Detailed DHCP Flow
Step 1: DISCOVER (Client → Network)
Client broadcasts DHCP DISCOVER
Source IP: 0.0.0.0 (no IP yet)
Destination IP: 255.255.255.255 (broadcast)
Source MAC: Client's MAC address
DHCP Message: "I need an IP address"
What happens:
- Client sends broadcast packet (no IP address yet)
- All DHCP servers on the network receive the request
- Client doesn't know which server will respond
Step 2: OFFER (Server → Client)
DHCP Server responds with DHCP OFFER
Source IP: Server's IP
Destination IP: 255.255.255.255 (broadcast) or client's MAC
Offered IP: 192.168.1.100
Subnet Mask: 255.255.255.0
Gateway: 192.168.1.1
DNS: 8.8.8.8
Lease Time: 3600 seconds (1 hour)
What happens:
- Server reserves an IP address for the client
- Server sends offer with network configuration
- Multiple servers may respond (client chooses one)
Step 3: REQUEST (Client → Network)
Client broadcasts DHCP REQUEST
Source IP: 0.0.0.0
Destination IP: 255.255.255.255
Requested IP: 192.168.1.100 (from chosen server)
Server ID: Server's IP that client chose
What happens:
- Client accepts one offer (usually first received)
- Client broadcasts request to inform all servers
- Other servers release their reserved IPs
Step 4: ACK (Server → Client)
DHCP Server sends DHCP ACK
Assigned IP: 192.168.1.100
Subnet Mask: 255.255.255.0
Gateway: 192.168.1.1
DNS: 8.8.8.8, 8.8.4.4
Lease Time: 3600 seconds
Renewal Time: 1800 seconds (50% of lease)
What happens:
- Server confirms IP assignment
- Client configures its network interface
- Client can now communicate on the network
DHCP Lease Management
Lease States
- Allocated: IP assigned to client
- Renewing: Client renewing lease (at 50% of lease time)
- Rebinding: Client trying different server (at 87.5% of lease time)
- Expired: Lease expired, IP available for reassignment
Lease Renewal Process
T=0: IP assigned (lease = 3600s)
T=1800: Client sends DHCP REQUEST to renew (50% of lease)
T=3150: Client broadcasts DHCP REQUEST (87.5% of lease)
T=3600: Lease expires, client must get new IP
Renewal (T1 = 50% of lease):
- Client sends unicast REQUEST to original server
- Server responds with ACK (extends lease)
Rebinding (T2 = 87.5% of lease):
- If renewal fails, client broadcasts REQUEST
- Any server can respond with ACK
Expiration:
- If rebinding fails, lease expires
- Client must start DORA process again
Examples
DHCP Server Configuration
1// Simple DHCP server simulation2class DHCPServer {3 private pool: string[];4 private leases: Map<string, LeaseInfo>;5 private leaseTime: number = 3600; // 1 hour67 constructor(network: string = "192.168.1.0/24") {8 this.pool = this.createPool(network);9 this.leases = new Map();10 }1112 private createPool(network: string
DHCP Client Implementation
1import dgram from 'dgram';23class DHCPClient {4 private mac: string;5 private socket: dgram.Socket;67 constructor() {8 this.mac = this.getMACAddress();9 this.socket = dgram.createSocket('udp4');10 this.socket.bind(68); // DHCP client port11 }1213 async discover(): Promise<DHCPOffer
DHCP Relay Agent
1class DHCPRelay {2 private serverIP: string;3 private interfaces: NetworkInterface[];45 constructor(serverIP: string) {6 this.serverIP = serverIP;7 this.interfaces = [];8 }910 relayDiscover(packet: DHCPPacket, receivedInterface: NetworkInterface): void {11 // Relay DISCOVER to DHCP server12 // Modify packet to include relay agent IP13 packet.giaddr = receivedInterface.ip; // Gateway IP14 packet.hops += 1;
Common Pitfalls
- DHCP exhaustion: Running out of available IP addresses. Fix: Monitor pool usage, reduce lease time, expand pool size
- Multiple DHCP servers: Conflicting IP assignments. Fix: Ensure only one DHCP server per network segment
- Lease expiration: Clients losing IP addresses. Fix: Implement proper renewal, monitor lease times
- DHCP relay misconfiguration: Clients not getting IPs across subnets. Fix: Configure relay agent IP (giaddr) correctly
- Static IP conflicts: Manually assigned IPs conflicting with DHCP pool. Fix: Exclude static IPs from DHCP pool
- Not handling NAK: Client not handling negative acknowledgment. Fix: Implement retry logic, fallback to new DISCOVER
- Security: Rogue DHCP servers. Fix: Use DHCP snooping on switches, authenticate DHCP servers
Interview Questions
Beginner
Q: Explain the DHCP process. What are the four steps?
A:
DHCP DORA Process:
-
DISCOVER (Client → Network)
- Client broadcasts: "I need an IP address"
- Source: 0.0.0.0, Destination: 255.255.255.255
- Client has no IP yet
-
OFFER (Server → Client)
- Server responds: "I can offer you 192.168.1.100"
- Includes: IP, subnet mask, gateway, DNS, lease time
- Multiple servers may respond
-
REQUEST (Client → Network)
- Client broadcasts: "I accept the offer for 192.168.1.100"
- Informs all servers which offer was chosen
- Other servers release their reserved IPs
-
ACK (Server → Client)
- Server confirms: "192.168.1.100 is yours"
- Client configures network interface
- Client can now communicate
Why broadcast?
- Client has no IP address initially
- Client doesn't know server's IP
- Multiple servers may be available
Intermediate
Q: How does DHCP lease renewal work? What happens if renewal fails?
A:
Lease Renewal Timeline:
Lease Time: 3600 seconds (1 hour)
T1 (Renewal): 1800 seconds (50% of lease)
T2 (Rebinding): 3150 seconds (87.5% of lease)
Expiration: 3600 seconds
Renewal Process (T1):
- Client sends unicast DHCP REQUEST to original server
- Server responds with ACK (extends lease)
- If successful, lease extended, process repeats
Rebinding Process (T2):
- If renewal fails, client broadcasts DHCP REQUEST
- Any DHCP server can respond with ACK
- If successful, lease extended with new server
Expiration:
- If rebinding fails, lease expires
- Client must release IP and start DORA again
- Client may get same or different IP
Example:
1// At T1 (50% of lease)2if (Date.now() >= lease.renewalTime) {3 const ack = await client.renew(lease); // Unicast to original server4 if (ack) {5 lease.expiresAt = Date.now() + ack.leaseTime * 1000;6 } else {7 // Renewal failed, wait for T28 }9}1011// At T2 (87.5% of lease)12if (Date.now() >= lease.rebindTime) {13 const ack clientlease
Senior
Q: Design a distributed DHCP system for a cloud provider that needs to handle millions of devices across multiple data centers. How do you ensure high availability, prevent IP conflicts, and handle lease management?
A:
1class DistributedDHCPSystem {2 private servers: DHCPServer[];3 private ipPool: IPPoolManager;4 private leaseStore: DistributedLeaseStore;5 private healthMonitor: HealthMonitor;67 constructor() {8 // Multiple DHCP servers for redundancy9 this.servers = [10 new DHCPServer('dc1'),11 new DHCPServer('dc2'),12 new DHCPServer('dc3')13 ];1415 // Centralized IP pool management16 this.ipPool =
Features:
- High Availability: Multiple servers with automatic failover
- Conflict Prevention: Distributed locks, atomic IP allocation
- Lease Management: Centralized lease store with expiration handling
- Consistent Hashing: Predictable server assignment for IP ranges
- Monitoring: Health checks, pool utilization, lease tracking
- Scalability: Horizontal scaling, distributed storage
-
IP Addressing (IPv4/IPv6) - DHCP automatically assigns IP addresses, understanding IP addressing explains DHCP's purpose
-
ARP & Reverse ARP - ARP resolves IP addresses to MAC addresses, DHCP assigns IP addresses, understanding both explains network configuration
-
DNS Resolution Flow - DHCP can provide DNS server information, understanding DNS complements DHCP knowledge
-
OSI Model (7 Layers) - DHCP operates at Layer 7 (Application) but assigns Layer 3 (Network) addresses, understanding the OSI model provides context
-
Subnetting & CIDR - DHCP assigns IP addresses within subnets, understanding subnetting helps configure DHCP scopes
-
DORA process: DISCOVER → OFFER → REQUEST → ACK for IP assignment
-
Broadcast communication: Client broadcasts because it has no IP initially
-
Lease management: IPs assigned for a limited time (lease), must be renewed
-
Renewal timing: T1 (50% of lease) for renewal, T2 (87.5%) for rebinding
-
DHCP relay: Forwards DHCP messages between network segments
-
High availability: Multiple servers, failover, distributed lease storage
-
Conflict prevention: Use distributed locks, atomic allocation, consistent hashing
-
Lease expiration: Cleanup expired leases, release IPs back to pool
-
Security: DHCP snooping, authentication, rogue server detection
Key Takeaways
DORA process: DISCOVER → OFFER → REQUEST → ACK for IP assignment
Broadcast communication: Client broadcasts because it has no IP initially
Lease management: IPs assigned for a limited time (lease), must be renewed
Renewal timing: T1 (50% of lease) for renewal, T2 (87.5%) for rebinding
DHCP relay: Forwards DHCP messages between network segments
High availability: Multiple servers, failover, distributed lease storage
Conflict prevention: Use distributed locks, atomic allocation, consistent hashing
Lease expiration: Cleanup expired leases, release IPs back to pool
Security: DHCP snooping, authentication, rogue server detection
Related Topics
IP Addressing (IPv4/IPv6)
DHCP automatically assigns IP addresses, understanding IP addressing explains DHCP's purpose
ARP & Reverse ARP
ARP resolves IP addresses to MAC addresses, DHCP assigns IP addresses, understanding both explains network configuration
DNS Resolution Flow
DHCP can provide DNS server information, understanding DNS complements DHCP knowledge
OSI Model (7 Layers)
DHCP operates at Layer 7 (Application) but assigns Layer 3 (Network) addresses, understanding the OSI model provides context
Subnetting & CIDR
DHCP assigns IP addresses within subnets, understanding subnetting helps configure DHCP scopes
What's next?