Questa è una versione PDF del contenuto. Per la versione completa e aggiornata, visita:
https://blog.tuttosemplice.com/en/serverless-finops-complete-guide-to-serverless-cost-optimization/
Verrai reindirizzato automaticamente...
In today’s cloud computing landscape, serverless cost optimization is no longer just about saving money, but a true engineering discipline necessary to ensure business sustainability. Let’s imagine the scenario of MutuiperlaCasa, a high-traffic mortgage comparison platform. Until yesterday, the infrastructure relied on clusters of always-on EC2 instances, sized to handle Monday morning traffic peaks, but largely underutilized during nights and weekends. The result? Wasted resources and an unsustainable OPEX (Operating Expense).
In this technical guide, we will analyze how adopting a FinOps mindset applied to a serverless architecture on AWS can radically transform the cost model, reducing expenses by up to 60% without compromising performance. We will explore advanced solutions for managing cold starts, intelligent workflow orchestration, and the use of spot computing capacity.
The first step towards serverless cost optimization is understanding where the inefficiency lies. In a traditional VM (Virtual Machines) based architecture, you pay for reserved capacity, regardless of actual usage. In a serverless model, you pay for the invocation and the duration of execution.
For MutuiperlaCasa, the migration involved breaking down the Java monolith into microservices based on AWS Lambda. However, a simple “lift-and-shift” of code into Lambda functions does not automatically guarantee savings. Without proper tuning, you risk spending even more due to incorrect memory configurations or prolonged execution times.
One of the main barriers to adopting Lambda for user-facing applications (like a real-time mortgage installment calculator) is the Cold Start problem. When a function hasn’t been invoked for a while, the cloud provider must initialize the execution environment, download the code, and start the runtime. For languages like Java (often used in the banking sector for its robustness), this can translate into latencies of several seconds.
To mitigate this problem without resorting to expensive Provisioned Concurrency (which effectively reintroduces a fixed cost similar to EC2), the winning strategy is using AWS Lambda SnapStart. This technology, available for managed Java runtimes, creates a snapshot of the initialized function’s memory and disk state and caches it.
Despite SnapStart, there are critical scenarios where variability is not tolerable. For MutuiperlaCasa‘s core services, such as the login API, a hybrid strategy can be adopted: use Application Auto Scaling to adjust Provisioned Concurrency based on predictable schedules (e.g., increase capacity at 08:00 and reduce it at 20:00). This balances performance guarantees with cost control.
A common mistake in serverless cost optimization is using Lambda functions to wait for responses from external services. In the case of MutuiperlaCasa, creditworthiness checks require queries to external systems (e.g., credit bureaus) that can take anywhere from 30 seconds to several minutes.
If we used a Lambda to wait for this response, we would pay for all that “idle” (waiting) time. The correct engineering solution is using AWS Step Functions.
To optimize costs, it is crucial to choose the correct workflow type:
By implementing Standard workflows for asynchronous calls, MutuiperlaCasa eliminated thousands of hours of “empty” Lambda execution, reducing the compute bill for these processes by 90%.
Not everything can be a Lambda function. Generating PDF contract reports or processing nightly logs are tasks that require long times and constant resources. This is where AWS Fargate, the serverless engine for containers, comes into play.
To maximize serverless cost optimization, the strategy involves the exclusive use of Fargate Spot. Spot instances leverage unused AWS capacity, offering discounts of up to 70% compared to On-Demand pricing.
The only downside of Spot instances is that they can be terminated with a two-minute warning if AWS needs the resources. To manage this in a production environment:
SIGTERM signal handler in the container to save state (checkpointing) to Amazon S3 or DynamoDB before shutdown.The rigorous application of these serverless cost optimization strategies led to the following results for MutuiperlaCasa:
Adopting serverless is not a magic wand for costs, but a powerful tool if governed by solid FinOps principles. The key to success lies in understanding the nuances of pricing models (such as the difference between paying for duration vs transition) and architecting software to leverage these differences. For fintech companies like MutuiperlaCasa, this approach not only frees up financial resources but allows for infinite scaling while maintaining healthy profit margins.
Serverless FinOps is a discipline that applies financial management principles to serverless cloud architectures, transforming cost optimization into an engineering practice. The main advantage lies in the shift from a fixed spending model based on reserved capacity to a pay-per-use model, where you pay only for the actual execution of code. This approach allows for eliminating waste for inactive resources and drastically reducing operating expenses, often by up to 60% compared to traditional infrastructures.
To avoid the fixed costs of Provisioned Concurrency while maintaining high performance, the best strategy is to use AWS Lambda SnapStart, especially for runtimes like Java. This technology creates and stores a snapshot of the initialized execution environment, allowing the function to start almost instantly upon request. In this way, minimal latencies are achieved by paying only for standard invocations, without having to keep instances always active.
Using Lambda functions to wait for responses from external services or long processes is financially inefficient because you pay for the entire time of idle waiting. AWS Step Functions, particularly with Standard Workflows, solves this problem by allowing execution to be paused without additional costs until an external response is received. You pay only for state transitions and not for the duration of the wait, generating significant savings on asynchronous processes.
AWS Fargate Spot is ideal for tasks that do not require immediate execution or absolute continuity, such as batch data processing, report generation, or log analysis. It offers discounts of up to 70% compared to On-Demand rates by leveraging unused cloud capacity. However, since instances can be interrupted with short notice, it is fundamental that applications are stateless and capable of saving their state to resume work in case of interruption.
A simple direct migration of code, known as «lift-and-shift», without adequate tuning can paradoxically increase costs instead of reducing them. The main risks include incorrect memory configurations that prolong billing duration and the improper use of Lambda functions for waiting tasks. To ensure savings, it is necessary to redesign the architecture by leveraging specific services for orchestration and optimizing code execution times.