Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a high performance web service from Nginx on AWS EC2 to AWS Lambda

On a project I’m working on, there are a number of web services implemented on AWS. The services that are relatively simple (DynamoDB insert or lookup) and will be used relatively infrequently have been implemented as Lambdas, which were perfect for the task. There is also a more complex web service which does a lot of string processing and regex matching which needs to be highly performant, that has been implemented in C++ (roughly 5K LOC) as a Nginx module and can handle in the region of 20K requests/s running on an EC2 instance (the service just takes in a small JSON payload, does a lot of string processing and regex matching against some reference data that sits in static data files on S3, and returns a JSON response under 1KB in size)

There is a push from management to unify our use of AWS services and have all the web services implemented as Lambdas.

My question is: can a high performance web service such as the C/C++ nginx compiled module running on EC2 that’s expected to run continuously and handle 20K to 100K req/s actually be converted to AWS Lambda (in Python) and expected to have the same performance or is this better left as is on EC2? What are the performance considerations to be aware of if converting to Lambda?

like image 634
Jupiterese Avatar asked Feb 05 '23 11:02

Jupiterese


1 Answers

Can Lambda do it? Yes. Should Lambda do it? No.

Why? Cost.

First, let's say you do handle 20k Requests / Second, every second for an entire day. That will then equate to 1.728 Billion requests in that day. In the free tier, you do get 1 Million requests free, so that drops the billable requests down to 1.727 Billion. Lambda charges $0.20 / Million Requests, so:

1.728 Billion requests * $0.20 / Million requests = $345.40

I'm pretty sure your cost for EC2 is lower than that per day. Taking the m4.16xlarge instance, with on-demand pricing, we get:

$3.20 / Hour * 24 Hours = $76.80

See the difference? But, Lambda also charges for compute time!

Let's say you include the c++ executable in your Lambda function (called from Python or Node, so we won't take into account the performance hit going from c++ to an interpreted language. Since Lambda charges in 100 Millisecond blocks, rounded up, for this estimate we will assume that all the requests finish within 100 Milliseconds.

Say you use the smallest memory size, 128 MB. That will give you 3.2 Million Seconds within the free tier, or 32 Million Requests given that they are all under 100 Milliseconds, free. But that still leaves you with 1.696 Billion Requests billable. The cost for the 128 MB size is $0.000000208 / 100 Milliseconds. Given that each request finishes under 100 Milliseconds, the cost for the execution time will be:

$0.000000208 / 100 Milliseconds * 1.696 Billion 100 Millisecond Units = $352.77

Adding that cost to the cost of the requests, you get:

 $345.40 + $352.77 = $707.17

EC2: $76.80
Lambda: $707.17

Note, this is just using the 20k Requests / Second number that you gave and is for a single day. If the actual number of requests differs, the requests take longer than 100 Milliseconds, or you need more memory than 128 MB, the cost estimate will go up or down accordingly.

Lambda has its place, but EC2 does also. Just because you can put it on Lambda doesn't mean you should.

like image 50
Jacob Lambert Avatar answered Feb 07 '23 08:02

Jacob Lambert