三种地址正好对应三层:MAC 地址是链路层的,在局域网里标识一块网卡;IP 地址是网络层的,用来在不同网络之间找到主机;端口是传输层的,标识主机上的哪个进程。不经过 NAT 的话,数据包在路上 IP 地址不变,MAC 地址每经过一跳都会换。
English PitchEnglish · ~60s
OSI is a seven-layer reference model—Physical, Data Link, Network, Transport, Session, Presentation, and Application—while the real-world TCP/IP stack uses four layers: Link, Network (IP), Transport (TCP/UDP), and Application (HTTP/DNS). Each layer encapsulates data with its own header on send and strips it on receive, letting protocols evolve independently—like HTTP/3 swapping TCP for QUIC. The three addresses map straight to those layers: a MAC address identifies a NIC on the local link and changes at every router hop; an IP address routes across networks to the destination host and stays unchanged end-to-end barring NAT; and a port targets the specific process listening on that host.
Key terms:TCP/IP four-layer model, encapsulation, MAC vs IP vs port, NAT
Likely follow-up: "Where does TLS sit in these models, and why doesn't a successful ping prove that a backend service port is reachable?"
TCP is connection-oriented and delivers a reliable, ordered byte stream using sequence numbers, ACKs, retransmissions, plus flow and congestion control. UDP is connectionless and message-oriented with just an 8-byte header—no handshake, no ordering, and no automatic retries—so packets can arrive out of order or be dropped. In practice, use TCP whenever data integrity matters, like HTTP/1.1, HTTP/2, or database connections, and choose UDP when low latency beats perfect delivery—like video calls or gaming, where waiting on TCP retransmissions causes head-of-line blocking—or for tiny single-packet lookups like DNS to avoid handshake overhead. That's also why QUIC builds custom reliability and multiplexing on top of UDP for HTTP/3.
Likely follow-up: "What does it mean that TCP is a byte stream rather than message-oriented, and how do application protocols handle message boundaries?"
SYN Flood 与 SYN Cookie:攻击者发送大量伪造源地址的 SYN,占满半连接队列。SYN Cookie 让服务端不保存半连接状态,而是把状态编码进 SYN+ACK 的初始序列号里,收到合法的 ACK 时再恢复出连接信息。
面试口语版中文 · 约 105 秒
三次握手是:客户端发 SYN,带上自己的初始序列号;服务端回 SYN 加 ACK,确认客户端的序列号,同时带上自己的;客户端再回一个 ACK。为什么是三次?因为要让双方都确认对方既能发也能收,并且互相知道对方的初始序列号。只有两次的话,服务端不知道客户端有没有收到自己的回复;而且网络里一个迟到的旧 SYN 到了,服务端就会白白建立一个连接。
四次挥手是:主动方发 FIN,表示我不再发数据了;对方回 ACK;等对方把剩下的数据发完,再发自己的 FIN;主动方回 ACK。因为 TCP 是全双工的,两个方向要分别关闭,对方收到 FIN 的时候可能还有数据没发完,所以 ACK 和 FIN 是分开的。
主动关闭的一方最后会进入 TIME_WAIT,等两倍的 MSL。一是万一最后那个 ACK 丢了,对方重发 FIN 时我还能回应;二是让这次连接里迟到的包在网络里消失,不会被新连接误收。短连接太多的话 TIME_WAIT 会占用大量端口,根本解决办法是用长连接和连接池。
English PitchEnglish · ~55s
TCP opens with a three-way handshake—SYN, SYN-ACK, ACK—so both sides confirm bidirectional reachability and sync initial sequence numbers; two steps would let a stale duplicate SYN leave half-open state on the server. Closing takes four steps—FIN, ACK, FIN, ACK—because TCP is full-duplex, so the other side can finish flushing data before sending its own FIN. The active closer stays in TIME_WAIT for 2MSL to re-ACK a retransmitted FIN if its final ACK is lost and let delayed segments expire. In production, TIME_WAIT port exhaustion usually signals short-lived connection churn—the real fix is persistent connection pooling, not disabling TIME_WAIT.
TCP guarantees reliability through four mechanisms: sequence numbers for ordering and deduplication, cumulative ACKs, checksums for corruption, and both timeout-based and fast retransmission after three duplicate ACKs. Flow control and congestion control both throttle the sender via sliding windows, but protect different things: flow control protects the receiver using its advertised receive window (rwnd), while congestion control protects the network using the sender's congestion window (cwnd)—growing exponentially in slow start, linearly in congestion avoidance, and backing off on loss. The effective send window—bytes in flight—is capped by min(rwnd, cwnd). In production, slow start is a major reason persistent connection pools beat short connections.
Key terms:sliding window, flow vs congestion control, slow start, fast retransmit
Likely follow-up: "How does Google's BBR differ from traditional loss-based congestion control like CUBIC, and what happens when a receiver advertises a zero window?"
HTTP/1.1 is text-based with keep-alive, but sequential responses on one connection cause application-level head-of-line blocking, forcing clients to open parallel connections. HTTP/2 introduces binary framing, HPACK header compression, and stream multiplexing over a single TCP connection—yet a single lost TCP packet still stalls every stream. HTTP/3 solves that by moving to UDP-based QUIC, giving each stream independent loss recovery, faster 1-RTT TLS 1.3 handshakes, and seamless network migration. In production, edge CDNs or load balancers usually terminate HTTP/2 and HTTP/3 for clients, while internal microservices communicate over pooled HTTP/1.1 or gRPC.
The real difference between GET and POST is semantics, not URL versus body. GET is safe and idempotent—for reads you can cache and retry freely—while POST triggers state-changing actions and isn't idempotent. Idempotency means running a request once or ten times yields the same server state: GET, PUT, and DELETE are idempotent, whereas retrying a timed-out POST safely in production requires an Idempotency-Key. For status codes, always distinguish 401 unauthenticated from 403 forbidden, 301 permanent from 302 temporary redirects, and 502 bad upstream response from 503 unavailable and 504 gateway timeout.
Key terms:idempotency, Idempotency-Key, 401 vs 403, 502 vs 504
Likely follow-up: "How do PUT and PATCH differ on idempotency, and how would you implement idempotency keys on a payment POST endpoint?"
追问3 个
PUT 和 PATCH 的区别?
PUT 用完整的表示替换资源,幂等;PATCH 只修改部分字段,是否幂等取决于具体操作(“设置为 5”幂等,“加 1”不幂等)。
Because standard HTTP is request-response, we use three patterns for real-time updates. Long polling holds an HTTP request open until data arrives or times out, then immediately re-polls—highly compatible, but costs a full HTTP round trip per message. SSE keeps a single HTTP response open (text/event-stream) for unidirectional server-to-client text pushes with built-in browser reconnect and Last-Event-ID, making it ideal for notifications, dashboards, and AI streaming. WebSocket upgrades over HTTP 101 into a full-duplex binary-or-text channel with tiny frame headers, suited for chat, collaborative editing, and gaming. For stateful WebSockets at scale, you also need heartbeats, reconnect catch-up, and Redis Pub/Sub or a broker for cross-instance fan-out.
Likely follow-up: "In a multi-instance deployment behind a load balancer, how do you route a WebSocket push to the specific server holding a user's connection?"
HTTP caching works in two steps: freshness-based caching serves a local copy without any network request while Cache-Control: max-age (or s-maxage for CDNs) is still fresh; once stale, conditional revalidation sends If-None-Match with the ETag and gets a bodyless 304 Not Modified if unchanged. Don't confuse directives: no-cache still caches but forces revalidation, no-store disables caching entirely, and private stops CDNs from caching user-specific data—I pair content-hashed immutable assets with no-cache HTML. For state, a Cookie is small client-side data auto-sent by the browser (hardened with HttpOnly, Secure, and SameSite), while a Session holds state server-side—often in Redis—and is referenced via a session ID stored in the cookie.
Likely follow-up: "How do you prevent users from getting stuck on stale JavaScript bundles after a frontend deploy, and what happens if a CDN accidentally caches a personalized response?"
HTTPS layers TLS between HTTP and TCP for encryption, integrity, and server authentication. Because asymmetric crypto is too slow for bulk payloads, TLS uses ephemeral ECDHE key exchange and certificate signatures during the handshake to negotiate a shared session key, then switches to fast symmetric ciphers like AES-GCM for application data. TLS 1.3 trims the handshake from 2-RTT to 1-RTT by including the client's key share in ClientHello, removes weak ciphers, and enforces forward secrecy so a compromised private key can't decrypt past traffic. In production, TLS usually terminates at the edge load balancer, with mTLS inside when zero-trust is required.
A TLS certificate binds a domain to a public key via a CA's signature. The browser checks four things: the signature chain from leaf and intermediates to a trusted root CA, the hostname in SAN, the validity dates, and revocation status (e.g., via CRL or OCSP Stapling). Because certificates are public, the server also signs handshake data (CertificateVerify) with its private key. That blocks MITM attacks both ways: forwarding the real cert fails without the private key, and presenting a fake cert fails without a trusted CA—unless clients disable checks or trust a rogue root CA. In microservices, mTLS verifies both peers, though authorization still stays in application logic.
Likely follow-up: "When a Java service calls an HTTPS API and fails with PKIX path building failed, what are the most likely causes and the right fix?"
追问3 个
Java 调用 HTTPS 接口报 PKIX path building failed,是什么原因?
证书链验证失败:服务器没有发送完整的中间证书,或者签发它的 CA 不在 JVM 的信任库(truststore)里。应修复服务端证书链或把 CA 加入信任库,而不是关闭校验。
这里每一跳都能展开,比如 DNS 缓存切换、TLS 1.3 握手或者四/七层负载均衡,您更想先聊哪一块?
English PitchEnglish · ~70s
Let me walk through the high-level flow in six steps first, and we can dive deeper anywhere you'd like. First, the browser parses the URL and checks its local HTTP cache. Next, DNS resolves the hostname via local caches and a recursive resolver iterating from root to authoritative servers, typically returning a CDN or load balancer IP. Then the browser runs a TCP three-way handshake and TLS 1.3 handshake to verify the certificate, negotiate a session key, and pick HTTP/2 via ALPN. After sending the HTTP request, the edge terminates TLS and routes through a load balancer and API gateway to the backend, cache, and database, returning HTML for the browser to render. Would you like me to zoom in on DNS, TLS, or the load-balancer and backend layer?
Key terms:DNS resolution, TLS termination, ALPN, API gateway
Likely follow-up: "If a user reports that the site isn't loading, what exact layer-by-layer CLI commands do you run to isolate where the chain broke?"
DNS 的作用是把域名翻译成 IP 地址。查询的时候先看本地缓存:浏览器缓存、操作系统缓存、hosts 文件。都没有的话,就去问配置好的递归解析器,比如运营商的 DNS 或者 8.8.8.8。
解析器如果也没有缓存,就替我们一级一级去问:先问根服务器,根告诉它 com 归谁管;再问 com 的顶级域服务器,它告诉 example.com 的权威服务器是谁;最后问权威服务器拿到真正的记录。解析器把结果按 TTL 缓存起来再返回。客户端问解析器是递归查询,要的是最终答案;解析器问各级服务器是迭代查询。
DNS 改了为什么不马上生效?因为沿途各级都有缓存,要等旧记录的 TTL 过期才会重新查。所以做切换之前,我会提前把 TTL 调小,比如 60 秒,改完稳定了再调回去。
DNS resolves hostnames through a layered lookup: browser cache, OS cache, and /etc/hosts, followed by a recursive query—typically over UDP 53—to a recursive resolver. On a miss, the resolver iterates from root to TLD to the authoritative nameserver for the A or CNAME record, caching the result for its TTL. DNS updates aren't instant because there's no push invalidation—every resolver keeps serving stale records until TTL expires. Before any production cutover, I lower the TTL to 60 seconds ahead of time, and watch out for JVM DNS caching and persistent connection pools keeping traffic pinned to old IPs.
Key terms:recursive vs iterative, authoritative nameserver, TTL, CNAME
Likely follow-up: "Why isn't DNS alone sufficient for instant failover or fine-grained load balancing, and when does DNS fall back from UDP to TCP?"
代理之后的客户端信息:经过负载均衡后,应用看到的源 IP 是负载均衡的地址。真实客户端 IP 放在 X-Forwarded-For,原始协议放在 X-Forwarded-Proto。X-Forwarded-For 可以被客户端伪造,只能信任由自己的代理追加的部分(按受信代理的层数从右往左取),不能直接取第一个值做限流或鉴权。
正向代理 vs 反向代理:正向代理代表客户端访问外部(公司上网代理、VPN);反向代理代表服务端接收请求(Nginx、负载均衡),客户端不知道后面的真实服务器。
有一点要注意:经过负载均衡以后,应用看到的来源 IP 是负载均衡的,真实客户端 IP 在 X-Forwarded-For 里。但这个头客户端可以伪造,只能信任自己的代理加上去的那部分,不能直接拿第一个值去做限流。
CDN 是把静态内容缓存到离用户近的边缘节点。域名通过 CNAME 指到 CDN,CDN 的 DNS 根据用户位置返回最近的节点;节点有缓存就直接返回,没有就回源站拿,再缓存起来。这样距离近了、源站压力也小了。
English PitchEnglish · ~60s
A load balancer distributes traffic across instances for scale and availability, pulling failed nodes via health checks. L4 balancers like NLB operate at the transport layer per connection using IP and port—fast and protocol-agnostic, though long-lived connections can pin traffic to old nodes. L7 balancers like Envoy or ALB inspect HTTP per request to route by URL or header and handle TLS termination, retries, and rate limiting at higher CPU cost—just don't blindly trust client-spoofable X-Forwarded-For headers. A CDN accelerates delivery by steering users via GeoDNS or Anycast to nearby edge nodes that terminate TLS, serve cached content locally with low RTT, and only hit origin on misses.
Key terms:L4 vs L7, health check, X-Forwarded-For, Anycast / edge caching
Likely follow-up: "Why can long-lived gRPC or HTTP/2 connections become unevenly balanced behind an L4 load balancer, and how do you safely extract the real client IP from X-Forwarded-For?"