Design a VM bandwidth rate limiter - 50 virtual machines share one 10 Gbps network card, each is promised a minimum speed, and idle bandwidth must be lent and taken back at once. The host has 1.2 microseconds per packet, so nothing on the packet path may ask a server. The staff-level signal is a hierarchical token bucket that lends per packet, delay for outgoing traffic but marks and drops for incoming, and a host that keeps enforcing when the control plane is gone.
Written by a Staff+ Engineer and an Engineering Manager
12+ years of experience in distributed systems and real-time messaging. Tech lead for large-scale chat applications serving billions of users. Over 12 years of industry experience with 6 years in engineering leadership, building and scaling tech infrastructure teams that deliver end-to-end large-scale distributed systems.
Finished this lesson?
Sign in to save your progress across devices.
Tip: use ← and → to move between lessons.
Was this lesson helpful?
Rehearse this exact question with the author
45-minute mock with a Staff+ engineer who interviews at your target company. Premium members save $20.
What to Design?
Could you answer these today? Tick what you already know.
This is not the rate limiter that counts API requests in Redis. The thing being limited here is bits per second on a network card, and the decision is made for every packet, on one machine.
The happy path is quick to draw: one token bucket per VM. The interview is decided by four questions about what that bucket cannot do. Each one is a deep dive later on this page, with three designs side by side: the one that sounds right, a better one, and the one that holds.
Swipe to see all columns
| The four questions, and the answer that sounds right | |||
|---|---|---|---|
| # | The interviewer asks | The answer that sounds right | What it misses |
| DD1 | A VM is guaranteed 100 Mbps and the other 49 VMs are idle. How fast may it send? | 100 Mbps: one token bucket per VM | 9.9 Gbps sit idle. Idle bandwidth must be lent, and the promise must still hold when everyone is busy |
| DD2 | That VM now uses the whole card. A neighbour wakes up. Who gives its 100 Mbps back, and how fast? | The controller sees the usage and lowers the limit | |
The same three habits answer all four:
Do not spend your questions on API details. Spend them on what the limiter promises, and where it runs.
Swipe to see all columns
| Scope Decisions | ||
|---|---|---|
| Question | Agreed answer | Why it matters |
| Is this an API rate limiter? | No. It limits bits per second per VM on the network card of one host. No Redis counters, no HTTP 429 | The decision is made per packet, inside the host |
| One host or the whole fleet? | Enforcement is per host. The fleet only needs each policy delivered to its host | The packet path never leaves the machine (DD2) |
| Outgoing traffic, incoming, or both? | Both, and not in the same way | |
Out of scope (say this out loud): rate limiting of API requests, traffic engineering across hosts, quality of service inside a VM, live migration, billing, and firewall, NAT or load balancing inside the limiter.
FR1 — Set a policy. An operator sets four numbers per VM: a guaranteed minimum, an optional cap, a weight, and a limit for incoming traffic. A policy that the host cannot honour is refused.
FR2 — Keep the guarantee. Every VM can send at its guaranteed speed, even when all 50 send at once. Together they never send more than the card can carry.
FR3 — Lend idle bandwidth. A VM may send faster than its guarantee with bandwidth that others are not using. The surplus is split by weight, and it goes back to its owner as soon as the owner sends. A limiter like this is called work-conserving: the card is never idle while someone has packets waiting.
FR4 — Limit incoming traffic. Each VM has a limit for what it receives, enforced on the host.
FR5 — Report usage. Per VM: speed in both directions, packets delayed, marked and dropped, and queue depth.
The key principle behind all five
Swipe to see all columns
| Five facts and the conclusions they do not supportRead each row as: the fact on the left does not, by itself, imply the claim in the middle. | ||
|---|---|---|
| Fact | Does not imply | Why the gap exists |
| A VM is above its guarantee | It must be slowed down | If the card has room, the extra is a loan, not a violation |
NFR1 — Fairness and isolation. No VM falls below its guarantee because of a neighbour. Surplus is split by weight.
NFR2 — A fast packet path. A decision takes far less than 1.2 microseconds, the work per packet does not grow with the number of VMs, and there is no remote call.
NFR3 — Fast reclaim. Lent bandwidth is back with its owner in under a second. This design needs about a millisecond.
NFR4 — Fast policy changes. A change reaches the host within 5 seconds.
NFR5 — Enforcement without the control plane. A host keeps its limits through a control-plane outage and through its own restart.
| Capacity Thought Experiment | |
|---|---|
| Parameter | Value |
| VMs on one host | 50 |
| Network card (NIC) | 10 Gbps, shared by all of them |
| Guaranteed per VM | 100 Mbps, so 5 Gbps are promised in total |
| Surplus when every VM uses its guarantee | 5 Gbps |
| One packet | 1,500 bytes, which is 12,000 bits |
| Packets at full speed | |
The number that identifies the real system is the gap between three times:
one packet 1.2 microseconds all the time the host has to decide
one network round trip ~500 microseconds about 400 packets have left meanwhile
one policy change ~6 seconds apart for the whole fleet
A decision that must be made 833,333 times a second cannot wait for an answer that takes 400 packets to arrive. And a policy that changes ten times a minute does not need to be fast. So the design is two systems: a data plane on every host that is fast, simple and asks nobody, and a control plane that is slow, exact and rarely used. The fleet size is an illustrative assumption.
Interviewer signal: candidates who draw a central limiter that every packet, or every connection, has to ask have missed the number. Candidates who say "the host decides per packet from its own memory, and the control plane only delivers policies" have found the system.
Three ideas shape everything:
The control plane stores what each VM is promised. It handles a few changes a minute and must be exact, so it gets a transactional database. The data plane is the code on the host that touches every packet. It handles 833,333 packets a second and keeps its state in memory. The two meet only when a policy changes.
A token bucket is a counter that fills at a fixed speed. A packet may leave when the counter holds enough tokens for its size. Give every VM one bucket that fills at its guarantee, and it can always send at that speed: this is its floor. Add one more bucket for the whole card. A VM whose own bucket is empty may take tokens from that shared bucket, if any are left. That is the loan. It is asked for again with every packet, so when the owner of the bandwidth starts sending, the loan shrinks by itself.
For outgoing traffic the host owns the queue in front of the card. It can hold a packet back for a moment. This is called shaping. For incoming traffic the sender has already put the packet on the wire. The host cannot un-send it. It can only deliver it, flag it, or throw it away. This is called policing.
The core recognition
This is a hierarchical token bucket on every host, fed by a small control plane. Hierarchical means: one bucket for the card at the top, one bucket per VM below it, and a VM borrows from the top when its own bucket is empty. The scheduler does not say yes or no to a packet. It says how soon.
| Design Decisions | |
|---|---|
| Design choice | Reason |
| Every packet is decided on the host, from memory | 1.2 microseconds leave no room for a remote call |
| One root bucket for the card, one class per VM with a rate and a ceiling | The rate is the promise. The ceiling limits the loan |
Two tables carry the control plane:
hostsandbandwidth_policy. The field to explain isapplied_version: a policy is stored when the database has it, and enforced when the host says so.
One row per physical machine.
Swipe to see all columns
| hosts | ||
|---|---|---|
| Field | Type | Comment |
host_id | STRING | Primary key, for example H-7 |
nic_capacity_bps | INT | The speed of the network card: 10 Gbps. The guarantees on this host may never add up to more |
One row per VM: what it is promised and how far it may go.
Swipe to see all columns
| bandwidth_policy | ||
|---|---|---|
| Field | Type | Comment |
host_id, vm_id | STRING | Unique together. One policy per VM |
min_guaranteed_bps | INT | The floor for outgoing traffic: 100 Mbps |
One row per VM every 10 seconds. Rows are only added, never changed.
Swipe to see all columns
| vm_usage | ||
|---|---|---|
| Field | Type | Comment |
time, host_id, vm_id | TIMESTAMP, STRING | Which VM, and which 10 seconds |
tx_bps | ||
Swipe to see all columns
| On the host, in memory only | ||
|---|---|---|
| State | What it holds | Why it is not stored |
| Root class | The tokens of the whole card, filling at 10 Gbps | It changes with every packet. A millisecond later the old value is worthless |
| One class per VM | Rate, ceiling, weight, its tokens, the time of the last refill, and its queue | Rebuilt from the policy after a restart. Tokens start full |
| One ingress bucket per VM | The limit, its tokens, the time of the last refill | |
Swipe to see all columns
| Storage | ||
|---|---|---|
| Store | Data | Consistency |
| PostgreSQL | hosts, bandwidth_policy | Strong, transactional. The source of truth for every promise |
| TimescaleDB | vm_usage: one row per VM every 10 s | Append only. A lost row is a gap in a chart, nothing more |
| Host memory | Tokens, queues, counters | Never stored. Rebuilt after a restart |
Swipe to see all columns
| What the operator calls | ||
|---|---|---|
| Endpoint | Request | Response |
PUT /v1/hosts/{host_id}/vms/{vm_id}/bandwidth-policy | Guaranteed minimum, optional cap, weight, ingress limit, and the version that was read | 200 with the new version and the capacity left on the host. 409 if the guarantees no longer fit, or the version is old |
GET /v1/hosts/{host_id}/vms/{vm_id}/bandwidth-policy | No body | The policy, its version and its applied_version |
Swipe to see all columns
| Between the Policy service and the Host agent: one stream | ||
|---|---|---|
| Message | Direction | Content |
SyncPolicies: connect | Host to control plane | The host id. The answer is the whole policy set of that host, with versions |
Policy changed | Control plane to host | One policy with its new version, the moment it is stored |
Applied | ||
A gRPC stream is one long-lived connection on which both sides can send messages at any time. The host opens it, so the control plane never has to find its way into a host. Policies go down, acknowledgements and usage go up.
There is no API on the packet path. A packet is not a request to anyone.
Present the design as four short stories, each in its own colour on one whiteboard: Phase 1 set a policy (black), Phase 2 send (blue), Phase 3 receive (green), Phase 4 report and reconnect (purple). Every story has six numbered steps. The number on an arrow is the same number in the animation and in the step table.
Swipe to see all columns
| The four phases | |||
|---|---|---|---|
| Phase | Colour | Story | What it leaves behind |
| Phase 1: Set a policy (FR1) | Black | An operator gives vm-a its policy | The policy row at version 1. A class and a bucket on the host. A copy in the local file |
| Phase 2: Send (FR2, FR3) | Blue | vm-a sends a backup on a quiet host. Then vm-b wakes up | Nothing stored. Tokens and queues live in memory |
How to read the picture
Each phase below is an interactive walkthrough of the same picture. It plays on its own. You can pause it, step with the arrow keys, and click any component to read its role.
Goal: accept only a promise the host can keep, and get it enforced on the host within a second.
/v1/hosts/H-7/vms/vm-a/bandwidth-policyOperator → Policy serviceNothing is written in this step.
Swipe to see all columns
| Phase 1 step by step (black arrows) | ||
|---|---|---|
| Step | What happens | Data written |
| 1 | The operator sends PUT /v1/hosts/H-7/vms/vm-a/bandwidth-policy: minimum 100 Mbps, no cap, weight 1, ingress limit 1 Gbps. | Nothing yet |
| 2 | The Policy service locks the row of H-7 and adds up its guarantees: 4.9 Gbps for 49 VMs. 4.9 plus 0.1 is 5.0, which fits in 10. In the same transaction it inserts the policy. | bandwidth_policy for vm-a: version 1, not applied yet |
Why it is built this way
applied_version shows when.A guarantee is a promise for the worst moment: all VMs sending at once. If the guarantees on a host add up to 10.1 Gbps on a 10 Gbps card, somebody is short exactly then. A refusal is one 409 and a VM placed on another host. A broken promise is an incident. Some providers do promise more than the card has, betting that not everybody is busy at once. If you do that, say so, and call the number a target, not a guarantee.
Goal: let a VM send at its guarantee without any wait, lend it what is idle, and take the loan back per packet.
sort the packet into its classvm-a → HTB schedulerNothing is written in this step.
Swipe to see all columns
| Phase 2 step by step (blue arrows) | ||
|---|---|---|
| Step | What happens | Data changed |
| 1 | vm-a sends a packet of 1,500 bytes. The scheduler sorts it into class vm-a. | Nothing |
| 2 | Class vm-a has tokens. The scheduler takes 1,500 and passes the packet to the card. No wait. | Class vm-a: 1,500 tokens less |
| 3 | vm-a wants more than 100 Mbps, and its own bucket is empty. The other 49 VMs are quiet, so the root still has tokens. The scheduler borrows 1,500 and sends. vm-a now runs at 10 Gbps: 0.1 its own, 9.9 borrowed. | |
Why it is built this way
TCP reads a lost packet as a sign of congestion. It sends the packet again and cuts its speed in half. A packet that waits a few microseconds arrives late, and TCP simply settles at the speed the scheduler allows. So a drop costs twice: the bytes travel again, and the connection crawls for a while. A queue has a cost too: packets in it are late. That is why it is capped.
Goal: keep a VM to its incoming limit without a queue, and make the sender slow down.
1,500 bytes for vm-cNetwork → NICNothing is written in this step.
Swipe to see all columns
| Phase 3 step by step (green arrows) | ||
|---|---|---|
| Step | What happens | Data changed |
| 1 | A packet for vm-c arrives at the card. It has already crossed the wire. Nothing can undo that. | Nothing |
| 2 | The card hands it to the Ingress policer, which looks up bucket vm-c: it fills at 1 Gbps and holds 1.25 MB. | Nothing |
| 3 | The bucket has tokens. The policer takes 1,500 and delivers the packet. | Bucket vm-c: 1,500 tokens less |
Why it is built this way
Not vm-c, and not the host. The TCP code in vm-c copies the mark into its next acknowledgement. The TCP code of the sender reads it and reduces its speed, exactly as it would after a loss, but nothing was lost. A sender that ignores marks keeps going until the bucket is empty. Then the drops do the job.
Goal: make usage visible, and keep the limits when the control plane cannot be reached.
read the counters of each VMHTB scheduler, Ingress policer → Host agentNothing is written in this step.
Swipe to see all columns
| Phase 4 step by step (purple arrows) | ||
|---|---|---|
| Step | What happens | Data written |
| 1 | Every 10 seconds the Host agent reads the counters of each VM from the scheduler and the policer: bytes, packets delayed, marked and dropped, queue depth. | Nothing |
| 2 | The agent sends 50 usage rows up the stream. | Nothing |
| 3 | The Policy service appends them to the Metrics DB. | vm_usage |
Why it is built this way
A token count changes 833,333 times a second, and a millisecond later the old value means nothing. Storing it would put a disk write on the packet path. After a restart every bucket simply starts full. The worst case is that a VM gets one extra burst of about 10 milliseconds. The only thing that must survive is the policy, and that is small and changes rarely.
The deep dives are the four questions from the top of the page. Each one compares three designs for the same scenario: a Bad one, which is usually the answer that sounds right; a Good one, which still leaves a gap; and a Great one, which holds. The answers start covered. Read the scenario, decide what you would build, then click each card.
Swipe to see all columns
| Deep Dive Map | |||
|---|---|---|---|
| Deep Dive | Topic | Scenario | NFR |
Scenario: At 02:00 only vm-a is busy. It sends a 450 GB backup, and the other 49 VMs are idle. At 09:00 all 50 VMs are busy. One of them, vm-d, was sold a guarantee of 1 Gbps, so the host now promises 5.9 Gbps and has 4.1 Gbps of surplus. How fast is vm-a at 02:00? What do vm-b and vm-d get at 09:00?
Three ways to build it. Decide what you would do first. The answers are covered: click a card to reveal it.
The idea. Every VM gets a token bucket that fills at its guaranteed speed. A VM can never send faster than that.
Where it breaks. At 02:00 vm-a is held at 100 Mbps while 9.9 Gbps of the card sit idle. Its backup takes 10 hours. On the full card it would take 6 minutes. Raising every bucket to 1 Gbps does not help: that is 50 Gbps of permission on a 10 Gbps card. At 09:00 the card overflows, packets are dropped at random, and vm-d gets far less than its 1 Gbps. A fixed bucket either wastes the card or breaks the promise.
The idea. No fixed limits. The scheduler serves the VMs that have packets in turn, each according to its weight. An idle VM takes no turn, so its share goes to the others.
What it fixes. The waste. At 02:00 vm-a is the only active VM and gets the whole card: 10 Gbps, 6 minutes.
What it still misses. A weight is a share, not a speed. At 09:00 every VM gets one fiftieth of the card, 200 Mbps. That is fine for vm-b. But vm-d was promised 1 Gbps and gets 200 Mbps. Give vm-d weight 10 and it gets 1.69 Gbps, but then its share of the idle bandwidth is ten times bigger too, its speed still moves whenever a neighbour starts or stops, and nothing can say "never more than 2 Gbps". One number cannot be the promise and the share of the surplus at once.
The key idea. Give every VM two numbers, a promise and a limit, and let one shared bucket lend whatever is not promised right now. This scheduler is called a Hierarchical Token Bucket, or HTB. Linux ships it.
How it works
The three designs side by side, in one picture:
Play the three options. Switch between them with the tabs at the top of the figure.
one token bucket per VMEgress schedulerNothing is written in this step.
Swipe to see all columns
| DD1 side by side | |||
|---|---|---|---|
| Bad: fixed buckets | Good: weighted shares | Great: HTB | |
| vm-a at 02:00, alone on the host | 100 Mbps. The backup takes 10 hours | 10 Gbps. 6 minutes | 10 Gbps. 6 minutes |
| vm-b at 09:00, promised 100 Mbps | 100 Mbps | 200 Mbps | 182 Mbps: 100 its own, 82 borrowed |
Check it against the scenario
| One busy VM at 02:00, fifty at 09:00 | |
|---|---|
| Situation | Outcome with the Great option |
| 02:00, vm-a is alone | 10 Gbps: 0.1 from its own bucket, 9.9 borrowed from the root |
| 09:00, vm-b | 100 Mbps guaranteed, plus 82 Mbps of the 4.1 Gbps surplus |
| 09:00, vm-d | 1 Gbps guaranteed, plus the same 82 Mbps |
| vm-a must never pass 2 Gbps | Set its ceiling to 2 Gbps. It borrows up to that and no further |
| A new VM asks for a guarantee of 5 Gbps | 5.9 plus 5 is 10.9, more than the card. Refused with 409 |
The trade-off to name. The promise is only as good as the admission check: HTB cannot give out 11 Gbps of guarantees on a 10 Gbps card. And every VM now has three numbers to choose and explain. In exchange, a floor and a loan stop fighting each other.
That does protect the floor, as long as someone keeps every weight in step with its guarantee and the admission check holds. But the weight then also decides who gets how much of the idle bandwidth, and you may want those to differ: a small VM that should get a large share of the surplus, or a large one that should get none. And there is still no ceiling. HTB keeps three questions apart: what is promised, how much may be borrowed, and in which proportion.
"A fixed token bucket per VM wastes the card: one busy VM is stuck at 100 megabits while 9.9 gigabits are idle. Weighted fair queueing uses the idle capacity, but a weight is a relative share, so it cannot express a guaranteed rate or a cap. I would use a hierarchical token bucket. The root class is the NIC at 10 gigabits. Each VM is a child class with a rate, which is its guarantee, and a ceiling, which is its cap. A VM sends from its own tokens first. When they run out it borrows from the root, in proportion to its weight, up to its ceiling. Admission control keeps the sum of the rates at or below the NIC, so the guarantees always fit."
Follow-up: "Why not build this yourself?" — Linux traffic control already has HTB with exactly these semantics. The work is in the control plane around it: admission, delivery of policies, and what happens when that delivery fails.
Scenario: It is 02:10. vm-a is alone and uses the whole card: 10 Gbps, 9.9 of them borrowed. Now vm-b wakes up and needs its 100 Mbps at once. At 09:00 the other 48 VMs wake up within a second. Who takes the bandwidth back from vm-a, and how long do the others wait?
Three ways to build it. Decide what you would do first. The answers are covered: click a card to reveal it.
The idea. Every VM has a limit. The Policy service watches the usage reports, sees who is idle, and pushes new limits: vm-a may use 10 Gbps while the others sleep.
Where it breaks. When vm-b wakes up, 10.1 Gbps are offered to a 10 Gbps card. The packets of both VMs pile up and some are dropped. That lasts until the next usage report, up to 10 seconds, plus the push. At 09:00, 14.8 Gbps are offered and one packet in three is dropped. And when the control plane is down, nobody rebalances at all. A loop of seconds is steering packets that take 1.2 microseconds.
The idea. Move the loop onto the host. Every 100 milliseconds the Host agent reads the counters and rewrites the limits: vm-a gets 10 Gbps minus what the others used.
What it fixes. The control plane is out of the loop. An outage no longer matters, and the reaction is a hundred times faster.
What it still misses. It is still a loop that runs behind the packets. When vm-b wakes up, the limits are old for up to 100 milliseconds: about 800 of its packets share a full queue with vm-a's. When 48 VMs wake up inside one tick, 14.8 Gbps are offered until the next one. With 50 VMs changing speed all the time, the limits are always one tick old. A shorter tick costs a timer and 50 rewrites each time.
The key idea. A loan is not a setting that somebody has to change back. It is a question the scheduler asks again for every packet: are there tokens left over right now?
How it works
The three designs side by side, in one picture:
Play the three options. Switch between them with the tabs at the top of the figure.
set limitsPolicy service → Host agent → Egress schedulervm-aupdateSwipe to see all columns
| DD2 side by side | |||
|---|---|---|---|
| Bad: control plane loop | Good: agent timer | Great: per packet | |
| Who takes the bandwidth back | The Policy service | The Host agent | Nobody. The next packet finds less to borrow |
| vm-b waits for its 100 Mbps | Up to 10 seconds | Up to 100 milliseconds | It does not. Its first packet leaves at once |
Check it against the scenario
| vm-b wakes up while vm-a uses the whole card | |
|---|---|
| Situation | Outcome with the Great option |
| vm-b sends its first packet after hours of silence | Its own bucket is full. The packet leaves at once |
| vm-a at that moment | Its next packets wait a few microseconds in its queue. It drops from 10 to 9.9 Gbps |
| 09:00, 48 more VMs wake up | Each takes its own 100 Mbps the same way. vm-a ends at 5.1 Gbps: 0.1 its own, 5.0 borrowed |
| The Policy service is down | Nothing changes. This path has no remote call |
| vm-b goes quiet again | The root has tokens left over. vm-a borrows them from its next packet on |
The trade-off to name. All of this must fit in 1.2 microseconds on a path that 50 VMs share. That is why the tree has two levels, why refill is computed and not timed, and why the scheduler keeps no state that needs a disk. It is fast because it is small.
In one millisecond the card sends 833 packets. vm-b at 100 Mbps needs 8 of them. vm-b has tokens of its own, so its packets go first, and vm-a gets the other 825. After that millisecond the split is already 9.9 to 0.1. Nothing was measured and no limit was rewritten. The only state that changed is a few token counters.
"Reclaim is not an event in this design. Every class sends from its own bucket first, and those packets have priority. The root bucket is charged for every packet that leaves the NIC, so its remaining tokens are exactly the idle capacity. A class above its rate has to borrow from the root for each packet. When the owner of the bandwidth starts sending, the root has less to lend and the borrower's packets wait in its queue. That converges within about a millisecond, with no control-plane call, no timer and no drop. Tokens are refilled lazily from the elapsed time when a packet arrives, so the fast path stays constant work per packet."
Follow-up: "Why only two levels in the tree?" — Each level is one more bucket to check per packet, and the budget is 1.2 microseconds. Per-team or per-application levels are better handled by the control plane, which can translate them into rates and weights for one flat level.
Scenario: A client on the network sends to vm-c at 2 Gbps. vm-c may receive 1 Gbps. By the time the host sees a packet, it has already crossed the wire. What do you do with the extra 1 Gbps?
Three ways to build it. Decide what you would do first. The answers are covered: click a card to reveal it.
The idea. Treat both directions the same. Put incoming packets for vm-c in a queue and release them at 1 Gbps.
Where it breaks. The queue grows by 125 MB every second. A 64 MB buffer is full after half a second. From then on every packet waits half a second, and new packets are dropped anyway. For 50 VMs the buffers take 3.2 GB of host memory. And nothing was saved: those packets had already used the wire and the card. A queue helps in front of the bottleneck. This one sits behind it.
The idea. Give every VM a token bucket for incoming traffic, with no room for bursts. A packet that finds tokens is delivered. A packet that finds none is dropped. There is no queue.
What it fixes. Memory and delay. The policer keeps a counter per VM and never holds a packet. The sender's TCP notices the loss and slows down.
What it still misses. A drop is the only signal, and a blunt one. The sender halves its speed, climbs back, and loses packets again, and every lost packet travels twice. A short request that loses its last packet can wait 200 milliseconds for a timeout. And a normal burst of 100 packets in a row overflows the bucket, although vm-c is far below 1 Gbps on average.
The key idea. Warn the sender while there is still room, and drop only when the warning did not work. Never queue.
How it works
The three designs side by side, in one picture:
Play the three options. Switch between them with the tabs at the top of the figure.
traffic for vm-cSender → NICNothing is written in this step.
Swipe to see all columns
| DD3 side by side | |||
|---|---|---|---|
| Bad: queue | Good: drop | Great: mark, then drop | |
| Extra delay for vm-c | Up to half a second | None | None |
| Host memory | 64 MB per VM, 3.2 GB in total | A counter per VM | A counter per VM |
Check it against the scenario
| 2 Gbps arrive for vm-c | |
|---|---|
| Situation | Outcome with the Great option |
| The sender's TCP understands ECN | It settles near 1 Gbps without losing a packet |
| A new connection sends 100 packets in a row, 150 KB | They fit in the 1.25 MB bucket. Nothing is marked |
| A flood that ignores marks, at 2 Gbps | 1 Gbps is delivered, 1 Gbps is dropped. No queue grows |
| Memory on the host | Two counters per VM |
| How exact is the limit? | Packets are counted after they arrive, so a burst can pass before the bucket reacts. Incoming limits are always less exact than outgoing ones |
The trade-off to name. The host only reacts. The extra 1 Gbps still crossed the wire and still used the card before it was dropped, so a flood hurts the neighbours a little even when the policer works. The real fix for traffic from inside the same cloud is one hop earlier, on the host of the sender.
Yes: its own host. There the traffic is outgoing, so the scheduler of that host can hold it to a speed before it ever reaches the wire. Outgoing shaping prevents, incoming policing only reacts. A cloud provider uses both: shaping where it owns the sender, policing as the last line for everything else.
"Egress and ingress are not symmetric, because the host only owns one of the two queues. Outgoing packets sit in my queue before the NIC, so I can delay them: that is shaping. An incoming packet has already consumed the link when I first see it, so delaying it saves nothing and costs memory and latency. I can only deliver it, mark it or drop it: that is policing, done in the virtual switch with a token bucket per VM. I give the bucket about ten milliseconds of burst room, set the ECN mark when it runs low so that TCP senders back off without loss, and drop only when it is empty."
Follow-up: "What if the sender does not react to marks or drops?" — Then the policer still caps what the VM receives, but the link stays busy. That traffic has to be stopped upstream: at the sender's host if it is ours, or at the edge of the network.
Scenario: From 03:00 to 03:20 host H-7 cannot reach the control plane. At 03:05 the Host agent restarts for an upgrade and rebuilds the scheduler from what it knows. At 03:10 an operator caps vm-a at 2 Gbps: the Policy service stores version 2 but cannot reach H-7. At 03:20 the link is back. What does H-7 enforce at 03:01, at 03:06 and at 03:21?
Three ways to build it. Decide what you would do first. The answers are covered: click a card to reveal it.
The idea. Every 30 seconds the Host agent asks the Policy service for the policies of its host. It keeps the answer in memory.
Where it breaks. A change waits up to 30 seconds, and the requirement says 5. With 10,000 hosts that is 333 requests a second to learn about 10 changes a minute. At 03:00 the polls start to fail, and the agent keeps what it has. At 03:05 it restarts: its memory is empty and nobody answers. H-7 now runs without limits, or blocks all traffic. It is slow when it works and empty after a restart.
The idea. One long-lived stream per host. When it connects, the host gets its whole policy set. After that, each change is pushed the moment it is stored.
What it fixes. Speed and waste. A change arrives in under a second, and an idle stream costs almost nothing.
What it still misses. The restart. At 03:05 the agent comes back with empty memory and nobody to ask. Until 03:20 H-7 has no limits: vm-a takes the card and vm-b loses its floor. Or it blocks all 50 VMs. Fifteen minutes without limits, because one process restarted at the wrong time.
The key idea. The host can always enforce the last thing it was told. The control plane is needed for changes, never for enforcement.
How it works
applied_version.The three designs side by side, in one picture:
Play the three options. Switch between them with the tabs at the top of the figure.
the policies of H-7Host agent → Policy service, every 30 sNothing is written in this step.
Swipe to see all columns
| DD4 side by side | |||
|---|---|---|---|
| Bad: poll, memory | Good: push, memory | Great: push, local copy, full sync | |
| A change reaches the host | Within 30 seconds | Within a second | Within a second |
| Load on the control plane | 333 requests a second for nothing | One idle stream per host | One idle stream per host |
Check it against the scenario
| 20 minutes without the control plane | |
|---|---|
| Situation | Outcome with the Great option |
| 03:01, the link is down | Version 1 is enforced. The host is marked stale |
| 03:06, the agent has restarted | Version 1 again, rebuilt from the local file in about a second |
| 03:10, the operator caps vm-a at 2 Gbps | Stored as version 2. applied_version is still 1, so the API shows that H-7 is behind |
| 03:21, the link is back | A full sync. Version 2 is enforced within a second and acknowledged |
The trade-off to name. During an outage a host enforces a policy that may be out of date, and nobody can change it. That is the right way to fail: a slightly old limit is far better than none. The cost is honesty about it: the stale flag and applied_version exist so that an operator can see which hosts are behind.
To send only what was missed, the Policy service would have to remember what each of 10,000 hosts has seen, and be right about it after its own restarts. The whole set of one host is 50 small rows. Sending all of them takes a few kilobytes and cannot be wrong. Use the cheap method that is always correct.
"The control plane is not on the enforcement path. Policies are pushed to each host agent over a long-lived gRPC stream, so a change lands in under a second, and the agent acknowledges the version it applied. The agent writes every applied policy set to local disk. If the stream breaks, the scheduler keeps shaping with what it has, and the host flags itself as stale. If the agent restarts during the outage, it rebuilds the classes from the local copy. On every reconnect it receives the full policy set for its host, so a push that was lost cannot stay lost. Only policy changes wait for the control plane. Enforcement never does."
Follow-up: "During the rebuild after a restart, do you block traffic or let it through?" — Let it through. The window is about a second, and blocking would take 50 VMs offline to prevent one second of unshaped traffic.
| If the interviewer keeps going | |
|---|---|
| Question | Short answer |
| Two operators add a VM to the same host at the same moment? | The check and the insert run in one transaction that locks the row of the host. The second request sees the first one's guarantee and gets 409 if it no longer fits. No overcommit by default |
| Two operators edit the same policy? | Each write names the version it read. The second one names an old version, gets 409, reads again and retries. No lock is held across the API call |
| With SR-IOV the VM talks to the card directly. Now what? | Its packets skip the virtual switch, so the scheduler never sees them. Three answers: cap each virtual function in the card's hardware, which gives a ceiling but no lending; keep VMs with a guarantee on the virtual switch; or sell both kinds and count the direct ones from the card's counters |
| How deep is a token bucket? | About 10 milliseconds of traffic at its speed. Deeper lets a long burst pass the limit. Shallower drops a normal burst although the average is fine |
Redis counters and HTTP 429 answer a different question. This limiter works on packets, inside one host, in microseconds.
A round trip takes as long as 400 packets. Every per-packet decision uses memory on the host and nothing else.
It holds a VM at 100 Mbps while 9.9 Gbps sit idle. The limiter must lend what nobody is using.
A weight is a share that moves with the neighbours. A guarantee is a speed, and it needs a bucket that fills at that speed.
Seconds, or even 100 milliseconds, are too slow for packets. Let the borrower ask again for every packet, and reclaim happens by itself.
The packets have already used the wire. Mark first, drop last, and never hold them.
Two requests see the same free capacity and both succeed. Lock the host row, add up, and insert in one transaction.
A restart during an outage leaves the host with no limits. Keep the last applied set on local disk and sync fully on every connect.
The shape of a strong answer: split the system into a control plane that stores promises and a data plane that keeps them. Decide every packet on the host. Give each VM a rate and a ceiling under one root, pay from its own tokens first and borrow the rest per packet. Delay outgoing packets, mark and drop incoming ones. Refuse a promise that does not fit. Push policies, acknowledge them, and keep the last good set on the host.
Decide every packet on the host
The host has 1.2 microseconds per packet. The scheduler and the policer work from memory, and the control plane is only needed when a policy changes.
Promise a floor, lend the rest
A VM always has its own tokens. Above them it borrows from the root, one packet at a time, so the loan ends the moment the owner sends.
Control what you own
Outgoing packets wait in a queue the host owns. Incoming packets have already arrived, so they are marked or dropped. And the host owns a copy of its policy.
| What the interviewer expects at each level | |
|---|---|
| Level | A strong answer shows |
| Junior | Scopes it as traffic shaping on one host, not API rate limiting. Names the control plane, the host agent, the scheduler and the virtual switch. Explains a token bucket, and knows that the two directions differ |
| Mid-level | Gets to a hierarchical token bucket quickly: guarantees plus lending. Explains shaping against policing and the admission check. Splits control plane from data plane without being asked, and pushes policies instead of polling |
| Senior | Explains how borrowing and reclaim work per packet, why refill is lazy and the tree has two levels. Raises SR-IOV and the control-plane outage unprompted, with the last known good policy. Defends each store by how it is read and written |
These aren't toy problems — they reflect real infrastructure challenges at companies that run thousands of shared hosts. If you want to walk through this design in a mock interview or get feedback on your own approach, book a session with us. We're here to help you sharpen your thinking, build your confidence, and land your next offer.
Pick an answer for each question, and you see at once whether it is right. Show explanation opens the answer, why it is right, and why each of the other options fails.
Question 1 of 10
vm-a is above its guaranteed 100 Mbps, and the card has idle capacity. What does the scheduler do with its next packet?
Question 2 of 10
Someone suggests one fixed bucket per VM, raised to 1 Gbps so that VMs can go faster. What is wrong with it on this host?
Question 3 of 10
Weighted fair queueing lends idle bandwidth. What can it not express?
Question 4 of 10
vm-a has borrowed 9.9 Gbps. vm-b wakes up and sends at its guaranteed 100 Mbps. How does vm-b get its bandwidth?
Question 5 of 10
A 10 Gbps card sends packets of 1,500 bytes. How much time does the host have per packet?
Question 6 of 10
Why are incoming packets marked or dropped, and not delayed like outgoing ones?
Question 7 of 10
The ingress bucket of vm-c is below half full. The policer delivers the packet with an ECN mark. What is the mark for?
Question 8 of 10
The Host agent loses its stream to the control plane. What does the host do?
Question 9 of 10
A 10 Gbps card, 50 VMs, each guaranteed 100 Mbps. How much is left to lend when every VM uses exactly its guarantee?
Question 10 of 10
The Host agent restarts. What has to survive the restart?
| A packet takes 1.2 microseconds and a control loop takes seconds. The card is overloaded in between |
| DD3 | A VM receives 2 Gbps and may take 1 Gbps. What happens to the rest? | Queue it, like outgoing traffic | Those packets have already used the wire. A queue behind the bottleneck only adds delay and memory |
| DD4 | The host cannot reach the control plane for 20 minutes. What does it enforce? | The host asks the control plane for its limits | No answer means no limits, or no traffic. The host must be able to enforce by itself |
| Outgoing packets can wait in a queue. Incoming packets have already arrived (DD3) |
| May a VM go above its guarantee? | Yes, with bandwidth nobody else is using. It gives it back when the owner returns | Fixed limits are not enough. The scheduler must lend and reclaim (DD1, DD2) |
| How fast must a policy change arrive? | Within 5 seconds | Push over an open connection, do not poll (DD4) |
| What if the control plane is down? | Hosts keep enforcing the last policy they received | Every host keeps its own copy (DD4) |
| The card has room right now | The loan can stay | The owner may send its next packet a microsecond from now |
| A packet is over the limit | It should wait in a queue | Only an outgoing packet can wait usefully. An incoming one has already used the wire |
| The policy is stored | The host enforces it | The host enforces what it has applied. That is why it reports the version back |
| The control plane is silent | The limits are off | The limits live on the host. Only changes need the control plane |
| 10 Gbps divided by 12,000 bits: 833,333 a second |
| Time per packet | 1.2 microseconds |
| Policy changes | At most 10 a minute, in the whole fleet |
| Usage reports | One row per VM every 10 seconds. With 500,000 VMs: 50,000 rows a second, 4.3 billion a day |
| Own tokens first, then borrow from the root | A VM inside its guarantee never waits behind a borrower |
| Refill is computed when a packet arrives | No timer per VM, and the same small work for every packet |
| Outgoing: a queue per VM, at most 1,000 packets | A short wait keeps TCP calm. A drop makes it send again |
| Incoming: a token bucket that marks first and drops last | The packet has already arrived. Only the sender can slow down |
| The guarantees on a host never add up to more than the card | A promise that cannot be kept is refused at the door |
| Policies in PostgreSQL, usage in TimescaleDB, tokens in memory | Small and exact, huge and append-only, too fast to store |
| Policies are pushed over an open stream and acknowledged by version | A change arrives in under a second, and the operator sees what is applied |
| Every host keeps a copy of its last policy set | It can enforce without the control plane |
max_cap_bps | INT, optional | The most the VM may send, loans included. Empty means: up to the whole card |
weight | INT | Its share when several VMs borrow at once. Default 1 |
ingress_limit_bps | INT | The limit for incoming traffic: 1 Gbps |
version | INT | Raised by every change. A write that names an old version is refused |
applied_version | INT | The last version the host confirmed. Lower than version means the host is behind (DD4) |
rx_bpsINT |
| Speed sent and received |
delayed, marked, dropped | INT | Packets that waited in the queue, were flagged, or were thrown away |
queue_depth | INT | Packets waiting in the outgoing queue of this VM |
| The same |
| Local policy file | The last policy set the agent applied, with versions | This one is on disk, on the host. It is the copy the host falls back on (DD4) |
| Host disk |
| The last applied policy set |
| A copy. Replaced by the next full sync |
DELETE /v1/hosts/{host_id}/vms/{vm_id}/bandwidth-policy | No body | 204. The guarantee goes back into the budget of the host |
GET /v1/hosts/{host_id}/bandwidth-usage | A time range | Usage per VM, read from TimescaleDB |
| Host to control plane |
| The version the host now enforces |
Usage | Host to control plane | 50 rows every 10 seconds |
| Phase 3: Receive (FR4) |
| Green |
| A client sends to vm-c at 2 Gbps. vm-c may take 1 Gbps |
| Two counters: marked and dropped |
| Phase 4: Report and reconnect (FR5, NFR5) | Purple | Usage goes up the stream. Then the link to the control plane breaks | Usage rows. The host in sync again |
| 3 |
| The operator gets 200: version 1, and 5.0 Gbps of capacity left on H-7. Had it not fitted, the answer would be 409. |
| Nothing |
| 4 | The Policy service pushes the policy to the Host agent of H-7, on the stream that is already open. | Nothing |
| 5 | The Host agent creates class vm-a in the scheduler: rate 100 Mbps, ceiling 10 Gbps, weight 1. It creates bucket vm-a in the policer: 1 Gbps. It saves the policy set to its local file. | Class vm-a, bucket vm-a, local file at version 1 |
| 6 | The Host agent reports back: applied version 1. | bandwidth_policy for vm-a: applied_version 1 |
| Root: 1,500 tokens less |
| 4 | vm-b wakes up and sends. It is inside its guarantee, so its packet uses its own tokens and leaves at once. | Class vm-b: 1,500 tokens less |
| 5 | vm-a asks the root again. This instant nothing is spare, so its packet waits in the queue of vm-a. It is delayed, not dropped. | Queue vm-a: 0 to 1 packet |
| 6 | A few microseconds later the root has tokens again, and the packet leaves. vm-a is at 9.9 Gbps, vm-b at 0.1 Gbps. | Queue vm-a: 1 to 0 |
| 4 | The client keeps sending at 2 Gbps. After 5 milliseconds the bucket is below half. Packets are still delivered, but with a mark that says: slow down. | vm-c: marked + 1 |
| 5 | After 10 milliseconds the bucket is empty. The policer drops the packet. There is no queue. | vm-c: dropped + 1 |
| 6 | vm-c tells the sender about the marks, and the sender also notices the loss. It falls back to 1 Gbps. Every packet finds tokens again. | Nothing |
| 4 | The operator sends GET /v1/hosts/H-7/bandwidth-usage and sees: vm-a sent 9.9 Gbps, nothing dropped. | Nothing |
| 5 | The link between H-7 and the control plane breaks. The scheduler and the policer keep working with the classes they have. The agent marks the host as stale and keeps its usage rows for later. | Nothing |
| 6 | The link is back. The agent connects and asks for a sync. The Policy service sends the whole policy set of H-7. The agent applies what differs, saves its file and reports the versions. | Local file updated. applied_version set |
| DD1 |
| Lend idle bandwidth, keep every promise |
| One busy VM at 02:00, fifty at 09:00 |
| NFR1 — Fairness and isolation |
| DD2 | Take lent bandwidth back | A neighbour wakes up while one VM uses the whole card | NFR2, NFR3 — Packet path, reclaim |
| DD3 | Limit traffic you did not send | 2 Gbps arrive for a VM that may take 1 Gbps | NFR1 — Isolation |
| DD4 | The control plane is gone, the limits stay | 20 minutes without the control plane, and an agent restart | NFR4, NFR5 — Policy changes, outage |
| vm-d at 09:00, promised 1 Gbps |
| 1 Gbps |
| 200 Mbps |
| 1.08 Gbps: 1 Gbps its own, 82 Mbps borrowed |
| Idle bandwidth | Wasted | Lent | Lent, split by weight |
| A cap of 2 Gbps for vm-a | Only as its one fixed limit | Cannot be expressed | Its ceiling |
| 48 VMs wake up at 09:00 |
| One packet in three dropped, for seconds |
| The same, until the next tick |
| Nothing dropped. vm-a slides to 5.1 Gbps |
| The control plane is down | No rebalancing at all | Works | Works |
| Work per packet | One bucket | One bucket | One bucket, sometimes two |
| Late, when the buffer is full |
| By losing packets |
| By marks first, by loss only if it ignores them |
| A normal burst of 100 packets | Queued | Mostly dropped | Delivered |
| vm-c receives more than 1 Gbps | No | No | No |
| 03:01, the link is down |
| Version 1 enforced |
| Version 1 enforced |
| Version 1 enforced |
| 03:06, after the agent restart | No limits, or no traffic | No limits, or no traffic | Version 1, rebuilt from the file |
| 03:21, the link is back | Version 2, within 30 seconds | Version 2 | Version 2 |
| The same. Only new changes have to wait |
| Why not a level per team or per application in the tree? | Every level is another bucket to check for every packet, inside 1.2 microseconds. Keep the tree at two levels and let the control plane turn teams into rates and weights |
| Why two databases? | Policies are few, must be exact and need a transaction. Usage is 4.3 billion rows a day that are only added. Mixing them makes both worse |
| A VM is deleted? | DELETE its policy. The class and the bucket are removed on the host, and its guarantee goes back into the budget of the host |