Have you deployed your Serverless Web Application in AWS using CloudFront, API Gateway, Lambda and still finds it’s not performing up to the expectations?
This article focuses on 5 Tips to improve Serverless Web Application Performance in general.
AWS Lambda function has a considerable cold start time. Even though it reduces when you increase the memory of individual functions, still it could provide a noticeable effect for your web application.
Implement a mechanism to keep Lambda functions WARM. You can do this using either of the following approaches.
If you have deployed your frontend (Angular, ReactJS, VueJS App) to S3 and serves the API using API Gateway you will come across CORS (Cross-Origin Resource Sharing). This is not a bad thing, but the problem is when you send headers like Authorization (Or Anything other than the basic headers), modern Browser sends a special request first called Preflight Request before sending the actual request.
This creates a waterfall effect where it adds twice the latency for the full request fulfillment. To avoid this, you can use AWS CloudFront and Plugin API Gateway and S3 both to the same domain and configure the routing there.
When connecting API Gateway to CloudFront, make sure you do the following
When you deploy your API Gateways, it internally attaches a CloudFront distribution to it. This creates a latency since it adds another hop for request forwarding.
You can set the endpoint type to REGIONAL to deploy them as regional endpoints to avoid this.
Even you use AWS CloudFront to serve the Frontend, you can further improve the performance by compressing the files such as JavaScript, CSS using GZIP and Upload to S3. Also make sure you use the correct Content-Encoding: gzip headers. This will reduce the required bandwidth to download the files in several factors.
In addition, enable Compress Objects Automatically in CloudFront Behavior configuration to automatically compress content for web requests that include Accept-Encoding: gzip in the request header. This allows CloudFront to compresses files of certain types for both Amazon S3 and custom origins.
Since CloudFront uses the best effort (Not guaranteed) when severs are not busy at Edge locations to compress the files to GZIP, storing the files compressed can provide better performance.
When you are using smaller memory for Lambda (e.g; 128MB) it uses, lower speed CPU also matching the memory. This can have adverse effects on your Lambda performance. Therefore it is important to increase memory (At least 512MB for production applications) appropriately based on the logic handled by the function.