<?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 Bharat on Medium]]></title>
        <description><![CDATA[Stories by Bharat on Medium]]></description>
        <link>https://medium.com/@bharatv90s?source=rss-9ce8429df334------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*ua_quw7htk0ZNIq18iAm7w.png</url>
            <title>Stories by Bharat on Medium</title>
            <link>https://medium.com/@bharatv90s?source=rss-9ce8429df334------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 17 Nov 2026 15:59:58 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@bharatv90s/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[System Design for Highly Scalable Distributed Gaming Leaderboard]]></title>
            <link>https://bharatv90s.medium.com/system-design-for-highly-scalable-distributed-gaming-leaderboard-74145740aa4d?source=rss-9ce8429df334------2</link>
            <guid isPermaLink="false">https://medium.com/p/74145740aa4d</guid>
            <category><![CDATA[aws]]></category>
            <category><![CDATA[mysql]]></category>
            <category><![CDATA[redis]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[rest-api]]></category>
            <dc:creator><![CDATA[Bharat]]></dc:creator>
            <pubDate>Sun, 28 Aug 2022 19:18:13 GMT</pubDate>
            <atom:updated>2022-08-28T19:18:13.677Z</atom:updated>
            <content:encoded><![CDATA[<h3>Online Gaming Leaderboard</h3><p>Online games show a leaderboard showcasing the top 10/100 players on their platform. Most of these games are online and the leaderboard keeps getting updated in real-time. Assume that a maximum of a million users will be logged into your gaming platform at any given time. The game may take 5 minutes or even an hour to complete. When the game is over, the score of that individual will be added to the leader board and only the top 100 players will be shown to the gamers. The leaderboard is an always-on widget even while the user is playing a game on your system. This leaderboard has 3 sections, (a) all-time high score, (b) last week&#39;s high score, and © last hour&#39;s high score. The all-time high score is just a static list and the score of each user gets updated as soon as they complete the game. But the (b) and © are a dynamic list created from a moving time window of the last 1 week and last 1 hour.</p><p>How would you design your system while addressing the below main requirements?</p><ol><li>The latency of the leaderboard response should be under 100 milliseconds.</li><li>Leader board API could be called with as high as 100K requests per minute frequency</li><li>Leaderboard API response should be consistent across all the gamers</li><li>When any game completes, the leaderboard should get updated within the next second across all the gamers</li><li>Redundancy and fault tolerance. If my application server or database or network infrastructure crashes, the leaderboard should continue to run.</li><li>Need to bring the service back online in the fastest possible time in case of a crash The design should include the choice of technology (language if you may, database choices, etc..) you will use to create this setup based on your experience. It should also include thoughts on how it will be deployed on a cloud infrastructure to cater to automatic scaling requirements. For the above performance, concurrency and redundancy requirements specify how will you plan for the capacity of your infrastructure.</li></ol><h3>Solution</h3><p>In summary of the solution, will be implementing a solution based on the -</p><ul><li>Persistent storage to store scores based on events (in our case ending of a game)</li><li>Cache with a data structure to implement the effective retrieval of top K players</li><li>Application logic exposed in form of rest-based microservices -</li><li>UPDATE SCORE based on Player Id and Points</li><li>LEADER BOARD based on the time range (ALL/WEEK/HOUR)</li></ul><h4>Data Modelling</h4><p>As in our problem we are more focused on the idea of solving, leader board problem, I am focusing on that part of data modeling right now.</p><p>I have chosen MySQL as my preferred choice of the database here cause it’s consistent with high throughput on aggregation with properly indexed fields.</p><p>Other honorable mentions could be -</p><ul><li>MongoDB (with imperative query language and power of map-reduce that’s highly efficient for aggregations)</li><li>OpenTSDB (time-series database, still exploring more about the use case and practical usage, especially for monitoring part along with Grafana)</li></ul><p>Below is the E-R diagram for the score table -</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/581/0*xpc_HsehdO1tUyU6.png" /></figure><h4>Capacity Estimation</h4><p>Database</p><p>Taking into consideration our Scores table, the max size per entry will be somewhere around -</p><p>Integer fields = 4 x 3 = 12 bytes Timestamp = 4 bytes Total = 16 bytes MAX per entry</p><p>Approximately for 1 Million users, we have active games suppose 1000 per user, which comes out to be 1 Billion as an estimate for records</p><p>16 x 1 Billion = 16 GB (table size)</p><p>We can have either master-slave configuration of MySQL replicas for failsafe or we can keep all master configurations in circular replication providing better fault tolerance. Cache</p><p>For REDIS the memory footprints are pretty standard To give you a few examples (all obtained using 64-bit instances):</p><p>An empty instance uses ~ 3MB of memory. 1 Million small Keys -&gt; String Value pairs use ~ 85MB of memory. 1 Million Keys -&gt; Hash value, representing an object with 5 fields, uses ~ 160 MB of memory.</p><p>Considering we have 3 buckets — ALL/WEEK/HOUR = 160 x 3 = 480 or max 512 MB RAM</p><p>In terms of distributed cache replicas, we can keep 2–3 of them.</p><h3><strong>Application</strong></h3><p>On the application side it’s pretty straightforward in terms of application clusters, to start we can have auto-scaling instances configured in AWS elastic beanstalk, for the start we can boot our application from 4 server instances running on docker containers.</p><p>Java Spring Boot Application -&gt; Image (registered to docker hub or another private repository)-&gt; Container (used by EBS along with credentials/configurations)</p><p>Load balancer — AWS ELB (Elastic Load Balancer) works fine in case of request load distribution as well, or we can alternatively use a slightly more cost-effective tool — NGINX</p><p>Optional (but good to have) — Varnish is the best tool for request collapsing to overcome the “stampede effect” on our leaderboard API for performance improvements.</p><h3>System Architecture</h3><p>Following is the design proposed for the solution majorly built on AWS Cloud.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*3zp7FrGyz6z0qZqj.png" /></figure><h4>Technology and Deployment Flow</h4><p>Considering our version control of source code to be git and using cloud repository provider GitHub, PFB is my proposed solution for the same -</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*A3SKCkVbwz3bpIrV.png" /></figure><h4>Solution Implementation</h4><p>In terms implementation module there are two main parts of it -</p><p><strong>Part 1: Updates</strong></p><p><strong>Section A. INITIAL DATA SET CREATION</strong> This will be a two-step process in 3 different categories of the leader board -</p><p><strong>Step 1</strong>: Retrieval of data from the persistence storage i.e. MySQL like</p><p><strong>QUERY -</strong></p><pre>SELECT player_id, SUM(score) as score_total FROM Scores GROUP BY player_id ORDER BY score_total DESC (additionally the query will have limits on a date in case of WEEK and HOUR)</pre><p>NOTE: INDEX — Must have on player_id, possibly can be added to created_at</p><p><strong>Step 2</strong>: Addition to Sorted Set in Redis, explanations -</p><ul><li>HOW DATA WILL BE STORED?</li><li>Data will be stored in the format of — Key (PLAYER_ID) and value (SUM OF SCORE TIME BOUND)</li><li>Using the ZADD command taking 3 arguments it will be added to the relevant set (ALL/WEEK/HOUR) Ex: (“name_of_set”, key, value)</li><li>Only WEEK and HOUR-based sets have TTL a calendar week and a clock hour respectively, so practically HOUR based set will be created 24 times a day and WEEK based set 52 times a year.</li><li>WHY?</li><li>Most of the operations supported by the Sorted Set take logarithmic time O(log(n)) which means that its execution time even for huge data sets is going to be very low.</li><li>And in the case of just accessing the score of a single player complexity will be O(1).</li></ul><p><strong>Section B. REGULAR UPDATE</strong></p><ul><li>For the case of WEEK and HOUR, the new entries coming to score tables will be added to SET for writing directly with modifications as a summation to existing score in sets</li><li>For ALL time we can move such updates to Async mode to avoid latency.</li></ul><p><strong>Part 2: Reads</strong></p><p>Once our creation and modification of Sorted sets in REDIS is done, we can move towards the leader board retrieval part, so we can leverage the power of ZREVRANGE command like --</p><pre>ZREVRANGE name_of_set 0 99 WITH SCORES</pre><p>This will directly fetch the top 100 players with a complexity of -</p><p>O(log (N) + M) // where N is total elements, M is the top few elements required</p><p>In our case M is negligible as compared to N practicality otherwise the time will be close to linear.</p><h4>Summary</h4><p>In the end, what we have here is the design and estimated implementation, problems and challenges will be the part of final implementation both in terms of code development and solutions architecting (mostly configurations of cloud infrastructure)</p><p>PS: The ideas and solutions implemented above can be considered as the first draft and are subjected to further improvements as upcoming iterations.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=74145740aa4d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Clean Code Talks — Don’t Look For Things!]]></title>
            <link>https://bharatv90s.medium.com/the-clean-code-talks-dont-look-for-things-83787f7820c7?source=rss-9ce8429df334------2</link>
            <guid isPermaLink="false">https://medium.com/p/83787f7820c7</guid>
            <category><![CDATA[google]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[clean-code]]></category>
            <category><![CDATA[code]]></category>
            <category><![CDATA[php]]></category>
            <dc:creator><![CDATA[Bharat]]></dc:creator>
            <pubDate>Sun, 22 Nov 2020 15:15:07 GMT</pubDate>
            <atom:updated>2020-11-22T15:15:07.964Z</atom:updated>
            <content:encoded><![CDATA[<h3>The Clean Code Talks — Don’t Look For Things!</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/550/1*tGQPrUuqmoyYbW_wU5-CFQ.png" /></figure><p>The source and idea for this post is via <a href="https://youtu.be/RlfLCWKxHJ0">Google TechTalks</a></p><p><strong>WHY</strong>? Clean Code is maintainable</p><p>Source code must be:</p><blockquote><strong><em>Readable &amp;&amp; Extensible &amp;&amp; Testable</em></strong></blockquote><p>We are going to stress the most neglected of these attributes.</p><p><strong>To test a method:</strong></p><p>1) we need to instantiate an object (system under test) and all the helpers(mock providers, stubs), etc. but it turns out that we put a lot of code in the constructor which makes it very difficult to create an object of it.</p><p>Suppose we have a pest call in our constructor. Now while testing, we actually have to make a pest call for the instantiation process.</p><pre>class Story{<br>    public function __construct() {<br>        $pestObj = new Pest();<br>        $this-&gt;content = $pestObj-&gt;get(&#39;http://www.getStory.com&#39;);<br>    }<br>}</pre><p>But instead of this, if I ask for what I need :</p><pre>class Story{<br>    public function __construct($pestObj) {<br>        $this-&gt;content = $pestObj-&gt;get(&#39;http://www.getStory.com&#39;);<br>    }<br>}</pre><p>What I can do now is, while testing, I can mock get method of $pestObj and we won&#39;t actually have to make get request.</p><p>Now everything looks alright but look at this for a moment:</p><p>1) Story knows about a pest client and makes a GET request just for its content.</p><p>How about this:</p><pre>class Story{<br>    public function __construct($text) {<br>        $this-&gt;content = $text;<br>    }<br>}</pre><p>and</p><pre>class StoryStore{<br>    public function __construct($pestObj) {<br>        $this-&gt;pest = $pestObj;<br>    }<br>    public function getStory(){<br>        return new Story($pest-&gt;get(&#39;http://www.getStory.com&#39;));<br>    }<br>}</pre><p>This helps because tests are about instantiating small portions of the application and run them and assert some output. And if it is difficult to instantiate an object it’s not just going to be difficult to test that object but also other parts that need that object. It’s a kind of transitive problem.</p><p>1) So the test case has to successfully navigate the constructor each time the object is needed. So constructor ideally should be very simple. Just simple assignments and we are good to go.</p><p><strong>Service Locator (context, service container):</strong></p><p>In this system, we pass the responsibility of creating very hard to instantiate objects to the service locator, and then in code, we pass this service locator around.</p><pre>class MyController extends Symfony\Bundle\FrameworkBundle\Controller\Controller {<br>    public function testAction() {<br>        $this-&gt;get(&#39;my_service&#39;)-&gt;func();<br>    }<br>}</pre><p>and Symfony Controller does implement ContainerAwareInterface and has a service locator role.</p><p>But the problem with service locators is that they encourage the APIs that lie. You can’t tell from looking at the definition of a class what its dependencies are because they aren’t being injected, instead they are being yanked out of the service locator</p><p>Now we need to test a class that only says I need a service locator. But we have no idea what all we need to make the test run. we have to see the source code. what all are needed to run the class or see where it crashes and what needs to be overridden and most of all it violates the law of Demeter.</p><p>The Law of Demeter also known as principle of least knowledge is a coding principle, which says that a module should not know about the inner details of the objects it manipulates. If a code depends upon the internal details of a particular object, there is a good chance that it will break as soon as the internal of that object changes.</p><p>(Always ask for the things/references you need instead of searching for those references.)</p><p>According to Law of Demeter, a method M of object O should only call following types of methods :</p><ul><li>Methods of Object O itself</li><li>Methods of Object passed as an argument</li><li>Method of an object, which is held in an instance variable</li><li>Any Object which is CREATED locally in method M</li></ul><p>More importantly, the method should not invoke methods on objects that are returned by any subsequent method calls specified above and as Clean Code says “talk to friends, not to strangers”</p><p>-&gt; Only Ask for objects you DIRECTLY need to operate on.</p><p>-&gt; $a-&gt;getX()-&gt;getY() is a no no</p><p>-&gt; $serviceLocator-&gt;getService() breaks law of demeter</p><p>-&gt; Dependency Injection of specific objects we need.</p><p>Also if we want to reuse the class that requires a service locator then we have to supply the client with all the compile-time dependencies which include the service locator. But service locator contains many other services and dependencies. So here we have a problem, in order to give a class we need to give service locator and in order to give service locator we need to give most part of our application.</p><p>Breaking the Law of Demeter is Like Looking for a Needle in the Haystack</p><pre>public class XMLUtils {<br>    public function getFirstBookCategoryFromXML(XMLMessage $xml) {<br>        return $xml-&gt;getXML()-&gt;getBooks()-&gt;getBookArrary(0)-&gt;getBookCategory();<br>    }<br>}</pre><p>This code is now dependent upon lot of classes e.g.</p><ul><li>XMLMessage</li><li>XML</li><li>Book</li><li>BookCategory</li></ul><p>This means this function knows about XMLMessage, XML, Book, and BookCategory. It knows that XML has a list of books, which in-turn has BookCategory, that’s a lot of information.</p><p>If any of the intermediate class or accessor methods in this chained method call changes, then this code will break.</p><p>It’s an understanding between you and codes how to traverse the haystack and get the thing you need. All the things in between are irrelevant all u need is the book Category</p><p>Fundamentally this is what’s wrong with the service locator, asking for things we don’t directly need but retrieving the needed from it.</p><p>In summary, abandon the use of a new operator. -&gt; new operator ideally should either be inside the factory or in tests.</p><p>But in some cases, it is perfectly OK like instantiating a HashMap(), a collection. No need to ask for it in the constructor. This is because they are the leaves in your application graph. There is nothing behind these. It’s the end of the road.</p><p>But for services, objects that need collaboration, those need to ask for their dependencies. Construction should be done either by DI container (Symfony container), or builders or factories.</p><p>Any kind of indirections we introduce in the code makes it very hard for writing tests. This cost becomes double the cost if this is done in the constructor. As of now it has to be shared by lots and lots of the tests that use that specific object.</p><p><strong><em>Writing Testable Code</em></strong></p><p><strong><em>Flaw #1: Constructor does Real Work</em></strong></p><ul><li>“new” keyword in a constructor or at field declaration</li><li>Static method calls in a constructor or at field declaration</li><li>Anything more than field assignment in constructors</li><li>Object not fully initialized after the constructor finishes (watch out for initialize methods)</li><li>Control flow (conditional or looping logic) in a constructor</li><li>Code does complex object graph construction inside a constructor rather than using a factory or builder.</li><li>Adding or using an initialization block</li></ul><p><strong><em>Flaw #2: Digging into Collaborators</em></strong></p><ul><li>Objects are passed in but never used directly (only used to get access to other objects)</li><li>Law of Demeter violation: method call chain walks an object graph with more than one dot (.)</li><li>Suspicious names: context, environment, principal, container, or manager</li></ul><p><strong><em>Flaw #3: Brittle Global State &amp; Singletons</em></strong></p><ul><li>Adding or using singletons</li><li>Adding or using static fields or static methods</li><li>Adding or using static initialization blocks</li><li>Adding or using registries</li><li>Adding or using service locators</li></ul><p><strong><em>Flaw #4: Class Does Too Much</em></strong></p><ul><li>Summing up what the class does includes the word “and”</li><li>The class would be challenging for new team members to read and quickly “get it”</li><li>The class has fields that are only used in some methods</li><li>The class has static methods that only operate on parameters</li></ul><p>So now we know how to write UNTESTABLE code can wreak havoc on the person writing UTs:</p><p><strong><em>Make Your Own Dependencies</em></strong> — Instantiate objects using new in the middle of methods, don’t pass the object in.</p><p><strong><em>Heavy Duty Constructors</em></strong> — Make constructors that do lots of work in them.</p><p><strong><em>Depend on Concrete Classes </em></strong>— Tie things down to concrete classes — to avoid interfaces wherever possible.</p><p><strong><em>Conditional Slalom </em></strong>— Always, always, feel good when writing lengthy if branches and switch statements. These increase the number of possible execution paths that tests will need to cover when exercising the code under test.</p><p><strong><em>Depend on Large Context Objects</em></strong> — Pass around ginormous context objects (or small ones with hard to construct contents).</p><p><strong><em>Use Statics</em></strong> — Statics, statics everywhere!</p><p><strong><em>Use More Statics</em></strong> — Statics are a really powerful tool to bring TDD Infected engineers to their knees. Static methods can’t be overridden in a subclass (sometimes subclassing a class and overriding methods is a technique for testing). When you use static methods, they can’t be mocked using mocking libraries.</p><p><strong><em>Use Global Flags</em></strong> — Why call a method explicitly? Just like using a flag to set a flag in one part of your code, in order to cause an effect in a totally different part of your application.</p><p><strong><em>Use Singletons Everywhere </em></strong>— Why pass in a dependency and make it obvious when you can use a singleton?</p><p><strong><em>Look for Everything You Need </em></strong>— By Looking for things you are asserting your object’s dominance as the object which knows where everything is. This will make the life of the tester hard since he will have to mimic the environment so that your code can get a hold of what it needs.</p><p><strong><em>Utils, Utils, Utils! </em></strong>— Code smell? No way — code perfume! Litter about as many util and helper classes as you wish. These folks are helpful, and when you stick them off somewhere, someone else can use them too. That’s code reuse, and good for everyone, right? Be forewarned, the OO-police will say that functionality belongs in some object, as that object’s responsibility.</p><p><strong>References:</strong></p><ol><li><a href="https://youtu.be/RlfLCWKxHJ0">youtu.be/RlfLCWKxHJ0</a></li><li><a href="http://en.wikipedia.org/wiki/Law_of_Demeter">en.wikipedia.org/wiki/Law_of_Demeter</a></li><li><a href="http://www.slideshare.net/theojungeblut/2013-106-clean-code-part-i-design-patterns">slideshare.net/theojungeblut/2013–106-clean..</a></li><li><a href="http://nschoenmaker.nl/2013/11/defining-symfony-2-controllers-in-two-ways/">nschoenmaker.nl/2013/11/defining-symfony-2-..</a></li><li><a href="http://misko.hevery.com/2008/07/24/how-to-write-3v1l-untestable-code/">misko.hevery.com/2008/07/24/how-to-write-3v..</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=83787f7820c7" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>