<?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 Himanshu on Medium]]></title>
        <description><![CDATA[Stories by Himanshu on Medium]]></description>
        <link>https://medium.com/@singh.sh.shivam?source=rss-e1eefd7f623a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*ZF1KtR_A7dRPtAR1</url>
            <title>Stories by Himanshu on Medium</title>
            <link>https://medium.com/@singh.sh.shivam?source=rss-e1eefd7f623a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 15:43:01 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@singh.sh.shivam/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[CurrentTarget vs Target in Events in Js]]></title>
            <link>https://medium.com/@singh.sh.shivam/currenttarget-vs-target-in-events-in-js-6f40af8b4e3b?source=rss-e1eefd7f623a------2</link>
            <guid isPermaLink="false">https://medium.com/p/6f40af8b4e3b</guid>
            <dc:creator><![CDATA[Himanshu]]></dc:creator>
            <pubDate>Mon, 29 Apr 2024 19:06:25 GMT</pubDate>
            <atom:updated>2024-04-29T19:06:25.203Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qHiLB36K7wI7XSEv2MxuvQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fcsrd_AhB4OZgXiywP3vSQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*2KY6b7QtI4U3OAWgA4rZfQ.png" /></figure><p>Suppose we are clicking on pink box , so event will propagate to blue then orange in bubbling phase right?</p><p>Thats it , current target will tell the current propagated state and target will tell you the division which you have clicked .</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6f40af8b4e3b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Singleton Design Pattern]]></title>
            <link>https://medium.com/@singh.sh.shivam/singleton-design-pattern-7998dd81c884?source=rss-e1eefd7f623a------2</link>
            <guid isPermaLink="false">https://medium.com/p/7998dd81c884</guid>
            <category><![CDATA[java]]></category>
            <category><![CDATA[singleton-pattern]]></category>
            <dc:creator><![CDATA[Himanshu]]></dc:creator>
            <pubDate>Sat, 20 Jan 2024 14:45:22 GMT</pubDate>
            <atom:updated>2024-01-20T14:45:22.378Z</atom:updated>
            <content:encoded><![CDATA[<ol><li>What is singleton Design Pattern</li><li>why we need it ?</li><li>How to create singleton class</li><li>Why constructer is private?</li><li>Why the class is sealed.</li></ol><p>/*package whatever //do not write package name here */<br>import java.io.*;<br>public sealed class Singleton {<br> // static class<br> private static Singleton instance;<br> private Singleton()<br> {<br> System.out.println(“Singleton is Instantiated.”);<br> }<br> public static Singleton getInstance()<br> {<br> if (instance == null)<br> instance = new Singleton();<br> return instance;<br> }<br> public static void doSomething()<br> {<br> System.out.println(“Somethong is Done.”);<br> }<br>}</p><p>class Program {<br> public static void main(String[] args)<br> {<br> Singleton.getInstance().doSomething();<br> }<br>}</p><p>ref:<a href="https://www.youtube.com/watch?v=UjP8YPTVONU">https://www.youtube.com/watch?v=UjP8YPTVONU</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7998dd81c884" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Hld of Twitter in 5 min]]></title>
            <link>https://medium.com/@singh.sh.shivam/hld-of-twitter-in-5-min-4de32ffcec2a?source=rss-e1eefd7f623a------2</link>
            <guid isPermaLink="false">https://medium.com/p/4de32ffcec2a</guid>
            <category><![CDATA[twitter-design]]></category>
            <category><![CDATA[system-design-of-twitter]]></category>
            <category><![CDATA[hld-of-twitter]]></category>
            <dc:creator><![CDATA[Himanshu]]></dc:creator>
            <pubDate>Mon, 08 Jan 2024 18:47:19 GMT</pubDate>
            <atom:updated>2024-01-08T18:47:19.210Z</atom:updated>
            <content:encoded><![CDATA[<p>Approch : Firstly, define the problem and then the solution !</p><p>Only design the <strong>core features</strong> of Twitter instead of everything.</p><h3>Discuss The Core Features</h3><ul><li>The user should be able to tweet (should be fast).</li><li>The user should be able to see Tweet (should be avaliable to everyone on single time (acc to there interest , state , or trending topics).</li><li>Login</li><li>Follow/unfollow</li><li>Retweet</li><li>Search is also a big feature</li></ul><p>Approch to follow ? Think for a normal solution first using MySQL Database</p><p>You need to fetch all the feeds from all the people a user follows(maybe 10 crore ) and render them in chronological order</p><p>eg virat kholi has 30 Lakh of user so when he posted anything it should be seen to 30 lakh user at that time which can be done by writing select query for everyuser , right ? which will take a huge amount of time and cost !</p><p><strong>We need to optimize this solution to solve this problem and for that, we will move to the high-level solution for this problem</strong></p><p>Twitter has 2<strong>00M+</strong> daily active users. On average, every second 10<strong>, 000 tweets</strong> are tweeted on Twitter. Every second <strong>6, 00, 000 Queries</strong> are made to get the timelines. Each user has on average 400 followers and some users like some celebrities have millions of followers. This characteristic of Twitter clears the following points…</p><ol><li>Twitter has a <strong>heavy read</strong> in comparison to writing so we should care much more about the <strong>availability </strong>and scale of the application for the heavy read on Twitter.</li><li>We can consider <strong>eventual consistency</strong> for this kind of system. It’s completely ok if a user sees the tweet of his follower a bit delayed</li><li>Space is not a problem as tweets are limited to 140 characters.</li><li>Tweet can be vedio or Image also (for that we have to use CDN)</li></ol><p><strong>Now lets talk about how we can fetch data faster from database?</strong></p><p>Fetching this huge amount of data from database is a very slow and heavy process !</p><p>To fetch data very fast we can use <a href="https://redis.io/docs/about/">redis</a> , it is just like a database which can be in the middle of user and main database which will store the data as a cache .(the read time of redis is very fast it uses many data structure such as hashmap , hashtable,etc..)</p><p>Points to study</p><ul><li><strong>1 </strong>too<strong> many relationships</strong></li><li>1 too one releationship</li></ul><p>Get all the tweet from the table and Once this is done sort the tweet by date and time and then display the information on the user timeline</p><p>As we have discussed that Twitter is <strong>read-heavy</strong> so the above approach won’t work always.</p><p><strong>Thats why caching layer is required here using Redis.</strong></p><p>Normally we can do :</p><p>A user’s Home Timeline contains all the latest tweets of the person and the pages that the user follows. Well, here you can simply fetch the users whom a user is following, for each follower fetch all the latest tweets, then merge all the tweets, sort all these tweets by date and time, and display them on the home timeline. This solution has some <strong>drawbacks</strong>. The Twitter home page loads much faster and these queries are heavier on the database so this huge search operation will take much more time once the tweet table grows to millions. Let’s talk about the solution now for this drawback…</p><p><strong>Suppose a user posted a vedio that will take 2 min to save in database and then all the follower will fetch the vedio and then it will get shown into there timeline ! right?</strong></p><p><strong><em>Fanout Approach ?:</em></strong> Fanout simply means spreading the data from a single point to all .(eg. virat to all his fans) Whenever a tweet is made by a user (Followee) do a lot of <strong>preprocessing </strong>and distribute the data into different users (followers) home timelines. In this process, you won’t have to make any database queries. You just need to go to the cache by user_id and access the home timeline data in Redis. So this process will be much faster and easier because it is an in-memory we get the list of tweets. Here is the complete flow of this approach…</p><p><strong>Normal Flow</strong></p><ol><li>User X is followed by three peopleand this user has a cache called user timeline. X Tweeted something.</li><li>Through Load Balancer, tweets will flow into back-end servers.</li><li>The server node will save the tweet in DB/cache</li><li>The server node will fetch all the users that follow User X from the cache.</li><li>The server node will inject this tweet into the in-memory timelines of his followers (fanout)</li><li>All followers of User X will see the tweet of User X in their timeline. It will be refreshed and updated every time a user will visit his/her timeline.</li></ol><p><strong>What if X has 33 million follwers ??</strong></p><p><strong><em>Optimised Solution [Mixed Approach (In-memory+Synchronous calls)]:</em></strong></p><ol><li><em>Precompute the home timeline of User A (follower of Virat) with everyone except Virat’s tweet(s)</em></li><li><em>Every user maintains the list of celebrities in the </em><strong><em>cache</em></strong><em> as well as whom that user is following. When the request will arrive (tweet from the celebrity) you can get the celebrity from the list, fetch the tweet from the user timeline of the celebrity and then mix the celebrity’s tweet at runtime with other tweets of User A.</em></li><li><em>So when User A accesses his home timeline his tweet feed is merged with Eminem’s tweet at load time. So the celebrity tweet will be inserted at runtime.</em></li><li>For inactive users don’t compute the timeline. People who don’t log in to the system for a quite long time (say more than 20 days.)</li></ol><p><strong>Search</strong> <strong>Feature</strong></p><ol><li>we can use elastic search</li><li>The tweet will be split into tags, words, and #tags, and then these words are indexed. This indexing is done at a big table or distributed table</li><li>Suppose if a user searches for ‘<em>bollywood</em>‘ then you will go through the table, will find the word ‘<em>bollywood</em>‘, then you’ll figure out all the references to all the tweets in the system, then it gives all the results that contain the word ‘<em>bollywood</em></li><li>Twitter uses the strategy of <strong>scatter and gather</strong> where it set up <strong>multiple servers or data centers</strong> that allow indexing(Distributed System ).</li><li>When Twitter gets a query (let’s say #Election2024) it sends the query to all the servers or data centers and it queries every <strong>Early Bird shard</strong>. All the early bird that matches with the query return the result. The results are returned, sorted, merged, and reranked !</li></ol><p>These are the major point that you can talk about in your Twitter HLD interview !</p><p>Apart from that you can talk about , how topic and tweet will trend , server at location level (DNS) , how can we restrict twitter in some state or country !</p><p>Thanks for reading ..</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4de32ffcec2a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Monolithic Vs MicroSevervice , What ?]]></title>
            <link>https://medium.com/@singh.sh.shivam/monolithic-vs-microsevervice-what-11094be20fd3?source=rss-e1eefd7f623a------2</link>
            <guid isPermaLink="false">https://medium.com/p/11094be20fd3</guid>
            <dc:creator><![CDATA[Himanshu]]></dc:creator>
            <pubDate>Fri, 29 Dec 2023 14:43:49 GMT</pubDate>
            <atom:updated>2023-12-29T14:43:49.158Z</atom:updated>
            <content:encoded><![CDATA[<h3>Monolithic Vs MicroSevervice , What ?</h3><p>Procedure Call (no network calls ) are faster !</p><p>Harder to scale</p><p>Single Databases</p><p>it can also be scaled as micro</p><p>Dependencies are always there</p><p>Easy to understand by beginner</p><p>eg. Stackoverflow</p><h3>MicroSevervice</h3><p>Complex to understand completely in one go and basically tightly coupled.</p><p>Handling transactions also is a challenge in microservices , so better to use monolithic</p><p>Sometimes a service is dependent on too much on other services and when it comes to logging you also need to track the flow of all your calls plus they add an extra network cost, you need to handle all the failures, have retry logic and fall back in place</p><p>Managing several micro-services databases could be on a long run challenging</p><p>Single point failure can cause whole service to go down</p><p>Eg . Facebook , Google</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=11094be20fd3" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>