{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# S4 lab notebook \u2014 hit rate, delivery cost and the edge\n",
    "\n",
    "The Cloudflare lab produces a small request table: one row per request, with the route (`asset` or `api`), its `cf-cache-status`, the bytes, and the edge and origin times. This notebook turns that table into the numbers the evidence bundle needs: the hit rate, the origin offload, a three-row delivery comparison, the TTL-floor decision and the Worker request headroom.\n",
    "\n",
    "The bundled `data/cdn_sample.csv` is a synthetic worked sample. Replace it with your own measurements from steps 7\u20139 of the lab. Every price here is illustrative and labelled as such; re-check provider prices before class.\n",
    "\n",
    "**You submit:** `cdn_results.json`, the cache-status evidence, the purge and versioned-key checks, the capability status codes, a 150\u2013250 word *what this proves* note, and the teardown log.\n",
    "\n",
    "<details class=\"th\" lang=\"th\">\n",
    "<summary>\u0e2a\u0e23\u0e38\u0e1b\u0e20\u0e32\u0e29\u0e32\u0e44\u0e17\u0e22</summary>\n",
    "\n",
    "\u0e41\u0e25\u0e47\u0e1a Cloudflare \u0e2a\u0e23\u0e49\u0e32\u0e07\u0e15\u0e32\u0e23\u0e32\u0e07\u0e04\u0e33\u0e02\u0e2d\u0e2b\u0e19\u0e36\u0e48\u0e07\u0e41\u0e16\u0e27\u0e15\u0e48\u0e2d\u0e2b\u0e19\u0e36\u0e48\u0e07\u0e04\u0e33\u0e02\u0e2d \u0e42\u0e14\u0e22\u0e21\u0e35\u0e04\u0e2d\u0e25\u0e31\u0e21\u0e19\u0e4c route (asset \u0e2b\u0e23\u0e37\u0e2d api) \u0e2a\u0e16\u0e32\u0e19\u0e30 cf-cache-status \u0e08\u0e33\u0e19\u0e27\u0e19\u0e44\u0e1a\u0e15\u0e4c \u0e41\u0e25\u0e30\u0e40\u0e27\u0e25\u0e32\u0e17\u0e35\u0e48\u0e40\u0e2d\u0e14\u0e08\u0e4c\u0e01\u0e31\u0e1a\u0e15\u0e49\u0e19\u0e17\u0e32\u0e07 \u0e42\u0e19\u0e49\u0e15\u0e1a\u0e38\u0e4a\u0e01\u0e19\u0e35\u0e49\u0e04\u0e33\u0e19\u0e27\u0e13\u0e2d\u0e31\u0e15\u0e23\u0e32\u0e41\u0e04\u0e0a\u0e16\u0e39\u0e01 \u0e01\u0e32\u0e23\u0e25\u0e14\u0e20\u0e32\u0e23\u0e30\u0e15\u0e49\u0e19\u0e17\u0e32\u0e07 \u0e15\u0e32\u0e23\u0e32\u0e07\u0e40\u0e1b\u0e23\u0e35\u0e22\u0e1a\u0e40\u0e17\u0e35\u0e22\u0e1a\u0e04\u0e48\u0e32\u0e43\u0e0a\u0e49\u0e08\u0e48\u0e32\u0e22\u0e2a\u0e32\u0e21\u0e41\u0e1a\u0e1a \u0e01\u0e32\u0e23\u0e15\u0e31\u0e14\u0e2a\u0e34\u0e19\u0e43\u0e08\u0e40\u0e23\u0e37\u0e48\u0e2d\u0e07 TTL \u0e02\u0e31\u0e49\u0e19\u0e15\u0e48\u0e33 2 \u0e0a\u0e31\u0e48\u0e27\u0e42\u0e21\u0e07 \u0e41\u0e25\u0e30\u0e08\u0e33\u0e19\u0e27\u0e19\u0e04\u0e33\u0e02\u0e2d\u0e17\u0e35\u0e48\u0e16\u0e36\u0e07 Worker \u0e02\u0e49\u0e2d\u0e21\u0e39\u0e25\u0e15\u0e31\u0e27\u0e2d\u0e22\u0e48\u0e32\u0e07\u0e40\u0e1b\u0e47\u0e19\u0e02\u0e49\u0e2d\u0e21\u0e39\u0e25\u0e2a\u0e31\u0e07\u0e40\u0e04\u0e23\u0e32\u0e30\u0e2b\u0e4c \u0e23\u0e32\u0e04\u0e32\u0e17\u0e31\u0e49\u0e07\u0e2b\u0e21\u0e14\u0e40\u0e1b\u0e47\u0e19\u0e15\u0e31\u0e27\u0e2d\u0e22\u0e48\u0e32\u0e07\u0e40\u0e1e\u0e37\u0e48\u0e2d\u0e01\u0e32\u0e23\u0e40\u0e23\u0e35\u0e22\u0e19\u0e23\u0e39\u0e49\n",
    "</details>\n",
    "\n",
    "<details class=\"zh\" lang=\"zh-Hans\"><summary>\u4e2d\u6587\u603b\u7ed3</summary>\n",
    "\n",
    "Cloudflare \u5b9e\u9a8c\u751f\u6210\u4e00\u5f20\u8bf7\u6c42\u8868\uff0c\u6bcf\u6b21\u8bf7\u6c42\u4e00\u884c\uff0c\u5217\u5305\u62ec route\uff08asset \u6216 api\uff09\u3001cf-cache-status \u72b6\u6001\u3001\u5b57\u8282\u6570\uff0c\u4ee5\u53ca\u8fb9\u7f18\u548c\u6e90\u7ad9\u7684\u65f6\u95f4\u3002\u8fd9\u4e2a notebook \u8ba1\u7b97\u7f13\u5b58\u547d\u4e2d\u7387\u3001\u6e90\u7ad9\u51cf\u8d1f\u3001\u4e09\u79cd\u6210\u672c\u5bf9\u6bd4\u30012 \u5c0f\u65f6 TTL \u4e0b\u9650\u7684\u51b3\u7b56\uff0c\u4ee5\u53ca\u5230\u8fbe Worker \u7684\u8bf7\u6c42\u6570\u3002\u793a\u4f8b\u6570\u636e\u662f\u5408\u6210\u6570\u636e\uff0c\u6240\u6709\u4ef7\u683c\u90fd\u53ea\u662f\u7528\u4e8e\u5b66\u4e60\u7684\u793a\u4f8b\u3002\n",
    "</details>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 0. Setup\n",
    "\n",
    "The notebook follows the course standard: Python 3.14, a bundled data resolver, a printed seed and assertions. S4 has no accelerator path, so the device line is always `cpu`.\n",
    "\n",
    "<details class=\"th\" lang=\"th\">\n",
    "<summary>\u0e2a\u0e23\u0e38\u0e1b\u0e20\u0e32\u0e29\u0e32\u0e44\u0e17\u0e22</summary>\n",
    "\n",
    "\u0e42\u0e19\u0e49\u0e15\u0e1a\u0e38\u0e4a\u0e01\u0e43\u0e0a\u0e49\u0e21\u0e32\u0e15\u0e23\u0e10\u0e32\u0e19\u0e23\u0e32\u0e22\u0e27\u0e34\u0e0a\u0e32: Python 3.14 \u0e15\u0e31\u0e27\u0e41\u0e01\u0e49\u0e40\u0e2a\u0e49\u0e19\u0e17\u0e32\u0e07\u0e02\u0e49\u0e2d\u0e21\u0e39\u0e25\u0e17\u0e35\u0e48\u0e23\u0e27\u0e21\u0e21\u0e32\u0e43\u0e2b\u0e49 \u0e1e\u0e34\u0e21\u0e1e\u0e4c seed \u0e41\u0e25\u0e30\u0e43\u0e0a\u0e49 assertion \u0e15\u0e23\u0e27\u0e08\u0e2a\u0e2d\u0e1a S4 \u0e44\u0e21\u0e48\u0e21\u0e35\u0e40\u0e2a\u0e49\u0e19\u0e17\u0e32\u0e07\u0e43\u0e0a\u0e49\u0e15\u0e31\u0e27\u0e40\u0e23\u0e48\u0e07\u0e2e\u0e32\u0e23\u0e4c\u0e14\u0e41\u0e27\u0e23\u0e4c \u0e2d\u0e38\u0e1b\u0e01\u0e23\u0e13\u0e4c\u0e08\u0e36\u0e07\u0e40\u0e1b\u0e47\u0e19 cpu \u0e40\u0e2a\u0e21\u0e2d\n",
    "</details>\n",
    "\n",
    "<details class=\"zh\" lang=\"zh-Hans\"><summary>\u4e2d\u6587\u603b\u7ed3</summary>\n",
    "\n",
    "notebook \u9075\u5faa\u8bfe\u7a0b\u6807\u51c6\uff1aPython 3.14\u3001\u5185\u7f6e\u7684\u6570\u636e\u8def\u5f84\u89e3\u6790\u5668\u3001\u6253\u5370 seed\uff0c\u4ee5\u53ca\u4f7f\u7528\u65ad\u8a00\u8fdb\u884c\u6821\u9a8c\u3002S4 \u6ca1\u6709\u4f7f\u7528\u786c\u4ef6\u52a0\u901f\u5668\u7684\u8def\u5f84\uff0c\u56e0\u6b64\u8bbe\u5907\u59cb\u7ec8\u662f cpu\u3002\n",
    "</details>\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import csv\n",
    "import json\n",
    "import math\n",
    "import os\n",
    "import sys\n",
    "from pathlib import Path\n",
    "\n",
    "SEED = 42\n",
    "print(\"seed\", SEED)\n",
    "print(\"Python\", sys.version.split()[0])\n",
    "assert sys.version_info >= (3, 11)\n",
    "DEVICE = \"cpu\"\n",
    "print(\"device\", DEVICE, \"(S4 has no accelerator path)\")\n",
    "\n",
    "def data_dir():\n",
    "    \"\"\"Find the bundled data: local data/, Kaggle input, then COURSE_DATA.\"\"\"\n",
    "    candidates = [Path(\"data\"), Path(\"/kaggle/input/networking-delivery\")]\n",
    "    env = os.environ.get(\"COURSE_DATA\")\n",
    "    if env:\n",
    "        candidates.append(Path(env))\n",
    "    for path in candidates:\n",
    "        if path and (path / \"cdn_sample.csv\").exists():\n",
    "            return path\n",
    "    raise FileNotFoundError(\"cdn_sample.csv not found; put it in data/\")\n",
    "\n",
    "DATA = data_dir() / \"cdn_sample.csv\"\n",
    "print(\"data\", DATA)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "rows = list(csv.DictReader(DATA.open()))\n",
    "for row in rows:\n",
    "    row[\"bytes\"] = int(row[\"bytes\"])\n",
    "    row[\"edge_ms\"] = int(row[\"edge_ms\"])\n",
    "    row[\"origin_ms\"] = int(row[\"origin_ms\"])\n",
    "routes = {row[\"route\"] for row in rows}\n",
    "print(\"requests\", len(rows), \"routes\", sorted(routes))\n",
    "assert len(rows) >= 10\n",
    "assert routes == {\"asset\", \"api\"}\n",
    "assert {row[\"cache_status\"] for row in rows} == {\"HIT\", \"MISS\", \"DYNAMIC\"}\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Hit rate and origin offload, by route\n",
    "\n",
    "The hit rate is the share of **cacheable asset requests** the edge answered. The API route is `no-store` by design, so its `DYNAMIC` rows always reach the Worker and must be counted separately: a single blended percentage would hide the fact that the API never uses the cache.\n",
    "\n",
    "Origin offload is about bytes, not requests. A single missed 15 MB video costs as much origin egress as 300 missed thumbnails, so report both counts and bytes.\n",
    "\n",
    "<details class=\"th\" lang=\"th\">\n",
    "<summary>\u0e2a\u0e23\u0e38\u0e1b\u0e20\u0e32\u0e29\u0e32\u0e44\u0e17\u0e22</summary>\n",
    "\n",
    "\u0e2d\u0e31\u0e15\u0e23\u0e32\u0e41\u0e04\u0e0a\u0e16\u0e39\u0e01\u0e04\u0e37\u0e2d\u0e2a\u0e31\u0e14\u0e2a\u0e48\u0e27\u0e19\u0e04\u0e33\u0e02\u0e2d asset \u0e17\u0e35\u0e48\u0e40\u0e2d\u0e14\u0e08\u0e4c\u0e15\u0e2d\u0e1a\u0e40\u0e2d\u0e07 \u0e2a\u0e48\u0e27\u0e19 API \u0e16\u0e39\u0e01\u0e15\u0e31\u0e49\u0e07\u0e40\u0e1b\u0e47\u0e19 no-store \u0e42\u0e14\u0e22\u0e2d\u0e2d\u0e01\u0e41\u0e1a\u0e1a \u0e08\u0e36\u0e07\u0e21\u0e35\u0e2a\u0e16\u0e32\u0e19\u0e30 DYNAMIC \u0e41\u0e25\u0e30\u0e16\u0e36\u0e07 Worker \u0e17\u0e38\u0e01\u0e04\u0e23\u0e31\u0e49\u0e07 \u0e15\u0e49\u0e2d\u0e07\u0e41\u0e22\u0e01\u0e19\u0e31\u0e1a\u0e44\u0e21\u0e48\u0e43\u0e2b\u0e49\u0e1b\u0e19\u0e01\u0e31\u0e1a asset \u0e01\u0e32\u0e23\u0e25\u0e14\u0e20\u0e32\u0e23\u0e30\u0e15\u0e49\u0e19\u0e17\u0e32\u0e07\u0e27\u0e31\u0e14\u0e40\u0e1b\u0e47\u0e19\u0e44\u0e1a\u0e15\u0e4c \u0e44\u0e21\u0e48\u0e43\u0e0a\u0e48\u0e08\u0e33\u0e19\u0e27\u0e19\u0e04\u0e33\u0e02\u0e2d \u0e40\u0e1e\u0e23\u0e32\u0e30\u0e27\u0e34\u0e14\u0e35\u0e42\u0e2d 15 MB \u0e17\u0e35\u0e48\u0e1e\u0e25\u0e32\u0e14\u0e2b\u0e19\u0e36\u0e48\u0e07\u0e04\u0e23\u0e31\u0e49\u0e07\u0e21\u0e35\u0e04\u0e48\u0e32\u0e2d\u0e35\u0e40\u0e01\u0e23\u0e2a\u0e40\u0e17\u0e48\u0e32\u0e01\u0e31\u0e1a\u0e20\u0e32\u0e1e\u0e15\u0e31\u0e27\u0e2d\u0e22\u0e48\u0e32\u0e07\u0e17\u0e35\u0e48\u0e1e\u0e25\u0e32\u0e14\u0e2b\u0e25\u0e32\u0e22\u0e23\u0e49\u0e2d\u0e22\u0e20\u0e32\u0e1e\n",
    "</details>\n",
    "\n",
    "<details class=\"zh\" lang=\"zh-Hans\"><summary>\u4e2d\u6587\u603b\u7ed3</summary>\n",
    "\n",
    "\u7f13\u5b58\u547d\u4e2d\u7387\u662f\u8fb9\u7f18\u81ea\u884c\u5e94\u7b54\u7684 asset \u8bf7\u6c42\u6240\u5360\u7684\u6bd4\u4f8b\u3002API \u6309\u8bbe\u8ba1\u88ab\u8bbe\u4e3a no-store\uff0c\u56e0\u6b64\u5176\u72b6\u6001\u4e3a DYNAMIC\uff0c\u5e76\u4e14\u6bcf\u6b21\u90fd\u4f1a\u5230\u8fbe Worker\uff1b\u5fc5\u987b\u628a\u5b83\u5355\u72ec\u8ba1\u6570\uff0c\u4e0d\u80fd\u4e0e asset \u6df7\u5728\u4e00\u8d77\u3002\u6e90\u7ad9\u51cf\u8d1f\u6309\u5b57\u8282\u8861\u91cf\uff0c\u800c\u4e0d\u662f\u6309\u8bf7\u6c42\u6570\uff0c\u56e0\u4e3a\u4e00\u6b21\u672a\u547d\u4e2d\u7684 15 MB \u89c6\u9891\u6240\u4ea7\u751f\u7684\u51fa\u7ad9\u6d41\u91cf\u8d39\u7528\uff0c\u7b49\u4e8e\u6570\u767e\u5f20\u672a\u547d\u4e2d\u7684\u7f29\u7565\u56fe\u3002\n",
    "</details>\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "asset_rows = [row for row in rows if row[\"route\"] == \"asset\"]\n",
    "api_rows = [row for row in rows if row[\"route\"] == \"api\"]\n",
    "\n",
    "asset_total = len(asset_rows)\n",
    "asset_hits = sum(row[\"cache_status\"] == \"HIT\" for row in asset_rows)\n",
    "asset_misses = asset_total - asset_hits\n",
    "hit_ratio = asset_hits / asset_total\n",
    "\n",
    "total_bytes = sum(row[\"bytes\"] for row in rows)\n",
    "asset_bytes = sum(row[\"bytes\"] for row in asset_rows)\n",
    "origin_bytes = sum(row[\"bytes\"] for row in rows if row[\"cache_status\"] != \"HIT\")\n",
    "api_bytes = sum(row[\"bytes\"] for row in api_rows)\n",
    "\n",
    "edge_sorted = sorted(row[\"edge_ms\"] for row in asset_rows)\n",
    "p95 = edge_sorted[int(0.95 * (asset_total - 1))]\n",
    "\n",
    "print(f\"asset: {asset_hits}/{asset_total} hits = {hit_ratio:.2f}; misses {asset_misses}\")\n",
    "print(f\"api:   {len(api_rows)} requests, {api_bytes} bytes, always DYNAMIC\")\n",
    "print(f\"sample {total_bytes/1e6:.2f} MB; origin {origin_bytes/1e6:.2f} MB \"\n",
    "      f\"({origin_bytes/total_bytes:.0%} of bytes)\")\n",
    "print(f\"edge p95 {p95} ms; edge max {max(row['edge_ms'] for row in asset_rows)} ms\")\n",
    "\n",
    "assert 0.5 < hit_ratio < 1.0\n",
    "assert origin_bytes < total_bytes\n",
    "assert p95 >= edge_sorted[len(edge_sorted) // 2]\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. The three-row delivery comparison\n",
    "\n",
    "The unit pushes about **135 GB/month** of viewer egress and stores about **40 GB**. Compare three delivery shapes for the same workload:\n",
    "\n",
    "| Shape | What you pay |\n",
    "|---|---|\n",
    "| DigitalOcean Droplet | a fixed monthly price, a 1 TB bandwidth allowance, then overage |\n",
    "| R2 + Cloudflare | storage per GB-month, **zero egress**, CDN included |\n",
    "| S3 + CloudFront | storage per GB-month, origin read, **edge egress per GB** |\n",
    "\n",
    "The prices below are illustrative (USD, checked as a teaching example on 2026-11-13, not a quote). The arithmetic, not the exact cents, is the lesson: when the edge price is zero, the shape of the bill changes.\n",
    "\n",
    "<details class=\"th\" lang=\"th\">\n",
    "<summary>\u0e2a\u0e23\u0e38\u0e1b\u0e20\u0e32\u0e29\u0e32\u0e44\u0e17\u0e22</summary>\n",
    "\n",
    "\u0e2b\u0e19\u0e48\u0e27\u0e22\u0e1c\u0e25\u0e31\u0e01\u0e2d\u0e35\u0e40\u0e01\u0e23\u0e2a\u0e43\u0e2b\u0e49\u0e1c\u0e39\u0e49\u0e0a\u0e21\u0e1b\u0e23\u0e30\u0e21\u0e32\u0e13 135 GB \u0e15\u0e48\u0e2d\u0e40\u0e14\u0e37\u0e2d\u0e19\u0e41\u0e25\u0e30\u0e40\u0e01\u0e47\u0e1a\u0e02\u0e49\u0e2d\u0e21\u0e39\u0e25\u0e1b\u0e23\u0e30\u0e21\u0e32\u0e13 40 GB \u0e40\u0e1b\u0e23\u0e35\u0e22\u0e1a\u0e40\u0e17\u0e35\u0e22\u0e1a\u0e2a\u0e32\u0e21\u0e23\u0e39\u0e1b\u0e41\u0e1a\u0e1a: Droplet \u0e04\u0e34\u0e14\u0e23\u0e32\u0e04\u0e32\u0e04\u0e07\u0e17\u0e35\u0e48\u0e1e\u0e23\u0e49\u0e2d\u0e21\u0e42\u0e04\u0e27\u0e15\u0e49\u0e32\u0e41\u0e1a\u0e19\u0e14\u0e4c\u0e27\u0e34\u0e14\u0e17\u0e4c 1 TB, R2 + Cloudflare \u0e04\u0e34\u0e14\u0e04\u0e48\u0e32\u0e40\u0e01\u0e47\u0e1a\u0e02\u0e49\u0e2d\u0e21\u0e39\u0e25\u0e41\u0e15\u0e48\u0e44\u0e21\u0e48\u0e04\u0e34\u0e14\u0e04\u0e48\u0e32\u0e2d\u0e35\u0e40\u0e01\u0e23\u0e2a\u0e41\u0e25\u0e30\u0e23\u0e27\u0e21 CDN, S3 + CloudFront \u0e04\u0e34\u0e14\u0e04\u0e48\u0e32\u0e40\u0e01\u0e47\u0e1a\u0e02\u0e49\u0e2d\u0e21\u0e39\u0e25\u0e41\u0e25\u0e30\u0e04\u0e34\u0e14\u0e2d\u0e35\u0e40\u0e01\u0e23\u0e2a\u0e17\u0e35\u0e48\u0e40\u0e2d\u0e14\u0e08\u0e4c\u0e15\u0e48\u0e2d GB \u0e23\u0e32\u0e04\u0e32\u0e40\u0e1b\u0e47\u0e19\u0e15\u0e31\u0e27\u0e2d\u0e22\u0e48\u0e32\u0e07\u0e40\u0e1e\u0e37\u0e48\u0e2d\u0e01\u0e32\u0e23\u0e2a\u0e2d\u0e19 \u0e44\u0e21\u0e48\u0e43\u0e0a\u0e48\u0e43\u0e1a\u0e40\u0e2a\u0e19\u0e2d\u0e23\u0e32\u0e04\u0e32 \u0e1a\u0e17\u0e40\u0e23\u0e35\u0e22\u0e19\u0e2d\u0e22\u0e39\u0e48\u0e17\u0e35\u0e48\u0e42\u0e04\u0e23\u0e07\u0e2a\u0e23\u0e49\u0e32\u0e07\u0e1a\u0e34\u0e25 \u0e44\u0e21\u0e48\u0e43\u0e0a\u0e48\u0e40\u0e28\u0e29\u0e2a\u0e15\u0e32\u0e07\u0e04\u0e4c\n",
    "</details>\n",
    "\n",
    "<details class=\"zh\" lang=\"zh-Hans\"><summary>\u4e2d\u6587\u603b\u7ed3</summary>\n",
    "\n",
    "\u8be5\u5355\u5143\u6bcf\u6708\u5411\u89c2\u4f17\u63a8\u9001\u7ea6 135 GB \u7684\u51fa\u7ad9\u6d41\u91cf\uff0c\u5e76\u5b58\u50a8\u7ea6 40 GB \u6570\u636e\u3002\u6bd4\u8f83\u4e09\u79cd\u5f62\u6001\uff1aDroplet \u6536\u53d6\u56fa\u5b9a\u4ef7\u683c\u5e76\u63d0\u4f9b 1 TB \u5e26\u5bbd\u914d\u989d\uff1bR2 + Cloudflare \u6536\u53d6\u5b58\u50a8\u8d39\u4f46\u4e0d\u6536\u51fa\u7ad9\u6d41\u91cf\u8d39\uff0c\u5e76\u5305\u542b CDN\uff1bS3 + CloudFront \u6536\u53d6\u5b58\u50a8\u8d39\uff0c\u5e76\u6309 GB \u6536\u53d6\u8fb9\u7f18\u51fa\u7ad9\u6d41\u91cf\u8d39\u3002\u4ef7\u683c\u53ea\u662f\u6559\u5b66\u793a\u4f8b\uff0c\u4e0d\u662f\u62a5\u4ef7\u3002\u8981\u70b9\u5728\u4e8e\u8d26\u5355\u7ed3\u6784\uff0c\u800c\u4e0d\u662f\u51e0\u5206\u94b1\u3002\n",
    "</details>\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "MONTHLY_EGRESS_GB = 135.0\n",
    "STORED_GB = 40.0\n",
    "\n",
    "# Illustrative USD prices; replace with the current provider table before class.\n",
    "DROPLET_FIXED = 6.00\n",
    "DROPLET_INCLUDED_GB = 1000.0\n",
    "DROPLET_OVERAGE = 0.01\n",
    "R2_STORAGE = 0.015\n",
    "R2_EGRESS = 0.00\n",
    "S3_STORAGE = 0.023\n",
    "CLOUDFRONT_EGRESS = 0.085\n",
    "\n",
    "droplet = DROPLET_FIXED + max(0.0, MONTHLY_EGRESS_GB - DROPLET_INCLUDED_GB) * DROPLET_OVERAGE\n",
    "r2 = STORED_GB * R2_STORAGE + MONTHLY_EGRESS_GB * R2_EGRESS\n",
    "s3_cf = STORED_GB * S3_STORAGE + MONTHLY_EGRESS_GB * CLOUDFRONT_EGRESS\n",
    "\n",
    "delivery = {\n",
    "    \"droplet_usd\": round(droplet, 2),\n",
    "    \"r2_cloudflare_usd\": round(r2, 2),\n",
    "    \"s3_cloudfront_usd\": round(s3_cf, 2),\n",
    "}\n",
    "print(json.dumps(delivery, indent=2))\n",
    "print(f\"CloudFront egress is {(MONTHLY_EGRESS_GB * CLOUDFRONT_EGRESS) / s3_cf:.0%} of the S3 shape\")\n",
    "\n",
    "assert r2 < s3_cf\n",
    "assert MONTHLY_EGRESS_GB * CLOUDFRONT_EGRESS > 0.5 * s3_cf\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. The TTL-floor exercise\n",
    "\n",
    "The Free plan's minimum **Edge TTL is 2 hours**. That floor decides how a publishing workflow must be designed:\n",
    "\n",
    "- updating **30 objects a day in place** needs one purge each: 30 purges a day, 900 a month. Free allows 5 purges per minute, so a single batch of 30 takes at least 6 minutes to clear.\n",
    "- uploading **30 versioned objects a day** needs **0 purges**: each new name is a new cache entry, and the long TTL stays safe.\n",
    "\n",
    "The versioned path is not free in storage \u2014 it retains old objects \u2014 but it removes the correctness risk and the purge rate limit from the publishing path.\n",
    "\n",
    "<details class=\"th\" lang=\"th\">\n",
    "<summary>\u0e2a\u0e23\u0e38\u0e1b\u0e20\u0e32\u0e29\u0e32\u0e44\u0e17\u0e22</summary>\n",
    "\n",
    "TTL \u0e02\u0e31\u0e49\u0e19\u0e15\u0e48\u0e33\u0e02\u0e2d\u0e07\u0e40\u0e2d\u0e14\u0e08\u0e4c\u0e1a\u0e19\u0e41\u0e1e\u0e25\u0e19\u0e1f\u0e23\u0e35\u0e04\u0e37\u0e2d 2 \u0e0a\u0e31\u0e48\u0e27\u0e42\u0e21\u0e07 \u0e0b\u0e36\u0e48\u0e07\u0e01\u0e33\u0e2b\u0e19\u0e14\u0e27\u0e34\u0e18\u0e35\u0e2d\u0e2d\u0e01\u0e41\u0e1a\u0e1a\u0e01\u0e32\u0e23\u0e40\u0e1c\u0e22\u0e41\u0e1e\u0e23\u0e48: \u0e01\u0e32\u0e23\u0e2d\u0e31\u0e1b\u0e40\u0e14\u0e15\u0e02\u0e2d\u0e07\u0e40\u0e14\u0e34\u0e21 30 \u0e0a\u0e34\u0e49\u0e19\u0e15\u0e48\u0e2d\u0e27\u0e31\u0e19\u0e15\u0e49\u0e2d\u0e07\u0e25\u0e49\u0e32\u0e07\u0e41\u0e04\u0e0a\u0e17\u0e38\u0e01\u0e0a\u0e34\u0e49\u0e19 \u0e27\u0e31\u0e19\u0e25\u0e30 30 \u0e04\u0e23\u0e31\u0e49\u0e07 \u0e40\u0e14\u0e37\u0e2d\u0e19\u0e25\u0e30 900 \u0e04\u0e23\u0e31\u0e49\u0e07 \u0e41\u0e25\u0e30\u0e02\u0e49\u0e2d\u0e08\u0e33\u0e01\u0e31\u0e14 5 \u0e04\u0e23\u0e31\u0e49\u0e07\u0e15\u0e48\u0e2d\u0e19\u0e32\u0e17\u0e35\u0e17\u0e33\u0e43\u0e2b\u0e49\u0e0a\u0e38\u0e14 30 \u0e0a\u0e34\u0e49\u0e19\u0e43\u0e0a\u0e49\u0e40\u0e27\u0e25\u0e32\u0e2d\u0e22\u0e48\u0e32\u0e07\u0e19\u0e49\u0e2d\u0e22 6 \u0e19\u0e32\u0e17\u0e35 \u0e2a\u0e48\u0e27\u0e19\u0e01\u0e32\u0e23\u0e2d\u0e31\u0e1b\u0e42\u0e2b\u0e25\u0e14\u0e0a\u0e37\u0e48\u0e2d\u0e41\u0e1a\u0e1a\u0e21\u0e35\u0e40\u0e27\u0e2d\u0e23\u0e4c\u0e0a\u0e31\u0e19 30 \u0e0a\u0e34\u0e49\u0e19\u0e15\u0e48\u0e2d\u0e27\u0e31\u0e19\u0e44\u0e21\u0e48\u0e15\u0e49\u0e2d\u0e07\u0e25\u0e49\u0e32\u0e07\u0e41\u0e04\u0e0a\u0e40\u0e25\u0e22 \u0e41\u0e15\u0e48\u0e15\u0e49\u0e2d\u0e07\u0e40\u0e01\u0e47\u0e1a\u0e27\u0e31\u0e15\u0e16\u0e38\u0e40\u0e01\u0e48\u0e32\u0e44\u0e27\u0e49\n",
    "</details>\n",
    "\n",
    "<details class=\"zh\" lang=\"zh-Hans\"><summary>\u4e2d\u6587\u603b\u7ed3</summary>\n",
    "\n",
    "\u514d\u8d39\u5957\u9910\u4e0a\u8fb9\u7f18\u7684\u6700\u5c0f TTL \u662f 2 \u5c0f\u65f6\uff0c\u8fd9\u51b3\u5b9a\u4e86\u53d1\u5e03\u6d41\u7a0b\u7684\u8bbe\u8ba1\u65b9\u5f0f\uff1a\u6bcf\u5929\u5c31\u5730\u66f4\u65b0 30 \u4e2a\u5bf9\u8c61\uff0c\u6bcf\u4e00\u4e2a\u90fd\u9700\u8981\u6e05\u9664\u7f13\u5b58\uff0c\u5373\u6bcf\u5929 30 \u6b21\u3001\u6bcf\u6708 900 \u6b21\uff1b\u800c\u6bcf\u5206\u949f 5 \u6b21\u7684\u9650\u5236\u4f7f\u4e00\u6279 30 \u4e2a\u5bf9\u8c61\u81f3\u5c11\u9700\u8981 6 \u5206\u949f\u624d\u80fd\u6e05\u5b8c\u3002\u81f3\u4e8e\u6bcf\u5929\u4e0a\u4f20 30 \u4e2a\u5e26\u7248\u672c\u540d\u79f0\u7684\u5bf9\u8c61\uff0c\u5219\u5b8c\u5168\u4e0d\u9700\u8981\u6e05\u9664\u7f13\u5b58\uff0c\u4f46\u5fc5\u987b\u4fdd\u7559\u65e7\u5bf9\u8c61\u3002\n",
    "</details>\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "OBJECTS_PER_DAY = 30\n",
    "FREE_PURGES_PER_MINUTE = 5\n",
    "DAYS = 30\n",
    "\n",
    "in_place_purges_day = OBJECTS_PER_DAY\n",
    "in_place_purges_month = in_place_purges_day * DAYS\n",
    "versioned_purges_day = 0\n",
    "minutes_to_clear_batch = math.ceil(OBJECTS_PER_DAY / FREE_PURGES_PER_MINUTE)\n",
    "\n",
    "ttl_decision = {\n",
    "    \"edge_ttl_floor_s\": 7200,\n",
    "    \"objects_updated_per_day\": OBJECTS_PER_DAY,\n",
    "    \"in_place_purges_per_day\": in_place_purges_day,\n",
    "    \"in_place_purges_per_month\": in_place_purges_month,\n",
    "    \"minutes_to_clear_one_batch\": minutes_to_clear_batch,\n",
    "    \"versioned_purges_per_day\": versioned_purges_day,\n",
    "}\n",
    "print(json.dumps(ttl_decision, indent=2))\n",
    "\n",
    "assert versioned_purges_day == 0\n",
    "assert in_place_purges_month == 900\n",
    "assert minutes_to_clear_batch == 6\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. Worker request headroom\n",
    "\n",
    "Static asset requests are answered by the edge and **never invoke the Worker**. The Free plan's 100,000 requests/day is a Worker limit, not a CDN limit. The unit's 5,000 thumbnails a day therefore cost zero Worker requests; only the API calls count.\n",
    "\n",
    "Assume each video view produces one catalogue read and one signed-file request, plus one health or list call: 300 views \u00d7 3 = 900 API requests a day. Check that against the limit.\n",
    "\n",
    "<details class=\"th\" lang=\"th\">\n",
    "<summary>\u0e2a\u0e23\u0e38\u0e1b\u0e20\u0e32\u0e29\u0e32\u0e44\u0e17\u0e22</summary>\n",
    "\n",
    "\u0e04\u0e33\u0e02\u0e2d\u0e44\u0e1f\u0e25\u0e4c\u0e41\u0e1a\u0e1a\u0e2a\u0e41\u0e15\u0e15\u0e34\u0e01\u0e17\u0e35\u0e48\u0e40\u0e2d\u0e14\u0e08\u0e4c\u0e15\u0e2d\u0e1a\u0e44\u0e21\u0e48\u0e40\u0e23\u0e35\u0e22\u0e01 Worker \u0e40\u0e25\u0e22 \u0e40\u0e1e\u0e14\u0e32\u0e19 100,000 \u0e04\u0e33\u0e02\u0e2d\u0e15\u0e48\u0e2d\u0e27\u0e31\u0e19\u0e02\u0e2d\u0e07\u0e41\u0e1e\u0e25\u0e19\u0e1f\u0e23\u0e35\u0e08\u0e36\u0e07\u0e40\u0e1b\u0e47\u0e19\u0e40\u0e1e\u0e14\u0e32\u0e19\u0e02\u0e2d\u0e07 Worker \u0e44\u0e21\u0e48\u0e43\u0e0a\u0e48\u0e02\u0e2d\u0e07 CDN \u0e20\u0e32\u0e1e\u0e15\u0e31\u0e27\u0e2d\u0e22\u0e48\u0e32\u0e07 5,000 \u0e20\u0e32\u0e1e\u0e15\u0e48\u0e2d\u0e27\u0e31\u0e19\u0e08\u0e36\u0e07\u0e44\u0e21\u0e48\u0e43\u0e0a\u0e49\u0e42\u0e04\u0e27\u0e15\u0e32 Worker \u0e21\u0e35\u0e40\u0e1e\u0e35\u0e22\u0e07\u0e04\u0e33\u0e02\u0e2d API \u0e40\u0e17\u0e48\u0e32\u0e19\u0e31\u0e49\u0e19\u0e17\u0e35\u0e48\u0e19\u0e31\u0e1a \u0e2a\u0e21\u0e21\u0e15\u0e34\u0e27\u0e48\u0e32\u0e27\u0e34\u0e14\u0e35\u0e42\u0e2d\u0e2b\u0e19\u0e36\u0e48\u0e07\u0e04\u0e23\u0e31\u0e49\u0e07\u0e17\u0e33\u0e43\u0e2b\u0e49\u0e40\u0e01\u0e34\u0e14\u0e01\u0e32\u0e23\u0e2d\u0e48\u0e32\u0e19\u0e41\u0e04\u0e15\u0e15\u0e32\u0e25\u0e47\u0e2d\u0e01\u0e2b\u0e19\u0e36\u0e48\u0e07\u0e04\u0e23\u0e31\u0e49\u0e07\u0e41\u0e25\u0e30\u0e04\u0e33\u0e02\u0e2d\u0e44\u0e1f\u0e25\u0e4c\u0e25\u0e07\u0e19\u0e32\u0e21\u0e2b\u0e19\u0e36\u0e48\u0e07\u0e04\u0e23\u0e31\u0e49\u0e07 \u0e1a\u0e27\u0e01\u0e04\u0e33\u0e02\u0e2d health \u0e2b\u0e23\u0e37\u0e2d list \u0e2b\u0e19\u0e36\u0e48\u0e07\u0e04\u0e23\u0e31\u0e49\u0e07 \u0e23\u0e27\u0e21 900 \u0e04\u0e33\u0e02\u0e2d\u0e15\u0e48\u0e2d\u0e27\u0e31\u0e19\n",
    "</details>\n",
    "\n",
    "<details class=\"zh\" lang=\"zh-Hans\"><summary>\u4e2d\u6587\u603b\u7ed3</summary>\n",
    "\n",
    "\u7531\u8fb9\u7f18\u5e94\u7b54\u7684\u9759\u6001\u6587\u4ef6\u8bf7\u6c42\u5b8c\u5168\u4e0d\u4f1a\u8c03\u7528 Worker\uff0c\u56e0\u6b64\u514d\u8d39\u5957\u9910\u6bcf\u5929 100,000 \u6b21\u8bf7\u6c42\u7684\u4e0a\u9650\u662f Worker \u7684\u4e0a\u9650\uff0c\u800c\u4e0d\u662f CDN \u7684\u4e0a\u9650\u3002\u6bcf\u5929 5,000 \u5f20\u7f29\u7565\u56fe\u56e0\u6b64\u4e0d\u6d88\u8017 Worker \u914d\u989d\uff0c\u53ea\u6709 API \u8bf7\u6c42\u624d\u8ba1\u6570\u3002\u5047\u8bbe\u4e00\u6b21\u89c6\u9891\u89c2\u770b\u4ea7\u751f\u4e00\u6b21\u76ee\u5f55\u8bfb\u53d6\u548c\u4e00\u6b21\u7b7e\u540d\u6587\u4ef6\u8bf7\u6c42\uff0c\u518d\u52a0\u4e00\u6b21 health \u6216 list \u8c03\u7528\uff1a\u6bcf\u5929\u5171 900 \u6b21\u8bf7\u6c42\u3002\n",
    "</details>\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "VIEWS_PER_DAY = 300\n",
    "API_CALLS_PER_VIEW = 3\n",
    "API_REQUESTS_PER_DAY = VIEWS_PER_DAY * API_CALLS_PER_VIEW\n",
    "WORKER_FREE_LIMIT_PER_DAY = 100_000\n",
    "THUMBNAILS_PER_DAY = 5_000\n",
    "\n",
    "monthly_api = API_REQUESTS_PER_DAY * DAYS\n",
    "worker_share = monthly_api / (WORKER_FREE_LIMIT_PER_DAY * DAYS)\n",
    "\n",
    "worker = {\n",
    "    \"api_requests_per_day\": API_REQUESTS_PER_DAY,\n",
    "    \"static_requests_per_day_not_counted\": THUMBNAILS_PER_DAY,\n",
    "    \"monthly_api_requests\": monthly_api,\n",
    "    \"free_limit_per_day\": WORKER_FREE_LIMIT_PER_DAY,\n",
    "    \"monthly_share_of_limit\": round(worker_share, 4),\n",
    "}\n",
    "print(json.dumps(worker, indent=2))\n",
    "\n",
    "assert API_REQUESTS_PER_DAY == 900\n",
    "assert worker_share < 0.01\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 5. Write the result file\n",
    "\n",
    "`cdn_results.json` is the artifact the evidence bundle points to. It carries the seed, the route split, the hit rate, the p95, the three-row comparison, the TTL decision and the Worker headroom. Every number is derived above and asserted before it is written.\n",
    "\n",
    "<details class=\"th\" lang=\"th\">\n",
    "<summary>\u0e2a\u0e23\u0e38\u0e1b\u0e20\u0e32\u0e29\u0e32\u0e44\u0e17\u0e22</summary>\n",
    "\n",
    "\u0e44\u0e1f\u0e25\u0e4c cdn_results.json \u0e04\u0e37\u0e2d\u0e0a\u0e34\u0e49\u0e19\u0e07\u0e32\u0e19\u0e17\u0e35\u0e48\u0e0a\u0e38\u0e14\u0e2b\u0e25\u0e31\u0e01\u0e10\u0e32\u0e19\u0e2d\u0e49\u0e32\u0e07\u0e16\u0e36\u0e07 \u0e40\u0e01\u0e47\u0e1a seed \u0e01\u0e32\u0e23\u0e41\u0e22\u0e01\u0e15\u0e32\u0e21 route \u0e2d\u0e31\u0e15\u0e23\u0e32\u0e41\u0e04\u0e0a\u0e16\u0e39\u0e01 p95 \u0e15\u0e32\u0e23\u0e32\u0e07\u0e40\u0e1b\u0e23\u0e35\u0e22\u0e1a\u0e40\u0e17\u0e35\u0e22\u0e1a\u0e2a\u0e32\u0e21\u0e41\u0e1a\u0e1a \u0e01\u0e32\u0e23\u0e15\u0e31\u0e14\u0e2a\u0e34\u0e19\u0e43\u0e08 TTL \u0e41\u0e25\u0e30\u0e42\u0e04\u0e27\u0e15\u0e32 Worker \u0e17\u0e38\u0e01\u0e04\u0e48\u0e32\u0e04\u0e33\u0e19\u0e27\u0e13\u0e41\u0e25\u0e30\u0e15\u0e23\u0e27\u0e08\u0e14\u0e49\u0e27\u0e22 assertion \u0e01\u0e48\u0e2d\u0e19\u0e40\u0e02\u0e35\u0e22\u0e19\u0e44\u0e1f\u0e25\u0e4c\n",
    "</details>\n",
    "\n",
    "<details class=\"zh\" lang=\"zh-Hans\"><summary>\u4e2d\u6587\u603b\u7ed3</summary>\n",
    "\n",
    "cdn_results.json \u6587\u4ef6\u662f\u8bc1\u636e\u5305\u6240\u5f15\u7528\u7684\u4ea7\u7269\u3002\u5b83\u4fdd\u5b58 seed\u3001\u6309 route \u7684\u5212\u5206\u3001\u7f13\u5b58\u547d\u4e2d\u7387\u3001p95\u3001\u4e09\u79cd\u5bf9\u6bd4\u8868\u3001TTL \u51b3\u7b56\u4ee5\u53ca Worker \u914d\u989d\u3002\u6bcf\u4e2a\u503c\u90fd\u5728\u5199\u6587\u4ef6\u4e4b\u524d\u5148\u8ba1\u7b97\u5e76\u7528\u65ad\u8a00\u6821\u9a8c\u3002\n",
    "</details>\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "results = {\n",
    "    \"seed\": SEED,\n",
    "    \"device\": DEVICE,\n",
    "    \"data\": str(DATA),\n",
    "    \"requests\": len(rows),\n",
    "    \"asset_requests\": asset_total,\n",
    "    \"api_requests\": len(api_rows),\n",
    "    \"asset_hits\": asset_hits,\n",
    "    \"asset_misses\": asset_misses,\n",
    "    \"hit_ratio\": round(hit_ratio, 3),\n",
    "    \"sample_mb\": round(total_bytes / 1e6, 2),\n",
    "    \"origin_mb\": round(origin_bytes / 1e6, 2),\n",
    "    \"edge_p95_ms\": p95,\n",
    "    \"monthly_egress_gb\": MONTHLY_EGRESS_GB,\n",
    "    \"monthly_origin_gb\": round(MONTHLY_EGRESS_GB * (1 - hit_ratio), 1),\n",
    "    \"delivery_usd\": delivery,\n",
    "    \"ttl\": ttl_decision,\n",
    "    \"worker\": worker,\n",
    "}\n",
    "out = Path(\"cdn_results.json\")\n",
    "out.write_text(json.dumps(results, indent=2))\n",
    "print(json.dumps(results, indent=2))\n",
    "\n",
    "assert json.loads(out.read_text())[\"hit_ratio\"] == results[\"hit_ratio\"]\n",
    "print(\"wrote\", out)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## What this proves\n",
    "\n",
    "Write 150\u2013250 words: what the hit rate did to origin egress; why the API route must be counted separately from the asset route; what the edge p95 shows that the mean hides; which of the three delivery shapes wins for this workload and why; and when a purge is better than a versioned name. Attach `cdn_results.json`, the cache-status evidence and the teardown log.\n",
    "\n",
    "## Verification\n",
    "\n",
    "**Mac (Apple Silicon, Python 3.14):** the sample table runs end to end; assertions pass; outputs cleared. Replace the sample with your own Cloudflare measurements. S4 has no accelerator path.\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python",
   "version": "3.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
