<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Nate Fonseka on Medium]]></title>
        <description><![CDATA[Stories by Nate Fonseka on Medium]]></description>
        <link>https://medium.com/@natefonseka?source=rss-b7bd13b3035------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*-LOlifU7KMYlQrrz8RIGGA.jpeg</url>
            <title>Stories by Nate Fonseka on Medium</title>
            <link>https://medium.com/@natefonseka?source=rss-b7bd13b3035------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 08:19:09 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@natefonseka/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[YACHT: Yet Another Config Handling Tool for K8s]]></title>
            <link>https://medium.com/@natefonseka/yacht-yet-another-config-handling-tool-for-k8s-93666162175c?source=rss-b7bd13b3035------2</link>
            <guid isPermaLink="false">https://medium.com/p/93666162175c</guid>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[config-management]]></category>
            <category><![CDATA[templating-go]]></category>
            <category><![CDATA[k8s-conig-management]]></category>
            <dc:creator><![CDATA[Nate Fonseka]]></dc:creator>
            <pubDate>Thu, 13 Dec 2018 22:44:13 GMT</pubDate>
            <atom:updated>2018-12-15T17:38:32.209Z</atom:updated>
            <content:encoded><![CDATA[<p>I want to present to you an open source tool I have been working on: <a href="https://github.com/nfons/yacht">https://github.com/nfons/yacht</a></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*9nMBMt-OugnruBr_M-WuEQ.png" /><figcaption>xkcd standards</figcaption></figure><h3>Why a Config Handler?</h3><p>In K8s We wanted to version control our K8s Resources via Git.</p><p>which means in a git repo, we store our immutable resources such as Services, Secrets, etc etc (basically everything but Deployments, statefulsets, replicasets, daemon sets). These resources will rarely change, but often differ very slightly from one k8s cluster to another.</p><p>I.E a service resource running on GKE might be:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f85bf32aebefc3a9313812b75b123d10/href">https://medium.com/media/f85bf32aebefc3a9313812b75b123d10/href</a></iframe><p>as you can see, the only thing that changes in above service.yaml is the `loadBalancerIP` . But in traditional methods, you would have multiple `service.yaml` files for each environment: Service-dev.yaml, Service-prod.yaml</p><h3>Why Another Config Handling Tool?</h3><p>Isn’t there already the likes of Helm? So why create another Config Handling tool?</p><p>Understanding and being comfortable with Kubernetes can be a daunting task..now couple that with the task of understanding and building with tools like Helm, it can be even more daunting.</p><p>I wanted to build a simple config management tool that even new users of Kubernetes can quickly grasp and leverage.</p><h3><strong>YACHT principles</strong></h3><p>Simplicity was the driving factor behind YACHT, as well as the ability to migrate to a more robust solution at a later time should the need arise.</p><p>Yacht leverages GO templating to achieve much of its functionality.</p><h3>The YACHT approach</h3><p>YACHT config management is as simple as copy and pasting resource yamls you find in the wild.</p><p>Yacht is made up of 2 resources:</p><p><strong>[Resource] yaml file</strong> and the<strong> ENV</strong> config file.</p><p>a Config File is named after your specific environment (i.e DEV.conf)<br>and is a javalike key-value file:</p><pre>HTTPSVC_LB_IP=gcr.io/google_containers/echoserver:1.8</pre><p>The Resource Yaml file can be any kubernetes resource yaml.<br>In the case of the above example service:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/513bbcced8b4da97ec9f91419ae8fb96/href">https://medium.com/media/513bbcced8b4da97ec9f91419ae8fb96/href</a></iframe><p>With this approach, instead of having multiple service.yaml files, we only need 1 file, with the templating.</p><p>To Deploy to a env:</p><p>yacht -e dev -f Service.yaml</p><p>if you had a prod env (with prod.conf)</p><p>yacht -e prod -f Service.yaml</p><p>Simple right? With this approach, you can get started using any yaml, and just template out the different parts as you need.</p><p>Please Feel free to try out Yacht @ <a href="https://github.com/nfons/yacht">https://github.com/nfons/yacht</a></p><p>as well as a simple demo env for yacht : <a href="https://github.com/nfons/yacht-test">https://github.com/nfons/yacht-test</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=93666162175c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Performance Testing Serverless GCP Cloud Functions]]></title>
            <link>https://medium.com/@natefonseka/performance-testing-serverless-gcp-cloud-functions-71555c550222?source=rss-b7bd13b3035------2</link>
            <guid isPermaLink="false">https://medium.com/p/71555c550222</guid>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[google-cloud-functions]]></category>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[google-cloud-platform]]></category>
            <category><![CDATA[kubernetes]]></category>
            <dc:creator><![CDATA[Nate Fonseka]]></dc:creator>
            <pubDate>Wed, 31 Oct 2018 19:24:47 GMT</pubDate>
            <atom:updated>2018-11-08T02:10:34.071Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/700/1*SEbCLjMtB-vcrSsixCEwkw.png" /></figure><p>If you read many of my <a href="https://medium.com/p/41f66611f54d?source=your_stories_page---------------------------">past</a> <a href="https://medium.com/capital-one-tech/applying-minification-and-uglification-to-aws-lambda-functions-dbc7ad75241">stories</a>, you could have probably guessed accurately I am a huge serverless fan.</p><p>I recently started to work on GCP CloudFunctions (GCF) and wanted to see the performance of cloud functions compared to other options.</p><p>During my brief experience, I started to see some terrible performances from GCP, and wanted to test it against a traditional Micro-Service deployment. and see if the fault was in my code, or with GCF</p><h3>The Code</h3><p>The code used for testing is something very generic and very basic. Take a HTTP request data, do some very-very light data manipulation, and store that object in a database.</p><p>The code base is very light and simple:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bd23b1204d4ee55160896e60e4e0c398/href">https://medium.com/media/bd23b1204d4ee55160896e60e4e0c398/href</a></iframe><p>We have the same code base for GCF, but just transformed for GCF: Instead of `app.post()` we have an `export.myfunction = (req,res)`</p><p>Using <a href="https://github.com/rakyll/hey">Hey</a> (<a href="https://github.com/rakyll/hey">https://github.com/rakyll/hey</a>) to do our concurrency and load testing, Here are the results for each type of deployment</p><h3>The Kubernetes Express Server deployment:</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/512/1*ceAcbqVs3h-qxewasI0fSw.png" /><figcaption>K8s Deployment.yaml limits and requests</figcaption></figure><p>Based on the deployment.yaml we are giving our deployment a memory limit of 200MiB and a CPU of ~ 0.5 GCP cpu cores.</p><p>Running `Hey` on this k8s deployment yields the following data:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ffIme9YNqNhjOQhP-bfwoQ.png" /><figcaption>Kubernetes Deployment 50 concurrent @ 0.26 sec average</figcaption></figure><p>Even though our code is not doing much logically, nor is it large code base. For pure throughput, I think the code base is a good litmus test for CRUD like service performance.</p><p>50 concurrency (~180 requests/sec) would be what I consider a bit above average for most applications, even some “enterprise” applications. This translates to about <strong>8 Million requests per day </strong>(if you “guestimate” a peak of about 12 hours) and about <strong>240 Million / month. </strong>At 24hr continuous its about 480 Million / month</p><p>To put this into perspective: Take <a href="https://acloud.guru/">acloud.guru</a>, which famously runs its backend via AWS Lambda functions: Only <a href="https://www.similarweb.com/website/acloud.guru">~1.5Million / month</a> . E-trade is at about ~14 Million / month. Yelp: ~140Million/ month. Obviously my “dumb” code is not doing nearly as much as an API service of Yelp…but you can hopefully extrapolate the data to see how it would scale at much more enterprise like code base.</p><h3>Cloud Function Deployment</h3><p>Just like the kubernetes application, We run Hey @ 50 concurrency to get the following results:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/850/1*lrEHybZTC_bg68zIcXH4sQ.png" /><figcaption>GCF @ 2GB mem allocation : 27.47 requsts /sec</figcaption></figure><p>But wait! thats not all, of those 1000 requests, 8 of them failed due to timeout. (we have set a 60 sec timeout on GCF)</p><p><strong>The GCF function took 5x longer to complete </strong>the same amount of records, at almost 4x the average speed, not to mention it is almost <strong>80%</strong> slower in requests per second.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/890/1*YChbvxodmLgImLTFi1Kj5A.png" /><figcaption>Higher is better</figcaption></figure><h3>WHAT IS GOING ON?!?</h3><p>OK, maybe some how there is some voodoo going on with my imports not playing nice in a Serverless world (highly doubt it, but I’ll give the benefit of doubt). So what about the most simplest of REST functionality? a simple health check function that does absolutely nothing but send “200 OK” back.</p><blockquote>res.status(200).send(“OK”);</blockquote><p><strong>Kubernetes:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/562/1*cCrRRcH05Dj_Z48m7tDNZA.png" /><figcaption>487/sec</figcaption></figure><p><strong>GCF</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/564/1*kYoPTp26yDLqh0d8-X5Fgg.png" /><figcaption>71/sec</figcaption></figure><p>The difference of GCF vs K8s application is a whopping <strong>~500% increase.<br></strong>OK, maybe the issue is with GCF cold starts…but that would still not answer why it is this slow, considering after the initial burst of container start ups, those containers should be available to re-process.</p><p>For testing sake, I created the same function as an AWS lambda function, behind API Gateway:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/536/1*BmNdQANy9KiWsKId0MmFdg.png" /><figcaption>Lambda + API GW</figcaption></figure><p>To visualize the difference on each type of deployment (higher is worse):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*JI-ZSk6ZD4VkqOnBQOPMag.png" /><figcaption>Performance of Avg response time for each deployment</figcaption></figure><p>You see in Lambda, we have a good equilibrium of performance vs cost. The slowness can be answered by the initial cold starts, and then quickly scales out when those containers are re-used. Even though the average is a bit slower, its not by much, and will easily be offset by the cost savings.</p><h3><strong>Conclusion</strong></h3><p>To be very blunt, When it comes to enterprise level applications, Google Cloud Functions are in the little leagues where as AWS lambda, and even ServerFULL applications are in the professional league. In my opinion, the cost benefit of running GCF vs the drastic performance hit unfortunately does not warrant any switch. If you are thinking of going serverless, go with AWS lambda or K8s serverless such as <a href="https://fission.io/">Fission</a> / Kubeless</p><p><strong>P.S:</strong><br>Yes all deployments are provisioned correctly. In fact GCF is much higher (2gb) vs AWS (128mb)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=71555c550222" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Kubeless vs Fission: The Kubernetes Serverless match up]]></title>
            <link>https://medium.com/@natefonseka/kubeless-vs-fission-the-kubernetes-serverless-match-up-41f66611f54d?source=rss-b7bd13b3035------2</link>
            <guid isPermaLink="false">https://medium.com/p/41f66611f54d</guid>
            <category><![CDATA[kubernetes-serverless]]></category>
            <category><![CDATA[k8s]]></category>
            <category><![CDATA[serverless]]></category>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[serverless-k8s]]></category>
            <dc:creator><![CDATA[Nate Fonseka]]></dc:creator>
            <pubDate>Wed, 25 Jul 2018 16:08:11 GMT</pubDate>
            <atom:updated>2018-12-19T01:20:28.652Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*Ew4oo6cq2mdU3vr32fGVyg.png" /><figcaption>Kubless by Bitnami vs Fission by Platform 9</figcaption></figure><p>If you are reading this article, you are probably looking into implementing some sort of serverless framework in your Kubernetes cluster…and why not? both Kubernetes and Serverless are the next big thing, and both of these offer a variety of benefits for modern enterprise applications.</p><p>But why Serverless on Kubernetes? Take a look at what a typical development-ops process of micro-service on k8s vs using serverless looks like:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AjBE0ibGa-oyIKS6pmdNOg.png" /><figcaption>less to do, in faster time.</figcaption></figure><p>With traditional microservices, you have to manage much of the underlying “Ops” such as writing the dockerfile, and relevant k8s yamls. Serverless abstracts that out, so you can focus on what matters: development</p><p>For a serverless framework there are few contenders, but today I want to compare <a href="https://kubeless.io/">Kubeless</a> and <a href="http://fission.io/">Fission</a> two of the more popular options. So, which one should you choose? Kubeless? or Fission? let’s compare the two.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BCq06aAYxajO681WzacsGQ.png" /><figcaption>Languages supported by each</figcaption></figure><p>As you can see with the exception of Binary and Ballerina, both frameworks support most of the popular languages. I do have to point out one thing here though: Kubeless supports Python 2.7 natively, whereas Fission only has support for Python 3 out of the box.</p><p>Now that we know what languages we can use to develop, how can we execute each framework?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/804/1*_I45PicoUml5ULNZjbTczQ.png" /></figure><p>Both Fission and Kubeless supports most common use cases for Serverless: http trigger via k8s ingress. Pub-sub, and time-based trigger (cron). The only difference is some specifics like kafka and Kinesis.</p><h3>Features / Architecture: Fission.</h3><p>When it comes to features, Fission wins hands down. Though I do have to give credit when its due to Kubeless for its <a href="https://github.com/kubeless/kubeless-ui">kubeless-ui</a>, a really cool component that allows you to edit and deploy Kubeless functions in real-time (very similar to the AWS lambda console).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HPRlDhFpcraIJRNUobaWkA.png" /><figcaption>Kubeless-UI</figcaption></figure><p>Fission also has something similar called Fission-ui, though it isn’t as UX friendly as Kubeless-ui, but it is by far more feature rich by providing bit more benchmarking and logging built-in.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*haIGn6dz3uEC6uXF.png" /><figcaption>fission-ui, definitely has more features than kubeless</figcaption></figure><p>However, I didn’t put much emphasis on the UI components because the likeliness of this UI feature being used in production grade enterprise applications would be slim (CI/CD anyone?). So as great as it is, using this feature would completely bypass CI/CD gates like unit tests, code reviews and version control.</p><p>Speaking of CI/CD, another thing I want to point out as a Pro for Kubeless is its integration with the serverless.io framework, which has an <a href="https://github.com/serverless/serverless-kubeless">addon for Kubeless</a>.</p><p><strong>Fission Workflow</strong></p><p>However, one of the more production ready points has to be given to Fission, with its <a href="https://docs.fission.io/0.8.0/workflows/">fission-workflow</a>, which Kubeless currently has no answer to.<br>Fission-workflow is basically Fissions implementation of <a href="https://aws.amazon.com/step-functions/">AWS step functions</a>. For the uninitiated, Fission workflow allows you to create a workflow of Fission functions to complete tasks with a specific order: I.e. You can have an output of one function, be the input to another, so on and so on, in sequential order.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/868/1*PLY2H_8qgQdX4IQDLguGTw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/462/1*XcCr8ODVozQq_4YUBQjRDw.png" /><figcaption>Output of fortune, as input to whalesay</figcaption></figure><p><strong>Kubeless…the Faux Serverless<br></strong>Another reason I gave the feature/architecture win for Fission over Kubeless is for what I consider to be Kubeless’ “faux” serverless. What many people assume to be serverless is undoubtedly the same architecture AWS Lambda functions use: Ephemeral, on demand and short-lived executions. Kubeless however (as of this post), <a href="https://github.com/kubeless/kubeless/issues/148">creates one pod per function and scales them as needed</a>. If you had two Kubeless functions, it would create 2 pods, and 2 deployments.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/942/1*QF8r0Zp6rQzs1jJf0PbczA.png" /><figcaption>2 Kubeless functions: hello and test.js</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1010/1*nteq10-e60VXNe5PyESn1g.png" /><figcaption>Each function gets its own pod</figcaption></figure><p>Whereas Fission stays true to serverless ephemeral nature, and executes your function in a environment pool-manager, whether you have 1 function, or 100 functions. Fission by default deploys your functions in pool-manager</p><p>Pool-manager is simply a “pool” of environments you create for each of your runtimes for your functions. Suppose you have 1 nodejs function that needs to be executed, you would create a nodejs environment as such:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xYnz9ISkp4ZwKzLVXIf4GA.png" /><figcaption>creates a nodejs env with node-0.9 runtime.</figcaption></figure><p>we can see the pools by running `fission env list`, and the number of pods as well</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*E0k6GyGbJKsOCvrZF2qi7A.png" /><figcaption>fission creates nodejs pool of pods</figcaption></figure><p>So theoretically in Kubeless if your node crashes just before a request comes in, that request will wait until a K8s creates a new pod for you. Whereas in Fission due to pool-manager, even if a node crashes your function can still execute in the remaining environment pods.</p><p>Another thing to keep in mind is that in an enterprise application, you will probably have hundreds of functions, which means in Kubeless case, it would create 100+ pods, and 100+ deployments, breaking the principle: <em>“on demand, pay for what you use.”</em></p><p><strong>Popularity: Kubeless</strong>.</p><p>Even though <a href="https://github.com/fission/fission">Fission (3.4K )</a> has more stars than <a href="https://github.com/kubeless/kubeless">Kubeless(3K)</a>, and more “watchers” (132 vs 119), in the popularity sense (though I would venture and say it is somewhat of a meaningless statistic), Kubeless by far is more popular than fission. Hell, the only reason I found out about Fission was because of Kubeless.<br>Kubeless (probably because of its namesake) gets talked about, written about, and generally better “PR” than fission…In fact I’m willing to bet you came to this story because of Kubeless. Though popularity should never be the sole reason we pick a tech-stack over features, as developers we want to use technologies that will be supported for a long time. And popularity is a big driving factor of that.</p><h3><strong>Winner: Fission.</strong></h3><p>It shouldn’t have been too much of a shocker…but the clear winner in my opinion is Fission. Though both Fission and Kubeless has a long way to go before it truly competes with the likes of AWS Lambda functions, Fission embodies everything serverless is: its easy to set up, and get started and more importantly, much more feature rich than Kubeless. I can’t reiterate enough that I find Kubeless to be a faux serverless (at least until they get a pool-manager implemented), and would only recommend using it if you have a specific use case where only Kubeless is available (like if you need to use kafka or kinesis)</p><p><strong>To Summarize:</strong></p><p>Fission:</p><ul><li>Supports most modern languages</li><li>Supports most use cases for triggers (like http, cron, MQ)</li><li>Truly serverless</li><li>Can create a workflow</li></ul><p>What are your thoughts? are my assumptions correct? Did I miss anything? Let me know!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=41f66611f54d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Applying Minification and Uglification to AWS Lambda Functions]]></title>
            <link>https://medium.com/capital-one-tech/applying-minification-and-uglification-to-aws-lambda-functions-dbc7ad75241?source=rss-b7bd13b3035------2</link>
            <guid isPermaLink="false">https://medium.com/p/dbc7ad75241</guid>
            <category><![CDATA[optimization]]></category>
            <category><![CDATA[lambda]]></category>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[capital-one]]></category>
            <category><![CDATA[aws-lambda]]></category>
            <dc:creator><![CDATA[Nate Fonseka]]></dc:creator>
            <pubDate>Thu, 19 Apr 2018 11:51:00 GMT</pubDate>
            <atom:updated>2018-11-08T02:06:02.185Z</atom:updated>
            <content:encoded><![CDATA[<h4>An Experiment in Optimization</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8BQlfNSvqCNQJqw2JNKruA.png" /></figure><p>AWS Lambda — or Functions as a Service (FaaS) — has definitely been a game changer in the IaaS ecosystem. It has allowed backend developers to pay for what they use with competitive prices and efficiency. My team at Capital One uses a few Lambda functions to quickly bootstrap functionalities that require both elasticity and availability. Though AWS Lambda is already highly optimized for speed, I’m an efficiency freak, so whenever I’m working on something I always ask myself, “How can we make this <em>even more</em> efficient?” So, over the previous few months, I’ve been experimenting with optimizing Lambda functions to see if I’m using them optimally.</p><p>When optimizing AWS Lambda functions, one thing to keep in mind is the ephemeral nature of function invocation. Take a traditional Express Node.js application — once the application is started, it will remain running on the machine until shut off. Due to the nature of the applications and technologies we work with, back-end engineers often overlook a common procedure that front-end engineers are more familiar with using: <em>minification and uglification</em>.</p><p>Could this be an answer to optimizing the efficiency of our Lambda functions?</p><h3>Shallow Dive into AWS Lambda — Cold Start vs Warm Start</h3><p>If you have a rudimentary understanding of AWS Lambda you probably have an idea of something like what the diagram shows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/864/1*8T3oB_mV2aL5x_v4mf9UIA.png" /><figcaption><em>Note: I realize that AWS is not using Docker Hub and has a highly customized registry but you get the point.</em></figcaption></figure><p>Here we have a cold start and warm start. We can think of the cold start as a EC2 instance that needs to download docker image from Docker Hub. We can think of the warm start as when the container is already downloaded in a EC2 instance. <em>Note, this is somewhat correct — what you won’t know from this diagram is that the “container” is not re-initialized.<br> </em><br> To test this theory, let’s run a simple Node application:</p><pre>var outer = Date.now();</pre><pre>exports.handler = function(context, event, callback) {</pre><pre>  var inner = Date.now();</pre><pre>  console.log(‘inner: ‘ + inner);</pre><pre>  console.log(‘outer: ‘ + outer);</pre><pre>}</pre><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f8de3f80dcdec1acd52dcf8f43a6f473/href">https://medium.com/media/f8de3f80dcdec1acd52dcf8f43a6f473/href</a></iframe><p>This is very basic — it gets two variables at run time and prints them out.<br> Theoretically, the two variables should be equal (or near equal).</p><p>Now, let’s run a Docker version of this application by running:</p><pre>docker run lilnate22/test-lambda.</pre><p>We will see the first time you run this command that it takes some time t o execute since you need to download the image from a registry (cold start). We should also see an output where the two variables are equal or near equal.</p><p>The next time you run <em>docker run lilnate22/test-lambda</em> it will be near instant (warm start) because it will already be downloaded on your machine. This is pretty basic stuff. <em>But what happens when we do the same thing in AWS?</em><br> <br> Let’s try it! Copy the above code and create a new blank Nodejs 6.10 lambda function. When you run the newly created function, it should take slightly longer (since AWS has to “download”). You will see in the first initialization that your inner and outer timings will be near equal. <em>But what happens when we do it again?</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/864/1*kUT2KWX3oOIvGVyoKvHp7g.png" /><figcaption>wait a minute….why are they different?</figcaption></figure><p>The hint lies in the fact that <strong>AWS only executes code inside handler functions during warm state runs. </strong>I.e. anything inside of <em>exports.handler = function()</em>. So, what does that mean? Suppose we have a Lambda function that gets us the daily lottery numbers:</p><p><a href="https://gist.github.com/nfons/c54449e587cd6d970bb4acff24e44f4a">https://gist.github.com/nfons/c54449e587cd6d970bb4acff24e44f4a</a></p><p>If we write our Lambda function like we wrote our Docker app, each Lambda execution would “fetch” the lottery numbers.</p><p>Instead, we should optimize it where we check if a dependency is already set and only execute the expensive fetch if it’s empty.</p><p><a href="https://gist.github.com/nfons/91bca1483d912d6f1a27ad0f37554d4c">https://gist.github.com/nfons/91bca1483d912d6f1a27ad0f37554d4c</a></p><p>In this way, in subsequent calls, we won’t have to do the process expensive functionality.</p><h3>Minify + Uglify = Speed + Cost.</h3><p>Minification / uglification is a common component of client-side JavaScript development. Front-end engineers are stressed on the importance of minifying CSS/HTML that’s delivered to browsers to help improve speed. But when it comes to your back-end developers (Service Developers), minification is often overlooked.</p><p>Take this example — if you were to search for “<a href="https://stackoverflow.com/questions/12068971/does-it-make-sense-to-minify-code-used-in-nodejshttps:/www.quora.com/Is-it-advisable-or-even-possible-to-minify-node-js-code">Nodejs minification</a>” on google</p><p>most front-end developers would be confused and ask “why?” Which makes sense when you think about traditional Node.js service runtime environments. In these environments you run, say an Express.js rest app, once and the app stays up taking in requests. Unlike client-side code, which is quasi “ephemeral”, there is really no need to send code through the wire to browsers.</p><h4>Minification</h4><p>Minification is a common frontend process of taking code and removing all unnecessary characters without changing code logic.</p><p>Suppose we have a code block like this:</p><pre><strong><em>[code]</em></strong></pre><pre><strong><em>var longvariable = “myname”</em></strong></pre><pre><strong><em>var longArray = [1,2,3,4,5,6]</em></strong></pre><pre><strong><em>//some comment about functionality perhaps?</em></strong></pre><pre><strong><em>for(var index = 0; index &lt; 6; index++){</em></strong></pre><pre><strong><em>console.log(longArray[index]);</em></strong></pre><pre><strong><em>}</em></strong></pre><pre><strong><em>[/code]</em></strong></pre><p>Minifiying it would remove all the whitespaces, tabs and comments to output a code block such as:</p><pre><strong><em>[code]</em></strong></pre><pre><strong><em>var index,longvariable=”myname”,longArray=[1,2,3,4,5,6] for(index=0;6&gt;index;index++)console.log(longArray[index])</em></strong></pre><pre><strong><em>[/code]</em></strong></pre><p>Just in our example, we have taken a six line code and minified it to two lines. If we combine this with Webpack (a bundler) which will take multiple dependent files and minify them to one file, we can reduce total lines in our app significantly.</p><h4>Uglification</h4><p>Uglification is one extra process that makes code even more leaner. Uglification takes all the variables and obfuscates/simplifies them, so our minified example:</p><pre><strong><em>var index,longvariable=”myname”,longArray=[1,2,3,4,5,6] for(index=0;6&gt;index;index++)console.log(longArray[index])</em></strong></pre><p>will look like something like this:</p><pre><strong><em>for(var o=”myname”,a=[1,2,3,4,5,6],e=0;e&lt;6;e++)console.log(a[e]);</em></strong></pre><h3>Why Minify + Uglify??</h3><p>A few reasons come to mind as to why you might want to minify and uglify. From my time working as a frontend developer, I knew one of the big benefits of minification/uglification is that it ensures the client browser has the smallest possible files to download. Applying that same principle to Lambda, with its ephemeral code functionality, I was hoping those same benefits could be applied to backend work.</p><p>But another thing to keep in mind is that AWS Lambda has <a href="https://docs.aws.amazon.com/lambda/latest/dg/limits.html">a limit of 75GB for all lambda functions in a region with a package limit of 50MB</a> each. So, even if our performance gains are not that drastic, perhaps we can eliminate the headache of hitting this 75GB limit.</p><p>I have created a sample application to illustrate this. Feel free to clone it here:<br> <a href="https://github.com/lilnate22/Lambda-Tester/">https://github.com/lilnate22/Lambda-Tester/</a></p><p>To run:</p><pre>npm run build</pre><pre>zip -r package-raw.zip .</pre><pre>zip -r package-ugly.zip bundle.js</pre><p>If you look at the statistics of the Zip files:</p><p><strong><em>Raw: 13.8MB vs Minified: 1MB. More than 13x the size!</em></strong></p><h3>Example</h3><p>But how about a real-world example?</p><p>At Capital One, we deal with a good amount of data from various vendors and partners. During a quarterly hackathon, we created a Lambda function that would automatically parse this data, and either strip them or mask them depending on the applicable data security rules.</p><p>We ran this application 100 times in both the warm state and cold state. Though AWS does not tell users how long warm state is, we made a safe assumption based on how Lambda works in containers that if we ran the application, immediately after execution completed it should be in the warm state. If we create 100 lambda functions with an integer appended to name, we can execute each of those once to calculate the cold state averages.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/654/1*C_jpGOXz-BEgGx8SkLy9_Q.png" /><figcaption>Data points for Minified and raw entries (in ms):</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/864/1*WfWOZAsqCQ8OrP4MLwWneA.png" /><figcaption>For a warm state execution, we see about a 40% reduction in execution time. <em>Note: we did see a higher memory footprint though.</em></figcaption></figure><p>I expected cold state executions to be even faster, but to my surprise we only saw about a 30% reduction between minified and raw functions. Though the values for cold state RAW was much more volatile and sporadic (high: 1667, low: 903) than that of minified (high: 969, low: 737).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/864/1*Q1YvmLXRwlSieuputXamLg.png" /></figure><h3>Results</h3><p>Now that we’ve shown the speed increase, what does that mean for our hackathon project? We did see a higher uptick in memory used (as reported by AWS). But to the best of my research, AWS did not charge by Mb (only what is max allocated).<br> <br> From a cost perspective, if we were to extrapolate the values we’d see a total savings for our hackathon project of ~20%</p><h3>Final Thoughts</h3><p>AWS Lambda has been a game changer for backend developers. Already highly optimized for speed, by borrowing the concepts of minification and uglification from frontend development we can help push our functions even further.</p><p>As an efficiency freak, I’ve played with this tactic in hackathon projects and personal experiments and find it quite promising. Could this be an answer to further optimizing the efficiency of Lambda functions? Perhaps. The results of using minification and uglification in this way may vary from application to application and project to project. Consider weighing your options carefully when optimizing your own AWS Lambda functions.</p><p><em>DISCLOSURE STATEMENT: These opinions are those of the author. Unless noted otherwise in this post, Capital One is not affiliated with, nor is it endorsed by, any of the companies mentioned. All trademarks and other intellectual property used or displayed are the ownership of their respective owners. This article is © 2018 Capital One.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dbc7ad75241" width="1" height="1" alt=""><hr><p><a href="https://medium.com/capital-one-tech/applying-minification-and-uglification-to-aws-lambda-functions-dbc7ad75241">Applying Minification and Uglification to AWS Lambda Functions</a> was originally published in <a href="https://medium.com/capital-one-tech">Capital One Tech</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>