Skip to content
POLARISAI ACADEMY
R03-101 · Navigator

Networking, DNS, HTTP and Traffic Control

You can follow a DNS chain until each TTL runs out, pick the first matching row in a reverse-proxy table, and mark a request allowed or answered with 429 from a token bucket and a sliding window you recompute by hand.

Level
Navigator
Time
22 h (estimate)
Prerequisites
F01–F07, R02-101
Course 1 of 4
Jump to

After this course you can

The student can configure reverse-proxy and rate-limiting middleware that serves normal traffic under 50ms while rejecting burst floods with compliant HTTP 429 and Retry-After headers and verifying HMAC webhook signatures.

DNS records and the TTL that is left

A DNS answer is a few lines you can read. An A record holds an IPv4 address. An AAAA record holds an IPv6 address. A CNAME record holds another name, and the lookup continues at that name. A TXT record holds a text string, such as an SPF policy, and a lookup for an address leaves that line unused. Take this zone. www.shop.example has a CNAME to shop.example with TTL 60. shop.example has an A record 203.0.113.10 with TTL 300, an AAAA record 2001:db8::10 with TTL 300, and a TXT record v=spf1 -all with TTL 300. A client that asks for the A of www.shop.example follows the CNAME and receives 203.0.113.10. A client that asks only for A receives no AAAA line in that answer.

TTL is a countdown in seconds, stored beside the record when the cache saves it. Both the CNAME and the A above are cached at 12:00:00. At 12:00:40 the elapsed time is 40 seconds. The CNAME still has 60 − 40 = 20 seconds, and the A still has 300 − 40 = 260 seconds, so both lines may be served from the cache and the address handed out is 203.0.113.10. At 12:01:30 the elapsed time is 90 seconds. The CNAME has 60 − 90, which is below zero, so that cache entry is due and the resolver asks again. The A record would still show 300 − 90 = 210 seconds, yet the chain needs a current CNAME before that address is used.

The HTTPS request after the name check

The client opens TCP to port 443 on 203.0.113.10 and runs TLS before any HTTP line. The certificate check compares the name the user typed with the names written on the certificate. The name shop.example matches a certificate that lists shop.example. The digits 203.0.113.10 identify the machine. They do not replace the name on the certificate. When the name matches and the certificate is inside its dates, the client sends HTTP. One request in this lesson uses method GET, path /v1/orders, version HTTP/1.1, Host api.shop.example, Accept-Encoding gzip, and Connection keep-alive.

Keep-alive reuses the TCP connection and the TLS session. Three GET requests sent in series on that one connection take one handshake, not three. If each response carries Connection: close, the same three requests take three handshakes. A 200 response means the body is the resource at that path. A 301 response carries a Location header, and the client issues a new request for that URL. A 404 response means this server has nothing at the path. When the body is gzip and Content-Length is 400, the wire carries 400 bytes. If the uncompressed size is 1000 bytes, the ratio of wire bytes to plain bytes is 400/1000 = 0.4.

A reverse proxy, first match, then the clock

The browser talks only to the proxy. The proxy keeps a table and uses the first row whose host and path both match. Row 1: host api.shop.example and path prefix /v1/ goes to 10.0.0.8 port 8080. Row 2: host api.shop.example and path prefix /hooks/ goes to 10.0.0.9 port 8080. Row 3: host shop.example and any path goes to 10.0.0.4 port 8080. Any other host and path gets 404 from the proxy, and the proxy opens no upstream socket. GET /v1/orders with Host api.shop.example uses row 1 and 10.0.0.8 port 8080. The same path with Host shop.example skips rows 1 and 2 because the host differs, then uses row 3 and 10.0.0.4 port 8080. GET /hooks/paid with Host api.shop.example uses row 2 and 10.0.0.9 port 8080.

Each row in this lesson uses a connect timeout of 500 milliseconds and a header timeout of 2000 milliseconds. The header clock starts when the upstream TCP connection is up. Add the proxy's own 6 milliseconds when you budget the client's wait. One successful call connects in 5 milliseconds, receives headers in 22 milliseconds, and spends 6 milliseconds in the proxy: 5 + 22 + 6 = 33 milliseconds, under a 50 millisecond budget. A second call connects in 80 milliseconds, which is under 500, then waits 2600 milliseconds for headers. 2600 is over 2000, so the proxy returns 504 to the client and forwards no body. The status this lesson uses for that cut-off is 504.

Token bucket, sliding window, and 429

A token bucket has a capacity and a refill rate. This lesson's bucket holds at most 4 tokens and refills 1 token per second, and the balance never rises above 4. On a request at time t the proxy sets tokens to the minimum of 4 and the old balance plus rate times the seconds since the last update, stores t as the last update, and then spends 1 token when at least 1 token is present. Five requests arrive at t = 0 against a full bucket. The first four are allowed and the balance goes 3, then 2, then 1, then 0. The fifth finds 0, stays at 0, and is refused. The wait for one token is (1 − 0) / 1 = 1 second. The lesson writes Retry-After as the smallest integer number of seconds that is at least that wait, so the header is Retry-After: 1 and the status is 429. A later request at t = 2 sees 0 + 1 × 2 = 2 tokens, spends one, and leaves 1.

A sliding window counts allowed requests whose times are strictly greater than t − W and at most t. Here W is 10 seconds and the limit is 5. The new request is allowed when that count is 4 or lower, because the new one then brings the total to 5 at most. Denied requests stay off the list. With allowed times 1, 2, 3, 4 and 5, a request at t = 6 looks at the window (−4, 6], finds all five, and is denied. A request at t = 11 looks at (1, 11], finds the times 2, 3, 4 and 5, four of them, and is allowed. Time 1 sits on the open edge t − 10 and stays outside that window. The response on a deny is again 429. The bucket can allow a burst of 4 at one instant. The window refuses a sixth stored hit inside the same 10 seconds.

An example

From the name to a 429, with the figures

Cache time is 10:00:00. api.shop.example is a CNAME with TTL 120 pointing at edge.shop.example. edge.shop.example is an A with TTL 300 pointing at 203.0.113.10. The check runs at 10:01:30, so 90 seconds have passed. The CNAME remaining is 120 − 90 = 30. The A remaining is 300 − 90 = 210. Both are above zero, so the client uses 203.0.113.10 and does not ask again yet. At 10:02:01 the elapsed time is 121 seconds. The CNAME remaining is 120 − 121, below zero, and the resolver asks again. The A line would still show 300 − 121 = 179 seconds, and that remainder waits unused until a fresh CNAME is in hand.

The request is GET /v1/orders with Host api.shop.example. Row 1 matches, so the upstream is 10.0.0.8 port 8080. Connect takes 5 milliseconds, headers take 22 milliseconds, and the proxy adds 6 milliseconds. The sum is 33 milliseconds. Connect is under 500 and headers are under 2000, so the proxy forwards the upstream status 200. A twin request with Host shop.example and the same path matches row 3 and goes to 10.0.0.4 port 8080. GET /hooks/paid with Host api.shop.example matches row 2 and goes to 10.0.0.9 port 8080.

The bucket on the proxy starts full, 4 tokens, with the last update at the same t = 0 as the burst. Five requests are decided in order at that instant. Allowed, balance 3. Allowed, balance 2. Allowed, balance 1. Allowed, balance 0. Denied, balance stays 0, status 429, Retry-After 1. The next request in this trace is at t = 2. Refill is 0 + 2 × 1 = 2, the request is allowed, and the balance left is 1. Six requests at that same t = 0 would allow four and refuse two, and each refusal would still carry Retry-After 1, because the balance is already 0 when the fifth and the sixth are judged.

A further call to 10.0.0.8 port 8080 connects in 80 milliseconds and then sits 2600 milliseconds with no header. 80 is under the 500 millisecond connect limit. 2600 is over the 2000 millisecond header limit. The proxy answers the client with 504 and forwards no body. On the sliding window, keep the separate list of allowed times 1, 2, 3, 4 and 5, with W = 10 and limit 5. The request at t = 6 is denied because five allowed times already sit in (−4, 6]. The request at t = 11 is allowed because four allowed times sit in (1, 11]. A gzip body with Content-Length 400 that inflates to 1000 bytes has wire ratio 0.4.

Exercise

Run the three clocks on paper

Use the zone from the first section, cached at 12:00:00. At 12:00:40, write the remaining TTL on the CNAME and on the A, and write the address you would hand out. At 12:01:30, write whether the resolver asks again, and write the A remaining that stays unused once the CNAME is due. Then route two requests. GET /hooks/paid with Host api.shop.example. GET /v1/orders with Host shop.example. Name the row and the upstream address and port for each. Then take the slow call: connect 80 milliseconds, headers 2600 milliseconds, limits 500 and 2000. Write the status. Then take a fresh bucket, capacity 4, rate 1 token per second, full, last update at t = 0. Six requests arrive at t = 0. Write how many are allowed, how many get 429, and the Retry-After integer on each refusal. Then one request arrives at t = 2, with no requests between. Write the balance after that decision. Finally use allowed times 1, 2, 3, 4, 5 with W = 10 and limit 5. Write allow or deny at t = 6 and at t = 11, and the count already in each window.

At 12:00:40 the CNAME has 20 seconds left and the A has 260 seconds left, and the address is 203.0.113.10. At 12:01:30 the resolver asks again, and the unused A remainder is 210 seconds. GET /hooks/paid with Host api.shop.example matches row 2 and goes to 10.0.0.9 port 8080. GET /v1/orders with Host shop.example matches row 3 and goes to 10.0.0.4 port 8080. The slow call returns 504. Of the six requests at t = 0, four are allowed and two get 429 with Retry-After 1. At t = 2 the refill reaches 2, the request is allowed, and the balance left is 1. The window at t = 6 already holds 5, so deny. The window at t = 11 holds 4, so allow.

Check questions

Answer on your own first. Then open a question to see the answer.

In the worked trace cached at 10:00:00, how many seconds of TTL remain on the CNAME at 10:01:30, and how many remain on the A?

30 seconds remain on the CNAME and 210 seconds remain on the A. Elapsed time is 90 seconds, from 120 − 90 and 300 − 90.

A request uses GET /v1/orders and Host shop.example. Which row matches, and which upstream address and port receive it?

Row 3 matches. The upstream is 10.0.0.4 port 8080. Rows 1 and 2 require Host api.shop.example.

A bucket of capacity 4 starts full. Five requests arrive at the same t = 0. How many are refused, what status do they get, and what Retry-After integer does this lesson send?

One request is refused. The status is 429 and Retry-After is 1. The first four spend the four tokens, and the wait for the next token is 1 second.

Allowed times are 1, 2, 3, 4 and 5. The window is 10 seconds and the limit is 5. What is the decision at t = 6, and what is the decision at t = 11?

Deny at t = 6, because the window (−4, 6] already holds 5. Allow at t = 11, because the window (1, 11] holds 4. Time 1 falls on the open edge and stays outside.

Take with you

You subtract the elapsed seconds from each TTL in the chain, send the request to the first proxy row that matches, and you let a token bucket or a sliding window decide between a forward and a 429 that carries Retry-After.

Final task

Submit a proxy/middleware service that passes 30 automated load, timeout, CORS, HMAC webhook verification, and token-bucket rate-limiting tests in the isolated container runner.

The final task is marked against fixed answer keys and automated tests once the tests open.

Terms in this course

22 terms in the glossary belong to this course.

More lessons coming

The first lesson is open to read. The rest of the course is being written, and tests and diplomas come later. Join the waitlist