Multithreading in Lambda? You’ll need to use this much memory
Lambda is a funny AWS service. When it comes to provisioning how powerful you want your compute, you need to select how much memory you want. Seems a little counterintuitive, right? Instead of the typical EC2 instance-type picker where you choose something that gives you the right balance of CPUs, memory, and I/O, Lambda only gives you a slider to choose how much memory you want between 128 and 10240 MB. All other values follow from that.
Say you have a program running that is CPU-constrained but light on memory — you’ll want to select the most CPUs you can for the least amount of memory (within reason).
Selecting the maximum amount of memory (10240 MB) grants you 6 CPUs. However, the minimum amount of memory to still get 6 CPUs is just 8846 MB.
It’s also worth noting that even selecting just 128 MB of memory, while it severely limits your I/O, still gets you 2 CPU cores. Those CPU cores are just as powerful as the ones you get for 10 GB of memory: Intel(R) Xeon(R) Processor @ 2.50GHz.
You’ll get 2 CPU cores from 128 MB all the way up to 3008 MB. At 3009 MB you’ll get 3 cores. At 5308 MB you’ll be bumped up to 4 cores. At 7077 MB you’ll get 5 cores, and at 8846 you get 6 cores. I’ve visualized this in the chart below.
How is this beneficial?
It’s beneficial to know where these boundaries are drawn so you can get the most compute for your buck. Say you’re running a lambda and you chose 3000 MB, you might as well add 9 extra MB to get access to a 3rd CPU core, indicative of a faster underlying instance. Alternatively, say you have a Lambda provisioned for 5000 MB. You should benchmark to see if your performance dramatically reduces at 3009 MB — if you’re not using all that memory, you might be running on the same instance type, but paying less if you don’t need to provision all that memory.
Leave a comment if this was useful. If anyone has a reliable way to test for I/O speed (either network I/O or disk I/O), let me know below and I’ll update this post with I/O speeds.
NB: These values are current for the NodeJS 14.x and Python 3.9 runtimes as of 2021–09–07 in us-west-2. Boundaries matched between these two runtime environments.
Node JS Code:
const os = require(‘os’);
exports.handler = async (event) => os.cpus().length;
Python Code:
import multiprocessing
def lambda_handler(event, context):
return multiprocessing.cpu_count()