Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is AWS Lambda good for real-time API Rest?

I'm learning about AWS Lambda and I'm worried about synchronized real-time requests. The fact the lambda has a "cold start" it doesn't sounds good for handling GET petitions.

Imagine a user is using the application and do a GET HTTP Request to get a Product or a list of Products, if the lambda is sleeping, then it will take 10 seconds to respond, I don't see this as an acceptable response time. Is it good or bad practice to use AWS Lambda for classic (sync responses) API Rest?

like image 633
Leandro Avatar asked Aug 28 '18 13:08

Leandro


2 Answers

As an AWS Lambda + API Gateway user (with Serverless Framework) I had to deal with this too.

The problem I faced:

  • Few requests per day per lambda (not enough to keep lambdas warm)
  • Time critical application (the user is on the phone, waiting for text-to-speech to answer)

How I worked around that:

The idea was to find a way to call the critical lambdas often enough that they don't get cold.
If you use the Serverless Framework, you can use the serverless-plugin-warmup plugin that does exactly that.
If not, you can copy it's behavior by creating a worker that will invoke the lambdas every few minutes to keep them warm. To do this, create a lambda that will invoke your other lambdas and schedule CloudWatch to trigger it every 5 minutes or so. Make sure to call your to-keep-warm lambdas with a custom event.source so you can exit them early without running any actual business code by putting the following code at the very beginning of the function:

if (event.source === 'just-keeping-warm) {
  console.log('WarmUP - Lambda is warm!');
  return callback(null, 'Lambda is warm!');
}

Depending on the number of lamdas you have to keep warm, this can be a lot of "warming" calls. AWS offers 1.000.000 free lambda calls every month though.

like image 42
Quentin Hayot Avatar answered Sep 28 '22 08:09

Quentin Hayot


Like most things, I think you should measure before deciding. A lot of AWS customers use Lambda as the back-end for their webapps quite successfully.

There's a lot of discussion out there on Lambda latency, for example:

  • 2017-04 comparing Lambda performance using Node.js, Java, C# or Python
  • 2018-03 Lambda call latency
  • 2019-09 improved VPC networking for AWS Lambda
  • 2019-10 you're thinking about cold starts all wrong

In December 2019, AWS Lambda introduced Provisioned Concurrency, which improves things. See:

  • 2019-12 AWS Lambda announces Provisioned Concurrency
  • 2020-09 AWS Lambda Cold Starts: Solving the Problem

You should measure latency for an environment that's representative of your app and its use.

A few things that are important factors related to request latency:

  • cold starts => higher latency
  • request patterns are important factors in cold starts
  • if you need to deploy in VPC (attachment of ENI => higher cold start latency)
  • using CloudFront --> API Gateway --> Lambda (more layers => higher latency)
  • choice of programming language (Java likely highest cold-start latency, Go lowest)
  • size of Lambda environment (more RAM => more CPU => faster)
  • Lambda account and concurrency limits
  • pre-warming strategy

Update 2019-12: see Predictable start-up times with Provisioned Concurrency.

Update 2021-08: see Increasing performance of Java AWS Lambda functions using tiered compilation.

like image 105
jarmod Avatar answered Sep 28 '22 08:09

jarmod