<?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 Rajrishi Vishwakarma on Medium]]></title>
        <description><![CDATA[Stories by Rajrishi Vishwakarma on Medium]]></description>
        <link>https://medium.com/@rajrishi?source=rss-3cab6bfb85fc------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*_QuF8U-k_8j8uFVAYqd-PQ.png</url>
            <title>Stories by Rajrishi Vishwakarma on Medium</title>
            <link>https://medium.com/@rajrishi?source=rss-3cab6bfb85fc------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 17:45:22 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@rajrishi/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: Audio Processing Service]]></title>
            <link>https://medium.com/@rajrishi/system-design-audio-processing-service-22186197d909?source=rss-3cab6bfb85fc------2</link>
            <guid isPermaLink="false">https://medium.com/p/22186197d909</guid>
            <category><![CDATA[system-design-project]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[system-design-interview]]></category>
            <category><![CDATA[system-design-concepts]]></category>
            <category><![CDATA[distributed-systems]]></category>
            <dc:creator><![CDATA[Rajrishi Vishwakarma]]></dc:creator>
            <pubDate>Thu, 29 Jan 2026 22:09:47 GMT</pubDate>
            <atom:updated>2026-01-29T22:09:47.550Z</atom:updated>
            <content:encoded><![CDATA[<p>Design a system that allows users to upload audio files and receive transcriptions. They should handle 100,000 audio files per day, with an average file size of 20 MB, and duration of 10 minutes. Users should recieve a notification when transcription<br>is complete.</p><h4><strong>Functional requirements:</strong></h4><p>- user should be able to upload audio files<br>- user receive notification when transcription is complete</p><h4>NFR:</h4><p>- 100,000 upload per day <br>- file size 20 MB<br>- duration 600 seconds</p><h4>BOE:</h4><p>compute:<br>QPS = 1<br>peak QPS = 10 * QPS = 10 req/sec</p><h4>storage:</h4><p>2TB storage per day <br>for a year storage. = 500*2 = 1PB</p><p>2000 words per audio file <br>total 2K * 10 characters= 20,000 bytes = 20 kb<br>2 GB/day = 800 GB a year</p><h4>API:</h4><p>POST /v1/files/upload</p><p>payload:<br>- filesize<br>- unique_id (fingerprint)</p><h4>DATA:</h4><p>User:<br>- userid</p><p>AudioFile:<br>- size<br>- unique_id<br>- url<br>- status<br>- userid</p><p>Transcription:<br>- unique_id<br>- Tid<br>- trasncriptionUrl</p><h4>HLD:</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*hJ61K6uH6rxEVIXOKZs9Ww.png" /></figure><h4>Deep Dive:</h4><blockquote><strong>Normal Flow:</strong></blockquote><p>Since the file size is small so it can be easily uploaded through out stateless upload service using simple REST.<br>uploadService upload the file to s3 and gets its url and update the metadata DB and then publish the message to Kafka.</p><p>worker service pulls the message and checks if already processed, if not then sends it to transcription service.</p><p>Transcript service process and store in s3 and update the event to processed queue, Wroker B consume messages and send <br>notification through notification service.</p><p>Wroker B also change the state from PROCESSING to PROCESSED and update the transcript url.</p><blockquote><strong>Failure Secnario:</strong></blockquote><p>since upload sercice is async it only cares about successful upload to s3 then it returns 201 created otherwise user can try to upload again.</p><p>if transcript service is not able to process the audio file or may be crashed the scheduler will pick up the job and put it back into queue if retryable.<br>otherwise after 3 attempts it puts in dead letter queue which is consumed by worker A/C to send failed notification to user.</p><blockquote><strong>Scalability &amp; Fault Tolerance:</strong></blockquote><p>Upload service is stateless and scalable.<br>Metadata DB can be sharded by user_id to meet the requirement. Here two tables for AudioFile and Transcripts can be denormalized as there is 1:1 mapping.</p><p>We’ve divided message queue of 3 types which is better still we can use user_id as partition key in a single message queue.<br>we’re using AWS S3 as managed service, so nothing to worry about as it can scale pretty well on demand and is suitable for blob storage.</p><p>Transcript service is stateless even if it crashes scheduler will reschedule the job and can scale during peak traffic.</p><p>Notification is 3rd party service that we’ll be using .</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=22186197d909" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Consistent Hashing]]></title>
            <link>https://medium.com/@rajrishi/consistent-hashing-4378303435cf?source=rss-3cab6bfb85fc------2</link>
            <guid isPermaLink="false">https://medium.com/p/4378303435cf</guid>
            <category><![CDATA[architecture]]></category>
            <category><![CDATA[microservice-architecture]]></category>
            <category><![CDATA[system-design-concepts]]></category>
            <dc:creator><![CDATA[Rajrishi Vishwakarma]]></dc:creator>
            <pubDate>Wed, 05 Mar 2025 09:32:44 GMT</pubDate>
            <atom:updated>2025-03-05T10:10:04.775Z</atom:updated>
            <content:encoded><![CDATA[<p>Consistent hashing helps in providing load balancing capability in distributed system. Let’s say i have multiple servers to accomodate high traffic, but we need a way to distribute the traffic to all servers equally. To achieve this lets discuss various approaches and what are its drawbacks</p><h4><strong>Approach 1: Simple Hashing</strong></h4><p>A simple hashing technique would generate hash by key and does a simple modulo operation by k where k denotes number of servers in the system.</p><blockquote>serverIndex = hash(key)%k<br>k = number of servers</blockquote><p>In the above approach the system might suffer high collision leading to overload on target server. It also causes issue when we’re dealing with dynamic env, removal of any 1 server would require rehashing of keys and same goes for addition of server, as hash generated gets invalidated due to change in servers.</p><h4><strong>Approach 2: Consistent Hashing</strong></h4><p>Consistent hashing propose the idea to keep servers on the ‘Virtual Ring’ (Circular array) and generate hash of keys and get the index they fall into. Once we have the index we can easily get id of the server which is just next to this index in clockwise direction.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/987/0*nL6RmCr8zanJ41Qg.png" /><figcaption>Fig-1: Request route in consistent hashed env.</figcaption></figure><p>This approach helps in making the system dynamic as addition or removal of nodes doesn’t cause complete rehash. Only portion of request or data is rehashed which are very small O(n^{-1}), n : number of servers.</p><p><em>Note: There is possibility in above diagram that majority of keys fall into same segment leading to overload on same node. It happens when number of nodes are very less.</em></p><p>The solution to above problem is, allocate virtual nodes across the ring with probabilistic distribution by replicating the service.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*T5CN_mGjCpotrLHT.png" /></figure><h4>Implementation:</h4><p>To achieve fast lookup of target Node Index of any request Balanced BST is used which stores index of all virtual nodes across the ring. As Balanced BST provides $O(logN)$ time for ‘peek’, ‘add’ and ‘remove’ it is preferred.</p><p>Let’s take an example, we have a service with multiple instances of it available and now we want to provide a load balancing capability to this service such that single node doesn’t face high traffic.</p><p>Steps to follow:</p><ol><li><strong>Assign each service instance to a hash ring</strong>.<br> — Hash the instance ID (e.g., &lt;<strong>IP&gt;:&lt;Port&gt;</strong> or <strong>Instance Name</strong>) onto the ring.<br> — Example: hash(“S1”), hash(“S2”), hash(“S3”) → positions on the ring.</li><li><strong>Use Virtual Nodes</strong> (to evenly distribute load).<br> — Instead of hashing each instance once, create multiple virtual nodes per instance.<br> — Example: hash(“S1#1”), hash(“S1#2”), hash(“S1#3”) to spread S1 across the ring.</li><li><strong>Route incoming requests based on hash</strong>.<br> — Hash the request (e.g., hash(clientIP), hash(sessionID), or hash(userID)).<br> — Find the <strong>next clockwise instance on the ring</strong>.</li><li><strong>When an instance is added/removed</strong>, only a small portion of traffic is re-routed.<br> — If S2 is removed, its traffic is <strong>shifted to the next closest node</strong>, instead of redistributing everything.</li></ol><pre>import java.nio.charset.StandardCharsets;<br>import java.security.MessageDigest;<br>import java.util.*;<br><br>public class ConsistentHashingLoadBalancer {<br>    private final SortedMap&lt;Integer, String&gt; ring = new TreeMap&lt;&gt;();<br>    private final int virtualNodes;<br><br>    public ConsistentHashingLoadBalancer(List&lt;String&gt; serviceInstances, int virtualNodes) {<br>        this.virtualNodes = virtualNodes;<br>        for (String instance : serviceInstances) {<br>            addInstance(instance);<br>        }<br>    }<br><br>    // Hash function (SHA-256)<br>    private int hash(String key) {<br>        try {<br>            MessageDigest md = MessageDigest.getInstance(&quot;SHA-256&quot;);<br>            byte[] digest = md.digest(key.getBytes(StandardCharsets.UTF_8));<br>            return Math.abs(ByteBuffer.wrap(digest).getInt());<br>        } catch (Exception e) {<br>            throw new RuntimeException(e);<br>        }<br>    }<br><br>    // Add an instance with virtual nodes<br>    public void addInstance(String instance) {<br>        for (int i = 0; i &lt; virtualNodes; i++) {<br>            int hash = hash(instance + &quot;#&quot; + i);<br>            ring.put(hash, instance);<br>        }<br>    }<br><br>    // Remove an instance and its virtual nodes<br>    public void removeInstance(String instance) {<br>        for (int i = 0; i &lt; virtualNodes; i++) {<br>            int hash = hash(instance + &quot;#&quot; + i);<br>            ring.remove(hash);<br>        }<br>    }<br><br>    // Get the instance for a given request<br>    public String getInstance(String key) {<br>        if (ring.isEmpty()) return null;<br><br>        int hash = hash(key);<br>        SortedMap&lt;Integer, String&gt; tailMap = ring.tailMap(hash);<br>        Integer targetHash = tailMap.isEmpty() ? ring.firstKey() : tailMap.firstKey();<br>        return ring.get(targetHash);<br>    }<br><br>    public static void main(String[] args) {<br>        List&lt;String&gt; instances = Arrays.asList(&quot;S1&quot;, &quot;S2&quot;, &quot;S3&quot;);<br>        ConsistentHashingLoadBalancer lb = new ConsistentHashingLoadBalancer(instances, 3);<br><br>        String request1 = &quot;client1&quot;;<br>        String request2 = &quot;client2&quot;;<br>        System.out.println(&quot;Request1 routed to: &quot; + lb.getInstance(request1));<br>        System.out.println(&quot;Request2 routed to: &quot; + lb.getInstance(request2));<br><br>        // Simulate instance failure<br>        lb.removeInstance(&quot;S2&quot;);<br>        System.out.println(&quot;After S2 removal, Request1 routed to: &quot; + lb.getInstance(request1));<br>    }<br>}</pre><p>The solution above is good enough for small systems but to scale it further enhancement is needed as it suffers from SPOF (single point of failure). <strong>ZooKeeper </strong>works in similar fashion as per above implementation, it also suffers with SPOF.</p><p>SPOF can be avoided by replicating these and each instance will maintain a snapshot of the index Tree which can be synced through <strong>Gossip Protocol.</strong> To check the difference in state between peers, Merkle Tree can be used.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/824/0*fn1u0RzO6Ai8hAt6.png" /></figure><p><strong>Implementation Using Gossip Protocol:</strong></p><pre>import java.nio.charset.StandardCharsets;<br>import java.security.MessageDigest;<br>import java.util.*;<br><br>public class GossipConsistentHashing {<br>    private final SortedMap&lt;Integer, String&gt; ring = new TreeMap&lt;&gt;();<br>    private final int virtualNodes;<br>    private final Random random = new Random();<br><br>    public GossipConsistentHashing(List&lt;String&gt; initialInstances, int virtualNodes) {<br>        this.virtualNodes = virtualNodes;<br>        for (String instance : initialInstances) {<br>            addInstance(instance);<br>        }<br>    }<br><br>    // Hash function (SHA-256 for better distribution)<br>    private int hash(String key) {<br>        try {<br>            MessageDigest md = MessageDigest.getInstance(&quot;SHA-256&quot;);<br>            byte[] digest = md.digest(key.getBytes(StandardCharsets.UTF_8));<br>            return Math.abs(Arrays.hashCode(digest));<br>        } catch (Exception e) {<br>            throw new RuntimeException(e);<br>        }<br>    }<br><br>    // Add instance with virtual nodes<br>    public synchronized void addInstance(String instance) {<br>        for (int i = 0; i &lt; virtualNodes; i++) {<br>            int hash = hash(instance + &quot;#&quot; + i);<br>            ring.put(hash, instance);<br>        }<br>    }<br><br>    // Remove instance<br>    public synchronized void removeInstance(String instance) {<br>        for (int i = 0; i &lt; virtualNodes; i++) {<br>            int hash = hash(instance + &quot;#&quot; + i);<br>            ring.remove(hash);<br>        }<br>    }<br><br>    // Get responsible instance for a given request<br>    public synchronized String getInstance(String key) {<br>        if (ring.isEmpty()) return null;<br><br>        int hash = hash(key);<br>        SortedMap&lt;Integer, String&gt; tailMap = ring.tailMap(hash);<br>        Integer targetHash = tailMap.isEmpty() ? ring.firstKey() : tailMap.firstKey();<br>        return ring.get(targetHash);<br>    }<br><br>    // Gossip Protocol: Exchange Hash Ring State with Other Nodes<br>    public synchronized void gossipExchange(GossipConsistentHashing peer) {<br>        SortedMap&lt;Integer, String&gt; peerRing = peer.getRingSnapshot();<br>        for (Map.Entry&lt;Integer, String&gt; entry : peerRing.entrySet()) {<br>            ring.putIfAbsent(entry.getKey(), entry.getValue());<br>        }<br>    }<br><br>    // Get a copy of the current hash ring state<br>    public synchronized SortedMap&lt;Integer, String&gt; getRingSnapshot() {<br>        return new TreeMap&lt;&gt;(ring);<br>    }<br><br>    public static void main(String[] args) {<br>        List&lt;String&gt; instances = Arrays.asList(&quot;S1&quot;, &quot;S2&quot;, &quot;S3&quot;);<br>        GossipConsistentHashing node1 = new GossipConsistentHashing(instances, 3);<br>        GossipConsistentHashing node2 = new GossipConsistentHashing(instances, 3);<br>        GossipConsistentHashing node3 = new GossipConsistentHashing(instances, 3);<br><br>        // Simulate Gossip between nodes<br>        node1.gossipExchange(node2);<br>        node2.gossipExchange(node3);<br>        node3.gossipExchange(node1);<br><br>        System.out.println(&quot;Node 1 routing clientA to: &quot; + node1.getInstance(&quot;clientA&quot;));<br>        System.out.println(&quot;Node 2 routing clientA to: &quot; + node2.getInstance(&quot;clientA&quot;));<br>        System.out.println(&quot;Node 3 routing clientA to: &quot; + node3.getInstance(&quot;clientA&quot;));<br><br>        // Simulate instance failure<br>        node1.removeInstance(&quot;S2&quot;);<br><br>        // More gossiping to propagate changes<br>        node1.gossipExchange(node2);<br>        node2.gossipExchange(node3);<br>        node3.gossipExchange(node1);<br><br>        System.out.println(&quot;After S2 removal, Node 1 routing clientA to: &quot; + node1.getInstance(&quot;clientA&quot;));<br>        System.out.println(&quot;After S2 removal, Node 2 routing clientA to: &quot; + node2.getInstance(&quot;clientA&quot;));<br>    }<br>}</pre><p>Above gossip exchange takes IP of peer in the example it is passed manually but a node should be able to identify its peer through seed node.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4378303435cf" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[From Colab To Heroku]]></title>
            <link>https://medium.com/@rajrishi/from-colab-to-heroku-bff7bea49d32?source=rss-3cab6bfb85fc------2</link>
            <guid isPermaLink="false">https://medium.com/p/bff7bea49d32</guid>
            <category><![CDATA[tensorflow]]></category>
            <category><![CDATA[mobilenet]]></category>
            <category><![CDATA[deep-learning]]></category>
            <category><![CDATA[heroku]]></category>
            <category><![CDATA[technology]]></category>
            <dc:creator><![CDATA[Rajrishi Vishwakarma]]></dc:creator>
            <pubDate>Thu, 25 Jun 2020 14:37:26 GMT</pubDate>
            <atom:updated>2020-06-25T15:03:51.046Z</atom:updated>
            <content:encoded><![CDATA[<iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4dbbbb2b6baa1fb3b2c9f62e8919df90/href">https://medium.com/media/4dbbbb2b6baa1fb3b2c9f62e8919df90/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e0de8a5a90fba2b5cc5f089d1a3ec969/href">https://medium.com/media/e0de8a5a90fba2b5cc5f089d1a3ec969/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bff7bea49d32" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>