<?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 Renato Nascimento on Medium]]></title>
        <description><![CDATA[Stories by Renato Nascimento on Medium]]></description>
        <link>https://medium.com/@rhnascimento?source=rss-a7bacfa34bc6------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*Qk0W_D_hZfX6JfhaYuF7Fw.png</url>
            <title>Stories by Renato Nascimento on Medium</title>
            <link>https://medium.com/@rhnascimento?source=rss-a7bacfa34bc6------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 08:20:20 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rhnascimento/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[“Kubeforming”: going full declarative with Kubernetes and AWS]]></title>
            <link>https://medium.com/inlocotech/kubeforming-going-full-declarative-with-kubernetes-and-aws-2507a90f683b?source=rss-a7bacfa34bc6------2</link>
            <guid isPermaLink="false">https://medium.com/p/2507a90f683b</guid>
            <category><![CDATA[backend-development]]></category>
            <category><![CDATA[kubernetes]]></category>
            <category><![CDATA[in-loco-tech]]></category>
            <category><![CDATA[terraform]]></category>
            <category><![CDATA[infrastructure-as-code]]></category>
            <dc:creator><![CDATA[Renato Nascimento]]></dc:creator>
            <pubDate>Mon, 09 Dec 2019 16:31:01 GMT</pubDate>
            <atom:updated>2019-12-09T22:12:48.843Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DwoHn8spqXEEpFj49-0XxA.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@micheile?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Micheile Henderson @micheile010</a></figcaption></figure><p><strong>Declarative infrastructures</strong> are a dream come true for any infra <em>team</em>. Having your cloud environment bootstrapped by an <strong>IaC</strong> (Infra as Code) tool, such as <em>Terraform</em>, means even more than gains in speed or cost reduction: it ensures that the knowledge required to get your systems up and running is crystallized in a functional blueprint, rather than sitting around on pages of perhaps-some-step-is-missing documentations.</p><p>Those tools have already reached an astonishing level of maturity. We encourage them to be the default approach to building cloud infrastructure in your organization.</p><p>Among IaC tools, the most adopted ones are the so-called <strong>declarative</strong>: you specify the desired state, and the system itself defines and executes the needed steps to achieve it.</p><p>In this post, we’ll show how we leveraged three declarative tools (<em>Terraform</em>, <em>eksctl</em>, and <em>Argo CD</em>) to deliver fully working and standardized Kubernetes clusters to our product teams.</p><h3>Step 0: preparing the cloud for your cluster</h3><p>AWS provides a managed service for Kubernetes clusters called Amazon EKS.</p><p>However, EKS does not live in isolation. It interacts with some other cloud services that we need to prepare prior to declaring your EKS Control Plane.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/482/1*mp-aeSwjA9s2eLFzniG9vw.png" /></figure><p><em>Terraform</em> receives a handful of parameters and provisions for us:</p><ul><li><strong>Amazon VPC</strong> private and public subnets on our VPC, across multiple AZs (availability zones), and utility security groups;</li><li><strong>Amazon Route53</strong> hosted zones to be managed by the cluster’s <a href="https://github.com/kubernetes-sigs/external-dns">external DNS</a>;</li><li>A<strong> CodeCommit </strong>repo to store cluster backups of our Kubernetes resources.</li></ul><p>With a blueprint, no more than a few parameters are needed to have these services properly configured: the name of the cluster itself, subnet positioning in the private IP range, and desired domains to be managed by Route53.</p><p><em>Terraform</em> outputs the state, including IDs, of the resources it just provisioned. They become parameters for the next step.</p><h3>Step 1: bringing that shiny new k8s cluster up</h3><p>Amazon has <a href="https://aws.amazon.com/about-aws/whats-new/2019/11/amazon-eks-adds-support-for-provisioning-and-managing-kubernetes-worker-nodes/">recently released</a> managed EKS node groups. However, they still can’t give us all the features that we may achieve while managing our own workers.</p><p>When these node groups reach a higher level of maturity, we will be able to bootstrap the entire cluster with Terraform, without having an extra layer of complexity. While that day hasn’t come, we are more than happy with our tool of choice, <a href="https://eksctl.io/"><em>eksctl</em></a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*A8aZq1pHUpbimb90.png" /><figcaption><em>how adorable :3</em></figcaption></figure><p><em>eksctl</em> is capable of interacting (via AWS CloudFormation) with EKS, so it bootstraps the control plane for you, and with AWS EC2, so it also makes sure your worker nodes are running with your desired configuration.</p><p>With <em>eksctl</em>, we have been able to configure a surprising range of workers flavors — including elastic spot instance groups to run Spark jobs, and database clusters leveraging i3’s local storage.</p><h3>Step 2: delivering add-ons to all clusters</h3><p>A brand new Kubernetes cluster is capable of many things, but it would be a mistake to consider it production-ready.</p><p>Production applications need a <strong>supporting infra</strong>: Prometheus should be up scraping metrics, Filebeat harvesting logs, ingress controllers making sure services are being exposed, and much more.</p><p>We call these supporting infra <strong>add-ons</strong>. They are delivered like any regular Kubernetes app: by uploading manifests to the Kubernetes API. Sounds declarative enough, huh?</p><p>However, what if we had a repo of what our ideal “In Loco cluster” looks like, and have this target configuration continuously delivered to all of our clusters?</p><p>In such a manner, we would eliminate the toil of ensuring our add-ons are on the same version across clusters. Also, we could use this very process to deliver the same level of configuration to a newly created cluster.</p><p><a href="https://argoproj.github.io/argo-cd/"><em>Argo CD</em></a> can help us to accomplish this vision.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/462/1*0AfKE-7YNdmvylLn49XUzQ.png" /></figure><p><em>Argo CD</em> offers a <strong>pull-based</strong> CD model, where an agent watches for changes in an artifacts repository and delivers them to the target system.</p><p>Add-ons may also be customizable from cluster to cluster, by writing <a href="https://github.com/kubernetes-sigs/kustomize">Kustomize</a> patches, such that we can achieve standardization without sacrificing flexibility completely.</p><h3>Conclusion</h3><p>Delivering and managing multiple clusters don’t have to feel like dying. With this pipeline, we could deploy and maintain our Kubernetes infra as code.</p><p>At the same time, we could maintain a uniform configuration across all clusters, which makes maintainability far easier.</p><p>Of course, there are some hidden details which glue all these steps together, so feel free to email me if you got stuck on implementing this :)</p><p>Happy provisioning!</p><h3>Are you interested?</h3><p>If you are interested in building context-aware products using a powerful location technology that genuinely cares about user’s privacy, then check out our <a href="https://jobs.kenoby.com/inlocoglobal">opportunities</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2507a90f683b" width="1" height="1" alt=""><hr><p><a href="https://medium.com/inlocotech/kubeforming-going-full-declarative-with-kubernetes-and-aws-2507a90f683b">“Kubeforming”: going full declarative with Kubernetes and AWS</a> was originally published in <a href="https://medium.com/inlocotech">Inloco Tech Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What it is like to work at a Brazilian software house]]></title>
            <link>https://medium.com/@rhnascimento/what-it-is-like-to-work-at-a-brazilian-software-house-5fa7ba9e347c?source=rss-a7bacfa34bc6------2</link>
            <guid isPermaLink="false">https://medium.com/p/5fa7ba9e347c</guid>
            <category><![CDATA[startup]]></category>
            <category><![CDATA[outsourcing]]></category>
            <category><![CDATA[software-house]]></category>
            <category><![CDATA[brazil]]></category>
            <category><![CDATA[remote-working]]></category>
            <dc:creator><![CDATA[Renato Nascimento]]></dc:creator>
            <pubDate>Sat, 08 Apr 2017 13:52:52 GMT</pubDate>
            <atom:updated>2017-04-08T13:52:52.349Z</atom:updated>
            <content:encoded><![CDATA[<p>Lots of friends have asked recently why I quit a steady job (which I loved) at one of the coolest software houses in Brazil. This is going to be a quick write-up on the pros and cons, to help techies thinking of joining/quitting software houses, or even managers who would like to improve their businesses.</p><p>For those who are not familiar with the term yet - software houses are companies that don’t own a product, rather, they help others to build their own.</p><p>Hope this initiates cool discussions around the topic!</p><h3>The coder</h3><p>First of all, I’d just like to share this chart that was published this last week, on <a href="https://stackoverflow.com/insights/survey/2017/">Stack Overflow</a> -</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/787/1*AeUFX7PyOxj3dA4KqGh7tw.png" /><figcaption>Based on 44008 developers who answered a survey.</figcaption></figure><p>Looks like we’re surfing the wave!</p><p>Outsourcing accounts for a big part of that. Brazilians are excellent programmers to assist tech companies overseas in building and maintaining their products. Here are the top 3 reasons:</p><h4>1. Idle highly-specialized workforce</h4><p>Computer science/engineering degrees are hard as hell to get in Brazil. Students have to go through lots of grad-level classes and tons of work to get a Bachelor’s. On the other hand, our startups are still taking off and have not completely absorbed the workforce.</p><h4>2. Overlapping in time zones</h4><p>This is a clear advantage over other countries with great developers. Our work shift has 4 hours of overlap with Bay Area, and as many as 6 hours with NY area. It’s easy for us to take part in meetings and get closer to product decisions.</p><h4>3. Weak language barrier</h4><p>A Portuguese speaker needs just a few hundred hours to get a working proficiency in English. Of course, mastering a second language is a journey of a lifetime (take it from me!), but just a few months of focused work on English will do the trick if your goal is to use it professionally, as both languages share some linguistic roots.</p><h3>The company</h3><p>A business model that is widely used by many software houses, and has proven very lucrative, is simply outsourcing programmers.</p><p>Your job is to be part of a remote team of other developers, as a contractor, delivering code and ensuring that the best practices are being adopted.</p><h4>Will accelerate your career…</h4><p>This one is the greatest pro. Big companies will trust you as much as they trust your software house. Of course, when you initiate in a new remote team, there will be some local colleagues to give you support anytime that you need a help or some advice.</p><p>It’s like having a close community, composed by professionals from various backgrounds, which will ensure you’ll have the smoothest transition getting into a new project or stack.</p><p>However, as you approach a more senior level on your projects, you’ll naturally start demanding less and less help from your fellows’ knowledge, which brings us to the next topic.</p><h4>…but will likely keep you stuck at local maximums</h4><p>If you’re doing really well on a project, it’s just strategic for the house to keep you at the same place. Otherwise, if someone took you spot, this would result in a big loss of momentum. Ironically, doing great often means you’ll stay at the same project, not necessarily the one you would aim for.</p><p>As time passes, your responsibilities start to get more vital to your remote team. At this point, you already started to work autonomously, and the role played by the software house itself, as an institution, starts to become irrelevant.</p><p>For a coder with knowledge or senority enough to act and take decisions independently, the software house will start feeling like a dispensible intermediary.</p><h4>Will keep a certain degree of secrecy</h4><p>I’ll give a counterargument to the topic above: paperwork sucks.</p><p>It feels good to delegate all the commercial stuff to a manager, rather than being an independent contractor and having to deal with it.</p><p>But, there’s a hidden cost you have to pay for the convenience. The point here is, often you simply won’t know what’s the difference between what a remote company pays for your work as a contractor, and the actual paycheck you receive from the software house.</p><p>A software house, operating in this model, has no more than two products: your work, and a brand. However, the value of the brand won’t help you a lot on your daily basis. So, how much are they profiting from your work? Would coders be upset if they knew it?</p><p>However, the darkest of contracts is that, some houses won’t allow you to be hired by their clients! Well, if there is a clause for that, that’s a clear statement that there are situations where there isn’t a reason for you to stay at the house, but per contractual force.</p><h3>The conclusion</h3><p>Working at a software house is a great way to get your first big steps as a software developer; or to be in touch with great projects, if you’re a senior already.</p><p>When you are to choose a software house to spend the next years of your career, take a look at the turnover rates, rather than at its portfolio. If coders are not leaving, this means their managers already found good solutions to the questions I pointed above. Or they simply don’t use the outsourcing model.</p><p>Unfortunately, I’m the one who’s leaving now. Excited to see what’s next 😛</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=5fa7ba9e347c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Halving Response Times: Lessons Learned]]></title>
            <link>https://medium.com/codeminer-42/halving-response-times-lessons-learned-ecb4fea96e92?source=rss-a7bacfa34bc6------2</link>
            <guid isPermaLink="false">https://medium.com/p/ecb4fea96e92</guid>
            <category><![CDATA[api]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[profiling]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[performance]]></category>
            <dc:creator><![CDATA[Renato Nascimento]]></dc:creator>
            <pubDate>Fri, 17 Mar 2017 13:22:44 GMT</pubDate>
            <atom:updated>2017-07-20T13:44:33.214Z</atom:updated>
            <content:encoded><![CDATA[<p>Response times under <em>100 ms</em> are capable of lightening anyone’s heart.</p><p>It’s even better when you reach that milestone as a side effect of some rounds of refactoring — even when performance isn’t an explicit priority, good engineering certainly shows measurable results.</p><p>I will talk you through what I have learned after reaching a nice <em>85 ms</em> average for a Rails API (which used to average up to <em>270 ms</em>) 🚗💨💨</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*lqEF1qqwSoTJLF5GaP3XOw.jpeg" /></figure><p><em>Note: </em>I’ll assume you are treating your DB well, okay? Improper N+1 queries or lack of otherwise useful indexes may be a major bottleneck. Also we’re supposing you have well-dimensioned instances for your application, considering of course the throughput it has to deal with.</p><h4>Slowest transactions should be your priority</h4><p>Some widely-used metrics will guide you to take care of your slow transactions first. For example, <a href="http://apdex.org/overview.html">apdex</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/303/1*qiS0W_gPVNCN_eCB0kBMqg.png" /></figure><p><em>T</em> is your <em>ideal</em> time in seconds, any request finishing below <em>T</em> will give full points to your application. <em>T = 0.2 s</em> is commonly adopted.</p><p>Between <em>T</em> and <em>4T</em>, well, that’s considered <em>tolerating</em>. Your application will be penalized in 50% on each request in that range.</p><p>Above <em>4T</em>, we enter the <em>frustated</em> range. Your application receives a zero score for those.</p><p>I personally consider this metric way better than just looking at the average.</p><p>Slower transactions will block your web workers, potentially queueing up requests and having a multiplied impact on the overall responses time, especially during throughput peaks.</p><p>In addition to that, if you use Heroku, be extra-cautious on this topic, as <a href="https://zeemee.engineering/scaling-rails-to-125-000-requests-per-minute-on-heroku-b4128a10a769#.s7yph8ojs">this article</a> states:</p><blockquote>Even worse, however, is that due to the random-routing algorithm Heroku uses for load balancing, a single slow dyno brings the entire app to its knees. It’s <a href="https://www.google.com/#q=heroku+performance+random+routing">well-known</a> that intermixing fast and slow response times in a single Heroku app wreaks havoc on overall app performance.</blockquote><h4>Caching is hard</h4><p>Just look around your code and you’ll see many opportunities to apply <a href="http://edgeguides.rubyonrails.org/caching_with_rails.html#fragment-caching">fragment caching</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*BOGMkYbcA1Bvc39o8Tnwow.jpeg" /></figure><p>Wait, there are other types of caching? Yes! But fragment caching is the most <strong>versatile</strong>. Plus it naturally escalates to <a href="http://edgeguides.rubyonrails.org/caching_with_rails.html#russian-doll-caching">russian-doll caching</a>. Take the word from <a href="https://www.railsspeed.com/">experts</a>:</p><blockquote>As a tip to newcomers to caching, my advice is to ignore action caching and page caching. The situations where these two techniques can be used is so narrow that these features were removed from Rails as of 4.0. I recommend instead getting comfortable with fragment caching.</blockquote><p>Well, make sure you know how to proper implement caching, otherwise you may blow things up!</p><p>That’s why:</p><p>When a key does not have enough information, you may find yourself wondering how you’re going to invalidate stale cache keys, and that’s where things start to become dangerous.</p><blockquote><em>There are only two hard things in Computer Science: cache invalidation and naming things.</em></blockquote><blockquote><em>— Phil Karlton</em></blockquote><p>Cache invalidation is complex and error-prone. Let’s see how we avoid having to use it.</p><p>A <em>good</em> key for any given data structure will contain at least those values:</p><ol><li>A kind;</li><li>An identifier;</li><li>A timestamp;</li><li>A version.</li></ol><p>The first two are simple to understand — among all your keys, you have to know <em>what</em> you’re looking for.</p><p>The third, a <em>timestamp</em>. It’ll prevent your app to serve stale data.</p><p>For Rails devs - you can use the method #cache_key to see these three in practice:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a561ac8f884b57254727343fa7cda8b0/href">https://medium.com/media/a561ac8f884b57254727343fa7cda8b0/href</a></iframe><p>What if I change my data? It’s simple, a brand new key will be used.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/efb15aa0a0456beba61a0ca9c6457b95/href">https://medium.com/media/efb15aa0a0456beba61a0ca9c6457b95/href</a></iframe><p>Your cache system will eventually forget the stale keys, as those aren’t being used anymore. It’s that simple!</p><p>And last, but not least, we also need a <em>version</em>.</p><p>Well, some developers may change details of a given data structure. During a deploy, the code may change and may not play well with the old version of the same data structure (which will still be returned by the cache).</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aeb682cf7900baa9b9bd0a8dd4dab34e/href">https://medium.com/media/aeb682cf7900baa9b9bd0a8dd4dab34e/href</a></iframe><p>This can cause a bug in production, and, take it from me, eventually it WILL cause a bug if you miss this simple step:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0062a883cd4d20e90bdd2b16bc80b59f/href">https://medium.com/media/0062a883cd4d20e90bdd2b16bc80b59f/href</a></iframe><p>The <em>v1</em> and <em>v2</em> on the snippet above identify the version of the data structure. Once your code changes, old versions will stop being retrieved.</p><p>However, a structural change may be subtle, yet problematic if it goes unnoticed by the developer. Use automated tests in your favor :)</p><p>More info on this <a href="https://signalvnoise.com/posts/3113-how-key-based-cache-expiration-works">article by DHH</a>.</p><h4>Algorithmic optimization</h4><p>I consider myself an old-school programmer. I like to spend time reading about some optimization problems (like <a href="https://www.quora.com/How-did-game-developers-pack-entire-games-into-so-little-memory-twenty-five-years-ago">this</a> and <a href="http://stackoverflow.com/questions/9229601/what-is-in-c-code">this</a>) that are near to irrelevant in today’s industry.</p><p>But the fact is that, algorithms or data structures that are, at a first glance, better suited for a problem, often present the same efficiency, or even are outperformed by their simpler counterparts, in real world situations. Well, if you don’t believe me (as you shouldn’t), watch this keynote by Bjarne Stroustrup, on a well-known case:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FYQs6IC-vgmo%3Ffeature%3Doembed&amp;display_name=YouTube&amp;url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DYQs6IC-vgmo&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FYQs6IC-vgmo%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/4736e9c3e496a76117e5a3324e862a74/href">https://medium.com/media/4736e9c3e496a76117e5a3324e862a74/href</a></iframe><p>So, I would mind the complexity of my blocks of code only if they seem extremely bad, or if they deal with a <em>n </em>too large. Otherwise, the rule of thumb here is <strong>trust</strong> and use <strong>your native standard library</strong> as much as possible, especially if you are dealing with Ruby.</p><p>Ruby (and Rails) does not offer all the algorithms and data structures you would find in a textbook. But, trying to outperform the algorithms it does offer has not been an easy task to me.</p><p>A much better way to say whether it is meaningful or not to optimize a piece of code, though, is by <strong>profiling</strong> an application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SkQShUVsCS0-Tm6nvw8NlA.png" /><figcaption>A flame graph.</figcaption></figure><p>Let’s say you just came up with a improvement for an algorithm, what would be the impact on the whole stack, given that realistic requests are being made?</p><p>Or, the other way around, what code fragments have the biggest impact on your stack, so we should look for an optimization?</p><p>We need answers to both questions. If you’re are a Ruby dev, playing around with <em>rack-mini-profiler</em> is definitely a good starting point. Install the gem and run (even locally) your app in production mode. Study your results and have fun exploring the flame graphs (like the one above 😛).</p><h4>Functional style pays off</h4><p>This topic is the most experimental one (for me) on this article.</p><p>An advantage of using Ruby is that your OO code can live alongside some well written functional-like code. Developers should not have problems composing functions, as operations like reduce and map are extensively available in the standard library.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/160/1*EA256dD4DhncDA3koixIKw.png" /></figure><p>When the business logic is beyond trivial, which was my case, you might want to start playing with some side effect free functions, composing them as needed, and using some patterns like <a href="http://multithreaded.stitchfix.com/blog/2015/06/02/anatomy-of-service-objects-in-rails/">stateless services objects</a>.</p><p>Why? Things get simpler when you do not use <a href="http://stackoverflow.com/a/12934971">free variables</a>. Otherwise, you need to ensure that all your variables will look good at all times, for any method. Going functional means you just need to worry about your input parameters and what’s being returned (and nothing else), as pure functions are free of side effects.</p><p>But, is functional more performatic? Well, not necessarily. But functional will help you to say on <em>what conditions</em> your performance falls short, while improving predictability and testability, by eliminating those free variables.</p><p>If you want to get more detailed arguments around this topic, check <a href="http://multithreaded.stitchfix.com/blog/2015/06/02/anatomy-of-service-objects-in-rails/">this article</a> by Dave Copeland.</p><h4>That’s it!</h4><p>Those topics helped me as a guideline for optimizing a Rails API. Hope they will be useful for you too!</p><p>If you like this article, also check this following one 😜</p><p><a href="https://blog.codeminer42.com/copycatting-netcat-from-node-js-to-c-fc8eb3fbfcd3">Copycatting netcat: from Node.JS to C</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ecb4fea96e92" width="1" height="1" alt=""><hr><p><a href="https://medium.com/codeminer-42/halving-response-times-lessons-learned-ecb4fea96e92">Halving Response Times: Lessons Learned</a> was originally published in <a href="https://medium.com/codeminer-42">The Miners</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Copycatting netcat: from Node.JS to C]]></title>
            <link>https://medium.com/codeminer-42/copycatting-netcat-from-node-js-to-c-fc8eb3fbfcd3?source=rss-a7bacfa34bc6------2</link>
            <guid isPermaLink="false">https://medium.com/p/fc8eb3fbfcd3</guid>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[c]]></category>
            <category><![CDATA[information-security]]></category>
            <dc:creator><![CDATA[Renato Nascimento]]></dc:creator>
            <pubDate>Tue, 22 Nov 2016 16:35:13 GMT</pubDate>
            <atom:updated>2016-11-23T19:21:54.742Z</atom:updated>
            <content:encoded><![CDATA[<h4>Let’s get to the heart of streams, how they work and some classical problems.</h4><p>Mastering web development is a cruel and endless journey. Part of this endeavor is understanding the dystopian world where all the magic sits on — at the end of the day, all comes down to bytes.</p><p>Let’s get down to some low-level concepts. At the end of this article, I will propose you to write your own web server from scratch, using pure C.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*qWoqHclEzo80XaN2jOUemg.jpeg" /></figure><h4>Just 3 lines of code in Node.JS</h4><p>Using netcat to open a stream is simple. To listen for connections, just type <strong>nc -l &lt;port&gt;</strong>, and to connect to it, open a second terminal (which can be on a second computer), and type <strong>nc &lt;host&gt; &lt;port&gt;</strong>.</p><p>Enjoy your stream.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/659/1*NGUZXMPTS49qS1G65UOy6A.gif" /></figure><p>No big deal. An echo server can be built in Node.JS in a second. This snippet will give us exactly the same behavior:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6608511eb1b3c3a2406883a8103819c7/href">https://medium.com/media/6608511eb1b3c3a2406883a8103819c7/href</a></iframe><p>Wait, there is a .<em>pipe()</em> in there. Is it the same concept of pipe we use in linux, when we need to concatenate processes? Yes. In Unix, <a href="https://en.wikipedia.org/wiki/Everything_is_a_file">everything is a file descriptor</a>, so streaming from/to standard I/O, network sockets or files on the disk is just a matter of redirecting.</p><h4>Dissecting Node.JS pipe</h4><p>So, let’s see what behavior the <em>pipe</em> method implements under the hood. Take a look in this code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dc2293cac0af72700858d28a814dd3b5/href">https://medium.com/media/dc2293cac0af72700858d28a814dd3b5/href</a></iframe><p>Whenever a client sends some data to our socket, a <em>readable</em> event will be fired, and <em>socket.read</em> will be called at least once until the internal buffer is completely drained.</p><p>This code exposes two interesting points about piping in Node.JS. First, it explicitly takes advantage of the unblocking nature of events. Second, it assembles data chunk by chunk, let’s see how this can be useful.</p><h4>Uploading large files</h4><p>Letting your client upload large objects may be not-so-great to your server.</p><p>Most programming languages will only deal with objects entirely loaded in memory. Let’s modify our code a little bit to prevent the resources of our server to be drained when using Node.JS.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7321a5691a3f91f9c18c21ac75d1ce55/href">https://medium.com/media/7321a5691a3f91f9c18c21ac75d1ce55/href</a></iframe><p>This will guarantee Node.JS will not deal with more than <em>CHUNK_SIZE</em> bytes at any given time.</p><h4>The building blocks in C</h4><p>Node.JS features an awesome abstraction of low-level I/O. At this point, we’re just one inch shy of another well-known abstraction, which will give us more understanding of how network communication works: <em>Berkeley Sockets</em>.</p><p><a href="http://cs.baylor.edu/%7Edonahoo/practical/CSockets/textcode.html">This site</a> is a great resource for code examples, where you’ll see these functions with a more detailed context. I’ll use <a href="http://beej.us/guide/bgnet/">this (free!) guide</a> as a reference.</p><p>First, let’s see the function we need for declaring a socket.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8f4ff052ac1af6310b3da131814b09fa/href">https://medium.com/media/8f4ff052ac1af6310b3da131814b09fa/href</a></iframe><p>We’ll use some macros to shape our socket. You can choose either PF_INET or PF_INET6 for your domain, to choose between IPv4 or IPv6. For your type and protocol we will be going with SOCK_STREAM and IPPROTO_TCP. That’s simply because TCP is a protocol designed to provide socket-like connections.</p><p>Now let’s use the file descriptor you have just gotten from the SO. As you’re building a server, let’s bind that socket to a port:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5206164e20a83f15394f8e6ce811f875/href">https://medium.com/media/5206164e20a83f15394f8e6ce811f875/href</a></iframe><p>The first argument is the return value of the socket function — in C, files descriptor are represented by integers. The second is a struct containing an address family, an address and a port, and the third, the size of this struct.</p><p>We now have the equivalent of a <em>net.createServer</em> call on Node.JS. Okay, so the clients are waiting patiently on line to be accepted. Let’s accept() them!</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0cb85a155b34784a57f120aaf36e3082/href">https://medium.com/media/0cb85a155b34784a57f120aaf36e3082/href</a></iframe><p>sockfd is the listening socket. On the other hand, addr is a struct that will help us identify the client we’re accepting. Now, the interesting part: accept() will return a brand new file descriptor, so we may talk to this client <em>in private</em>. The rule is simple: for every new client, a new socket.</p><p>Awesome. Waiting for a client to arrive and be accepted is a blocking operation by default. Past that, it’s time to talk to this client using those two functions:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/95a8f13c0c80020d59f23445ba6bac4f/href">https://medium.com/media/95a8f13c0c80020d59f23445ba6bac4f/href</a></iframe><p>send()ing and recv()ing look very much alike. You just need to use the file descriptor that was given when you accepted the client, and provide a pointer to the first byte you want to send (or to be written), along with how many bytes you want to send (or to be received).</p><p>Just to draw a parallel between this and what we’ve made with Node.JS — see that we have control over the width of our stream, the same way we did with <em>CHUNK_SIZE</em>!</p><p>In summary, that’s how our echo server in C will look like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c5c55bf6cb1fa452daa316ce378937e3/href">https://medium.com/media/c5c55bf6cb1fa452daa316ce378937e3/href</a></iframe><h4>The Heartbleed bug</h4><p>What if I say that now you are able to understand the <a href="http://www.forbes.com/sites/josephsteinberg/2014/04/10/massive-internet-security-vulnerability-you-are-at-risk-what-you-need-to-do/#43271b158c3d">arguably worst vulnerability found since commercial traffic began to flow on the Internet</a>?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/341/1*y_vVKwAJVYou-J4C0arViw.png" /><figcaption>Heartbleed logo.</figcaption></figure><p>Heart<em>beat</em> was an extension protocol in OpenSSL. It acts pretty much like an echo server. When a receiver receives a <em>HeartbeatRequest</em> message, the receiver should send back an exact copy of the received message in the <em>HeartbeatResponse</em> message. If the content is the same, the secure connection will be kept.</p><p>I’ll give a clue of what the bug is. Read the following xkcd cartoon and take a look at the send() and recv() interfaces on the previous section.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*MirIzw2jJy_cZqp0-9ayYg.png" /></figure><p>Exactly. It is perfectly possible that, by mistake, or by induced mistake, you send more bytes than your actual buffer has. Let’s illustrate this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4995d529e8492d5df704e11456b2e595/href">https://medium.com/media/4995d529e8492d5df704e11456b2e595/href</a></iframe><p>This is known as <a href="https://en.wikipedia.org/wiki/Buffer_over-read">buffer over-read</a>, which is commonly associated with languages like C and C++. Take a deep breath and thank Node.JS for making this kind of exploit much less likely to happen to your apps.</p><p>But, to be fair with C, there are tools like Valgrind to prevent an application to have this kind of memory errors, we just need to test them correctly! By the way, if you want to read more about this bug, check <a href="http://heartbleed.com/">its own site</a> and this <a href="https://www.google.com.br/url?sa=t&amp;rct=j&amp;q=&amp;esrc=s&amp;source=web&amp;cd=1&amp;cad=rja&amp;uact=8&amp;ved=0ahUKEwiVyebX17vQAhWIkZAKHY8dBiQQFgggMAA&amp;url=https%3A%2F%2Fwww.ibm.com%2Fdeveloperworks%2Fcommunity%2Ffiles%2Fbasic%2Fanonymous%2Fapi%2Flibrary%2F38218957-7195-4fe9-812a-10b7869e4a87%2Fdocument%2Fab12b05b-9f07-4146-8514-18e22bd5408c%2Fmedia&amp;usg=AFQjCNEbpzMFvURBBQ_Gk85mu_b9EiF6aQ&amp;bvm=bv.139250283,d.Y2I">neat pdf</a> by IBM.</p><h4>Writing your own webserver</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*BDociupMokJ0QMIsXOubAA.gif" /><figcaption>New powers ahead.</figcaption></figure><p>Great job! Now that you know the anatomy behind socket connections, implementing an application protocol like HTTP can be fairly straightforward.</p><p>You may implement a small subset of the HTTP 1.0 protocol, for example. Parse a GET request header, along with any path parameters it may contain, and return a proper header following what was asked —that can be either a plain text or a binary file.</p><p>Some questions will naturally arise.</p><p>How would you fork (or thread) your C application to accept and handle multiple clients? What’s the practical difference between simply forking/threading and having an event loop like in Node.JS? What would be the overhead of streaming with HTTP(S)? How do we improve TTFB (Time to First Byte)? What does the architecture behind CDNs look like?</p><p>The search for those answers will certainly make you a better web developer. 😉</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fc8eb3fbfcd3" width="1" height="1" alt=""><hr><p><a href="https://medium.com/codeminer-42/copycatting-netcat-from-node-js-to-c-fc8eb3fbfcd3">Copycatting netcat: from Node.JS to C</a> was originally published in <a href="https://medium.com/codeminer-42">The Miners</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>