My Accidental 3–5x Speed Increase of AWS Lambda Functions

Vlad Holubiev
Serverless Zone
Published in
3 min readDec 12, 2016

--

TL;DR: Allocate to your function as much memory as your wallet can allow.

Why? Well, then read below.

A Story About My Impatience

I needed 2 functions to make several HTTP requests and HTML parsing.

Microservice? Overkill and boring. It’s 2016. Let’s go for Lambda.

The next where people usually go after Landing Page is Pricing Section. And so did I:

The table below shows the approximate price per 100ms associated for different memory sizes.

The list continues down to 1536 MB. My thoughts at that moment were:

Cool, my code requires only ~50 MB of memory to work. I can start with the cheapest plan. I’m going to save a whole bunch of money!

Wrong.

But I didn’t know that yet. There is no mention about provided CPU resources, so I assumed it’s already high enough. Maybe those top memory options are intended for Java? 😅

A Misleading?

I thought Lambda is perfect for short CPU-intensive tasks. Why not, when the first official tutorial describes creating thumbnails from images using ImageMagick? What else if not image resizing can eat all your CPU?

So I selected the lowest memory plan, installed Serverless and with confidence dived into coding.

Performance Discovery

The code is deployed. Ready to go.

To my honest surprise, the function was much slower on AWS than even on a laptop.

2.2 seconds? Am I running this on a calculator or what.

A 15 minutes after vain attempts to find a bug in my code I ended up here:

Q: How are compute resources assigned to an AWS Lambda function?

… choosing 256MB of memory allocates approximately twice as much CPU power to your Lambda function as requesting 128MB of memory and half as much CPU power as choosing 512MB of memory.

Okay, so that’s how it works… The more memory I choose the more CPU I get. Pretty much makes sense, like EC2 instances types.

I adjusted config and oh my, this is 7 times faster now:

I don’t blame AWS for hiding this information by no means. It is written on Product Details page. 10th from 10 Key Features at the very very bottom 😅

Today I Learned: Be less impatient about new tools. Read FAQs/READMEs/Docs first.

Today You Learned: Memory options in Lambda impact on overall function performance, including I/O, network and CPU.

What about the price? It will be more or less aligned as you increase performance and decrease processing time. This handy tool helped me to choose optimal 1024 MB:

Let’s finish with some inspirational quote:

Make It Work. Make It Right. Make It Fast.

Kent Beck

Of course, writing good software requires adhering to this order. But what’s much more important — is the result. So don’t forget to make it.

--

--