ENع
→Compute
الدرس 04 · السحابة

Compute: VMs and serverless

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

10 دقائق10 خطوات5 أسئلةBeginner
عن هذا الدرس

Compute is the processing power that runs your code, and the cloud rents it to you in two common shapes. A virtual machine is a slice of a physical server that you size and manage, billed for every second it is on. A serverless function is just your code: the platform starts copies when requests arrive and removes them when they stop. Follow the same small Python handler through four requests both ways and compare the seconds each one counts.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 4 · تفاعلي
01 / 10

A computer you rent

Compute means processing power: a CPU and memory that run your code. In the cloud that power sits in physical servers in a provider's data centre. You never touch the machine; you rent a slice of it.

compute = CPU + memory that run your code

cloud: real servers in a data centre
you rent a slice, on demand
the provider owns and fixes the hardware
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. A computer you rent

    Compute means processing power: a CPU and memory that run your code. In the cloud that power sits in physical servers in a provider's data centre. You never touch the machine; you rent a slice of it.

    compute = CPU + memory that run your code
    
    cloud: real servers in a data centre
    you rent a slice, on demand
    the provider owns and fixes the hardware
  2. One server, many VMs

    A hypervisor is software that splits one physical server into several virtual machines (VMs). Each VM gets its own share of CPU and memory and its own operating system, kept apart from its neighbours.

    physical server
      └ hypervisor
          ├ your VM: own OS, CPU share, memory
          └ another VM: own OS, CPU share, memory
    
    VMs cannot see each other's memory or files
  3. Your virtual machine

    You pick a size, say 2 virtual CPUs (vCPUs) and 4 GiB of memory, then install your app on it. The VM is yours to run: you update its operating system and decide when to add more VMs.

    size: 2 vCPU · 4 GiB memory
    
    you manage:
      the operating system and its updates
      your app (here: the handler)
      adding VMs when traffic grows
  4. Always on, always billed

    Requests arrive now and then; between them the VM sits waiting. It is billed for every second it is switched on, busy or idle. Left on for one hour, that is 3600 seconds, even for just 4 requests.

    VM switched on for 1 hour
      billed: 60 × 60 = 3600 s
      requests served: 4
    
    per-second billing, 60 s minimum
    (Amazon EC2 Linux, Google Compute Engine)
  5. Serverless: just the code

    Serverless turns this around. You upload only a function, like this Python handler, and the provider runs the servers for you. There is no VM to size or update. AWS Lambda is a well-known example.

    you upload:
      def handler(event, context):
          name = event["name"]
          return "Hello, " + name
    
    the provider runs, sizes and updates
    the servers underneath
  6. The first request: a cold start

    The first request finds nothing running. The platform starts a fresh copy of your function, called an instance: it loads the code, starts Python and runs any set-up code. That extra wait is a cold start.

    request 1 → no instance running
      start instance 1:
        load the code
        start Python
        run set-up code
      = cold start (extra wait)
  7. The handler runs

    Now the handler runs. event is the request, {"name": "Ada"}, and it returns "Hello, Ada". Say that takes 0.12 seconds. You pay for run time, counted to the millisecond, plus a tiny fee per request. The cold-start set-up time is billed too.

    handler({"name": "Ada"}, context)
      name = "Ada"
      → "Hello, Ada"
    
    run time: 0.12 s (assumed)
    billed to the nearest 1 ms (AWS Lambda)
    + cold-start set-up time, also billed
  8. More requests, more copies

    Three requests arrive at once. On AWS Lambda each instance handles one request at a time by default, so the platform starts two more copies, side by side. That is scaling out, and it is automatic. Run time: 4 × 0.12 = 0.48 seconds.

    3 requests at the same moment
      instance 1 (warm) takes one
      instances 2 and 3 cold-start
      = scaling out, automatic
    
    run time: 4 × 0.12 s = 0.48 s
  9. Scale to zero

    When the requests stop, the instances wait idle for a while, then the platform shuts them down. With no code running, the function is billed nothing for its time. The next request brings a cold start again.

    no requests
      instances idle for a while
      then shut down: 3 → 0
    
    function run time: still 0.48 s
    next request → cold start
  10. Which one to choose

    Same 4 requests: the VM was billed 3600 seconds; the function ran 0.48, plus cold starts. VMs suit steady or long-running work and full control. Serverless suits short, bursty tasks; one Lambda call can run at most 15 minutes.

    VM          3600 s  always on, full control
    function    0.48 s  run time (+ 3 cold starts)
    
    VM: steady, long-running, custom OS
    serverless: short, bursty, no servers
    Lambda limit: 900 s (15 min) per call

اختبار يتاح بعد الخطوة الأخيرة.