Skip to content
KALANBLON
Show contents

Chapter 2 of 7 · 20 min

Compute and storage

By the end of this chapter, you'll be able to

  • Compare virtual machines, managed containers and serverless functions
  • Compute from what traffic one option becomes cheaper than another
  • Tell block storage, object storage and managed databases apart
  • Take data transfer fees into account

Three ways to run code

  • a virtual machine (VM): a rented server, always on, billed by the hour whether it works or not. You manage everything running on it;
  • a managed container: you provide an image, the platform runs it and adds copies when traffic grows;
  • a serverless function: you provide a function, the platform runs it for each request and bills per call and duration. With no traffic, you pay nothing. “Serverless” does not mean without servers: just that you never deal with them.

Each option has its domain. Let's compare a VM and a serverless function with example prices, invented for the exercise:

VM_PER_MONTH = 30.0
PRICE_PER_MILLION = 0.20
DURATION_S, MEMORY_GB = 0.2, 0.5
PRICE_GB_SECOND = 0.0000167

def serverless(requests):
    compute = requests * DURATION_S * MEMORY_GB * PRICE_GB_SECOND
    return requests / 1_000_000 * PRICE_PER_MILLION + compute

for requests in (100_000, 1_000_000, 10_000_000, 50_000_000):
    s = serverless(requests)
    best = "serverless" if s < VM_PER_MONTH else "VM"
    print(f"{requests:>11,} requests/month: serverless {s:>7.2f} $, VM {VM_PER_MONTH:.2f} $ → {best}")
    100,000 requests/month: serverless    0.19 $, VM 30.00 $ → serverless
  1,000,000 requests/month: serverless    1.87 $, VM 30.00 $ → serverless
 10,000,000 requests/month: serverless   18.70 $, VM 30.00 $ → serverless
 50,000,000 requests/month: serverless   93.50 $, VM 30.00 $ → VM

At low traffic, serverless is unbeatable: you pay almost nothing. Beyond a certain volume, the VM, paid as a flat rate, becomes cheaper again. Cost is not the only criterion: a serverless function can take a little time to start after a period of inactivity (the cold start), and has a limited execution time.

Choose according to the real load, and recompute when it changes.

Step 5 of 5
Text description of the animation
  1. Full control, fixed cost: ideal for a stable, sustained load.
  2. The platform adjusts the number of containers to the traffic.
  3. Serverless costs nothing when idle, but every call is billed.
  4. With these example prices, both options cost the same at about 16 million requests per month.
  5. Choose according to the real load, and recompute when it changes.

Three ways to keep data

  • block storage: a virtual disk attached to a VM, fast, used by the system and the databases installed on it;
  • object storage: files (“objects”) kept in spaces called buckets, reachable through a web address, almost unlimited and cheap. Ideal for images, videos, backups. S3 is the name of Amazon's service, which became a standard that other providers imitate;
  • a managed database: a database (PostgreSQL, MySQL…) that the provider installs, updates and backs up for you.

Transfer fees

Providers often bill data leaving for the Internet (egress), while incoming data is usually free. For a site serving lots of videos or images, this item can exceed everything else. A CDN (content delivery network) keeps copies of files close to users: it speeds up the site and reduces egress from your storage.

Mini-lab: your own break-even pointPython 3.13

Output

Click “Run”: Python will load in your browser (about 10 MB the first time).

Your code runs in your browser, isolated from the page. · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor

Your turn

Worked exercise

Worked exercise: finding the break-even point

With the chapter's example prices (VM at 30 $ per month, serverless at 0.20 $ per million calls plus 0.0000167 $ per gigabyte-second, 0.5 GB functions running for 0.2 s), compute the number of monthly requests from which the VM becomes cheaper.

Hint 1
The serverless cost is proportional to the number of requests: compute the cost of one request.
Hint 2
Break-even point: the VM's cost divided by the cost of one request.
Show the solution and common mistakes
VM_PER_MONTH = 30.0
per_request = 0.20 / 1_000_000 + 0.2 * 0.5 * 0.0000167

break_even = VM_PER_MONTH / per_request
print(f"cost of one request: {per_request:.9f} $")
print(f"break-even: {break_even:,.0f} requests per month")
print(f"about {break_even / 30 / 86_400:.1f} requests per second, continuously")
cost of one request: 0.000001870 $
break-even: 16,042,781 requests per month
about 6.2 requests per second, continuously

Below about 16 million requests per month (about 6 per second, if they were spread evenly), serverless is cheaper with these prices. You would also need to check that a single VM is enough to absorb this traffic at peak times: otherwise, you need two, and the break-even point doubles.

Common mistakes:

  • Forgetting the “duration × memory” part: the per-call part alone would give a break-even at 150 million requests, ten times too high.
  • Reasoning on an average: real traffic has peaks, and the peak is what sizes the VMs.

Summary

  1. 1VM, managed container, serverless function: from most controlled to most managed.
  2. 2Serverless is unbeatable at low traffic; at high, stable traffic, a flat rate can cost less.
  3. 3Block storage for disks, object storage for files, a managed database for structured data.
  4. 4Egress fees can dominate the bill: a CDN reduces them.
  5. 5Prices change: recompute with those of the provider, the region and the moment.

Your progress is saved on this device. Create a free account to keep your progress on all your devices.