The Chase is the Prize

So many people believe that BUYING THINGS OR GETTING YOUR WAY is the key to happiness. - Every time you get what you want or a prize, you get a small surge of a happy chemical called Dopamine. - In…

Smartphone

独家优惠奖金 100% 高达 1 BTC + 180 免费旋转




The best ways to save money on Lambda

When it comes to cost, Lambda charges you based on the amount of memory you allocate to the function and how long this function runs for by the millisecond. And it also charges you a fixed fee of 20 cents per million invocations.

There can be other costs involved, such as the data transfer cost, but also when you enable certain features. Such as when you allocate additional space to the /temp storage, or when you use provisioned concurrency. But for vanilla on-demand functions, your main costs are:

The cost per millisecond of execution time is scaled proportionally to the amount of memory you allocate. As an example, one second of execution for a 512MB function would be 4 times the cost for one second of execution of a 128MB function.

The most effective way to save money on your Lambda functions is to make sure that you don’t allocate more memory than your functions actually need.

It’s also worth pointing out that, for the vast majority of your Lambda functions, there are likely no meaningful cost savings to be had.

Lambda cost itself is usually pretty far down the list of the most expensive items in your monthly AWS bill. Often behind the likes of API Gateway and CloudWatch by a large distance.

There are exceptions to this of course. But as the famous saying goes…

The trick is in identifying that critical 3% of cases where we should optimise our Lambda function.

You should always optimise the memory setting for functions that use provisioned concurrency.

Because provisioned concurrency has an uptime cost per month per unit of concurrency. And that cost scales proportionally to the amount of memory allocated to the function. For example, one provision concurrency for a 128MB function has an uptime cost of just $1.40 per month, plus variable costs based on the no. of invocations and execution time. But one provision concurrency for a 10GB function would have an uptime cost of $111.61 per month, plus those variable costs as well.

With scaling effects like this, it’s easy to make mistakes that can increase your Lambda cost by orders of magnitude.

Another group of functions that fall into that critical 3% are functions that are invoked often (e.g. millions of times a month) and have a long execution time.

We can likely make meaningful savings on these functions by allocating less memory. This usually works great for functions that spend a lot of their execution time just sitting idle, and waiting for a response from API calls. So all that CPU cycles are just getting wasted anyway.

Funny enough, in some cases, it might be cheaper to allocate more memory, not less! Because the amount of CPU resources and network bandwidth is allocated proportionally to the amount of allocated memory. More memory equals more CPU cycles, which equals getting whatever CPU-intensive task done faster. And because we’re charged by the millisecond, so faster execution at a higher cost per millisecond can still work out to be cheaper than a longer execution time at a lower cost per millisecond.

Luckily for us, we don’t have to rely on guesswork. We can work this out using a data-driven approach.

Once it’s deployed, you can start an execution using a JSON payload like this:

This tells the state machine to test my function against an array of different memory settings, with 100 invocations each.

I have to provide a payload for the invocations. Luckily, this function doesn’t require anything from the payload, so I can send an empty JSON object.

It supports three different strategies: performance, cost and balanced. With the “balanced” strategy, I’m essentially asking for the memory setting that gives me the most bang for my buck.

Once the state machine execution finishes, you can check its output tab and find the best memory setting in the result.

With the visualization URL, I can see how the performance and cost of this function changed with each memory setting.

Lambda power tuning is a really powerful tool to have in your locker and when it’s used in the right circumstance it can give you significant cost savings.

But once again, you shouldn’t do this by default. Because there are no meaningful cost savings to be had in most Lambda functions and this cost saving is not free. You still have to work for it, including:

Here, you will find functions that have a meaningful cost and are allocated with more memory than it’s actually using, which you can see in the Avg. Memory column. These are the functions you should consider tuning.

As I mentioned earlier, you should also optimise functions that are using provisioned concurrency. Because the uptime cost for these functions can be significant. So using the wrong memory setting with these functions can have a huge cost implication.

Another way to cut down on Lambda execution costs is to switch to the ARM-based Graviton2 processor. It’s about 25% cheaper per millisecond of execution time compared to functions that are running on the x86 architecture.

Having said that, if your function is IO-heavy and spends most of its time waiting for responses from other AWS services such as DynamoDB, then they’re usually good candidates to switch to ARM. Because raw CPU performance is not going to have a big impact on the overall performance of the function in these cases.

Finally, another way to save money on Lambda is to use provisioned concurrency.

This might seem counter-intuitive at first, but let me explain.

With provisioned concurrency, the cost per millisecond of execution time is about 70% cheaper.

If you’re able to keep these provisioned concurrencies busy, say 60% of the time, then you’ll break even against the on-demand pricing. And anything beyond that 60% threshold would be cost savings.

This technique would only work for high throughput functions and there’s a potentially steep penalty if you get it wrong because of the aforementioned uptime cost. There are also operational overheads for using provision concurrency. So as far as optimising Lambda cost is concerned, I don’t recommend this technique unless you really know what you’re doing! Instead, use provisioned concurrency as a way to eliminate Lambda cold starts, as it’s intended for.

In the workshop, I will give you a quick introduction to AWS Lambda and the Serverless framework, and take you through topics such as:

and much more!

If you register early, you can also get 30% off general admission with our early bird tickets!

Add a comment

Related posts:

Hydrogen Peroxide and Water to Stimulate Growth and Fight Pests

As a self-proclaimed plant papi, one of the things I encounter very often with my myriad of plants…pests. Chances are you’ve also come across your fair share of little menaces with your own plants…

GUARD AGAINST DOMESTIC VIOLENCE DURING ISOLATION.

Weeks of isolation will increase tension between spouses and family members. How will you cope?

Ahn Bo Hyun

Aku adalah penikmat kdrama. Mungkin, banyak orang berpikir kdrama hanyalah sinetron remaja menye2 yang tidak pernah ada di kehidupan nyata. Tapi, menurutku seiring perkembangan jaman, kdrama sudah…