Skip to content
Tech Atlas, home
Search
Esc
  • All roadmapsBrowse every learning path
  • ExploreFind a technology area
  • ProjectsGuided projects and builds
  • My AtlasYour learning dashboard
Lesson 5 of 6 · Worked example · 20 min · revision 1

Offload ratio, egress cost and edge compute

Work through a monthly estimate for a media site: request and byte hit ratios per content class, origin load and egress, a CDN bill from checked list prices, a range for the uncertain input, and a cost-aware decision about which logic to run at the edge.

Contentsand lesson path

What you will learn

  • Estimate CDN offload and egress cost, and decide which logic (redirects, auth checks, routing) to run at the edge.

Prerequisites: Caching Strategies

01. The problem

A magazine publishes articles, photos and short videos, and serves everything through a CDN. Its traffic grew, and the team must answer four questions before the budget meeting. How much of the traffic does the CDN really take off the origin? How much does the origin still send, and what does that cost? What will the CDN bill be? And which pieces of logic should move to the edge, given that edge code is billed per request?

All numbers in this lesson are assumptions for practice, not data from a real company. Prices are list prices checked on official pages in November 2026 where stated, and clearly labelled examples otherwise. Prices change, and contracts differ, so treat every price as an input to replace with your own.

02. Step 1: Write the assumptions per content class

A single hit ratio for the whole site hides the answer, because the content classes behave very differently. Split the traffic into classes and give each one three numbers for a month: requests, average response size, and the share of requests the CDN answers from cache. Two hit ratios come from the CDN's analytics; the other three are reasoned guesses. HTML is partly personalised and short-lived, the API serves public data with short lifetimes, fingerprinted JavaScript and CSS almost never miss, and the video catalogue has a long tail of old clips that are rarely watched, so many of them are not cached when someone asks.

Assumptions · one month (30 days), decimal units: 1 GB = 10^9 bytes, 1 TB = 1,000 GB
class         requests/month   avg response   request hit ratio   basis
HTML pages    150,000,000      45 KB          55%                 guess
public API    400,000,000      3 KB           80%                 guess
images        900,000,000      120 KB         97%                 analytics
JS and CSS    350,000,000      60 KB          99.5%               analytics
video         100,000,000      1.8 MB         75% (65% to 95%)    guess, widest

03. Step 2: Two hit ratios, by hand for one class

Two ratios answer two different questions. The request hit ratio is hits divided by all requests: it tells you how many requests the origin is spared, which drives its CPU, connections and request fees. The byte hit ratio is bytes served from cache divided by all bytes delivered: it tells you how much data the origin is spared, which drives origin egress (data sent out of a network, which providers bill per GB). Within one class we assume hits and misses have the same average size, so the two ratios are equal there; across classes they differ, because a miss on a video costs 600 times the bytes of a miss on an API call. Here is the video class by hand:

Video class · hand arithmetic
bytes delivered   100,000,000 x 1.8 MB          = 180,000,000 MB = 180 TB
missed requests   100,000,000 x (1 - 0.75)      = 25,000,000
origin bytes      25,000,000 x 1.8 MB           = 45 TB
origin egress     45,000 GB x $0.05/GB (example) = $2,250 a month

04. Step 3: All classes, the bill and the range

The script repeats the arithmetic for every class and adds prices. We assume readers are in North America and Europe. Delivery to them uses CloudFront's tiered list prices for the United States, Canada and Europe, which share one table in the AWS Price List published on 19 October 2026: $0.085 per GB for the first 10 TB of the month, falling step by step to $0.020 per GB above 5 PB. HTTPS requests cost $0.0100 per 10,000 in North America and $0.0120 in Europe; the script uses the North American price, which understates the bill by at most $380 a month (2%) if every reader is in Europe. Other regions cost more, and their higher tiers fall more slowly: for this lesson's 317 TB, the same list gives about $26,000 to $30,000 of delivery for India, Japan, Australia or South America, 55% to 77% more than here. Each tier price applies only to the gigabytes inside that tier. The price list writes tier limits in GB (10,240 GB for "10 TB"), and the script feeds them decimal gigabytes, which is close enough for planning. For origin egress there are two cases. CloudFront's documentation says data transfer from AWS origins such as S3 or a load balancer to CloudFront is free. An origin at another host pays that host's internet egress price, here an example $0.05 per GB, not a quote. It needs only Python 3.12.

Python · offload_cost.py
"""offload_cost.py: CDN offload, origin egress and a monthly bill from assumptions."""

GB = 10**9  # decimal units: 1 GB = 10^9 bytes, 1 TB = 1,000 GB

# One month of traffic per content class:
# (requests, average response bytes, share of requests answered from the CDN cache)
CLASSES = {
    "HTML pages": (150_000_000, 45_000, 0.55),
    "public API": (400_000_000, 3_000, 0.80),
    "images": (900_000_000, 120_000, 0.97),
    "JS and CSS": (350_000_000, 60_000, 0.995),
    "video": (100_000_000, 1_800_000, 0.75),
}

# Checked list prices: CloudFront, AWS Price List of 2026-10-19; the United States,
# Canada and Europe share this table (assumption: readers are there).
# (tier size in GB, USD per GB); the last tier has no upper limit.
DELIVERY_TIERS = [(10_240, 0.085), (40_960, 0.080), (102_400, 0.060),
                  (358_400, 0.040), (536_576, 0.030), (4_194_304, 0.025),
                  (float("inf"), 0.020)]
PER_10K_HTTPS = 0.0100  # USD per 10,000 HTTPS requests, North America ($0.0120 Europe)
ORIGIN_EGRESS = {"origin in the CDN's cloud": 0.0,  # AWS origin -> CloudFront
                 "origin at another host": 0.05}  # EXAMPLE price, not a quote


def delivery_cost(gb: float) -> float:
    cost, left = 0.0, gb
    for size, price in DELIVERY_TIERS:
        used = min(left, size)
        cost, left = cost + used * price, left - used
    return cost


def totals(classes: dict) -> dict:
    req = sum(r for r, _, _ in classes.values())
    hits = sum(r * h for r, _, h in classes.values())
    size = sum(r * b for r, b, _ in classes.values())
    miss_bytes = sum(r * b * (1 - h) for r, b, h in classes.values())
    return {"req": req, "req_hit": hits / req, "tb": size / GB / 1000,
            "byte_hit": 1 - miss_bytes / size, "origin_tb": miss_bytes / GB / 1000,
            "origin_req": req - hits}


t = totals(CLASSES)
print(f"requests {t['req'] / 1e9:.2f} billion, delivered {t['tb']:,.1f} TB")
print(f"request hit ratio {t['req_hit']:.1%}, byte hit ratio {t['byte_hit']:.1%}")
print(f"origin serves {t['origin_req'] / 1e6:,.0f} M requests, "
      f"{t['origin_tb']:,.1f} TB")
for name, (r, b, h) in CLASSES.items():
    share = r * b * (1 - h) / GB / 1000 / t["origin_tb"]
    print(f"  {name:<11} {r / t['req']:6.1%} of requests  {share:6.1%} of origin bytes")
cdn = delivery_cost(t["tb"] * 1000) + t["req"] / 10_000 * PER_10K_HTTPS
print(f"CDN bill: delivery + requests = ${cdn:,.0f}")
for where, price in ORIGIN_EGRESS.items():
    print(f"origin egress, {where}: ${t['origin_tb'] * 1000 * price:,.0f}")
print("video hit ratio -> byte hit ratio, origin TB, egress at $0.05/GB")
for video_hit in (0.65, 0.75, 0.85, 0.95):
    r, b, _ = CLASSES["video"]
    s = totals(CLASSES | {"video": (r, b, video_hit)})
    print(f"  {video_hit:.0%} -> {s['byte_hit']:.1%}, {s['origin_tb']:5.1f} TB, "
          f"${s['origin_tb'] * 1000 * 0.05:,.0f}")
Terminal
python3 offload_cost.py
Expected output
requests 1.90 billion, delivered 316.9 TB
request hit ratio 89.4%, byte hit ratio 83.7%
origin serves 201 M requests, 51.6 TB
  HTML pages    7.9% of requests    5.9% of origin bytes
  public API   21.1% of requests    0.5% of origin bytes
  images       47.4% of requests    6.3% of origin bytes
  JS and CSS   18.4% of requests    0.2% of origin bytes
  video         5.3% of requests   87.2% of origin bytes
CDN bill: delivery + requests = $18,725
origin egress, origin in the CDN's cloud: $0
origin egress, origin at another host: $2,581
video hit ratio -> byte hit ratio, origin TB, egress at $0.05/GB
  65% -> 78.0%,  69.6 TB, $3,481
  75% -> 83.7%,  51.6 TB, $2,581
  85% -> 89.4%,  33.6 TB, $1,681
  95% -> 95.1%,  15.6 TB, $781

Read the result line by line:

  • The ratios disagree. The CDN answers 89.4% of requests but only 83.7% of bytes. A dashboard that shows only the first number makes the origin look better protected than it is.
  • Video dominates origin bytes. It is 5.3% of requests and 87.2% of the bytes the origin sends. Better caching for images or scripts would hardly move origin egress; better caching for video would.
  • The origin still serves 201 million requests a month, about 78 per second on average. Peaks are several times higher, so size the origin for the peak, as in Estimating a photo-sharing service end to end.
  • The CDN bill is about $18,725, almost all of it delivery to users, which no cache hit ratio reduces. Origin egress is $0 or about $2,581 depending only on where the origin runs.
  • The range. Video hit ratio is the most uncertain input, so the script varies it. Taking 95%, 75% and 65% as best, expected and worst cases, origin egress is 15.6, 51.6 and 69.6 TB a month. A shield or upper tier for video (lesson 1) is the lever that moves this line, and measuring the real ratio is worth more than refining any other guess.

Some providers also sell flat-rate plans instead of per-GB pricing (CloudFront has them), which changes the comparison, so check which model applies before you compare providers.

05. Step 4: Which logic belongs at the edge

Edge compute means running your own small functions inside the CDN, at the location that receives the request. CloudFront offers CloudFront Functions (sub-millisecond JavaScript with no network access, intended for jobs such as cache key normalisation, header changes, redirects and token checks) and Lambda@Edge (longer functions that can call other services). Cloudflare offers Workers. A function can answer the request itself, change it before the cache lookup, or change the response.

Ask three questions about each candidate. Does it need only the request plus small configuration that changes rarely? Does it save a round trip to the origin, or improve the cache key, on the requests it runs for? Can it tolerate configuration that is slightly out of date? Edge data stores such as Workers KV are built for reads: Cloudflare documents that a change in Workers KV can take 60 seconds or more to be visible in other locations. The magazine's candidates sort out like this:

  • Good at the edge: about 2,000 redirects from old article URLs, which the edge can answer without calling the origin; turning the Accept header into avif, webp or jpeg before the image cache lookup; checking the signature and expiry of links to members-only videos (lesson 3); choosing a regional origin from the visitor's country.
  • Keep at the origin: posting a comment, updating a subscription or reading a member's billing history, which all need the authoritative database in one region. Running them at the edge only adds a hop between the edge and that database.

Cost depends on where you attach a function, because it is billed for every request it runs on, hits included. With list prices checked in November 2026 (CloudFront Functions: first 2 million invocations free, then $0.10 per million; Cloudflare Workers Standard: $5 a month including 10 million requests and 30 million CPU milliseconds, then $0.30 per million requests and $0.02 per million CPU milliseconds), compare attaching an A/B-test function to every request with attaching it to HTML only:

Hand arithmetic · one month, list prices checked November 2026
CloudFront Functions, every request   (1,900 M - 2 M) x $0.10 per M      ~ $190
CloudFront Functions, HTML only       (150 M - 2 M) x $0.10 per M        ~ $15
Workers, every request, 1 ms CPU      $5 + 1,890 M x $0.30 + 1,870 M x $0.02 per M
                                                                         ~ $609
Workers, HTML only, 1 ms CPU          $5 + 140 M x $0.30 + 120 M x $0.02 per M
                                                                         ~ $49

The function is only useful on HTML, yet attaching it to every path multiplies its cost by about 12 to 13, and it adds a little latency to every image and video request too. Scope edge code to the paths that need it.

06. Check your understanding

Practice check · 3 questions

Reading a CDN cost model

Use the model and prices from this lesson unless a question gives others.

Q1. The magazine moves its origin from another host into the same cloud as its CDN, where origin-to-CDN transfer is free. Traffic and hit ratios do not change. Which part of the monthly cost falls?
Q2. A team attaches an edge function that assigns A/B test groups to all paths, including images and video segments that never use the group. What is the main cost of that choice?
Q3. A price list charges $0.085 per GB for the first 10 TB of a month and $0.080 per GB for the next 40 TB. A site delivers 30 TB this month (1 TB = 1,000 GB). What is the delivery cost?
Practice only: not graded, not saved, not evidence.

07. Apply it

For the practice project, replace the classes in offload_cost.py with your own: the static site's HTML, its fingerprinted assets and the protected media file. Take hit ratios from your CDN's analytics after a few days of real traffic, and the prices from your provider's current price page or plan. Then list every piece of logic you considered for the edge, with the paths it should run on and its monthly cost at your request volume.

08. Review prompt

Without looking back, answer in your own words:

  1. What does the request hit ratio measure, what does the byte hit ratio measure, and which one drives origin egress?
  2. Why can a content class with few requests dominate origin bytes?
  3. Which parts of a CDN bill does a higher hit ratio reduce, and which does it not?
  4. Give two kinds of logic that suit the edge and two that do not, with the reason for each.

References

Your progress

Reading state

Sign in to save that you finished reading this lesson.

Sign in to mark reading complete

Prove this skill

6 questions · about 5 minutes · scored automatically. Marking a lesson as read never demonstrates a skill.
Take the skill check