<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jiju Thomas Mathew</title>
    <description>The latest articles on DEV Community by Jiju Thomas Mathew (@jthoma).</description>
    <link>https://dev.to/jthoma</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F664563%2Ffe9cf53c-581f-4179-9544-28750ef69d86.png</url>
      <title>DEV Community: Jiju Thomas Mathew</title>
      <link>https://dev.to/jthoma</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jthoma"/>
    <language>en</language>
    <item>
      <title>From $40 to $7: How We Slashed Video Transcoding Costs with Serverless and Spot Instances</title>
      <dc:creator>Jiju Thomas Mathew</dc:creator>
      <pubDate>Fri, 24 May 2024 23:21:23 +0000</pubDate>
      <link>https://dev.to/jthoma/from-40-to-7-how-we-slashed-video-transcoding-costs-with-serverless-and-spot-instances-o6d</link>
      <guid>https://dev.to/jthoma/from-40-to-7-how-we-slashed-video-transcoding-costs-with-serverless-and-spot-instances-o6d</guid>
      <description>&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Challenge: Expensive and Inflexible Transcoding&lt;/strong&gt;&lt;/p&gt;

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

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

&lt;p&gt;&lt;strong&gt;The Solution: Serverless and Spot Instances to the Rescue&lt;/strong&gt;&lt;br&gt;
To address these challenges, we adopted a serverless architecture leveraging AWS Lambda functions and cost-effective EC2 Spot Instances. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;FTP File Processing &amp;amp; S3 Upload Lambda:&lt;/strong&gt; 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.&lt;br&gt;
&lt;strong&gt;Process Decider Lambda:&lt;/strong&gt; 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.&lt;br&gt;
&lt;strong&gt;Transcoder Lambda:&lt;/strong&gt; This Lambda utilizes a pre-compiled ffmpeg binary from a lambda layer to process smaller video files directly within the serverless environment.&lt;br&gt;
&lt;strong&gt;Spot Bidder Lambda:&lt;/strong&gt; 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.&lt;br&gt;
&lt;strong&gt;EC2 Spot Instance:&lt;/strong&gt; 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.&lt;br&gt;
DynamoDB: This NoSQL database stores detailed metadata about each processed video file, including processing time and completion status.&lt;br&gt;
SNS Topics: These topics are used for sending notifications regarding Lambda execution, spot instance launch/termination, and processing completion.&lt;br&gt;
IAM Roles: Granular IAM roles are assigned to each component, ensuring least privilege access and enhanced security.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9rmrex0483suwr17csgy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9rmrex0483suwr17csgy.png" alt="The architecture diagram" width="800" height="514"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Benefits Achieved: A Cost-Effective and High-Performance System&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The migration to a serverless architecture with spot instances yielded significant benefits:&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Lessons Learned: The Power of Serverless and Spot Instances&lt;/strong&gt;&lt;br&gt;
This migration project highlighted the power of serverless architectures and spot instances for cost-effective and scalable cloud solutions.&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Future Considerations: Continuous Improvement&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We're constantly striving to refine our system:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Error Handling and Retries&lt;/strong&gt;: Implementing robust error handling mechanisms with retries for failed processing attempts will further enhance system reliability.&lt;br&gt;
&lt;strong&gt;Monitoring and Logging&lt;/strong&gt;: Granular monitoring and logging across all components will provide valuable insights for troubleshooting and performance optimization.&lt;br&gt;
&lt;strong&gt;Testing and Scalability&lt;/strong&gt;: Regular stress testing under high loads ensures the system scales effectively and maintains performance during peak workloads.&lt;br&gt;
&lt;strong&gt;Conclusion: A Winning Transformation&lt;/strong&gt;&lt;br&gt;
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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

</description>
      <category>costcontrol</category>
      <category>performanceboost</category>
      <category>serverless</category>
    </item>
    <item>
      <title>Refactored a Lambda Heap to use Layers</title>
      <dc:creator>Jiju Thomas Mathew</dc:creator>
      <pubDate>Sat, 10 Jul 2021 05:54:10 +0000</pubDate>
      <link>https://dev.to/jthoma/refactored-a-lambda-heap-to-use-layers-og2</link>
      <guid>https://dev.to/jthoma/refactored-a-lambda-heap-to-use-layers-og2</guid>
      <description>&lt;p&gt;Till recently, in fact, till last week, was not too worried about writing all code into single code folder, and mapping multiple &lt;a href="https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-function.html"&gt;AWS::Serverless::Function&lt;/a&gt; into individual named handlers. Till I stumbled on this &lt;a href="https://medium.com/disney-streaming/a-betmanage-node-js-aws-lambda-b17df4ceb1b2"&gt;article&lt;/a&gt;, where I started wondering how my folder structure and sam templates were going into the stack. A detailed inspection was not required, though this was the time when I used the GUI ( after a long time ). But the outcome showed how pathetic the condition was.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SmJcGSJ8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dfo2xo8ds35dr0a06cys.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SmJcGSJ8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dfo2xo8ds35dr0a06cys.png" alt="Filtered view of mess"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Well, it is clear that the whole mess is being uploaded into all the function code. What does this mean – holy grail, any one small change here or there, would update all the functions – last modified is the same, all functions will have the node_modules and other artifacts like templates and custom modules.&lt;/p&gt;

&lt;p&gt;Well I started today to do the optimization, armed with the credits earned from the &lt;a href="https://aws.amazon.com/developer/community/community-builders/"&gt;AWS Community Builder&lt;/a&gt; Program, duplicated the codebase to another folder, and started the refactoring. I might have copied the template code from &lt;a href="https://docs.aws.amazon.com/lambda/latest/dg/nodejs-tracing.html"&gt;here&lt;/a&gt;, which explains my layer with a different description in the screenshot. It took me about 3 or 4 layer versions to strike the correct structure.&lt;/p&gt;

&lt;p&gt;Starting with lib/ then adding a nodejs/ beneath that, and finally moving node_modules into this folder. The structure is simple that our layer should have the runtime folder in which we can have the modules. Now was confused about how to make my custom modules, which were not available on any registry, to be available for the handlers to import using require. Just thought that by creating a folder inside node_modules and putting all my custom modules into that should work. Well, it worked, though some relative paths started breaking. Finally these also were resolved by using /opt/nodejs/node_modules/custom-module/ instead of ./ &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7mWicf5q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2wa4r5s9ig14ze3b14jg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7mWicf5q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2wa4r5s9ig14ze3b14jg.png" alt="Final filtered view"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--osKiAN15--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3g2dz46w3bt7osnzav0q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--osKiAN15--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3g2dz46w3bt7osnzav0q.png" alt="Layer Download"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Basically, the size difference is one big advantage, and when we run deploy for a minor change in any handler, instead of packing the whole whopping 500kb, the single handler is zipped and uploaded for the deployment. Also change in one handler will not affect the container version for other functions which could be a better tradeoff for heavily active applications taking advantage of reuse of active containers.&lt;/p&gt;

&lt;p&gt;Now considering to add one more layer with our custom modules, since these can get evolved and require slight modifications during further development. Currently since these are along with the main layer, changes could bring in the upload of the whole node_modules folder.&lt;/p&gt;

&lt;p&gt;After a round of smoke testing, the whole codebase was migrated back into the production system and deployed there. Thereafter deploy was completed, a full round of functional tests was run. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.jijutm.com/aws/refactored-a-lambda-heap-to-use-layers/index.html"&gt;Read the article at jijutm.com&lt;/a&gt;&lt;/p&gt;

</description>
      <category>serverless</category>
      <category>node</category>
    </item>
  </channel>
</rss>
