<?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 Tiago Matos on Medium]]></title>
        <description><![CDATA[Stories by Tiago Matos on Medium]]></description>
        <link>https://medium.com/@ktachyon?source=rss-a3a8b40abe95------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*K7HYZMUM0rseTolZLPE6vA.jpeg</url>
            <title>Stories by Tiago Matos on Medium</title>
            <link>https://medium.com/@ktachyon?source=rss-a3a8b40abe95------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 22:20:31 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ktachyon/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[Proving the Earth is not flat using just math]]></title>
            <link>https://medium.com/@ktachyon/proving-the-earth-is-not-flat-using-just-math-21ba748ebac2?source=rss-a3a8b40abe95------2</link>
            <guid isPermaLink="false">https://medium.com/p/21ba748ebac2</guid>
            <category><![CDATA[flat-earth]]></category>
            <category><![CDATA[debunking]]></category>
            <category><![CDATA[math]]></category>
            <dc:creator><![CDATA[Tiago Matos]]></dc:creator>
            <pubDate>Thu, 31 May 2018 02:48:24 GMT</pubDate>
            <atom:updated>2019-06-07T10:57:59.215Z</atom:updated>
            <content:encoded><![CDATA[<p>Flat Earthers are getting desperate. Selecting very specific data to try to prove their point or disprove the globe earth. Still, one of the sciences they have been using to prove their model is math, specifically trigonometry. What they don’t realise is that trigonometry is actually their worst enemy when attempting to prove the flat earth model.</p><p>One of the things flat earthers believe is that the Sun doesn’t go below the horizon. Even though it’s clearly visible from basically anywhere that the Sun disappears bottom first, they insist that the Sun actually moves so far away that it stops being visible by the naked eye. Though this is clear to anyone that is not blind, for this article, we’re going to imagine that’s the case.</p><p>Also, this requires the apparent size of the Sun to change. This doesn’t happen either, but they insist it does. We’re going to imagine that’s the case too.</p><p>So, to start with, we need some initial data:</p><ul><li>Diameter of the Flat Earth… Let’s say it’s the same as the circumference of the earth: 40 000 km</li><li>Distance to the Sun: 3 862 km (according to <a href="https://wiki.tfes.org/Distance_to_the_Sun">https://wiki.tfes.org/Distance_to_the_Sun</a>)</li><li>Angular size of the Sun: 32 arc minutes (0.54º)</li><li>Angular limit of the human eye: 1 arc minute (0.01666º)</li></ul><p>According to the flat earth model, the Sun will be rotating above the Earth in some kind of spiral, going in and out to “explain” the equinoxes, but for this, using the average position of the Sun is enough. All we need is the distance between the Sun position at noon and midnight in the same place. That should be half of the flat earth diameter, so 20 000 km.</p><p>So, based on this data, we will want to check if it’s possible to have the Sun floating above the flat earth and it’s angular size so small that’s no longer possible for the human eye to see.</p><p>So, at noon, the Sun will be right above us. And we’re assuming midnight will be the time the Sun “sets” (though it clearly happens before — just giving the flat earth model far more space than needed to try to make it work).</p><p>For starters, the easiest thing to figure out: How far away would the Sun need to be in order to stop being seen? Knowing that the Sun’s angular size is 0.54º (32 arc min), let’s calculate the size of the Sun:</p><blockquote>α = 2 * arcsin( d / 2D )<br>D = 3 862 km<br>α = 0.54º</blockquote><blockquote>0.54º = 2 * arcsin( d / (2 * 3 862) )<br>0.27º = arcsin( d / 7 724 )<br>0.00471237 = d / 7 724<br>d = 36.39834588 km</blockquote><p>So, at that altitude, for that apparent size, the Sun has to be about 36.4 km in diameter. Knowing that we can now calculate the distance it needs to be from the observer in order to have it’s angular size be 1 arc minute (or 0.02º):</p><blockquote>α = 2 * arcsin( d / 2D )<br>d = 36.39834588 km<br>α = 0.02º</blockquote><blockquote>0.017º = 2 * arcsin( 36.39834588 / 2D )<br>0.0085º = arcsin( 36.39834588 / 2D )<br>0.00014835 = 36.39834588 / 2D<br>2D = 245 354.539130435<br>D = 122 677.269565217 km</blockquote><p>Now, that’s the distance at an angle, so we need to calculate it’s projection on the ground using the pythagorean theorem:</p><blockquote>H² = O² +A²<br>H = 122 677.269565217 km<br>O = 3 862 km</blockquote><blockquote>122 677.26956521⁷² = 3 862² + A²<br>A² = 15 049 712 467.9769 -14 915 044<br>A = sqrt(15 034 797 423.9769)<br>A = 122 616.464734459 km</blockquote><p>We just placed the Sun outside the Flat Earth, so that’s not going to work. But we do have a maximum distance we want it to be at: 20 000 km. So, at that distance what is the angular position of the Sun?</p><p>Again, using the pythagorean theorem:</p><blockquote>O = 3 862 km<br>A = 20 000 km</blockquote><blockquote>H² = 3 862² + 20 000²<br>H = sqrt(414 915 044)<br>H = 20 369.4635177267 km</blockquote><p>Now, to find the angle:</p><blockquote>sin(a) = O / H<br>O = 3 862 km<br>H = 20 369.4635177267 km</blockquote><blockquote>sin(a) = 3 862 / 20 369.4635177267<br>α = sin^-1(0.1895975315)<br>α = 10.92929754º or 655.75785239775 arc minutes</blockquote><p>So, if, somehow the Sun disappeared it would still be about 11º above the horizon. Or, in another terms, you could fit the apparent size of the sun at noon nearly 22 times between the position where the Sun would disappear and the horizon… or 656 times what it should look like at that point. Seems too much, right?</p><p>Let’s try something else. When would the Sun be so close to the horizon that a human would no longer be able to see it? For that we need to calculate the position of the Sun when it’s perspective would place it at 0.017º (or 1 arc minute) above the horizon:</p><blockquote>tan(a) = O / A<br>a = 0.017º<br>O = 3 862 km</blockquote><blockquote>tan(0.017º) = 3 862 / A<br>A = 3 862 / 0.00029671<br>A = 13 016 076.3034613 km</blockquote><p>This is impossible, right? There’s no way the Sun could get that far on a flat earth model.</p><p>Still, we’re using the distance that the Flat Earth Society is saying the Sun is at from the ground. Maybe they’re wrong. Maybe there’s a distance that fits, right?</p><p>1000 km up? Nope… that would require 3 370 294 km to get close enough to the horizon.</p><p>100 km up? Nope… that would still require 337 029 km.</p><p>10 km up? Nope… still requires 33 703 km. And that would require the Sun to be below the altitude of flight of comercial planes.</p><p>Let’s calculate the actual value:</p><blockquote>tan(0.017º) = O / 20 000<br>O = 0.00029671 * 20 000<br>O = 5.9342 km</blockquote><p>Ok, so we found out how high the Sun needs to be for the flat earth model to work, right? Not quite…</p><p>So, at 5.9 km up, with an apparent size of 32 arc min (or 0.54º), what would the distance for the Sun to cross the limit of the human vision (1 arc min, or 0.017º)? Let’s calculate the new sun size:</p><blockquote>α = 2 * arcsin( d / 2D )<br>D = 5.9342 km<br>α = 0.54º</blockquote><blockquote>0.54º = 2 * arcsin( d / (2 * 5.9342) )<br>0.27º = arcsin( d / 11.8684 )<br>0.00471237 = d / 11.8684<br>d = 0.05592829211 km (about 56 meters)</blockquote><p>And, it would disappear at a distance of…</p><blockquote>α = 2 * arcsin( d / 2D )<br>d = 0.05592829211 km<br>α = 0.017º</blockquote><blockquote>0.017º = 2 * arcsin( 0.05592829211 / 2D )<br>0.0085º = arcsin( 0.05592829211 / 2D )<br>0.00014835 = 0.05592829211 / 2D<br>2D = 377.0023061004<br>D = 188.5011530502 km</blockquote><p>So, the Sun wouldn’t be visible after moving about 188.5 km since noon (377 km total of daylight). Those would be some really short days, right?</p><p>Well… maybe the apparent size of the Sun is wrong. Everyone is tricking flat earthers these days. Since we already know a lot of data should be easy to extrapolate the diameter of the Sun:</p><blockquote>α = 2 * arcsin( d / 2D )<br>α = 0.017º<br>D = 20 000 km</blockquote><blockquote>0.017º = 2 * arcsin( d / (2 * 20 000) )<br>0.0085º = arcsin( d / 40 000 )<br>0.00014835 = d / 40 000<br>d = 5.934 km (oh, boy… this is going to be funny)</blockquote><p>And it’s apparent size at noon would be…</p><blockquote>α = 2 * arcsin( d / 2D )<br>d = 5.934 km<br>D = 5.9342 km</blockquote><blockquote>α = 2 * arcsin( 5.934 / (2 * 5.9342) )<br>α = 2 * arcsin( 5.934 / 11.8684 )<br>α = 2 * arcsin( 0.4999831485 )<br>α = 2 * 29.99888512<br>α = 59.99777024º (112.5 times the real angular size of the Sun — or… basically take over 33% of the sky)</blockquote><p>There you go, ladies and gentleman! You just need to make the Sun have 5.9 km in diameter so it takes up about 33% of the sky, have it float at just 5.9 km high and somehow have it slow down when passing over somebody’s head and you nearly have something that resembles what actually happens in the real globe earth. I mean, ignoring the fact that the sun actually disappears behind the horizon, that it doesn’t change size, and that you can’t actually see it from above when on a plane or on a somewhat tall mountain… Oh, and you’ll also need to make the moon 112.5 times bigger so we don’t lose the eclipses… and have it float around the same place…</p><p>Also, the mountain peaks over 3 km high in the path of the sun probably wouldn’t exist as those would probably be destroyed. And you could actually see the sun from above… and probably turn it into an oven.</p><p>You know what would actually work? If you flattards stopped being so ridiculous…</p><p>Best regards,<br>Globe earth “believer”.</p><p>EDIT: I was using <em>arctan</em> instead of <em>arcsin</em> before, so I wasn’t even considering the sun as a sphere. Now it is, which makes the results more accurate and even worse for the flat earth case.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=21ba748ebac2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Faster Sieve of Eratosthenes]]></title>
            <link>https://medium.com/mathematica-stories/faster-sieve-of-eratosthenes-e705ca2156d9?source=rss-a3a8b40abe95------2</link>
            <guid isPermaLink="false">https://medium.com/p/e705ca2156d9</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[prime-numbers]]></category>
            <category><![CDATA[mathematics]]></category>
            <category><![CDATA[math]]></category>
            <dc:creator><![CDATA[Tiago Matos]]></dc:creator>
            <pubDate>Tue, 20 Dec 2016 15:01:50 GMT</pubDate>
            <atom:updated>2016-12-20T15:12:23.846Z</atom:updated>
            <content:encoded><![CDATA[<p>About a decade ago I was participating in a lot of programming challenges and one of those challenges was posted on a programming forum where people were interested in finding out who could make the most optimized version of common mathematical problems.</p><p>One of those common math problems is the ability to find out prime numbers. I took the challenge and implemented an optimized sieve of eratosthenes by assuming that the number 2 was a prime and halving the memory space required, like many others. My solution was the second fastest implementation of all the participants, according to the benchmarks, but both the first and second fastest solutions were essentially the same. But while the participant’s solutions were being benchmarked, I found a very discrete pattern that allowed me to come up with another solution that was, by far, faster than any solution presented in the challenge. I still got second place because the challenge “owner” (and winner of the challenge) didn’t want to benchmark my new solution.</p><p>Disclaimer: I’m not a mathematician. I’m a coder who happened to trip on this while participating on a challenge and that kept investigating up to a certain point.</p><p>My solution starts with the assumption that everyone knows that <strong>the numbers 2 and 3</strong> are prime numbers. Everyone knows that, past the number 2, only odd numbers can be prime. But I took it one step further by eliminating all odd numbers that are multiple of 3 as well.</p><p>A list of points:</p><ul><li>Multiples of 3 are either odd or even (thanks Captain Obvious);</li><li>Even multiples of 3 are composed by multiplying 2 and 3, so they’re essentially multiples of 6;</li><li>There are no other odd numbers besides those before and after 6 that are not multiples of 3(because around odd multiples of 3 you’ll only find even numbers and with the odd numbers found around multiples of 6, you’re basically covering every number possible);</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/479/1*bKiFufyZAjFvWRcyEtKWYA.png" /><figcaption>There are no other odd numbers besides those before and after 6 that are not multiples of 3</figcaption></figure><p>By doing this, you effectively cut a nice chunk of the memory requirements… except you can’t actually know how many memory slots to jump like this. Or can you?</p><p>The sieve is split into two columns: one that is <em>6*n-1</em> and other that is <em>6*n+1</em>. Lets remove all multiples of 5 from the columns:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/361/1*63LpTWnMIdT-8Sz_2MO0Cg.png" /><figcaption>Removing multiples of 5 from both columns</figcaption></figure><p>Do you see a pattern? It can be expressed as <em>6*(5*n+1)-1 </em>and <em>6*(5*n–1)+1</em>. Lets try to do the same for the number 7:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*ViCqQKJAOMdvQvvrEUgM4Q.png" /><figcaption>Removing multiples of 5 and 7 from both columns</figcaption></figure><p>Notice the formulas become inverted. For this case we have to express is as <em>6*(7*n+1)+1 </em>and <em>6*(7*n–1)-1</em>. And the pattern gets messier when we move towards the second row of prime numbers:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/480/1*nVnzjMpe2X7u2hE5yl5hhg.png" /><figcaption>Removing multiples of 5, 7, 11 and 13 from both columns</figcaption></figure><p>This breaks our distance between non primes for the same prime across columns. Now the formulas for 11 are <em>6*(11*n+2)-1 </em>and <em>6*(11*n–2)+1</em> and for 13 are <em>6*(13*n+2)+1 </em>and <em>6*(13*n–2)-1</em>.</p><p>But there’s still a pattern there. If you keep doing this for the next set of numbers, you’ll notice that the value that changes is incremented every time we move one row. E.g.: the formulas for 17 and 19 are <em>6*(17*n+3)-1,</em> <em>6*(17*n–3)+1 </em>and <em>6*(19*n+3)+1,</em> <em>6*(19*n–3)-1 </em>respectively. So, the change between these formulas are dependent on another variable: the index of the number in the column.</p><p>Knowing this, implementing a sieve that works the same way as the existing sieves becomes easy. But we can still make these formulas simpler. Lets call <em>pc1</em> and <em>pc2</em> the variables that correspond to prime numbers in the columns 1 and 2 respectively, and <em>i</em> the index (non zero based) where we can find the prime number. From what we know from before, this is what we have:</p><p><em>6*(pc1*n+i)-1, 6*(pc1*n-i)*n+1</em></p><p><em>6*(pc2*n+i)+1, 6*(pc2*n-i)-1</em></p><p>The <em>i</em> value can actually be calculated by applying a simple division by 6 on the closest multiple of 6 to the prime number (because its the <em>nth</em> multiple of 6 which this system is based on), which means that, respectively for each column:</p><p><em>i = (pc1+1)/6</em></p><p><em>i = (pc2–1)/6</em></p><p>So:</p><p><em>6*(pc1*n+(pc1+1)/6)-1, 6*(pc1*n-(pc1+1)/6)+1</em></p><p><em>6*(pc2*n+(pc2-1)/6)+1, 6*(pc2*n-(pc2–1)/6)-1</em></p><p>And because we should always simplify our formulas:</p><p><em>6*pc1*n+pc1, 6*pc1*n-pc1</em></p><p><em>6*pc2*n+pc2, 6*pc2*n-pc2</em></p><p>So, notice that now we have the same exact formulas for both columns, which means that we should discard the <em>pc1</em> and <em>pc2</em> nomenclature with something more generic such as <em>p</em>:</p><p><em>6*p*n+p, 6*p*n-p</em></p><p>We still have two formulas, one for the column matching the prime number and other for the opposite (respectively) which is enough to simplify the sieve implementation, but mathematically speaking, both formulas can be condensed into a single formula that yields two results:</p><p><em>6*p*n±p</em></p><p>Whatever the prime number <em>p</em>, this formula will always provide two <strong>non-prime</strong> numbers (for each iteration of <em>n)</em> next to multiples of 6 where <em>p</em> is one of its factors.</p><p>Using this strategy it’s not only possible to reduce the memory needs by 2/3 against a common sieve, but also save a lot of cycles and memory reads and writes.</p><p>You can find my implementation of this sieve @ <a href="https://github.com/KTachyon/FastPrimeSieve">https://github.com/KTachyon/FastPrimeSieve</a></p><p>The repo contains a XCode project, but you just need to compile the main.m file to get it running. On my machine it calculated the sieve for the first billion numbers in just under 4 seconds.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e705ca2156d9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/mathematica-stories/faster-sieve-of-eratosthenes-e705ca2156d9">Faster Sieve of Eratosthenes</a> was originally published in <a href="https://medium.com/mathematica-stories">Mathematica Stories</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[JavaScript concurrency model]]></title>
            <link>https://medium.com/@ktachyon/javascript-concurrency-model-dc98dacab527?source=rss-a3a8b40abe95------2</link>
            <guid isPermaLink="false">https://medium.com/p/dc98dacab527</guid>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[javascript]]></category>
            <dc:creator><![CDATA[Tiago Matos]]></dc:creator>
            <pubDate>Thu, 24 Nov 2016 17:43:00 GMT</pubDate>
            <atom:updated>2016-11-24T17:43:00.301Z</atom:updated>
            <content:encoded><![CDATA[<p>Many developers look at JavaScript and think that, since it seems to have no threading ability it can’t multitask, but that is not really true. JavaScript has actual threading support by using <em>Web Workers</em>, and Node.js provides the ability to <em>fork</em> a process and a module called <em>cluster</em>, both allowing the development of multiprocess applications.</p><p>While those solutions exist, they are beyond the scope of this article, because JavaScript concurrency is much more than just threads and processes since <strong>it can actually multitask using a single process with a single thread</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/1*PueLSvGobiiL2Apgmj_wxw.jpeg" /></figure><p>You should be familiar with how applications are developed using a <em>run loop </em>to keep the software alive and running some code when there is some kind of user interaction. JavaScript has something similar, but people call it the <em>event loop</em>. While both concepts are quite similar, the <em>event loop</em> is a little bit more granular right out-of-the-box than most <em>run loop</em> implementation found in many frameworks.</p><p>In JavaScript, when an event happens, the <em>event loop</em> will queue some code that will run in response to that event. Every time there’s availability to run something, it will run the next task on the queue and it will <strong><em>run to completion</em></strong> before running another task.</p><p>JavaScript is an asynchronous, I/O bound language. This means that anything that requires some sort of I/O (user input, read from disk, network response, responses from other processes,… — really anything you’d like to turn into I/O) should be handled asynchronously. So, you register what you want to do once you get the data from an I/O source and the <em>event loop</em> will call that code when that event happens. This allows for the <em>event loop</em> to run some other code it might have on the queue while waiting for events to happen. That’s how you can achieve concurrency using a single process, single thread application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/638/1*Te1HNFdDpgCn9ImQivtwRA.jpeg" /><figcaption>JavaScript event loop</figcaption></figure><p>As an example, I took the usual approach a lot of Java/Android developers use to get information from a server:</p><pre>HttpGet httpGet = new HttpGet(url);<br>HttpResponse httpResponse = httpClient.execute(httpGet);<br>doSomethingWith(httpResponse);</pre><p>If you look closely at those lines, you’ll see that you’re executing the get request on the right side of the second line and expecting the response to be returned to the variable on the left side. If the server takes 5 seconds to respond, that second line will take 5 seconds to execute. So you’re blocking the execution of your program at that point.</p><p>Obviously, you can’t run that on your main thread because if you do, the user won’t be able to do anything with your app while it’s waiting for the server to respond, so you’re basically going to create a new thread (or some abstraction that will create a thread for you, such an AsyncTask) and run that code there. That new thread will get blocked during the execution of the request.</p><p>In JavaScript, you’d do something like this (using the <a href="https://www.npmjs.com/package/request">request</a> library):</p><pre>request(url, function(error, response) {<br>    <strong>if</strong> (error) { handleError(error); }<br>    else { doSomethingWith(response); }<br>});</pre><p>Here, you’re passing a callback function as an argument. That callback will be registered to run by the <em>event loop</em> when the respective event happens. Unlike what you saw on the Java example where you need a separate thread to be able to wait for the server response, the single JavaScript thread won’t block while waiting for the server to answer.</p><p>Notice that, since there’s not any real concurrency (as in, there’s nothing running at the same time), there are no low level racing conditions that could cause conflicts at the memory level. Also, there is no thread creation latency, since there’s no need to create threads to get an asynchronous flow.</p><p>Keep in mind that JavaScript’s <em>event loop</em> is great for small tasks that won’t take over a lot of CPU time, because if the task takes too long, you’ll basically block the <em>event loop</em> from running more tasks until the bigger task is completed. If you really need to do large computations, you should consider using one of the approaches I have pointed in the first paragraph, or turn that computation into I/O.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dc98dacab527" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building a distributed web platform]]></title>
            <link>https://medium.com/@ktachyon/building-a-distributed-web-platform-4d57e0f54402?source=rss-a3a8b40abe95------2</link>
            <guid isPermaLink="false">https://medium.com/p/4d57e0f54402</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[software-architecture]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[distributed-systems]]></category>
            <category><![CDATA[cloud-computing]]></category>
            <dc:creator><![CDATA[Tiago Matos]]></dc:creator>
            <pubDate>Thu, 10 Nov 2016 18:17:44 GMT</pubDate>
            <atom:updated>2016-11-10T18:19:04.770Z</atom:updated>
            <content:encoded><![CDATA[<p>For a long time, web developers have been developing dynamic web platforms using stacks like LAMP with all the layers running on the same server. While the approach has been validated by many, at some point, when it becomes successful, it becomes impossible to scale without changing the entire architecture of the platform.</p><p>In the past few years, a lot of JavaScript frameworks have spawned (Angular, Ember, Backbone, React,…) and some developers have adopted those frameworks to start moving part of the computing requirements they had on their servers to the clients’ web browsers, allowing for developers to tap into the potential computing power of their clients’ computers.</p><p>While this technique is not new (it’s called <strong>thin client</strong> and the approach has been used since in the 80s using terminals to access multi-user mainframes), it’s one approach to distributing the load of your server in order to have more resources to serve more clients.</p><p>By moving your interface assembly to the browser brings you another benefit. In computer science there is one design principle called <strong>Separation of Concerns</strong> (or <strong>SoC</strong>, for short) which consists of separating a computer program into distinct sections, where each section addresses a separate concern. Assembly of the interface and handling business logic benefit from this separation and by moving all the interface to the web browser, you’re basically forcing that all interface decisions must be implemented in a very distinct section of the code. And, with that, all your server has to do now is to manipulate and deliver data back to the client.</p><p>While moving all the interface assembly to the browser frees up some resources in your server, potentially allowing you to serve more clients, this does not provide you with an architecture that has the potential to grow indefinitely. There is still a limit on how many clients you can serve based on how powerful your server is. Even if you fully optimize your code and upgrade your hardware to have the best server available, your platform will still hit a limit (besides the fact that buying the best hardware is extremely pricey). The ideal solution is to be able to add more servers.</p><p>In order to add more servers to deal with business logic we need to separate the state from the logic. State is everything that is usually stored somewhere (databases, filesystems,…). Anything that would usually be stored on the server must be moved somewhere else where it can be accessed by every server dealing with business logic.</p><p>By separating the storage means from the servers where you apply your business logic, you’re making your business logic server stateless. By making it stateless you gain the ability to add as many servers you’d like (behind a load balancer) and get tons of scalability. Besides that, you gain the ability to update your servers without any downtime (well… most of the time).</p><p>Finally, we must deal with the persistence/storage layer (databases and filesystems). After the previous architectural decisions are in place, the storage is the only place that has no infinite scalability. I haven’t had the need to really explore this beyond what I have been working with, so it’s possible there are a lot other solutions to this than what I’m able to describe here.</p><p>Regarding file storage, there is not much to say. All you need is to have some sort of distributed file system (also known as DFS) or cloud based storage service. There are a lot or services that can provide you with this out-of-the-box, such as Amazon S3, Google Cloud Storage and Microsoft Azure.</p><p>Storage is somewhat easier to scale because there are no real relationships between files and you can spread the filesystem across multiple machines. This it’s not something new: RAID storage systems have existed for a long time and they have been solving these kinds of problems by splitting data across multiple drives. Distributed file systems are just one level above that.</p><p>As for databases, there are few approaches depending on the type of database you’re working with. For the past couple of years I have been working with ElasticSearch (for document oriented databases) and PostgreSQL (for relational databases).</p><p>Non-relational databases are easily distributed, because of the lack of relations between data, much like the files in a filesystem. An ElasticSearch index is easily distributed across multiple machines and setting up an ElasticSearch cluster is quite simple to do and it provides with all the redundancy you need to keep your data safe. Most document oriented / NoSQL solutions out there provide sharding and clustering configuration which make them distributable by default.</p><p>The real issue here is relational databases. Relational databases where never built to scale. They were designed to maintain data integrity and integrity checks within a distributed environment are just not feasible. The solution most people will point to is to buy better hardware for the database server and hope it can handle the load. While I don’t disagree with this, there is still something you can do before you actually need to spend a lot of money on a shiny new server.</p><p>To mitigate database load you can setup a Master-Slave replication environment. With this you can have multiple copies of the database from where you can read and all the writes happen on the master. The master is responsible to write all the changes back to the slaves.</p><p>Reading from a database is far more common that writing in most use cases, so this allows the developer to reduce the load on the place where all the writes happen and distribute the reads across multiple machines. Keep in mind that while this might solve problems related with number of request to a database, it won’t solve problems related with the size of your indexes.</p><p>Relational databases can also be sharded, but most solutions for that come with a really high price tag and it’s something I have never bothered to explore.</p><p>Anyway, if you’re starting a new project and believe your database might need to scale one day, you should try to avoid relational databases.</p><h3>Summary</h3><p>So, if you want to start building a platform which you’ll want to scale in the future, these are a few things you should do if you want to get ready for the future:</p><ul><li>Move the UI building to the web browser; The server should only have to deal with data</li><li>Separate your persistence (file storage and databases) from your business logic to make it stateless</li><li>Use a distributed file system or cloud based storage service to store your files</li><li>If possible, use a non-relational database</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4d57e0f54402" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>