CaseStudy: When serverless is not the best solution

Saad Afzal

Saad Afzal

· 5 min read
AWS Lambda vs EC2

In today's cloud-centric world, companies have multiple choices for deploying their applications. Two popular options in the AWS ecosystem are EC2 instances and AWS Lambda. While serverless architectures, such as Lambda, are attractive due to their simplicity and automatic scaling, there are scenarios where traditional server-based approaches like EC2 are more economical and efficient. This blog post delves into a real-world example from the industry where EC2 was chosen over Lambda due to economic reasons and the ability to handle high traffic as estimated by a major news agency.

The Scenario: A News Aggregator Platform

Imagine a news aggregator platform that collects and disseminates news articles from various sources. The platform is designed to handle high volumes of traffic, especially during peak news events such as elections, natural disasters, or major sports events. The platform's traffic can surge unexpectedly, and it needs to provide a seamless user experience without latency or downtime.

The Challenge: Handling Unpredictable High Traffic

One of the key challenges faced by the platform is managing the unpredictability of traffic spikes. The platform's architecture must be robust enough to handle these spikes without incurring excessive costs. AWS Lambda, with its serverless architecture, seemed like a perfect fit initially due to its automatic scaling and pay-as-you-go pricing model.

However, a detailed analysis and forecast by the Finish News Agency revealed some critical insights:
- During major news events, traffic could surge to millions of requests per second.
- Sustained high traffic for prolonged periods, especially during global news events, would lead to exorbitant costs if handled by Lambda.

Cost Analysis: EC2 vs. Lambda

To make an informed decision, the platform team conducted a cost analysis comparing EC2 instances with AWS Lambda under high-traffic conditions. Here are the key findings:

1. Lambda Pricing Model:
- AWS Lambda charges based on the number of requests and the duration of execution.
- High traffic scenarios with millions of requests can lead to significant costs.
- The platform would incur additional costs for storage and data transfer, further escalating expenses.

2. EC2 Pricing Model:
- EC2 instances have a fixed hourly rate.
- Reserved instances or spot instances could be used to further reduce costs.
- With auto-scaling groups, EC2 can handle variable traffic efficiently, scaling up during peak times and scaling down during low traffic periods.

The Decision: Choosing EC2

Given the cost implications and the ability to predict and handle traffic spikes, the platform team decided to go with EC2 instances. Here's why:

- Predictable Costs: EC2 instances provided a more predictable cost structure. With reserved instances, the team could budget effectively for peak and off-peak times.
- Scalability: By leveraging auto-scaling groups, EC2 could dynamically adjust the number of instances based on real-time traffic, ensuring high availability and performance.
- Economic Efficiency: For sustained high traffic, EC2 proved to be more economical compared to Lambda. The cost per request significantly decreased with EC2, especially when traffic was consistently high.

Implementation: High Traffic Estimation and Scaling

The platform team collaborated with the Finish News Agency to develop an estimation model for traffic based on historical data and real-time analytics. This model helped in:

- Forecasting Traffic Spikes: Predicting when traffic would surge allowed the team to proactively scale the EC2 instances.
- Optimizing Resource Allocation: Ensuring that there were enough instances during peak times and scaling down during low traffic periods to save costs.
- Monitoring and Alerts: Implementing monitoring tools to keep track of traffic patterns and setting up alerts for any unexpected spikes.

Conclusion

While serverless architectures like AWS Lambda offer flexibility and automatic scaling, they are not always the most cost-effective solution for every scenario. In cases of high and sustained traffic, as demonstrated by our news aggregator platform, EC2 instances can provide better economic efficiency and predictable cost management. By leveraging traffic forecasts from the Finish News Agency, the platform was able to implement a scalable and cost-effective solution with EC2.

This blog underscores the importance of understanding your application's traffic patterns and conducting a thorough cost analysis before choosing a cloud architecture. Each use case is unique, and the best choice often depends on a balance between cost, scalability, and performance requirements.

Saad Afzal

About Saad Afzal

With a Master's degree in Structural Engineering, I began my journey in engineering consultancy, where I discovered my passion for automation and software development. As I delved deeper, I integrated Python scripts into my workflows, revolutionising structural design and analysis.

Driven by a desire to embrace the scalability of web applications, I transitioned into full-stack development and cloud engineering. Through relentless self-study, I honed my skills and collaborated with esteemed organizations to develop cutting-edge solutions. Today, I specialize in architecting robust systems and leveraging cloud technologies to create scalable, secure, and user-centric applications.

Copyright © 2024 CodingStruct. All rights reserved.
Made by Saad Afzal· Github