<?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: Numerica Ideas</title>
    <description>The latest articles on DEV Community by Numerica Ideas (@numericaideas).</description>
    <link>https://dev.to/numericaideas</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%2F1031746%2F0c249b71-b2eb-4835-b76d-e79dc4239b82.jpg</url>
      <title>DEV Community: Numerica Ideas</title>
      <link>https://dev.to/numericaideas</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/numericaideas"/>
    <language>en</language>
    <item>
      <title>Strategies for Optimizing AWS Lambda Cold Starts</title>
      <dc:creator>Numerica Ideas</dc:creator>
      <pubDate>Sun, 30 Apr 2023 18:28:31 +0000</pubDate>
      <link>https://dev.to/numericaideas/strategies-for-optimizing-aws-lambda-cold-starts-4k27</link>
      <guid>https://dev.to/numericaideas/strategies-for-optimizing-aws-lambda-cold-starts-4k27</guid>
      <description>&lt;p&gt;This article was originally authored by "Ismael Messa" on NumericaIdeas's &lt;a href="https://medium.com/r/?url=https%3A%2F%2Fblog.numericaideas.com%2Flambda-cold-starts-optimization-strategies"&gt;blog&lt;/a&gt;:&lt;/p&gt;


&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://blog.numericaideas.com/lambda-cold-starts-optimization-strategies" class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://res.cloudinary.com/practicaldev/image/fetch/s--9yjFWL_D--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://assets.numericaideas.com/2023/02/Lambda-Optimization-Strategies.png" height="450" class="m-0" width="800"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://blog.numericaideas.com/lambda-cold-starts-optimization-strategies" rel="noopener noreferrer" class="c-link"&gt;
          Lambda Cold Starts Optimization Strategies
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;p class="truncate-at-3"&gt;
          Let's dive into the different strategies to reduce AWS Lambda functions Cold Starts: SnapStart, Provisioned Concurrency, and Custom Warmer.
        &lt;/p&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
        blog.numericaideas.com
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;


&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Cold Starts&lt;/strong&gt;, which refer to the delay in starting a Lambda invocation, are a common issue encountered by &lt;strong&gt;Serverless&lt;/strong&gt; platforms. In situations where low latency is essential, Cold Starts can disrupt the smooth operation of workloads. To address this problem, various strategies have been developed, including &lt;strong&gt;Lambda SnapStart&lt;/strong&gt;, &lt;strong&gt;Provisioned Concurrency&lt;/strong&gt;, and &lt;strong&gt;Custom Warmer&lt;/strong&gt;, each with its own approach. This article aims to compare and contrast these three strategies based on various factors.&lt;/p&gt;

&lt;p&gt;The YouTube Channels in both English (En) and French (Fr) are now accessible, feel free to subscribe by clicking &lt;a href="https://www.youtube.com/@numericaideas/channels?sub_confirmation=1"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lambda SnapStart
&lt;/h2&gt;

&lt;h3&gt;
  
  
  How it Works
&lt;/h3&gt;

&lt;p&gt;As previously mentioned, &lt;strong&gt;SnapStart&lt;/strong&gt; is a performance optimization technique designed to reduce the initialization time of a Lambda function. This strategy is fully supported by AWS and works by creating a snapshot of the function during the version release process. When the function is subsequently invoked, the cached version is reused, effectively preventing Cold Starts. By using SnapStart, &lt;strong&gt;Cold Starts&lt;/strong&gt; can be improved by up to &lt;strong&gt;90%&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Pricing
&lt;/h3&gt;

&lt;p&gt;SnapStart is a &lt;strong&gt;free&lt;/strong&gt; feature and does not require any additional cost.&lt;/p&gt;

&lt;h3&gt;
  
  
  Supported Runtime
&lt;/h3&gt;

&lt;p&gt;It's available for only Java(11) runtime (Correto) at the moment.&lt;/p&gt;

&lt;h3&gt;
  
  
  Complexity to Set Up
&lt;/h3&gt;

&lt;p&gt;It can be accessed via the &lt;strong&gt;AWS console&lt;/strong&gt; and does not necessitate any modifications to your source code. Simply activate the feature and let it work its magic.&lt;/p&gt;

&lt;h3&gt;
  
  
  Limit
&lt;/h3&gt;

&lt;p&gt;One implication of using SnapStart's snapshot resuming approach is that ephemeral data or credentials may not have any expiry guarantees. This means that if your code utilizes a library that generates an expiring token at the function level, it could expire when a new instance of the function is launched via SnapStart.&lt;/p&gt;

&lt;p&gt;Moreover, if your code establishes a long-term connection to a network service during the &lt;strong&gt;init phase&lt;/strong&gt;, the connection will be lost during the invocation process.&lt;/p&gt;

&lt;p&gt;Here's a more detailed article that covers its impacts and how to set it up:&lt;br&gt;
&lt;/p&gt;
&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://blog.numericaideas.com/lambda-performance-improvement-with-snapstart" class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://res.cloudinary.com/practicaldev/image/fetch/s--6RDmJ8dp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://assets.numericaideas.com/2022/12/NI-Lambda-SnapStart.png" height="450" class="m-0" width="800"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://blog.numericaideas.com/lambda-performance-improvement-with-snapstart" rel="noopener noreferrer" class="c-link"&gt;
          Lambda Performance Improvement with SnapStart
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;p class="truncate-at-3"&gt;
          SnapStart is a performance optimization option that helps to reduce the initialization time of Lambda functions at no additional costs.
        &lt;/p&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
        blog.numericaideas.com
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;


&lt;h2&gt;
  
  
  Provisioned Concurrency
&lt;/h2&gt;

&lt;h3&gt;
  
  
  How it Works
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Provisioned Concurrency&lt;/strong&gt; is an AWS feature that keeps your function warm and ready to respond in a matter of milliseconds at the scale you specify. With this feature enabled, you can select the number of instances of your function that run concurrently to handle incoming requests, rather than relying on Lambda to launch new instances as requests arrive (in-demand).&lt;/p&gt;

&lt;p&gt;The distinctive aspect of Provisioned Concurrency is its rapid startup time, which is attributed to the fact that all setup processes, including the initialization code, occur before invocation. This ensures that the function remains in a state where your code is downloaded and the underlying container structure is configured. It is worth noting that this feature is only available with published versions or aliases of your function.&lt;/p&gt;

&lt;h3&gt;
  
  
  Pricing
&lt;/h3&gt;

&lt;p&gt;There are additional costs related to it :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You pay for &lt;strong&gt;how long&lt;/strong&gt; provisioned capacity is active.&lt;/li&gt;
&lt;li&gt;You pay &lt;strong&gt;how many&lt;/strong&gt; concurrent instances should be available.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Supported Runtime
&lt;/h3&gt;

&lt;p&gt;It is available for all runtimes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Complexity to Set Up
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;Provisioned Concurrency&lt;/strong&gt; option can be accessed via various channels, including the AWS Console, Lambda API, AWS CLI, AWS CloudFormation, or Application Auto Scaling, and does not require any modifications to the existing source code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Limit
&lt;/h3&gt;

&lt;p&gt;Provisioned Concurrency is not supported with &lt;a href="https://aws.amazon.com/lambda/edge/"&gt;Lambda@Edge&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Custom Warmer
&lt;/h2&gt;

&lt;h3&gt;
  
  
  How it Works
&lt;/h3&gt;

&lt;p&gt;The &lt;strong&gt;Custom Warmer&lt;/strong&gt; strategy aims to prevent &lt;strong&gt;Cold Starts&lt;/strong&gt; by keeping the function warm through a &lt;strong&gt;pinging mechanism&lt;/strong&gt;. This is achieved by utilizing &lt;a href="https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-rules.html"&gt;AWS EventBridge Rules&lt;/a&gt; to schedule function invocations at regular intervals. By selecting a specific frequency, typically every &lt;strong&gt;15 minutes&lt;/strong&gt;, the function is automatically triggered, ensuring that it remains warm.&lt;/p&gt;

&lt;p&gt;Also, it's generally implemented by some open-source libraries but you are free to built a custom one manually.&lt;/p&gt;

&lt;h3&gt;
  
  
  Pricing
&lt;/h3&gt;

&lt;p&gt;No cost is required! There are no additional charges for rules using &lt;strong&gt;Amazon EventBridge&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Supported Runtime
&lt;/h3&gt;

&lt;p&gt;You can use it with any runtime you need.&lt;/p&gt;

&lt;h3&gt;
  
  
  Complexity to Set Up
&lt;/h3&gt;

&lt;p&gt;To implement a Warming strategy, some changes to the source code are necessary since the Warmer triggers a function invocation after a specific period.&lt;/p&gt;

&lt;p&gt;The function needs to identify whether it is a call from the Warmer and adjust its behavior accordingly, as demonstrated in the following example:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ol5fUpr7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/267sfhy9ewkszuxf35q3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ol5fUpr7--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/267sfhy9ewkszuxf35q3.png" alt="Warmer Call Checker - NodeJS" width="800" height="342"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A sample implementation is available in the following &lt;a href="https://github.com/numerica-ideas/ni-microservice-nodejs"&gt;repository&lt;/a&gt; with the &lt;strong&gt;NPM&lt;/strong&gt; script &lt;code&gt;npm run job:warm:env&lt;/code&gt;.&lt;/p&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--A9-wwsHG--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev.to/assets/github-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/numerica-ideas"&gt;
        numerica-ideas
      &lt;/a&gt; / &lt;a href="https://github.com/numerica-ideas/ni-microservice-nodejs"&gt;
        ni-microservice-nodejs
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      NumericaIdeas NodeJS basic microservice for server (EC2) and cloud function (Lambda) deployments.
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;
&lt;h1&gt;
NumericaIdeas - Microservice (NodeJS) &lt;a href="https://blog.numericaideas.com" rel="nofollow"&gt;&lt;img src="https://camo.githubusercontent.com/3ed1ad9a5ae4565a632294f75c6467e7c64587a4cb06c6026eb5d500fef1fa4f/68747470733a2f2f686974732e736565796f756661726d2e636f6d2f6170692f636f756e742f696e63722f62616467652e7376673f75726c3d68747470732533412532462532466769746875622e636f6d2532466e756d65726963612d69646561732532466e692d6d6963726f736572766963652d6e6f64656a7326636f756e745f62673d253233373943383344267469746c655f62673d2532333535353535352669636f6e3d2669636f6e5f636f6c6f723d253233453745374537267469746c653d6869747326656467655f666c61743d66616c7365" alt="Hits"&gt;&lt;/a&gt;
&lt;/h1&gt;
&lt;h2&gt;
Architecture Diagram&lt;/h2&gt;
&lt;p&gt;&lt;a rel="noopener noreferrer" href="https://github.com/numerica-ideas/ni-microservice-nodejsarchitecture.png"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--kYszwx_Q--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://github.com/numerica-ideas/ni-microservice-nodejsarchitecture.png" alt="Diagram"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;This project serves as a simple micro-service usable to build highly scalable backends in an hybrid setup as part of a cloud migration, either in the cloud (Lambda or EC2) or into a custom VPS.&lt;/p&gt;
&lt;p&gt;Let's note that for EC2 (VPS) deployment, Redis is mainly in used for inter-components communication, while AWS-SNS is used for the Lambda path.&lt;/p&gt;
&lt;p&gt;More explanations about these architecture decisions are present in a blog post: &lt;a href="https://blog.numericaideas.com/multipurpose-cloud-migration-nodejs" rel="nofollow"&gt;https://blog.numericaideas.com/multipurpose-cloud-migration-nodejs&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
Prerequisites&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;MongoDB&lt;/strong&gt; the NoSQL database management system.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;NodeJS&lt;/strong&gt; installed and running, version &amp;gt;=10 (14 is recommended).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Redis&lt;/strong&gt; installed and running, it's used for the caching layer and some inter-services communication.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;AWS CLI&lt;/strong&gt; for lambda deployments using NPM Scripts.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Note: &lt;em&gt;AWS CLI&lt;/em&gt; isn't required since the CI/CD pipeline will always auto-deploy the App on changes made on develop (dev) and master (prod) branches.&lt;/p&gt;
&lt;h2&gt;
Running&lt;/h2&gt;
&lt;p&gt;First, you should install the dependencies by typing &lt;code&gt;npm install&lt;/code&gt;…&lt;/p&gt;
&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/numerica-ideas/ni-microservice-nodejs"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;h3&gt;
  
  
  Limit
&lt;/h3&gt;

&lt;p&gt;It is important to note that this approach does not guarantee a complete elimination of Cold Starts. For instance, if the function is behind a &lt;strong&gt;Load Balancer&lt;/strong&gt;, it may not always be effective since the LB can direct traffic to instances that are not warmed. Additionally, in production environments where functions &lt;strong&gt;scale out&lt;/strong&gt; to handle increased traffic, there is no assurance that the new instances will be warmed up in time.&lt;/p&gt;

&lt;p&gt;- - - - - - -&lt;/p&gt;

&lt;p&gt;We have just started our journey to build a network of professionals to grow even more our free knowledge-sharing community that'll give you a chance to learn interesting things about topics like cloud computing, software development, and software architectures while keeping the door open to more opportunities.&lt;/p&gt;

&lt;p&gt;Does this speak to you? If &lt;strong&gt;YES&lt;/strong&gt;, feel free to &lt;a href="https://discord.numericaideas.com"&gt;Join our Discord Server&lt;/a&gt; to stay in touch with the community and be part of independently organized events.&lt;/p&gt;

&lt;p&gt;- - - - - -&lt;/p&gt;

&lt;p&gt;If the &lt;strong&gt;Cloud&lt;/strong&gt; is of interest to you this &lt;a href="https://www.youtube.com/watch?v=0II0ikOZEYE"&gt;video&lt;/a&gt; covers the 6 most &lt;strong&gt;Important Concepts&lt;/strong&gt; you should know about it:&lt;br&gt;
&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/0II0ikOZEYE"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Important&lt;/strong&gt;: other articles are published on NumericaIdeas's &lt;a href="https://blog.numericaideas.com/"&gt;blog&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;
&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://blog.numericaideas.com/" class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://res.cloudinary.com/practicaldev/image/fetch/s--GLxs_AMz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://blog.numericaideas.com/site-meta.png" height="671" class="m-0" width="800"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://blog.numericaideas.com/" rel="noopener noreferrer" class="c-link"&gt;
          Numerica Ideas
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;p class="truncate-at-3"&gt;
          Tech topics to sharpen your cloud and software architecture skills
        &lt;/p&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
        blog.numericaideas.com
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;


&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In summary, each of these strategies has its own unique approach to mitigating Cold Starts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;SnapStart&lt;/strong&gt; takes advantage of the SnapShots technique.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Custom Warmer&lt;/strong&gt; implements a scheduled ping mechanism.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Provisioned Concurrency&lt;/strong&gt; uses provisioned functions instances.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It should be noted that &lt;strong&gt;Cold Starts&lt;/strong&gt; are not a critical issue for most functions, as they occur in only about &lt;strong&gt;1% of invocations&lt;/strong&gt;. Nonetheless, we hope to have covered the significant differences between these strategies. If you have any suggestions or comments, please feel free to share them in the comments section below.&lt;/p&gt;

&lt;p&gt;Thanks for reading this article, recommend and share if you enjoyed it. Follow us on &lt;a href="https://www.facebook.com/numericaideas"&gt;Facebook&lt;/a&gt;, &lt;a href="https://twitter.com/numericaideas"&gt;Twitter&lt;/a&gt;, &lt;a href="https://www.linkedin.com/company/numericaideas"&gt;LinkedIn&lt;/a&gt; for more content.&lt;/p&gt;

</description>
      <category>aws</category>
      <category>lambda</category>
      <category>serverless</category>
      <category>cloud</category>
    </item>
  </channel>
</rss>
