AWS Step Function vs. AWS Lambda - Part 2
Tags: serverless, aws, lambda, stepfunction
Published: 2021-11-25 13:49:46

Do you also feel the tension of the cover image? It is time for a battle again. 🥊 After I published [the first part](https://dev.to/aws-builders/aws-step-function-vs-aws-lambda-benchmark-54hj) of my comparison, I was overwhelmed about the amount of feedback I received. May it be comments on my post, discussions on twitter or LinkedIn.

The fact that the initial post triggered a lot of inspiring discussions is very valuable. While reading through your feedback it was kind of obvious that there is a need for a second part. 

I received a lot of feedback about optimizations for AWS Lambda and that people are curious how this affects the performance in comparison to our state machine. We will also take a closer look on the perspective of costs to get a more complete view how the services differ.

Here we are. 

Like in our first part, again all experiments are triggered using [Apache Bench](https://httpd.apache.org/docs/2.4/programs/ab.html) with the following parameters.

`ab -n 15000 -c 1 https://hash.execute-api.eu-central-1.amazonaws.com/.../`

`-n` configures the total amount of requests that are triggered - in our case 15.000
`-c` is the number of concurrent requests - in our setup 1

⚠️ **IMPORTANT:** it is important to consider, that the results from apache-bench are not 100% accurate. The measured throughput depends on the hardware and network capabilities of my local workstation. For upcoming benchmarks, I consider to use something like CloudShell. 
But apache-bench gives some very early feedback and potential indications. Hence we use these results in combination with the Lambda duration and Step-Function execution duration.

## 🔋 Optimizing our Lambda function

So what is the goal of our upcoming experiments? We want to 
apply some optimizations on our Lambda function with a clear focus to decrease latencies. Based on the feedback I got, there were two main approaches for optimization:

1. Reusing downstream http connections by activating keep-alive settings.
2. Improving overall execution performance by increasing the allocated memory.

### Reusing Connections with Keep-Alive in Node.js

For short-lived operations, such as in our case writing and reading to and from S3, the latency overhead of setting up a TCP connection might be greater than the operation itself. To activate http keep-alive you simply have to set an environment variable in your Lambda function configuration.

```
Environment:
  Variables:
    AWS_NODEJS_CONNECTION_REUSE_ENABLED: 1
```
In case you already use v3 of the AWS JS SDK, this setting is [enabled by default](https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/node-reusing-connections.html). For v2 you have to [explicitly activate it](https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/node-reusing-connections.html).

Let us deploy the change and start our first test. Let us first start with analyzing the Apache Bench reports. The complete reporting is available on [GitHub](https://github.com/cremich/aws-sf-lambda-benchmark/tree/main/benchmark). Here some highlights:

* The Lambda function was able to process all requests 43 seconds faster compared to the state machine.
* Both the state machine and the Lambda function were able to process round about 7 requests per second
* The mean time per request for the Lambda function was 131ms and 134ms for state machine.

Looking at these results, this little tweak of activating TCP keep-alive helped a lot to speed up the Lambda function. In terms of end-2-end performance and latency, both solutions are now very close to each other.

Let us take a closer look into CloudWatch and X-Ray to confirm the observations.

![latencies with keep-alive](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aysa1t8hlox1s73vb6gd.png)

The average execution time of the state-machine is 46.4ms and Lambda performs with 49ms.

![x-ray service map with keep alive](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nt9uhcwylc7ujpjnr8ow.png)
 
Here things are still looking interesting. The Lambda function duration on average still has some up and downs during the execution of the test while the duration of the state-machine is stable. Both solutions show some cold-start behavior while it seems that the state-machine needs less time to become "warm".

But in total the impact on the Lambda function performance is very impressive compared to the results in the first part.

### Give the Lambda function some RAM

But the question is: how much memory does my Lambda function need? The range is quite large from 128 MB to 10.240 MB. 
There is an awesome open source tool called "[Lambda Power Tuner](https://github.com/alexcasalboni/aws-lambda-power-tuning)" that helps you to determine your memory settings based on different strategies like speed, cost or balanced.

> If you use "cost" the state machine will suggest the cheapest option (disregarding its performance), while if you use "speed" the state machine will suggest the fastest option (disregarding its cost). When using "balanced" the state machine will choose a compromise between "cost" and "speed"

Source: [Lambda Power Tuner @ AWS Serverless Application Repository](https://serverlessrepo.aws.amazon.com/applications/arn:aws:serverlessrepo:us-east-1:451282441545:applications~aws-lambda-power-tuning)

In my case the "Lambda Power Tuner" suggested 256 MB as "Best cost" and 2048 MB as "Best Time". 

![lambda-power-tuner-output](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i0v4xxyzdlvi7x79gfvc.png)

Awesome, now we have a good start for the final tests.

#### Best time setting

As we aim to reduce latency, let us first start with the proposed "Best Time" setting of 2048 MB memory and let us have a look at the apache-bench metrics:

* The Lambda function was able to process all requests 81 seconds faster compared to the state machine.
* Both the state machine and the Lambda function were able to process round about 8 requests per second
* The mean time per request for the Lambda function was 121ms and 127ms for state machine.

Compared to our first test, there are some improvement but they seem to be marginally on average. Let us try to get some more insights using CloudWatch and X-Ray.

![cloudwatch-latencies-2048](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1mf2qilnwy8s087eeldh.png)
 
For the most parts, the duration of the Lambda function is just below the execution time of the state-machine. 
The average execution time of the state-machine is 45.1ms and Lambda shines with 41.8ms.

![xray-service-map-2048](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ma1m8l4tv1x4m9emw9q4.png)

What would happen, if we set our memory configuration to the setting considered as "Best cost"? Let us review the results in the next chapter.

#### Best cost setting

In short again our apache-bench metrics:
* The Lambda function was able to process all requests 155 seconds faster compared to the state machine.
* The state machine was able to process 7.5 requests per second while the Lambda function processes 8 requests per second
* The mean time per request for the Lambda function was 122ms and 132ms for state machine.

CloudWatch and X-Ray results also confirm very close results. 

![cloudwatch-256](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vlbnp90x0645ljlc2jjq.png)

The average execution time of the state-machine is 54.8ms and Lambda is just in the lead with 50.5ms.

![xray-256](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xjifyz7mgjxujcivhhse.png)

## 💰 Cost comparison

Based on the scale of my test, the AWS Cost Explorer was not really helpful as the load I generated was too low. The AWS calculator is a helpful tool to better compare the costs of both services. 

The estimate is [publicly available](https://calculator.aws/#/estimate?id=16d3b9fd0f064aac0f7d743fb47ad2b2044ad91e) if you want to have a detailed look. 

I calculated with 5 million invocations per month per service. Based on our test results, I was able to determine very precise values for the parameter that influence pricing like Lambda invocation duration/state-machine execution or consumed memory. The monthly costs are:

* 8 USD for AWS Lambda with 2048MB memory (Best time)
* 1.83 USD for AWS Lambda with 265MB memory (Best cost)
* 5.52 USD for the AWS Step Function express workflow

## 💡 Conclusion

In this part we covered some important aspects like options to improve the performance of a Lambda function. I think it is again very important to mention, that this benchmark should not be interpreted as "use Step Functions whenever you can". 

My goal was more to raise discussions about the importance to not build you decision based on hypothesis or rumors. Make your decision based on data to make the best of all kind of  decisions you can make.

I would again like to point out a quote from [Eric Johnson](https://twitter.com/edjgeek) at the [serverless office hours](https://www.youtube.com/watch?v=zdmCYPvOHoo):

> Use Lambda to transform not to transport

Or in my words: the best code is the code that is never written.

☝️ And here come the thing and this is very important to keep in mind:

BOTH SERVICES ARE AWESOME.

If you have the need to write a Lambda function, you will be able to solve a lot of problems. But depending on what you want to achieve, Step Functions give you a lot of power to get the same results without writing ANY line of code, while making up your mind about things like TCP keep-alive or how to figure out what the best memory setting is. In all tests, AWS Lambda showed the well-known cold-start behavior that is something you should keep in mind. AWS Step Function also needs some warm-up time but it is not really comparable to AWS Lambda cold-starts. There was an interesting discussion around this on twitter:

{% twitter 1453733187666857985 %}

It only remains to say: happy coding AND happy orchestrating! 🥳 I really hope that my analysis and the approach to decision-making helps you in deciding towards or against one of these services for your individual use cases.

---
About the author: 

👋 Hi my name is Christian. I am working as an AWS Solution Architect at [DFL Digital Sports GmbH](https://www.dfl.de/en/about/subsidiaries/about-dfl-digital-sports-gmbh/). Based in cologne with my beloved wife and two kids. I am interested in all things around ☁️ (cloud), 👨‍💻 (tech) and 🧠 (AI/ML).

With 10+ years of experience in several roles, I have a lot to talk about and love to share my experiences. I worked as a software developer in several companies in the media and entertainment business, as well as a solution engineer in a consulting company. 

I love those challenges to provide high scalable systems for millions of users. And I love to collaborate with lots of people to design systems in front of a whiteboard.

You can find me on [LinkedIn](https://www.linkedin.com/in/christian-bonzelet/) or [Twitter](https://www.twitter.com/cremich)

---
Cover Image by <a href="https://tenor.com/users/miguelangelvenegasgordillo">miguelangelvenegasgordillo</a> on <a href="https://tenor.com/view/civilwar-captainamerica-gif-14387051">Tenor</a>