Course contentsShow
← Cloud and Infrastructure
Lesson 475 of 2,9899. Serverless ComputeFree lesson

Function Memory and CPU Allocation

How memory settings determine CPU allocation and impact function performance and cost in serverless environments.

Function Memory and CPU Allocation

You'll learn how memory configuration directly controls CPU resources and affects both performance and cost in serverless functions.

The Core Relationship

In serverless platforms, you don't configure CPU directly—you configure memory, and CPU is allocated proportionally. This is different from traditional compute where you pick CPU and memory separately.

Think of it like ordering at a restaurant with a fixed menu: you choose the meal size (memory), and the kitchen automatically gives you proportional sides (CPU). A 512 MB function gets half the CPU of a 1024 MB function, even if your code barely uses any memory.

How It Works

When you set your function's memory allocation (typically between 128 MB and 10 GB), the cloud provider:

  1. Allocates proportional CPU power: More memory = more vCPU share
  2. Affects execution speed: CPU-intensive tasks run faster with higher memory settings
  3. Changes cost per millisecond: Higher memory = higher per-ms cost, BUT faster execution may reduce total billable time

The Performance-Cost Balance

A common mistake is choosing the minimum memory to save money. If your function is CPU-bound (doing calculations, processing data), increasing memory can actually reduce costs because the function completes faster.

Example: A function at 256 MB takes 2000ms (2000 × low rate). The same function at 1024 MB takes 600ms (600 × 4× rate). Even though the per-ms rate quadrupled, total execution time dropped, potentially saving money.

Key Takeaway: Memory settings control CPU allocation in serverless functions—right-sizing memory optimizes both performance and cost, requiring testing to find the sweet spot for your workload.