When talking about serverless, two advantages immediately pop up:
These advantages are mainly referred to as “FaaS” (Function-as-a-Service), which many cloud providers are now offering, such as AWS Lambda, Azure Functions, Google Cloud Functions and more.
Serverless is much more than FaaS — our managed database service that is responsible for scaling, encryption, backups, performance, logs and more IS Serverless. The same applies to managed queues, storages, web APIs and other services.
Ultimately, every application is comprised of 3rd party APIs. They are great because they allow us to focus on our product development, rather than implementing an in-house solution that someone has already implemented before — and probably better than what we can do. And the best part — we care neither about scaling nor about the infrastructure.
Such 3rd party APIs can provide us with user management (Auth0), payments (Stripe), communication (Twilio), and there are much more out there.
In serverless, there’s one major caveat regarding APIs. When we pay-per-use, or specifically, we pay for the amount of time that our code is running, fast interactions with APIs are crucial! We will need to measure the performance in order to understand the impact on our cost and our customer’s experience.
Let’s start with a quick example — setting up a Lambda function in Python, that returns the user details for a given ID. For user management and authentication we are going to use Auth0.
Our main handler.py that represents the Lambda function looks like this:
Deployment is being done using the Serverless framework. You can see the full code in GitHub.
To get the idea of how long it takes to get the response from Auth0 we can add time prints. That is very hard to maintain and analyze. As we said, we prefer a managed service that will take care of it. We will use a performance monitoring solution for Serverless — Epsagon. Once we set up the library in our code, we will get a full visualization of this function’s trace:
We can observe that it takes ~600ms for Auth0 to get the response from Auth0. Running this operation several more times yields a result of ~500ms on average. The additional idle time that we wait for the response, affecting us in a higher cost, and user experience.
At this point, there are few things we can do in order to optimize the performance:
For every component in the application’s architecture, there are several APIs to choose from. This list summarizes some key metrics to evaluate:
Serverless is much more than functions — this is our entire application with managed resources and 3rd party APIs.
When integrating these with functions, it is important to benchmark their performance with the external resources and APIs and understand their impact on both on our cost and on customers experience.
Originally published at blog.epsagon.com.