Networks are the backbone of distributed systems. Understanding how networks behave, fail, and recover is essential for building reliable systems. If you think of a distributed system as a team of people working together, the network is the phone system connecting them. And like a phone system, it can have static, dropped calls, crossed wires, and dead zones — except in distributed systems, you have to design your software to keep working despite all of these problems simultaneously.
For most applications, the standard Linux TCP/IP stack is sufficient. However, at Staff/Principal level, when building ultra-high-performance systems (like HFT platforms or distributed databases like FaRM or eRPC), the OS kernel itself becomes the bottleneck.
Staff Tip: Only reach for RDMA/DPDK if your p99 latency requirements are sub-100 microseconds. For most “at-scale” systems, optimizing your serialization (Protobuf) and batching is more impactful than kernel bypass.
As we scale beyond 100Gbps networking, even Kernel-Bypass (DPDK) starts to consume significant CPU cycles just for processing packets. The modern solution is to move the entire Networking Data Plane into dedicated hardware.
A DPU (or SmartNIC) is a “computer in front of the computer.” It contains its own ARM cores, memory, and specialized hardware accelerators.
AWS Nitro: Perhaps the most famous example. AWS moved VPC networking, EBS storage encryption, and management logic off the main Xeon CPU onto dedicated Nitro cards.
Offloaded Tasks:
Encryption (TLS/IPSec): Zero CPU cost for the application.
By using DPUs, cloud providers can offer “Bare Metal” instances that still have full VPC networking and EBS support, because the infrastructure logic lives on the DPU, not in the host OS.
Level
Networking Logic
CPU Cost
Latency
Standard
Kernel Stack
High
100μs+
Advanced
User-space (DPDK)
Medium
10μs+
Principal
Hardware (DPU/Nitro)
Zero
< 2μs
Staff Tip: When designing for “Cloud Native” scale, the DPU is the boundary that allows you to separate the Tenant workload from the Provider infrastructure.
The traditional way. The DNS server detects the user’s IP (or their resolver’s IP) and returns the IP of the closest DC.
Pros: Simple to implement.
Cons: DNS Caching. Even with low TTLs, some resolvers or browsers cache results for minutes. If a DC fails, users may still be routed to it until the cache expires.
Anycast allows multiple physical servers (in different parts of the world) to share the same IP address.
┌─────────────────────────────────────────────────────────────────────────────┐│ HOW ANYCAST WORKS │├─────────────────────────────────────────────────────────────────────────────┤│ ││ User in London [Internet (BGP)] User in Tokyo ││ │ │ │ ││ │ (Request to 1.1.1.1) │ (Request to 1.1.1.1) │ ││ ▼ │ ▼ ││ ┌──────────────┐ │ ┌──────────────┐││ │ London Edge │◄────────────────────┴─────────────────────►│ Tokyo Edge │││ │ (IP 1.1.1.1) │ │ (IP 1.1.1.1) │││ └──────────────┘ └──────────────┘││ ││ BGP (Border Gateway Protocol) ensures that the user's packet takes the ││ shortest "path" (AS hops) to the nearest router advertising that IP. ││ │└─────────────────────────────────────────────────────────────────────────────┘
Benefits of Anycast:
Lowest Latency: Packets naturally flow to the closest network edge.
Instant Failover: If the London Edge goes down, BGP stops advertising that IP from there. The internet automatically routes the next packet to the next closest DC (e.g., Paris).
DDoS Mitigation: Anycast naturally spreads the load of a flood attack across many global points of presence (PoPs).
Staff Tip: For a global, high-availability service, use Anycast for your entry point (Edge/LB) and use DNS as a secondary layer for steering traffic between specific backends.
Google’s high-performance RPC framework. Used internally at Google for virtually all inter-service communication (billions of RPCs per second), and increasingly the standard for microservice communication in the broader industry.
// user.proto - Define your service contract.// This file is the "single source of truth" -- client and server// code is auto-generated from it, eliminating hand-maintained API docs.syntax = "proto3";service UserService { // Unary RPC rpc GetUser(GetUserRequest) returns (User); // Server streaming rpc ListUsers(ListUsersRequest) returns (stream User); // Client streaming rpc CreateUsers(stream User) returns (CreateUsersResponse); // Bidirectional streaming rpc Chat(stream ChatMessage) returns (stream ChatMessage);}message GetUserRequest { string id = 1;}message User { string id = 1; string name = 2; string email = 3;}
Pros:
Binary protocol (efficient)
Strong typing with Protocol Buffers
Streaming support
Generated client/server code
Cons:
Binary = harder to debug
Browser support requires grpc-web
Steeper learning curve
HTTP-based APIs with JSON payloads.
# Clientimport requestsresponse = requests.get( "https://api.example.com/users/123", headers={"Authorization": "Bearer token"})user = response.json()# Server (FastAPI)from fastapi import FastAPIapp = FastAPI()@app.get("/users/{user_id}")async def get_user(user_id: str): user = await db.get_user(user_id) return user
Interview Question: “What can go wrong with an RPC call?”
┌─────────────────────────────────────────────────────────────────────────────┐│ RPC FAILURE SCENARIOS │├─────────────────────────────────────────────────────────────────────────────┤│ ││ 1. REQUEST LOST ││ ───────────── ││ Client──►Network──X ││ Result: Server never sees request ││ Solution: Timeout + retry ││ ││ 2. REQUEST DELIVERED, SERVER CRASHED BEFORE PROCESSING ││ ──────────────────────────────────────────────────── ││ Client──►Server (crashes) ││ Result: Request lost ││ Solution: Timeout + retry ││ ││ 3. SERVER PROCESSED, RESPONSE LOST ││ ───────────────────────────────── ││ Client──►Server──►Processing──►Response──X ││ Result: Client times out, retries ││ Problem: WORK DONE TWICE! ││ Solution: Idempotency ││ ││ 4. SERVER SLOW (not dead) ││ ─────────────────────── ││ Client──►Server (processing slowly)──►Timeout ││ Result: Client thinks server is dead, retries ││ Problem: Request processed multiple times! ││ Solution: Idempotency + longer timeouts ││ ││ 5. SERVER OVERLOADED ││ ───────────────── ││ Client──►Server (queue full)──►429 / Connection refused ││ Result: Request rejected ││ Solution: Retry with backoff + circuit breaker ││ ││ KEY INSIGHT: ││ ──────────── ││ The client can NEVER know for sure if the server processed the request. ││ This is the fundamental uncertainty of distributed systems -- the ││ "Two Generals Problem." Every design decision you make is a strategy ││ for coping with this irreducible uncertainty. ││ │└─────────────────────────────────────────────────────────────────────────────┘
# Phi Accrual Failure Detector Implementationimport mathimport timefrom collections import dequeclass PhiAccrualDetector: """ Phi Accrual Failure Detector (Cassandra, Akka use this) Instead of binary "dead/alive", outputs a suspicion level (phi). Higher phi = more suspicious that node is dead. Real-world analogy: Imagine you have a friend who texts you "good morning" every day. If they normally text between 7:00-7:15 AM and it's now 7:20, you're slightly worried. By 8:00 AM, you're quite worried. By noon, you're calling hospitals. The phi value captures this graduated suspicion mathematically -- it's the negative log of the probability that the node is still alive given how long it's been since the last heartbeat. A phi of 1 means ~10% chance the node is dead. A phi of 8 means ~0.0000001% chance it's alive -- effectively dead. """ def __init__(self, threshold: float = 8.0, window_size: int = 1000): self.threshold = threshold # phi > threshold --> consider dead # Cassandra uses 8; Akka defaults to 10 self.window_size = window_size self.intervals = deque(maxlen=window_size) self.last_heartbeat = None def heartbeat(self): """Record a heartbeat arrival.""" now = time.time() if self.last_heartbeat is not None: interval = now - self.last_heartbeat self.intervals.append(interval) self.last_heartbeat = now def phi(self) -> float: """ Calculate the suspicion level (phi). Returns infinity if no heartbeats received. Returns ~0 if heartbeat was just received. The math: We model heartbeat inter-arrival times as a normal distribution. Phi is -log10(P(next_heartbeat > time_since_last)). This naturally adapts to network jitter -- a noisy link with high variance tolerates longer gaps before raising the alarm. """ if self.last_heartbeat is None or len(self.intervals) == 0: return float('inf') now = time.time() time_since_last = now - self.last_heartbeat # Calculate mean and variance of intervals mean = sum(self.intervals) / len(self.intervals) variance = sum((x - mean) ** 2 for x in self.intervals) / len(self.intervals) std_dev = math.sqrt(variance) if variance > 0 else mean / 4 # Calculate phi using normal distribution CDF # P(X > time_since_last) where X ~ N(mean, std_dev) y = (time_since_last - mean) / std_dev probability = 1 - 0.5 * (1 + math.erf(y / math.sqrt(2))) # Convert to phi if probability == 0: return float('inf') return -math.log10(probability) def is_available(self) -> bool: """Check if node is considered available.""" return self.phi() < self.threshold# Usagedetector = PhiAccrualDetector(threshold=8.0)# Simulate heartbeatsfor _ in range(10): detector.heartbeat() time.sleep(1.0) # Normal 1-second heartbeatprint(f"Phi: {detector.phi()}") # Low phiprint(f"Available: {detector.is_available()}") # Truetime.sleep(5.0) # Miss several heartbeatsprint(f"Phi after delay: {detector.phi()}") # High phiprint(f"Available: {detector.is_available()}") # Likely False
┌─────────────────────────────────────────────────────────────────────────────┐│ GOSSIP PROTOCOL │├─────────────────────────────────────────────────────────────────────────────┤│ ││ ANALOGY: Like how rumors spread in a social network ││ ││ ALGORITHM: ││ ────────── ││ Every T seconds: ││ 1. Pick random peer from known nodes ││ 2. Exchange state information ││ 3. Merge received state with local state ││ ││ EXAMPLE: Membership gossip ││ ───────────────────────── ││ ││ Round 1: ││ ┌───┐ ┌───┐ ┌───┐ ┌───┐ ││ │ A │────►│ B │ │ C │ │ D │ ││ │{A}│ │{B}│ │{C}│ │{D}│ ││ └───┘ └───┘ └───┘ └───┘ ││ ││ Round 2: After gossip ││ ┌───┐ ┌───┐ ┌───┐ ┌───┐ ││ │ A │ │ B │────►│ C │ │ D │ ││ │A,B│ │A,B│ │ C │ │ D │ ││ └───┘ └───┘ └───┘ └───┘ ││ ││ After O(log N) rounds: All nodes know about all others ││ ││ PROPERTIES: ││ ─────────── ││ ✓ Scalable: O(log N) rounds to converge ││ ✓ Fault-tolerant: Works with node failures ││ ✓ Decentralized: No single point of failure ││ ✓ Eventually consistent: All nodes converge ││ ││ USED BY: Cassandra, Consul, SWIM, Serf ││ │└─────────────────────────────────────────────────────────────────────────────┘
Question: What is the Two Generals Problem and how does it relate to distributed systems?Answer:
The Two Generals Problem demonstrates the impossibility of achieving consensus over an unreliable channel.Scenario:
Two armies (A and B) must attack simultaneously to win
They communicate via messengers through enemy territory
Messengers can be captured (messages lost)
The Problem:
A sends “Attack at dawn” to B
A doesn’t know if B received it
B sends “Acknowledged” to A
B doesn’t know if A received the ack
A sends “Acknowledged your ack” to B
This continues infinitely!
Implications for Distributed Systems:
No protocol can guarantee agreement with unreliable messaging
This is why we need:
Timeouts and retries
Idempotency
Eventual consistency (accept uncertainty)
Consensus protocols (for synchronous systems)
Q3: Design a distributed notification system
Question: Design a system that sends notifications to millions of users with at-least-once delivery.Architecture:
Explain the difference between at-most-once, at-least-once, and exactly-once delivery semantics. Which one would you choose for a payment processing system and why?
Strong Answer:
At-most-once means the sender fires and forgets. No retries. If the message is lost, it is gone. This is suitable for metrics or telemetry where losing a few data points is acceptable. UDP-based protocols often provide this.
At-least-once means the sender retries until it gets an acknowledgment. The message will definitely arrive, but it may arrive more than once. This is the default for most message queues (Kafka, RabbitMQ). The receiver must be prepared to handle duplicates.
Exactly-once is the holy grail. It means every message is processed exactly one time. True exactly-once across an unreliable network is technically impossible (Two Generals Problem). What systems actually provide is “effectively exactly-once” through a combination of at-least-once delivery plus idempotent processing on the receiver side.
For a payment system, I would choose at-least-once delivery with idempotent processing. The reason: losing a payment (at-most-once) is unacceptable, and exactly-once is not achievable in the general case. So I ensure every payment request carries an idempotency key. If the same key arrives twice, the second request returns the cached result of the first without re-executing the payment. This gives the caller the illusion of exactly-once while the underlying transport provides at-least-once.
Follow-up: How would you implement an idempotency layer for this payment system at scale?I would maintain an idempotency store — a fast key-value store like Redis or DynamoDB — that maps idempotency keys to their response. Before processing any payment, the service checks this store. If the key exists, it returns the stored response. If not, it processes the payment, stores the result under the key, and returns it. The critical subtlety is atomicity: the “check, process, store” sequence must be atomic with respect to concurrent retries of the same key. I would use a distributed lock or a compare-and-swap operation on the idempotency key to prevent two workers from processing the same payment simultaneously. The idempotency key has a TTL (e.g., 24 hours) to prevent unbounded storage growth. For additional safety, I would include a unique request ID in the payment’s database record and use a database-level unique constraint as a second line of defense.
You are debugging a production incident where an RPC call between Service A and Service B is intermittently timing out. Walk me through your debugging approach.
Strong Answer:
First, I determine the scope: is this affecting all calls to Service B, or only calls from Service A? If Service B’s latency is elevated for all callers, the problem is likely on the server side (GC pauses, resource exhaustion, slow dependency). If only Service A is affected, the problem is likely network-related or specific to the A-B path.
I check distributed traces (Jaeger/Zipkin) to see where the latency is. Is the time spent in network transit, in Service B’s queue, or in Service B’s processing? If the trace shows the request arriving at B quickly but B responding slowly, it is a server-side issue. If the request takes a long time to arrive, it is a network issue.
For network issues, I check for packet loss and retransmission rates between A and B (TCP retransmit metrics, ss -ti output). A common culprit is a congested switch or a misconfigured firewall doing deep packet inspection. I also check if A and B are in different availability zones, which adds cross-AZ latency and increases the chance of intermediate network issues.
For server-side issues on B, I look at B’s CPU, memory, GC pause logs, thread pool utilization, and connection pool exhaustion. A full connection pool means B is waiting for a downstream dependency, not that B itself is slow.
I also check the timeout configuration: is A’s timeout reasonable given B’s p99 latency? If A has a 100ms timeout but B’s p99 is 95ms, you will see frequent timeouts just from normal tail latency.
Follow-up: The investigation reveals that Service B has occasional 2-second GC pauses. How does this affect the broader system, and what would you do about it?A 2-second GC pause in Service B has cascading effects. During the pause, B cannot process requests or send heartbeats. If B is behind a load balancer, the LB may keep sending requests to B, which pile up in B’s TCP receive buffer. When B resumes, it processes a burst of stale requests, some of which have already been timed out by their callers, wasting resources. Meanwhile, callers that timed out may have retried to another instance, creating duplicate processing. If B is a Raft leader, a 2-second pause can trigger a leader election (typical election timeout is 1-2 seconds). Mitigation strategies: first, tune the GC — switch to a low-pause collector like ZGC or Shenandoah, reduce heap size, or reduce allocation rate. Second, add health-check-based load balancing that removes B from the pool during pauses. Third, implement request deadlines: when B resumes, it should check if each queued request’s deadline has passed and discard expired ones rather than processing them.
What is the Phi Accrual failure detector and why is it better than a simple timeout-based detector?
Strong Answer:
A simple timeout detector uses a fixed threshold: if no heartbeat arrives within T milliseconds, the node is declared dead. The problem is choosing T. Too short and you get false positives (declaring a healthy but slow node as dead). Too long and genuine failures take too long to detect. In a system with variable network latency, no single T value is optimal.
The Phi Accrual detector (used by Cassandra and Akka) replaces the binary dead/alive decision with a continuous suspicion level. It maintains a sliding window of recent heartbeat inter-arrival times, computes their mean and standard deviation, and then calculates the probability that the node is still alive given how long it has been since the last heartbeat. The phi value is the negative log of this probability.
A phi of 1 means roughly a 10% chance the node is dead. A phi of 8 means effectively certain it is dead. The threshold is configurable: Cassandra defaults to phi=8.
The key advantage is adaptiveness. If the network becomes jittery and heartbeat intervals increase, the detector automatically adjusts its expectations. A fixed timeout would start producing false positives; the Phi detector would simply widen its confidence interval.
Follow-up: What are the failure modes of the Phi Accrual detector itself?The Phi detector has two main failure modes. First, the cold-start problem: when a node first joins the cluster, there is no heartbeat history to build a distribution from. During this window, the detector either uses a default conservative timeout or declares the node “unknown” until enough samples are collected. Second, the detector assumes heartbeat intervals follow a roughly normal distribution. If the actual distribution is bimodal (e.g., fast when not under GC, very slow during GC), the normal approximation underestimates the probability of long gaps, leading to premature suspicion during GC pauses. Cassandra mitigates this by using a fairly high phi threshold (8) and a large sample window (1000 intervals). A more sophisticated approach would use a non-parametric distribution or explicitly model the bimodal behavior.
Compare gRPC and REST for inter-service communication in a microservices architecture. When would you pick one over the other?
Strong Answer:
gRPC uses HTTP/2, binary serialization (Protocol Buffers), and provides strong typing with code generation. REST uses HTTP/1.1 (typically), text-based JSON, and relies on conventions (OpenAPI) for typing.
I would choose gRPC for internal service-to-service communication in a microservices backend. The reasons: binary serialization reduces payload size by 3-10x compared to JSON, HTTP/2 multiplexing eliminates head-of-line blocking and reduces connection overhead, and the generated client/server stubs eliminate an entire class of integration bugs (mismatched field names, wrong types). gRPC also natively supports streaming, which is critical for real-time data flows.
I would choose REST/JSON for external-facing APIs (public APIs, mobile clients, browser clients). JSON is human-readable, universally supported, and easy to debug with curl. gRPC in the browser requires grpc-web, which adds complexity. REST also has better caching semantics (HTTP caching headers, CDN support).
The hybrid approach most companies use: gRPC internally between microservices, REST/JSON externally at the API gateway. The gateway translates between the two.
Follow-up: What are the operational challenges of running gRPC at scale that you would not face with REST?The biggest operational challenge is debugging. gRPC traffic is binary, so you cannot simply tcpdump and read the payload like you can with JSON. You need gRPC-aware tooling (grpcurl, Envoy’s gRPC access logging, or middleware that logs deserialized payloads). Second, load balancing is more complex. HTTP/2 connections are long-lived and multiplexed, so traditional L4 (TCP) load balancers will pin all traffic from one client to one server. You need L7 load balancing that understands HTTP/2 frames and distributes individual RPCs across backends. Third, schema evolution requires discipline. If you delete a field in a proto definition without proper deprecation, you can silently break consumers. REST/JSON is more forgiving because unknown fields are typically ignored. These are solvable problems, but they require investment in tooling and process that REST does not demand.