<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Priyan Jeyaram</title>
    <description>The latest articles on Forem by Priyan Jeyaram (@priyan).</description>
    <link>https://forem.com/priyan</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1107674%2F948235ed-b87e-48e3-8ba2-ebd65b8e59b2.png</url>
      <title>Forem: Priyan Jeyaram</title>
      <link>https://forem.com/priyan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/priyan"/>
    <language>en</language>
    <item>
      <title>Public Learning - Day 0</title>
      <dc:creator>Priyan Jeyaram</dc:creator>
      <pubDate>Sun, 15 Mar 2026 11:16:59 +0000</pubDate>
      <link>https://forem.com/priyan/public-learning-day-0-p4</link>
      <guid>https://forem.com/priyan/public-learning-day-0-p4</guid>
      <description>&lt;h2&gt;
  
  
  Day 0: Cracking the System Design Code 🏗️
&lt;/h2&gt;

&lt;p&gt;Welcome to my learning log! I'm documenting my journey into the world of high-scale systems. Feel free to roast my logic in the comments or, you know, gently correct me so I don't build a digital house of cards.&lt;/p&gt;




&lt;h3&gt;
  
  
  1. The Fundamentals (The "Don't Break the App" Phase)
&lt;/h3&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Performance vs. Scalability&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Performance:&lt;/strong&gt; How fast is it &lt;em&gt;right now&lt;/em&gt;? 

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Metrics:&lt;/em&gt; Throughput (Req/sec), Response Time, CPU/Memory Usage.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Scalability:&lt;/strong&gt; How well does the system handle growth? 

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;The Test:&lt;/em&gt; If performance tanks the moment load increases, your system &lt;strong&gt;doesn't scale&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Latency vs. Throughput&lt;/strong&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Latency:&lt;/strong&gt; The "waiting" time per request (e.g., 1 API call = 120ms).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Throughput:&lt;/strong&gt; How many requests can we shove through the pipe per second? (e.g., 567 RPS).&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;The Batch Paradox:&lt;/strong&gt; A system with a &lt;strong&gt;10-second latency&lt;/strong&gt; that processes &lt;strong&gt;50,000 jobs/sec&lt;/strong&gt; is perfectly valid. High latency doesn't always mean a "bad" system—it depends on the goal.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Availability vs. Consistency (CAP Theorem)&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;In a distributed world, you usually have to pick a side:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Availability (AP):&lt;/strong&gt; Every request receives a response (even if the data is slightly stale).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Consistency (CP):&lt;/strong&gt; Every node returns the &lt;em&gt;exact same&lt;/em&gt; data at the same time (even if it means blocking requests until sync is done).&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;The Bottleneck "Golden Rule"&lt;/strong&gt;
&lt;/h4&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;"A system is only as fast as its slowest component."&lt;/strong&gt;&lt;br&gt;
(Spoiler: It’s usually the Database.)&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h3&gt;
  
  
  🧠 Brain Teasers: Putting Theory into Practice
&lt;/h3&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;Q1: The Sudden Traffic Spike&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Scenario:&lt;/strong&gt; You have 1 server (8 cores) handling 2k RPS. Suddenly, traffic hits &lt;strong&gt;20k RPS&lt;/strong&gt;. How do you scale using &lt;em&gt;pure infrastructure&lt;/em&gt; (no code changes)?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The "Interview-Ready" Answer:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Vertical Scaling:&lt;/strong&gt; "Scale Up" by adding more CPU/RAM to the existing machine.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Horizontal Scaling:&lt;/strong&gt; "Scale Out" by adding more servers behind a &lt;strong&gt;Load Balancer&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Partitioning:&lt;/strong&gt; Split the workload via &lt;strong&gt;Sharding&lt;/strong&gt; or &lt;strong&gt;Regional Routing&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;




&lt;h4&gt;
  
  
  &lt;strong&gt;Q2: The Throughput Showdown&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Which system is superior?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;System A:&lt;/strong&gt; 10ms latency, processes 1 request at a time.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;System B:&lt;/strong&gt; 200ms latency, processes 200 requests in parallel.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;The Math:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;System A:&lt;/strong&gt; $1 / 0.01s = 100$ req/sec.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;System B:&lt;/strong&gt; $200 / 0.2s = 1,000$ req/sec.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Winner:&lt;/strong&gt; &lt;strong&gt;System B&lt;/strong&gt;. Even though it's "slower" per request, its throughput is &lt;strong&gt;10x higher&lt;/strong&gt; due to parallelism.&lt;/p&gt;




&lt;h4&gt;
  
  
  &lt;strong&gt;Q3: WhatsApp: Messages vs. Money&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Should a messaging app prioritize Consistency or Availability?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Verdict:&lt;/strong&gt; &lt;strong&gt;Availability (AP)&lt;/strong&gt;. &lt;br&gt;
During a network partition, the system must still accept messages. They can synchronize later (&lt;strong&gt;Eventual Consistency&lt;/strong&gt;). &lt;br&gt;
&lt;em&gt;Note:&lt;/em&gt; If you're building &lt;strong&gt;WhatsApp Pay&lt;/strong&gt;, the rules change—you &lt;strong&gt;must&lt;/strong&gt; be &lt;strong&gt;CP&lt;/strong&gt; (Consistent) because nobody likes "eventual" money.&lt;/p&gt;




&lt;h4&gt;
  
  
  &lt;strong&gt;Q4: The Need for Speed&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Flow:&lt;/strong&gt; &lt;code&gt;Client → API (5ms) → Redis (1ms) → PostgreSQL (50ms)&lt;/code&gt;. What is the &lt;strong&gt;minimum&lt;/strong&gt; latency?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Math:&lt;/strong&gt;&lt;br&gt;
In a &lt;strong&gt;Cache Hit&lt;/strong&gt; scenario, we skip the slow DB.&lt;br&gt;
&lt;code&gt;API Logic (5ms) + Redis (1ms) = 6ms&lt;/code&gt;. &lt;br&gt;
Minimum latency is &lt;strong&gt;6ms&lt;/strong&gt;.&lt;/p&gt;




&lt;h4&gt;
  
  
  &lt;strong&gt;Q5: The Bitly Bottleneck&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Scenario:&lt;/strong&gt; 50M redirects/day. Where does the first bottleneck appear?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Reality Check:&lt;/strong&gt;&lt;br&gt;
50M/day is roughly &lt;strong&gt;580 RPS&lt;/strong&gt;. A single modern server handles this easily. However, as you scale, the &lt;strong&gt;Database&lt;/strong&gt; becomes the bottleneck because every redirect requires a lookup.&lt;br&gt;
&lt;strong&gt;Solution:&lt;/strong&gt; Introduce &lt;strong&gt;Redis caching&lt;/strong&gt; so frequently accessed URLs never even touch the DB.&lt;/p&gt;




&lt;h4&gt;
  
  
  &lt;strong&gt;Q6: The "Insta-Feed" Problem (Push vs. Pull)&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;How do we generate a feed for 200M users?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Pull Model:&lt;/strong&gt; Fetch posts from all 500 people you follow on login. 

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Result:&lt;/em&gt; Massive &lt;strong&gt;Fan-in&lt;/strong&gt; query. 2500+ posts fetched per login = &lt;strong&gt;DB Meltdown&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Push Model:&lt;/strong&gt; When someone posts, "Fan-out" that post to every follower's pre-built feed table.

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Result:&lt;/em&gt; Fast reads, but &lt;strong&gt;Celebrity Meltdown&lt;/strong&gt;. If a star with 600M followers posts, the system has to perform 600M writes instantly.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;The Instagram Hybrid:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Normal Users:&lt;/strong&gt; Push (Fan-out) to feeds.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Celebrities:&lt;/strong&gt; Use the Pull model. We only fetch their posts when a user actually opens the app and merges them into the feed.&lt;/li&gt;
&lt;/ul&gt;




&lt;h4&gt;
  
  
  &lt;strong&gt;Q7: The "Viral Tweet" Like Counter&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Do you&lt;/strong&gt; &lt;code&gt;UPDATE tweets SET likes = likes + 1&lt;/code&gt; &lt;strong&gt;or store every like as a row?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Surprise Winner:&lt;/strong&gt; &lt;strong&gt;Storing every row (Option B) scales better.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Problem with Option A:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Row Locking:&lt;/strong&gt; The DB locks the row during every update.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Write Contention:&lt;/strong&gt; 100k people liking a tweet at once = 100k updates fighting for one row. The DB chokes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;The Pro Architecture:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;strong&gt;Write:&lt;/strong&gt; Store each like in its own row (No locking, just appending).&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Cache:&lt;/strong&gt; Use a &lt;strong&gt;Distributed Counter&lt;/strong&gt; in Redis (&lt;code&gt;INCR tweet:id:likes&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt; &lt;strong&gt;Extreme Scale:&lt;/strong&gt; Use &lt;strong&gt;Sharded Counters&lt;/strong&gt; where likes increment one of many "buckets" to avoid hitting a single Redis hotkey.&lt;/li&gt;
&lt;/ol&gt;




&lt;p&gt;&lt;strong&gt;What am I missing?&lt;/strong&gt; If you've handled a 600M follower fan-out lately, let me know how much sleep you lost in the comments!&lt;/p&gt;

</description>
      <category>systemdesign</category>
      <category>architecture</category>
      <category>learning</category>
      <category>learnwithai</category>
    </item>
  </channel>
</rss>
