AWS Lambda is a compute service that runs code without provisioning or managing servers. AWS lambda executes the code only when needed and scales automatically, from a few requests per day to thousands per second. AWS charges only for the compute time the code consumes and there is no charge when the code is not running.
AWS Lambda functions need to be coded in languages supported by AWS Lambda (that is, Node.js, Java, C# and Python) and run within the AWS Lambda standard runtime environment and resources provided by Lambda. If we have to manage our own compute resources then Amazon Web Services provides other services like Amazon EC2 or Elastic Beanstalk. The intent of this blog is to compare the execution/response time of web services executed through Lambda function triggered by API Gateway and that of standalone web services executed in Amazon EC2 instance. The web service in EC2 is written in PHP and Lambda functions in Java.
Web service using API Gateway and AWS Lambda
Web service on Amazon EC2
Hardware and Software Specification
The response time graph for 100, 200 and 300 sample requests are as below. The graph results clearly indicates that the execution time for Lambda function triggered through API Gateway is less than that taken by standalone web service running in a AWS EC2 instance. Both are using the same RDS DB instance to fetch results. The test API fetches 40 records from 2 report tables using JOIN operations.
Figure 1 : 100 sample requests
Figure 2 : 200 sample requests
Figure 3 : 300 sample requests
The response time graph shows a steep increase in execution time as the number of requests increases for web service executed in AWS EC2. In case of AWS API Gateway the execution time is almost steady for most of the requests. But Lambda functions are quite fast when executed independently in AWS console. Enabling API cache in API Gateway, optimizing lambda runtime code, increasing memory allocated to execute lambda function might help in reducing the overall execution time. Enabling API cache and increasing memory will add to the AWS cost.
The testing results may vary based on the configuration changes/fine-tune done on the AWS EC2 instance or the API Gateway, Lambda function. For e.g. if API caching were implemented in AWS EC2 then response time results might vary for the above sample requests. API caching is available out of the box in API Gateway but in AWS EC2 it has be provisioned using external caching servers like Memcached or Redis.
Resources that can help you start, build and support your digital journey.Find out more
Let's craft delightful digital experiences together.
Tell us more about your vision.