DEV Community

Jiju Thomas Mathew
Jiju Thomas Mathew

Posted on

From $40 to $7: How We Slashed Video Transcoding Costs with Serverless and Spot Instances

We all know the struggle of managing cloud costs, especially when it comes to workloads with fluctuating demands. In this blog post, I'll share how we successfully migrated our video transcoding system from a costly EC2 on-demand setup to a serverless architecture with spot instances, achieving a dramatic reduction in monthly bills while significantly improving performance.

The Challenge: Expensive and Inflexible Transcoding

Our previous video transcoding system utilized a continuously running EC2 instance to process video files uploaded via FTP. This approach had several drawbacks:

High Cost: Even during idle periods with no video uploads, the EC2 instance incurred a recurring monthly cost of around $40.
Limited Scalability: The single instance couldn't automatically scale to handle bursts of video uploads, leading to processing delays.
Slow Processing Times: Videos could take up to an hour to process due to the fixed resources of the EC2 instance.
High Error Rate: We experienced a concerning 10% error rate during video processing.

The Solution: Serverless and Spot Instances to the Rescue
To address these challenges, we adopted a serverless architecture leveraging AWS Lambda functions and cost-effective EC2 Spot Instances.

FTP File Processing & S3 Upload Lambda: This Lambda is on scheduled trigger checks for video file upload to the FTP server. It retrieves the file, copies it to a designated S3 bucket, and adds metadata for tracking.
Process Decider Lambda: This Lambda analyzes the uploaded video file. If the file size is below a certain threshold, it triggers the Transcoder Lambda for processing. For larger files, it initiates the Spot Bidder Lambda.
Transcoder Lambda: This Lambda utilizes a pre-compiled ffmpeg binary from a lambda layer to process smaller video files directly within the serverless environment.
Spot Bidder Lambda: This Lambda estimates the processing time for the large video file and analyzes spot instance pricing trends across different regions. The file information is pushed into an SQS Queue. It then requests a spot instance with the most competitive pricing to handle the transcoding task.
EC2 Spot Instance: Upon launch, the spot instance retrieves processing details from an SQS queue and utilizes ffmpeg to transcode the large video file. Once completed, it updates the SQS queue and terminates itself.
DynamoDB: This NoSQL database stores detailed metadata about each processed video file, including processing time and completion status.
SNS Topics: These topics are used for sending notifications regarding Lambda execution, spot instance launch/termination, and processing completion.
IAM Roles: Granular IAM roles are assigned to each component, ensuring least privilege access and enhanced security.

The architecture diagram

Benefits Achieved: A Cost-Effective and High-Performance System

The migration to a serverless architecture with spot instances yielded significant benefits:

Drastic Cost Reduction: Our monthly bill plummeted from $40 to a mere $7, a remarkable 82.5% cost saving!
Automatic Scaling: Lambda functions and spot instances automatically scale based on workload, eliminating idle costs and ensuring efficient resource utilization.
Blazing-Fast Processing: Video processing times are now down to a single minute, a significant improvement over the previous one-hour wait.
Near-Zero Error Rate: Our error rate has practically vanished thanks to the inherent reliability of serverless functions and spot instances.

Lessons Learned: The Power of Serverless and Spot Instances
This migration project highlighted the power of serverless architectures and spot instances for cost-effective and scalable cloud solutions.

Event-driven architecture: Utilizing event triggers for Lambda functions streamlines the workflow and ensures resources are used only when needed.
Process Splitting: Dividing the work into smaller, independent Lambdas enhances modularity and facilitates individual scaling for each process.
Intelligent Decision Making: Leveraging Lambdas for file size analysis and spot instance cost optimization automates decision-making and minimizes resource costs.

Future Considerations: Continuous Improvement

We're constantly striving to refine our system:

Error Handling and Retries: Implementing robust error handling mechanisms with retries for failed processing attempts will further enhance system reliability.
Monitoring and Logging: Granular monitoring and logging across all components will provide valuable insights for troubleshooting and performance optimization.
Testing and Scalability: Regular stress testing under high loads ensures the system scales effectively and maintains performance during peak workloads.
Conclusion: A Winning Transformation
The migration of our video transcoding system from EC2 on-demand to a serverless architecture with spot instances proved to be a resounding success. We achieved significant cost savings, improved processing speed and reliability, and gained a highly scalable solution. This case study demonstrates the potential of serverless architectures and spot instances for optimizing cloud resource utilization and managing costs effectively.

If you're considering a similar migration, feel free to leave a comment below or contact me directly for further details. We're happy to share our learnings and help you embark on your own cloud optimization journey.

Top comments (0)