<?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 tngan on Medium]]></title>
        <description><![CDATA[Stories by tngan on Medium]]></description>
        <link>https://medium.com/@tngan?source=rss-dbdb90e67420------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*gHQlFI_Inr0JWyCulHJYTg.jpeg</url>
            <title>Stories by tngan on Medium</title>
            <link>https://medium.com/@tngan?source=rss-dbdb90e67420------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 12:19:13 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@tngan/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[Short note — Regret before push]]></title>
            <link>https://medium.com/@tngan/short-note-regret-before-push-7b327b0d5426?source=rss-dbdb90e67420------2</link>
            <guid isPermaLink="false">https://medium.com/p/7b327b0d5426</guid>
            <category><![CDATA[git]]></category>
            <dc:creator><![CDATA[tngan]]></dc:creator>
            <pubDate>Fri, 28 Sep 2018 16:24:47 GMT</pubDate>
            <atom:updated>2018-09-28T16:24:47.609Z</atom:updated>
            <content:encoded><![CDATA[<p>A rescue manual for you when you regret before you git push</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/220/0*JrEZ-eQqPhUM_OfL.png" /><figcaption>git figure from official website</figcaption></figure><p>When we were young, we must have re-clone the repository without finding a correct way to get rid of git-related issues.</p><p>We use Git for the version control of your code. Once it’s being committed, those changes are staged. However, it is not equivalent to no regret at all. Even though we have the evil command to do force push, git push -f . Don’t use it if you don’t know what you are doing.</p><p>Before we push code to the main branch, for example, dev, staging or master. We mainly work on so-called feature branch, the branch owner maybe only you, so you can do whatever you want to do even hard reset and force push. Practice makes perfect, also practice turns into habit, try to do that as least as possible. Well, then what if you regret after you do commit your code, maybe the typo in the commit messages, or squash the commits with garbage or meaningless messages (e.g. todo, wip, dirty, etc).</p><p>In this note, some useful commands are introduced.</p><p><strong>Reword</strong></p><p>No worry when you want to change the commit messages after you committed the code.</p><pre>&gt; git rebase -i HEAD~1 </pre><p>Then you will see a list of the commit starting from the n commits before the current HEAD whereas n is specified in the command HEAD~n . You will see a list of message.</p><pre>pick 960716e This is the goodest commit ever</pre><p>Change the word from pick to reword then save the file. It will then navigate you into the screen that you are very familiar with which is the commit page.</p><pre>This is the best commit ever<br><br># Please enter the commit message for your changes. Lines starting<br># with &#39;#&#39; will be ignored, and an empty message aborts the commit.<br>#</pre><p>Save it and leave the editor, then your commit message will be rewritten.</p><p><strong>TO BE CONTINUED …</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7b327b0d5426" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Short note — Consistent Hashing]]></title>
            <link>https://medium.com/@tngan/short-note-consistent-hashing-543a726c807d?source=rss-dbdb90e67420------2</link>
            <guid isPermaLink="false">https://medium.com/p/543a726c807d</guid>
            <category><![CDATA[consistent-hashing]]></category>
            <category><![CDATA[caching]]></category>
            <dc:creator><![CDATA[tngan]]></dc:creator>
            <pubDate>Sun, 16 Sep 2018 09:06:19 GMT</pubDate>
            <atom:updated>2018-09-16T09:06:19.821Z</atom:updated>
            <content:encoded><![CDATA[<p>A small trick in the caching</p><h4>The scalability in Discord</h4><p>Recently I am reading an article <a href="https://blog.discordapp.com/scaling-elixir-f9b8e1e7c29b">How Discord Scaled Elixir to 5,000,000 Concurrent Users</a>, the team has found pitfalls while sending messages between Erlang processes, so that they have decided to work on a kind of task distributor that can guarantee the number of call function in the process to be bound by the number of its remote nodes in order to avoid overloading.</p><pre>Discord is a distributed system achieved through <a href="https://en.wikipedia.org/wiki/Consistent_hashing">consistent hashing</a>, so that they can access the data very fast by the node lookup.</pre><h4><strong>Classic caching scenario</strong></h4><p>Well, what is consistent hashing? Let’s think of a common use case. When we want to do image caching to avoid hitting backend flow, or even query your database everytime it needs, you would choose Redis or some other key-value storage services.</p><p>For example, we have 2 Redis instances and want to cache 50,000 images with a <strong>unique file name</strong>, the first thing comes in your head would be,</p><pre>Let&#39;s split the image cache randomly into these two instances. No lazy instance is left.</pre><p>Then the next thing you would do is to design the key-value pair. Apparently, the key is the hash of that file name and we could use the base64 encoded representation of that image. That’s perfect, then we can get the cache next time when we need to find that image but an easy lookup of the hash of a particular file name. But wait</p><pre>We have set up two nodes, hmm, which one should I suppose to get the right record?</pre><p>Of course, looping through all the instances must not be an option. Therefore, it’s time to think about how to do better partitioning. <a href="https://redis.io/topics/partitioning">Hash partitioning</a> is one of the solutions that suggested in Redis documentation. By turning the hash into a number and find the remainder divided by the number of instances (N).</p><pre>file_name |&gt; hashing() |&gt; turn_number() |&gt; rem(N)</pre><p>For example, if the remainder is 0, then we put that cache into instance 1 and etc. If we want to find the image cache next time, we just simply apply the above formula again and find the instance where its cache supposed to be stored. If no cache is found, that means this image has no cache so far. We are good, we successfully split all the cache into all of our instances.</p><h4>Scale to N+/-1</h4><p>The above approach is very simple and easy to implement. However, this approach has an obvious defect. One day when we scale up once the current 2 instances are almost full, we need to set up a new instance, N is increased to 3.</p><p>Imagine we have one image is stored (ID: 25899) in instance 2 before scaling — rem(id, N), now the new formula becomes <strong>rem(id, N+1)</strong>. Another scenario is that one instance which is taken down, the formula becomes <strong>rem(id, N-1)</strong>. These make the image cache no longer to be found in the instance where it is supposed to be stored.</p><pre>rem(25899, 1) = 0 # N-1 after taking down, instance 1 :(<br>rem(25899, 2) = 1 # N   before scaling, instance 2<br>rem(25899, 3) = 0 # N+1 after scaling, instance 1 :(</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/907/0*pfSS8l-K-bXhY6Sd.JPG" /><figcaption><a href="https://commons.wikimedia.org/wiki/File:Avalanche_on_Everest.JPG">Avalanche — <em>Ilan Adler</em></a></figcaption></figure><p>It’s not only about the cache of one single image, it comes across and affects all the image cache. For this reason, we need another approach — <a href="https://en.wikipedia.org/wiki/Consistent_hashing">consistent hashing</a>.</p><h4>Ring-shaped hashing space</h4><p>Consistent hashing is very similar to the one we described above, instead of a dynamic N, we set N equals to 2³² and set the hashing space to be circular (the ring shape). By using the same formula we used, project the image cache into the ring hashing space. The hashes of multiple caches instances are also projected into that space, the hash can be created from its unique identifier like the process identifier or IP address.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/984/1*rXdb_DHgR6Xws5SdloyXiw.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1016/1*o2Jl30GrLUpDltcmxbKUQA.png" /><figcaption><strong>Ring structure hashing space</strong></figcaption></figure><p>According to the above figure, the image cache will be stored to the clockwise closest cache instance. For example, image 1 and 4 are stored in cache 1 whereas image 2 is stored in cache 2. Let’s recall the scaling issue, when we remove or add cache instance into our system, the dynamic N will affect the entire read/write process because the image cache is not fetched into and stored from the cache instance where it is supposed to be there. So, let’s test that with our new approach.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/860/1*mJzN3y_CquySTVVSK3bBlQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/716/1*l1yOb441lG-oss8KblcpDg.png" /><figcaption><strong>N -/+ 1 with consistent hashing</strong></figcaption></figure><p>When cache instance 1 is crashed for some reasons, maybe shutdown accidentally, or overloading, the image 1 and 4 will be needed to map to cache 2 instead of cache 1. Therefore, the affected space is only limited to those images stored in the crashed cache instance instead of the entire system. If we need to add one more cache instance, we just need to map image 4 from cache instance 1 to 4, instead of recomputing the remainder with N=4 then remapping all the images into correct cache 4 with the updated formula.</p><p>This approach tries to minimize the effort we need to handle for and make it easy and efficiently scale up. However, because of the computation of number and modulo operation, an unbalanced distribution could have happened. In other words, some instances might handle very few caches whereas some instances handle a lot.</p><h4>Unbalanced distribution</h4><p>Intuitively, we expect the cache can be evenly distributed in the ring hashing space in order to split the cache in a more balanced way. By adding some “virtual nodes” into the ring which represent the copy of each of the cache instance. Those virtual nodes are not physical instances, they are just a projection in order to make the distribution more balanced.</p><h4>More readings</h4><p><a href="https://www.cs.princeton.edu/courses/archive/fall09/cos518/papers/chash.pdf">Consistent hashing and random trees: distributed caching protocols for relieving hot spots on the World Wide Web — <strong>David Karger</strong></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=543a726c807d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Short Elixir note — Module and Atom]]></title>
            <link>https://medium.com/@tngan/short-elixir-note-module-and-atom-96785102419e?source=rss-dbdb90e67420------2</link>
            <guid isPermaLink="false">https://medium.com/p/96785102419e</guid>
            <category><![CDATA[elixir]]></category>
            <dc:creator><![CDATA[tngan]]></dc:creator>
            <pubDate>Sat, 08 Sep 2018 10:02:43 GMT</pubDate>
            <atom:updated>2018-09-08T10:02:43.613Z</atom:updated>
            <content:encoded><![CDATA[<p>In Elixir world, I almost get inspired every day. I will keep writing a short note to explain some of the interesting topics from day to day.</p><p><strong>Module as an atom ?</strong></p><p>We always call a module function with the following format.</p><pre>defmodule Hi do<br>  def my_name_is(), do: &quot;tngan&quot; <br>end</pre><pre>Hi.my_name_is() # &quot;tngan&quot;</pre><p>As we know, Elixir has a great interoperability with Erlang libraries. Therefore, we can call the function with an atom. It might confuse the newbie that the type of:string.</p><pre>:string.trim &quot;test             &quot; # output is &quot;test&quot;</pre><p>In fact, we can use iex to check this out.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*F9uEw8d4HdsaBkMDIfZZ1w.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xjJV5cJeWAgDuc8vb_guiw.png" /><figcaption>Info of our Hi module and :string module</figcaption></figure><p>Yeah, so the module is an atom with a prefix Elixir in order to differentiate the module from Erlang libraries. In other words, the Elixir module function can be called via:</p><pre>:&quot;Elixir.Hi&quot;.my_name_is() # &quot;tngan&quot;</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=96785102419e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A series of interesting topics in Node.js and Elixir]]></title>
            <link>https://medium.com/@tngan/a-series-of-interesting-topics-in-node-js-and-elixir-a64c171b60fa?source=rss-dbdb90e67420------2</link>
            <guid isPermaLink="false">https://medium.com/p/a64c171b60fa</guid>
            <category><![CDATA[robotics]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[tngan]]></dc:creator>
            <pubDate>Wed, 16 May 2018 12:42:49 GMT</pubDate>
            <atom:updated>2018-05-16T12:42:49.387Z</atom:updated>
            <content:encoded><![CDATA[<p><em>You can cook well only when you know everything about the ingredients.</em></p><p>When a developer is young and lack of experience, his work is error prone, and takes several hours to debug a wrong closing bracket (remember ?).</p><p>When a developer grows, he is used to be extremely hungry all the time, he starts to learn to use different tools and even know how to write multiple languages and able to start his own projects after work.</p><p>Then, what is the real boundary to differentiate a junior and senior developer ? It’s not only about how beauty code he can write, not about how fast he can resolve an issue. Basically, we are not learning the syntax of language, but the context of the language. In other words, it’s how depth you know behind the syntax, every languages has its own purpose, advantages, and holes, you can always learn and practice syntax from time to time, but the most important thing is to learn the context of language, the reasons behind the syntax, the story behind the creation,</p><p>Like most of the developers, we are far aways from the elementary level, however, it seems to be too steep to walk through to next level. Therefore, it’s the perfect time to do vertical learning instead of horizontal exploration. This blog post is an index page of my learning notes for some advanced and interesting topics in Node.js and Elixir world. I hope to get about 10–15 topics done by the end of this year.</p><ul><li>Myth behind Plug</li><li>Fun experiment on EventLoop</li><li>Myth behind quote and macros</li><li>Exploring how V8 engine works</li><li>… To be updated</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a64c171b60fa" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Short Story of my open source journey]]></title>
            <link>https://medium.com/@tngan/short-story-of-my-open-source-journey-d0a48a1b974b?source=rss-dbdb90e67420------2</link>
            <guid isPermaLink="false">https://medium.com/p/d0a48a1b974b</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[single-sign-on]]></category>
            <category><![CDATA[engineering]]></category>
            <category><![CDATA[open-source]]></category>
            <dc:creator><![CDATA[tngan]]></dc:creator>
            <pubDate>Sun, 06 May 2018 14:55:23 GMT</pubDate>
            <atom:updated>2018-05-06T14:55:23.893Z</atom:updated>
            <content:encoded><![CDATA[<h4>Just love to share</h4><p>I have started working on open source project from 2015. As a newbie developer, I dared to share my code (well, actually the word “share” is bi-directional, but never mind when no one knows you).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1021/1*Rt67fYLxbXrXZeu66LKacQ.png" /><figcaption><a href="https://github.com/tngan/samlify">samlify</a> (yes, I use VS Code for daily work)</figcaption></figure><p><strong>Why reinventing the wheel ?</strong></p><p>The project is to build an extensible API of SAML based Single Sign On for Node.js community. In the software world, people always talk about DRY (Don’t Repeat Yourself), there were so many related projects hosted on Github at that moment. In fact, I have not imagined that someone who would even use it after I have made my first commit. The original thought was to practice, share knowledge, flatten the entry learning curve, and make contributions back to the community.</p><p><strong>Receive my first ever PR</strong></p><p>The first pull request was notified after the module was published on NPM for 1–2 weeks. “Yea, someone starts using it” I thought while I was traveling in Taiwan and suddenly got notified at night.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/833/1*Ueg37GtNIYV4hkUhqJbM1g.png" /><figcaption><em>My first ever PR (Summer 2015)</em></figcaption></figure><p>“Aha, spelling and grammar mistake.” Nevertheless, it was still a valid PR and one step forward. All bug reports even word correction are so cool, it implies that the thing I am doing is valuable.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/806/1*Z6zw5-CEPZI4O-eFXQaRIA.png" /><figcaption>The second and third PR (Summer 2015)</figcaption></figure><p>More PRs were on the way in coming few weeks. Those are small bug fixes and fine tune of documentation. That is an extraordinary experience for a junior developer, not only because people are looking at your project, but they contribute back to you by making PR.</p><p><strong>My first open talk and stargazer </strong>⭐</p><p>Unless you can explain thoroughly to your audience, you cannot understand better what you have done. Therefore, I went back to my alma mater to have a night talk about this project. Eventually, I got the first stargazer unexpectedly one month later. The number of stars doesn’t mean your code quality is better, it’s also a form of encouragement for those developers who spend their own time (most likely the sleeping time) to make the world better.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/466/1*F8zOXLWilTB55BBH4zDmwQ.png" /><figcaption>Night talk in Open Innovation Lab (Sep 2015)</figcaption></figure><p><strong>Continuous improvement</strong></p><p>When this project got more attentions, I have tried to respond the issues and pull requests as fast as I can, probably within 30 minutes after the issue was opened. Meanwhile, the code base is kept refactored, integrated with new skills (TypeScript) and after 1 year. More and more interesting little stories happened last year like the <a href="https://stackoverflow.com/questions/46276648/how-to-secure-endpoints-in-sso-integration-using-samlify-in-node-js">first question in Stack Overflow</a>, <a href="https://www.whitehats.nl/blog/xml-signature-wrapping-samlify">the first CVE report</a>. All bug reports, feature requests, security vulnerabilities drive and motivate further development from day to day.</p><p><strong>Get support for coffee and books </strong>☕ 📚</p><p>An unexpected email was from somewhere in one morning last year, then I have received my first free coffee. This project is now sponsored by <a href="https://auth0.com/">Auth0</a>, a company which provides Single Sign On and token based authentication service.</p><p>Most importantly, all the credits ❤️ go to those developers developing those <a href="https://github.com/tngan/samlify/blob/master/package.json#L35-L61">dependencies</a> I am taking from the community, this project won’t exist without their contributions. Developing, maintaining open source project is really tough indeed, we learn from community and contribute back. <strong>Be humble as a learner, be hungry as a child, be curious as an adventurer.</strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d0a48a1b974b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>