Is AWS Lambda good for real-time API Rest?

Viewed 7791

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?

3 Answers

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:

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

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.

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.

We have used AWS Lambda quite successfully with reasonable and acceptable response times. (REST/JSON based API + AWS Lambda + Dynamo DB Access).

The latency that we measured always had the least amount of time spent in invoking functions and large amount of time in application logic.

There are warm up techniques as mentioned in the above posts.

Related