CSE Roadmap

Common tier · about 4 hours · 8 parts

Part 6 of 8 · 3 min read

Cost

The bill and what to cut.

What costs money. The cloud CDN charges for data transferred out. The book's rough estimate: 5 million daily users watching 5 videos of 0.3 GB each at 2 cents a GB comes to 5,000,000 × 5 × 0.3 × 0.02 = 150,000 dollars a day. Even with discounts for big customers, the cost is large.

Ways to cut it (from the book, with additions).

  1. Serve only the most popular content from the CDN. Access follows a long-tail pattern: a few items are very popular and many have few viewers. Serve the tail from cheaper storage servers.
  2. For less popular content, store fewer versions, and encode short videos on demand.
  3. Some content is popular only in some regions, so do not copy it to other regions.
  4. For the largest companies, build their own CDN and place servers inside internet providers' networks. This improves viewing and cuts bandwidth charges, but it is a very large project.
  5. Raise the hit ratio: better cache keys, consistent hashing in the PoP, larger regional caches, and compression.

Scale. Add PoPs near users, and add servers inside a PoP. Capacity planning compares peak demand per PoP with its network and disk limits, with spare room for failover (when one PoP fails, its neighbours take its load, so each needs spare capacity, such as 30 per cent).

Failure.

  • Server fails inside a PoP: the local load balancer removes it, and the consistent hashing sends its URLs to others (a brief dip in hit ratio).
  • A PoP fails: routing moves users to the next one.
  • Origin fails: serve stale content, and use a secondary origin.
  • CDN fallback (from the book): if the CDN has an outage, clients should detect it and fetch from the origin. For a website this can mean a second hostname, or a script that retries the origin.
  • A bad purge or configuration: roll out config changes gradually (canary), and keep the ability to roll back.

Security. HTTPS everywhere. Signed URLs or tokens with an expiry for private content. Protection from denial of service attacks: the CDN absorbs large floods because of its size, and rate limits per client. A web application firewall can run at the edge.

A worked example (made up). A site serves 400 TB a month at 3 cents a GB: 400,000 GB × 0.03 = 12,000 dollars. Raising the hit ratio from 90 to 95 per cent halves the origin traffic from 10 per cent to 5 per cent of requests, saving origin egress and compute. If the CDN price tiers fall to 2 cents past 300 TB, the bill becomes 300,000 × 0.03 + 100,000 × 0.02 = 11,000 dollars.

Read it as

A CDN trades money and complexity for speed and for protection of the origin. The design work is deciding what goes in, for how long, and how to take it out.

Check yourself: 1. Which two numbers drive the cost? 2. How does the long tail help cut cost? 3. What should a client do if the CDN is down?