<?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 Samir on Medium]]></title>
        <description><![CDATA[Stories by Samir on Medium]]></description>
        <link>https://medium.com/@sa8103339?source=rss-4917855a024d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*3CiMcy2EqVJ7frFqnTnp2w.jpeg</url>
            <title>Stories by Samir on Medium</title>
            <link>https://medium.com/@sa8103339?source=rss-4917855a024d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 18:30:51 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sa8103339/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[How Our System Handles Failures & Stores Data Efficiently: Logging, Metrics, Automation & Databases…]]></title>
            <link>https://medium.com/@sa8103339/how-our-system-handles-failures-stores-data-efficiently-logging-metrics-automation-databases-963d7cd33ca3?source=rss-4917855a024d------2</link>
            <guid isPermaLink="false">https://medium.com/p/963d7cd33ca3</guid>
            <dc:creator><![CDATA[Samir]]></dc:creator>
            <pubDate>Sun, 11 Oct 2026 14:45:20 GMT</pubDate>
            <atom:updated>2026-10-11T14:46:00.851Z</atom:updated>
            <content:encoded><![CDATA[<h3>How Our System Handles Failures &amp; Stores Data Efficiently: Logging, Metrics, Automation &amp; Databases | Part 4</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*IEJZ_oHQicH3hdOQUFIB0A.png" /></figure><p>As I continued learning system design, I started noticing something interesting.</p><p>Building an application is only one part of the problem. The real challenge begins when people start using it at scale.</p><p>Imagine you have built an Instagram-like application. Everything works perfectly when you test it with a few users. You can log in, upload photos, like posts, and send messages.</p><p>But what happens when millions of users start using your application?</p><p>Your APIs might become slow. Database queries might take longer than expected. Some requests might fail, and a server might crash without you immediately knowing why.</p><p>How would you identify the problem? <br>How would you understand what caused it? <br>And how could your system respond without requiring a developer to manually fix every issue?</p><p>These questions bring us to three important concepts in system design: <strong>Logging, Metrics, and Automation.</strong></p><p>There is another important decision we need to make when designing the database behind our application: should we store information in separate, related tables, or combine some of that information to make reads faster?</p><p>That is where <strong>Normalization and Denormalization</strong> come in.</p><p>In this part of my System Design learning journey, I’ll explain all five concepts with practical examples and show how they fit into a real application.</p><h3>1. Logging: How Do We Know What Happened Inside Our Application?</h3><p>Let’s start with a simple situation.</p><p>A user opens your application and tries to log in. Instead of reaching the home page, they receive an error.</p><p>From the user’s perspective, the application is broken. But from the developer’s perspective, several things could have gone wrong.</p><ul><li>Was the password incorrect?</li><li>Did the login API fail?</li><li>Could the application not connect to the database?</li><li>Did the server encounter an unexpected exception?</li></ul><p>Without additional information, you would have to guess.</p><p>This is why applications generate <strong>logs</strong>.</p><p>A log is a record of an event that occurred inside a system. It helps us understand what the application was doing at a particular time.</p><p>For example, a backend application might generate logs like these:</p><pre>[10:30:01] User login successful<br>[10:30:05] Payment API failed<br>[10:30:06] Database connection error<br>[10:30:10] Request processing completed</pre><p>These are illustrative examples. A real application would generally include additional context, such as a request ID, the affected service, and the severity of the event.</p><p>Now imagine that the user reports a login problem at 10:30 AM.</p><p>You can search the application logs around that time and discover that the login request failed because the database connection could not be established.</p><p>Instead of randomly changing your login code, you now have a much clearer place to investigate.</p><p>That is the value of logging.</p><h3>What should an application log?</h3><p>Depending on the application, useful log events can include:</p><ul><li>Successful and failed operations.</li><li>API errors and unexpected exceptions.</li><li>Database connection failures.</li><li>Payment processing failures.</li><li>Background job failures.</li><li>Important application state changes.</li></ul><p>Logs should also have meaningful severity levels.</p><p>For example:</p><ul><li><strong>INFO:</strong> Something important happened during normal operation.</li><li><strong>WARNING:</strong> Something unusual happened that may require attention.</li><li><strong>ERROR:</strong> An operation failed.</li><li><strong>DEBUG:</strong> Additional details that help developers investigate problems.</li></ul><p>You don’t want to log every tiny operation without a reason. Too much logging creates noise, increases storage requirements, and makes important errors harder to find.</p><p>You should also avoid writing passwords, authentication tokens, payment card details, or other sensitive information into logs.</p><h3>Where do logs go?</h3><p>In a small Python application, you can start with Python’s built-in logging module.</p><p>For example, in a FastAPI backend:</p><pre>import logging<br>from fastapi import FastAPI</pre><pre>app = FastAPI()</pre><pre>logging.basicConfig(level=logging.INFO)</pre><pre>logger = logging.getLogger(__name__)</pre><pre>@app.get(&quot;/&quot;)<br>def home():<br>    logger.info(&quot;Home endpoint was requested&quot;)<br>    return {&quot;message&quot;: &quot;Hello World&quot;}</pre><p>Whenever someone requests the home endpoint, the application generates an informational log.</p><p>In a production system, logs can be collected and searched using tools such as the ELK Stack or Grafana Loki. This becomes especially useful when your application runs across multiple servers and services.</p><p>Imagine having 50 servers. Checking the log file on each server manually would be inconvenient. Centralized logging allows you to search relevant events from one place.</p><p><strong>The main idea is simple: logging helps us investigate individual events and understand what happened inside the application.</strong></p><p>But logs alone are not enough.</p><p>If your application receives millions of requests, you cannot manually inspect every log to understand whether the system is healthy.</p><p>We need a way to measure its overall performance.</p><h3>2. Metrics: How Do We Know Whether Our System Is Healthy?</h3><p>Suppose your application is running on several servers.</p><p>You want to know:</p><ul><li>How many requests are arriving every second?</li><li>How quickly are APIs responding?</li><li>How much CPU and memory are the servers using?</li><li>What percentage of requests are failing?</li></ul><p>These questions are answered using <strong>metrics</strong>.</p><p>A metric is a numerical measurement that helps us understand the performance or health of a system over time.</p><p>Imagine your monitoring dashboard displays the following values:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*nE9DsGg8eFwB86mKWgVb2w.png" /></figure><p>These values are examples, not recommended production targets.</p><p>A dashboard like this gives you a quick overview of what is happening across your system.</p><p>For example, CPU usage of 85% might indicate that a server is under significant load. An error rate of 8% might suggest that too many requests are failing.</p><p>However, a single number rarely tells the whole story.</p><p>An 85% CPU reading may be perfectly manageable for one workload but concerning for another. You need to compare metrics with historical behavior, expected traffic, and your application’s performance requirements.</p><h3>Which metrics matter most?</h3><p>For a typical web application, I would start with a few important categories.</p><p><strong>1. Request rate</strong></p><p>This measures how many requests your application receives over a period of time.</p><p>If your application normally receives 1,000 requests per second but suddenly receives 10,000, you need to understand whether this is expected traffic or an unusual spike.</p><p><strong>2. Response time</strong></p><p>This measures how long the application takes to respond to a request.</p><p>For example, if an API usually responds in 100 milliseconds but starts taking 3 seconds, users will notice the difference.</p><p>Average response time is useful, but it can hide slow requests. Percentiles such as p95 and p99 help you understand how slow requests affect the users experiencing the worst delays.</p><p><strong>3. Error rate</strong></p><p>This measures the proportion of requests that fail.</p><p>Suppose your application receives 10,000 requests, and 100 of them fail.</p><p>The error rate is:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YbrlnUW5Jqw90VttkQ0uUA.png" /></figure><p>Now imagine that 1,500 requests fail out of the same 10,000 requests.</p><p>The error rate has increased to 15%.</p><p>That is a strong signal that something may be wrong.</p><p><strong>4. Resource utilization</strong></p><p>CPU, memory, disk usage, and network activity help you understand whether your infrastructure is approaching its capacity.</p><p>If CPU usage remains near 95% while response times increase, the application might need additional computing capacity or a more efficient way of processing requests.</p><p>Other bottlenecks, such as a database connection pool running out of available connections, may not be obvious from CPU usage alone.</p><h3>Logging vs. Metrics: What’s the difference?</h3><p>Consider this situation.</p><p>Your application suddenly starts returning errors to users.</p><p>Metrics show that the error rate increased from 1% to 15% at approximately 10:30 AM.</p><p>This tells you <strong>when the problem became visible and how widespread it is</strong>.</p><p>But it doesn’t necessarily tell you why the errors occurred.</p><p>You then search the logs around 10:30 AM and discover repeated database connection failures.</p><p>Now you have a much stronger understanding of the problem.</p><p>This is the difference:</p><ul><li>Metrics help identify unusual behavior and measure its impact.</li><li>Logs help investigate individual events and their possible causes.</li></ul><p>Tools such as Prometheus can collect metrics, while Grafana can display them in dashboards and help visualize trends.</p><p>Together, they give developers a clearer picture of application health.</p><p>However, identifying a problem is only part of the job. We also need to decide what the system should do when something goes wrong.</p><h3>3. Automation: What If the System Could Respond Automatically?</h3><p>Imagine your application experiences a sudden traffic spike.</p><p>CPU usage increases, requests become slower, and users begin experiencing errors.</p><p>Without automation, someone might need to notice the problem, investigate it, and manually add more servers or change the system configuration.</p><p>That takes time.</p><p>Automation allows predefined actions to happen when certain conditions are met.</p><p>For example:</p><ul><li>Send an alert when the error rate becomes unusually high.</li><li>Add application instances when traffic increases and scaling conditions are satisfied.</li><li>Restart a failed process when a restart is an appropriate recovery action.</li><li>Run scheduled background jobs.</li><li>Execute automated tests and deployment steps when new code is submitted.</li></ul><p>The important word here is <strong>predefined</strong>.</p><p>The system does not magically understand every problem. Engineers define the conditions, actions, and safeguards in advance.</p><h3>Example: Automatic scaling</h3><p>Suppose your application usually runs on two application servers.</p><p>During a major event, traffic increases sharply, and the servers begin approaching their capacity.</p><p>You could configure an autoscaling system with a rule such as:</p><p>“When CPU usage remains above a defined threshold for a specified period, increase the number of application instances, provided the scaling limits allow it.”</p><p>The flow might look like this:</p><ol><li>Traffic increases.</li><li>CPU usage and response times rise.</li><li>The monitoring system detects the change.</li><li>The autoscaling mechanism evaluates its rules.</li><li>Additional application instances are started if the scaling conditions are satisfied.</li><li>The load balancer distributes incoming requests across the available instances.</li></ol><p>This connects directly to the load balancing and horizontal scaling concepts discussed in earlier parts of this series.</p><p>Adding servers is not always the solution, though. If the real bottleneck is a database that cannot handle more connections, scaling the application servers may make the problem worse.</p><p>That is why automation must be designed around the actual failure conditions.</p><h3>Example: Automatic alerts</h3><p>Suppose your API’s error rate suddenly reaches 20%.</p><p>An alerting rule can notify the engineering team through an appropriate notification channel.</p><p>The alert should provide useful context, such as the affected service, the time the issue started, and a link to relevant dashboards.</p><p>This helps developers respond more quickly instead of discovering the issue through user complaints.</p><h3>Can we automatically restart every failed service?</h3><p>Not necessarily.</p><p>A restart can help when a process becomes stuck or enters an unrecoverable state. But repeatedly restarting a service will not fix a database outage, a bad deployment, or an exhausted external API quota.</p><p>In some situations, automatic restarts can even hide the underlying problem or create repeated failures.</p><p>A good automation strategy includes safeguards, retry limits, health checks, and alerts when automatic recovery does not work.</p><p>Tools such as Kubernetes can manage application instances and restart containers under configured conditions. GitHub Actions can automate tasks such as running tests and deploying code. Cloud platforms also provide autoscaling and monitoring capabilities.</p><p><strong>Automation helps the system respond to known conditions, but the rules must be designed carefully.</strong></p><h3>4. How Logging, Metrics, and Automation Work Together</h3><p>Let’s put these concepts together using our FastAPI application.</p><p>Imagine users start reporting that the application is very slow.</p><p>Here’s how the three components can help.</p><p><strong>Step 1: Metrics detect the problem.</strong></p><p>The dashboard shows that API response time has increased from 120 milliseconds to 3 seconds. The error rate has reached 20%.</p><p>This tells us that the system is experiencing a serious performance issue.</p><p><strong>Step 2: Logs help investigate the cause.</strong></p><p>We search the logs and discover repeated errors indicating that the database connection pool has run out of available connections.</p><p>The application cannot obtain connections quickly enough to serve incoming requests.</p><p>Now we have a possible explanation for the slowdown.</p><p><strong>Step 3: Automation responds according to the situation.</strong></p><p>The monitoring system can alert the engineering team immediately.</p><p>If the problem is caused by insufficient application capacity, scaling the application instances might help. If the database connection pool is exhausted, engineers may need to investigate connection limits, connection leaks, query duration, or database capacity.</p><p>Automatically adding application servers without checking the database’s capacity could make the issue worse.</p><p>This example shows why detection, investigation, and response need to work together.</p><p>Metrics tell us that something is wrong. Logs help us investigate what happened. Automation executes predefined responses or alerts the people who need to act.</p><p>In larger distributed systems, <strong>distributed tracing</strong> adds another useful signal. It follows a request as it travels through different services and helps identify where time is being spent.</p><p>For example, tracing might reveal that a request spends 50 milliseconds in the API service but 2.8 seconds waiting on a database operation.</p><p>That information helps narrow down the bottleneck much faster.</p><p>These practices are part of what engineers commonly call observability: understanding a system’s internal behavior by examining the information it produces.</p><h3>5. Moving to Database Design: Normalization vs. Denormalization</h3><p>Now let’s move to another important database design decision.</p><p>Imagine you are building an employee management system. You need to store employee names, departments, and department locations.</p><p>The easiest approach might be to put everything in one table.</p><p>But as we saw earlier, storing repeated information creates problems. At the same time, splitting everything into separate tables means we may need JOINs to retrieve combined information.</p><p>This is the trade-off behind normalization and denormalization.</p><h3>What is normalization?</h3><p>Normalization is the process of organizing database tables to reduce unnecessary duplication and keep related information in appropriate places.</p><p><em>In simple words</em> :<em> Normalization means splitting data into separate, related tables to avoid repeating the same information and keep data consistent.</em></p><p>Suppose our original table looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VS8XHWHBgyQUYa0ikip-rQ.png" /></figure><p>The problem is that the department’s location is repeated for every employee in that department.</p><p>If the HR department moves from Building A to Building C, we must update every relevant row.</p><p>Missing even one row could leave the database with conflicting information.</p><p>We can avoid this by separating employee information from department information.</p><p><strong>Employee table</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*U5tsNh5UIXy2fqxsx2-hkQ.png" /></figure><p><strong>Department table</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/913/1*LZrqG8mPfnjFxEgHDgZ-pw.png" /></figure><p>Now, the employee table stores employee-specific information, while the department table stores department-specific information.</p><p>The department location is stored once instead of being repeated for every employee.</p><p>If HR moves to Building C, we update the department record:</p><pre>UPDATE Department<br>SET Building_Location = &#39;Building C&#39;<br>WHERE Department = &#39;HR&#39;;</pre><p>We don’t need to update every HR employee.</p><p>That is the main advantage of normalization: it reduces unnecessary duplication and makes shared information easier to maintain.</p><p>In a production database, we would usually give each department a stable identifier, such as Department_ID, and reference it from the employee table using a foreign key. This avoids depending on department names as identifiers.</p><h3>But how do we retrieve information from both tables?</h3><p>Suppose we want to find Bob’s department location.</p><p>His name is stored in the Employee table, while the building location is stored in the Department table.</p><p>We can combine the information using an SQL JOIN.</p><pre>SELECT<br>    e.Employee_ID,<br>    e.Employee_Name,<br>    d.Building_Location<br>FROM Employee e<br>JOIN Department d<br>    ON e.Department = d.Department<br>WHERE e.Employee_ID = 2;</pre><p>The JOIN matches the department in Bob’s employee record with the corresponding department record.</p><p>The result gives us Bob’s name and his department’s building location.</p><p>This is a perfectly reasonable design. But when an application repeatedly needs information from several large tables, the additional joins can sometimes become a performance concern.</p><p>The important point is that JOINs are not inherently bad. Relational databases are designed to perform them. We should only treat them as a problem when the actual workload shows that they are contributing to performance issues.</p><p>That brings us to denormalization.</p><h3>6. What Is Denormalization?</h3><p>Denormalization is the deliberate introduction of redundant data to improve particular read operations or simplify frequently used queries.</p><p>Using our employee example, we could combine the information from the two normalized tables into a single table again.</p><p><em>In simple words : Denormalization means combining data from separate tables into one table, even if some information gets repeated, to make reading data simpler or faster.</em></p><p><strong>Employee_Details table</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*23iPV4g-_IPxzD3C2yQUXA.png" /></figure><p>Now the employee’s name, department, and building location are available in one place.</p><p>We can retrieve Bob’s details without joining two tables:</p><pre>SELECT Employee_Name, Department, Building_Location<br>FROM Employee_Details<br>WHERE Employee_ID = 2;</pre><p>This can make the query simpler and may improve read performance, depending on the database and workload.</p><p>Why would we choose this approach?</p><p>Imagine that a reporting dashboard repeatedly displays employee names alongside department locations. If the same information is requested frequently and the database spends significant time combining tables, a denormalized representation might help.</p><p>However, denormalization introduces a cost.</p><p>If HR moves from Building A to Building C, every duplicated HR location must be updated correctly. Otherwise, some rows may show the old location.</p><p>It can also increase storage requirements and make inserts, updates, deletes, and synchronization more complicated.</p><p>This is why denormalization should be a deliberate design choice rather than a default approach.</p><h3>Where does denormalization make sense?</h3><p>It can be useful for workloads that perform many reads and repeatedly need the same combined information.</p><p>Examples include:</p><ul><li>Analytics dashboards.</li><li>Reporting systems.</li><li>Product listing pages.</li><li>Read-optimized database views.</li><li>Some data warehouse workloads.</li></ul><p>For instance, an e-commerce application might frequently display a product’s name, price, category, and brand together.</p><p>A read-optimized representation can make those queries easier to serve.</p><p>But we must also consider how frequently the underlying data changes and how quickly those changes need to appear in the read-optimized representation.</p><p>For a dashboard, a small delay may be acceptable. For a financial transaction, stale data could cause serious problems.</p><h3>7. Normalization vs. Denormalization: The Trade-Off</h3><p>Here’s a simple comparison.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1007/1*-oB-3HGbQUxoKk53vjj_HQ.png" /></figure><p>Neither approach is universally better.</p><p>Normalization helps maintain a clean and consistent representation of data. Denormalization accepts some duplication when the benefits for a particular workload justify the additional maintenance.</p><p>Also, don’t assume that every normalized database has slow reads or that every denormalized database has slow writes. Indexes, query plans, transactions, data size, and workload patterns all affect performance.</p><p>The right design depends on the application.</p><h3>8. How Would We Make This Decision in a Real System?</h3><p>Let’s return to our employee management application.</p><p>Suppose employees frequently change departments, department locations change regularly, and many operations update the database.</p><p>A normalized schema is a sensible starting point because it allows department information to be maintained in one place.</p><p>Now suppose the company builds a large reporting dashboard that repeatedly retrieves employee and department information. As the data grows, measurements show that particular queries are taking too long.</p><p>Instead of redesigning the entire database, we could optimize the affected queries, add appropriate indexes, or create a read-optimized representation.</p><p>For example, we could maintain a separate table or materialized view containing the information required by the dashboard.</p><p>The main tables remain organized, while the reporting system gets data in a form that is easier to retrieve.</p><p>However, this design introduces another responsibility: keeping the read-optimized data synchronized with the original records.</p><p>We must decide how updates will be propagated, how failures will be handled, and whether temporary delays are acceptable.</p><p>This is the kind of trade-off that comes up repeatedly in system design.</p><p>We rarely choose a technology simply because it sounds faster. We choose it because it solves a specific problem without introducing more complexity than the application can justify.</p><h3>9. Connecting Everything to System Design</h3><p>At this point, we have covered five closely related ideas.</p><p>Logging helps us understand what happened inside an application.</p><p>Metrics help us measure its performance and identify unusual behavior.</p><p>Automation allows the system to respond to predefined conditions, such as increased traffic or service failures.</p><p>Normalization helps us organize data and reduce unnecessary duplication.</p><p>Denormalization allows us to duplicate selected information when it provides a useful read-performance benefit.</p><p>Consider how these concepts might work together in a growing application.</p><p>The application receives a sudden increase in traffic. Metrics show that response times and error rates are rising. Logs help engineers identify whether the problem is related to application code, database connections, or another service. Automation can send alerts or scale components when the configured conditions are satisfied.</p><p>At the database level, engineers can investigate slow queries and determine whether indexing, query optimization, or a read-optimized data model would help.</p><p>These decisions are connected. Increasing application capacity will not necessarily solve a database bottleneck. Denormalizing data will not automatically fix every slow query. Restarting a service will not resolve an underlying configuration problem.</p><p>A good system design considers how the components behave together rather than optimizing each one in isolation.</p><h3>Final Thoughts</h3><p>One thing I’m learning through system design is that building a working application and operating a reliable application are two different challenges.</p><p>As a system grows, we need ways to understand its behavior, detect problems, respond to failures, and organize data so that it can be retrieved and updated efficiently.</p><p>Logging, metrics, and automation help us operate the system.</p><p>Normalization and denormalization help us make database design decisions based on the application’s requirements.</p><p>The common thread is understanding trade-offs. There is no single configuration that works best for every application. What matters is knowing what problem you’re trying to solve, understanding the costs of each approach, and making decisions based on how your system actually behaves.</p><p>This is how I’m slowly getting a better understanding of how real-world systems work.</p><p><em>Thanks for reading…. Hope it helps you to understand these stuffs :)</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=963d7cd33ca3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How Our System Keeps Growing: Sessions, Data Centers & Message Queues | Part 3]]></title>
            <link>https://medium.com/@sa8103339/how-our-system-keeps-growing-sessions-data-centers-message-queues-part-3-2b5893cc3890?source=rss-4917855a024d------2</link>
            <guid isPermaLink="false">https://medium.com/p/2b5893cc3890</guid>
            <dc:creator><![CDATA[Samir]]></dc:creator>
            <pubDate>Sun, 04 Oct 2026 18:03:25 GMT</pubDate>
            <atom:updated>2026-10-04T18:03:25.902Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1EWmznVvF6P7-oyj2pDVKQ.png" /></figure><p>In the previous parts, I started with a very simple system and slowly added things as new problems appeared.</p><p>We went from a single server to multiple servers, then added a Load Balancer, Database Replication, Caching, and CDN.</p><p>But as the system grows, more problems start appearing.</p><p>For example:</p><p>What happens when a user’s session is stored on one particular server?</p><p>What happens if our entire data center goes down?</p><p>And what if some tasks take too long and slow down our main application?</p><p>These are some of the things I learned next.</p><h3>1. What is Session Data?</h3><p>Before going further, I came across a term called <strong>State</strong> or <strong>Session Data</strong>.</p><p>In simple words, session data is information related to a particular user’s session.</p><p>For example:</p><ul><li>Whether the user is logged in</li><li>Session information</li><li>User-specific details</li><li>Session tokens</li></ul><p>The important question is:</p><p><strong>Where do we store this information?</strong></p><p>This leads us to two different approaches:</p><p><strong>Stateful Architecture</strong></p><p>and</p><p><strong>Stateless Architecture</strong></p><h3>2. Stateful Architecture</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EaeTIyvjUmPgaztI4ibWaA.png" /></figure><p>Let’s imagine we have three web servers:</p><p><strong>Server 1</strong></p><p><strong>Server 2</strong></p><p><strong>Server 3</strong></p><p>Suppose I log into the application and my request goes to Server 1.</p><p>Server 1 stores my session information.</p><p>Now I send another request.</p><p>If that request goes to Server 2, Server 2 doesn’t have my session information.</p><p>So it doesn’t know that I am already logged in.</p><p>The request needs to go back to Server 1.</p><p>This means our Load Balancer now has to remember:</p><p><strong>“This user belongs to Server 1.”</strong></p><p>This is commonly handled using something called <strong>Sticky Sessions</strong>.</p><p>But now our system becomes more complicated.</p><p>There is another problem too.</p><p>Imagine Server 1 crashes.</p><p>The users whose session information was stored there may lose their sessions and might have to log in again.</p><p>And what if we want to remove Server 1 because traffic is low?</p><p>We can’t simply remove it if it is holding active users’ session data.</p><p>So storing user state directly inside individual web servers makes scaling harder.</p><h3>3. Stateless Architecture</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/817/1*w8NogYnvR6fywjKOhUk5aQ.png" /></figure><p>So what if we simply don’t store session data inside the web servers?</p><p>Instead, we move it somewhere shared.</p><p>For example:</p><p><strong>Web Servers → Shared Session Store</strong></p><p>The session information can be stored in an external database or an in-memory store such as Redis.</p><p>Now something important changes.</p><p>A user’s request doesn’t need to go to the same server every time.</p><p>For example:</p><p>Request 1 → Server 1</p><p>Request 2 → Server 3</p><p>Request 3 → Server 2</p><p>All of these servers can access the same session information from the shared store.</p><p>This is called a <strong>Stateless Architecture</strong>.</p><p>The web servers themselves don’t keep the user’s session state.</p><p>And this makes scaling much easier.</p><p>If traffic increases, we can add more servers.</p><p>If traffic decreases, we can remove servers.</p><p>We don’t have to worry about which server is holding a particular user’s session.</p><p>This was an important concept for me because it connects directly with what we learned earlier about Horizontal Scaling.</p><p>The more independent our web servers are, the easier it becomes to scale them.</p><h3>4. But What If Our Entire Data Center Goes Down?</h3><p>Now imagine we have built a pretty scalable system.</p><p>We have:</p><ul><li>Multiple Web Servers</li><li>Load Balancer</li><li>CDN</li><li>Cache</li><li>Database Replication</li><li>Stateless Web Servers</li></ul><p>Looks good.</p><p>But there is still a bigger problem.</p><p>What if all of these servers are running inside the same data center?</p><p>Imagine that data center suddenly has a major outage.</p><p>Power failure.</p><p>Hardware problem.</p><p>Network issue.</p><p>Whatever the reason, the entire data center becomes unavailable.</p><p>Now our whole application can go down.</p><p>So instead of keeping everything in one location, we can have multiple <strong>Data Centers</strong> in different geographical regions.</p><p>For example:</p><p><strong>Data Center 1 → US-East</strong></p><p><strong>Data Center 2 → US-West</strong></p><p>Now if one location has a major problem, the other location can continue serving users.</p><p>But now we have another question:</p><p><strong>How does the user know which data center they should connect to?</strong></p><p>This is where GeoDNS comes in.</p><h3>5. GeoDNS</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/757/1*L2L49Q6B0ZwFHU-jfZ4psg.png" /></figure><p>We already learned about DNS in Part 1.</p><p>Normal DNS basically helps convert a domain name into an IP address.</p><p><strong>GeoDNS</strong> takes this idea a little further.</p><p>It can return different IP addresses based on the geographic location of the user.</p><p>For example:</p><p>A user in the Eastern US might be sent to:</p><p><strong>US-East Data Center</strong></p><p>A user in the Western US might be sent to:</p><p><strong>US-West Data Center</strong></p><p>The basic idea is to send users to a suitable or nearby data center.</p><p>This can help reduce latency and improve the overall user experience.</p><h3>6. What If One Data Center Goes Down?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/767/1*q5CKiWSylhy-3JmA8pOplQ.png" /></figure><p>Let’s say the US-West data center suddenly goes offline.</p><p>We don’t want users there to simply see an error.</p><p>GeoDNS can redirect the traffic to the healthy data center.</p><p>So instead of:</p><p><strong>User → US-West</strong></p><p>the request can be redirected to:</p><p><strong>User → US-East</strong></p><p>This gives us another layer of reliability.</p><p>But there is a catch.</p><p>If users suddenly move from one data center to another, the required data needs to be available there too.</p><p>That means our data can’t just exist in one location.</p><p>We need to keep data synchronized between data centers.</p><p>This can include:</p><ul><li>Database data</li><li>Session data</li><li>Cache data</li></ul><p>Otherwise, a user might be redirected to another data center and find that their required information isn’t there.</p><h3>7. One More Problem: Some Tasks Take Too Long</h3><p>Now let’s come back to our application.</p><p>Imagine a user uploads 20 images and asks the application to apply some processing to them.</p><p>Maybe we need to:</p><ul><li>Crop images</li><li>Resize them</li><li>Apply filters</li><li>Process them</li></ul><p>These operations might take some time.</p><p>Should our Web Server sit there waiting for everything to finish?</p><p>Probably not.</p><p>Because while it is busy processing those images, it isn’t free to handle other users’ requests.</p><p>So we need a way to move these heavy tasks somewhere else.</p><p>This is where <strong>Message Queues</strong> come in.</p><h3>8. What is a Message Queue?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fEnTWxE3o_rxmp8b7rfM7A.png" /></figure><p>The basic idea is actually quite simple.</p><p>A Message Queue sits between the application that creates a task and the system that processes it.</p><p>Think of it like this:</p><p><strong>Producer → Message Queue → Consumer</strong></p><p>The <strong>Producer</strong> creates a task and puts it into the queue.</p><p>The <strong>Queue</strong> safely holds that task.</p><p>The <strong>Consumer</strong> picks up the task and processes it.</p><p>For example:</p><p><strong>Web Server → Queue → Worker</strong></p><p>The Web Server doesn’t have to wait for the Worker to finish.</p><p>It can simply put the task into the queue and continue handling other requests.</p><h3>9. Image Processing Example</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4gfB7lVG3h3ul4UjVT4WZA.png" /></figure><p>Let’s take our image example again.</p><p>A user uploads three images.</p><p>The Web Server receives the request.</p><p>Instead of processing everything immediately, it can put three tasks into the queue:</p><p><strong>Process Image 1</strong></p><p><strong>Process Image 2</strong></p><p><strong>Process Image 3</strong></p><p>The Web Server can then quickly respond to the user that the request has been received.</p><p>Meanwhile, background worker servers can pick up these tasks from the queue and process them.</p><p>So the flow becomes:</p><p><strong>User → Web Server → Message Queue → Workers</strong></p><p>The Web Server remains available for new users while the Workers handle the heavy processing in the background.</p><p>This is called <strong>Asynchronous Processing</strong>.</p><h3>10. Why Is This Useful?</h3><p>There is another thing I found interesting about Message Queues.</p><p>The Producer and Consumer don’t have to be available at exactly the same time.</p><p>Imagine the workers are busy processing other tasks.</p><p>The new tasks can simply wait inside the queue.</p><p>When a worker becomes available, it picks up the next task.</p><p>So the queue acts like a buffer between the two systems.</p><p>This also helps when traffic suddenly increases.</p><p>Suppose normally we have 2 workers.</p><p>Suddenly, thousands of tasks arrive.</p><p>Instead of changing the Web Server layer, we can add more workers.</p><p>For example:</p><p><strong>2 Workers → 10 Workers</strong></p><p>Now more workers can process the queue and clear the backlog faster.</p><p>When traffic goes down, we can reduce the number of workers again.</p><p>This means the Producer and Consumer sides can scale independently.</p><h3>11. What If a Worker Crashes?</h3><p>Suppose a worker is processing some tasks and suddenly crashes.</p><p>The idea of a Message Queue is that pending messages remain in the queue until they are successfully processed.</p><p>So another healthy worker can pick up the pending work.</p><p>This gives the system better reliability.</p><p>It also makes it easier to isolate problems.</p><p>If one worker has an issue, it doesn’t necessarily bring down the entire application.</p><h3>12. Our System Has Come a Long Way</h3><p>If we look at where we started, the architecture has changed quite a lot.</p><p>We started with:</p><p><strong>User → One Server → Database</strong></p><p>Then we gradually added solutions as new problems appeared.</p><p>Now we have concepts like:</p><p><strong>DNS</strong></p><p><strong>Load Balancer</strong></p><p><strong>Multiple Web Servers</strong></p><p><strong>Stateless Architecture</strong></p><p><strong>CDN</strong></p><p><strong>Caching</strong></p><p><strong>Database Replication</strong></p><p><strong>Multiple Data Centers</strong></p><p><strong>GeoDNS</strong></p><p><strong>Message Queues</strong></p><p>And what I like about this way of learning System Design is that each component now has a reason for being there.</p><p>We didn’t just add a Load Balancer because “large systems use Load Balancers.”</p><p>We added it because multiple servers created a problem: <strong>who should receive the request?</strong></p><p>We didn’t add caching just because it’s popular.</p><p>We added it because repeatedly hitting the database for the same data can become expensive.</p><p>And we didn’t add Message Queues randomly.</p><p>We needed a way to handle heavy background tasks without making the main application wait.</p><p>That’s what is making System Design easier for me to understand.</p><p>Instead of remembering a list of technologies, I’m trying to understand <strong>what problem each one is solving.</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/408/1*jhDnL7gDhe9Lz9PbdVXDTg.gif" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2b5893cc3890" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How Our Simple System Keeps Scaling: Database Replication, Caching & CDN | Part 2]]></title>
            <link>https://medium.com/@sa8103339/how-our-simple-system-keeps-scaling-database-replication-caching-cdn-part-2-2b977d0035cd?source=rss-4917855a024d------2</link>
            <guid isPermaLink="false">https://medium.com/p/2b977d0035cd</guid>
            <category><![CDATA[database-replication]]></category>
            <category><![CDATA[caching]]></category>
            <category><![CDATA[content-delivery-network]]></category>
            <dc:creator><![CDATA[Samir]]></dc:creator>
            <pubDate>Sun, 27 Sep 2026 12:01:46 GMT</pubDate>
            <atom:updated>2026-09-27T12:15:01.461Z</atom:updated>
            <content:encoded><![CDATA[<p><strong>Database Replication</strong>: What Happens When Your Database Goes Down?</p><p>In Part 1, I learned how a simple website can slowly scale from one server to multiple servers using things like Horizontal Scaling and Load Balancers.</p><p>But there was still one problem left.</p><p>The Database.</p><p>Our architecture looked something like this:</p><p>Users → Load Balancer → Multiple Web Servers → One Database</p><p>Now think about it.</p><p>We have multiple web servers, but all of them are still depending on one database.</p><p>So what happens if that database goes down?</p><p>The whole application can have a serious problem.</p><p>This is where I came across a concept called Database Replication.</p><h3>1. What is Database Replication?</h3><p>The basic idea is actually pretty simple.</p><p>Instead of keeping our data in only one database, we create copies of that database. These copies are called replicas.</p><p>So instead of:</p><p>Database</p><p>we now have:</p><p>Database → Replica 1 → Replica 2 → Replica 3</p><p>The main purpose is to make the system more reliable and also handle more database traffic.</p><p>This was the first time I started understanding why large systems don’t want to depend on just one database server.</p><h3>2. Master and Slave Databases</h3><p>A common way to understand database replication is using a Master-Slave architecture.</p><p>Today, you will also commonly hear the terms Primary and Replica/Secondary.</p><p>The idea is the same:</p><p>Master → handles writes</p><p>Slaves/Replicas → handle reads</p><p>For example, suppose a user creates an account.</p><p>The application needs to save that information.</p><p>That is a write operation.</p><p>So the request goes to the Master database.</p><p>Similarly, when a user simply opens their profile, the application needs to fetch information.</p><p>That’s a read operation.</p><p>That request can be handled by one of the replica databases.</p><p>So the basic idea becomes:</p><p>Write → Master</p><p>Read → Replica</p><h3>3. How Do the Replicas Get the Data?</h3><p>This is the interesting part.</p><p>Suppose we have:</p><p>Master Database</p><ul><li>Replica 1</li><li>Replica 2</li></ul><p>A user makes a change to the data.</p><p>For example:</p><pre>User changes their name</pre><p>The change is first made on the Master database.</p><p>The Master then replicates that change to the replica databases.</p><p>So eventually:</p><ul><li>Master → Replica 1</li><li>Master → Replica 2</li></ul><p>Both replicas receive the updated data.</p><p>This keeps the databases synchronized.</p><h3>4. Why Do We Need More Replicas?</h3><p>Now imagine your application has millions of users.</p><p>Usually, an application can receive a lot more read requests than write requests.</p><p>For example, think about a social media application.</p><p>A user might upload a post once, but thousands of people might view that post.</p><p>So we may have:</p><p>Few writes</p><p>A lot of reads</p><p>If one database has to handle all these read requests, it can become overloaded.</p><p>Instead, we can add more replicas.</p><p>For example:</p><pre>                 → Replica 1<br>                /<br>Master Database → Replica 2<br>                \<br>                 → Replica 3</pre><p>Now read requests can be distributed across these replicas.</p><p>This reduces the load on a single database server.</p><h3>5. How Does This Improve Performance?</h3><p>Imagine 10,000 users are trying to read data at the same time.</p><p>If one database handles all 10,000 requests, it has to do all the work itself.</p><p>But if we have multiple replicas, the requests can be distributed.</p><p>For example:</p><p>Replica 1 → 3,000 requests</p><p>Replica 2 → 3,000 requests</p><p>Replica 3 → 4,000 requests</p><p>Now the workload is spread across multiple machines.</p><p>This can improve the overall performance of the system.</p><h3>6. What If a Replica Goes Down?</h3><p>Let’s say we have:</p><p>Master</p><p>Replica 1</p><p>Replica 2</p><p>Replica 3</p><p>Now Replica 2 suddenly goes offline.</p><p>We still have Replica 1 and Replica 3.</p><p>So the application can continue sending read requests to the healthy replicas.</p><p>We can also create a new replica and synchronize it with the Master.</p><p>Once it is ready, it can start handling read requests again.</p><p>So one replica going down does not necessarily mean the whole application has to go down.</p><h3>7. But What If the Master Goes Down? 🤔</h3><p>This is where things get more interesting.</p><p>Remember, the Master is handling our write operations.</p><p>So if the Master goes down, we cannot simply stop accepting writes.</p><p>One of the replicas can be promoted to become the new Master.</p><p>For example:</p><p>Before:</p><pre>Master<br>  ↓<br>Replica 1<br>Replica 2</pre><p>If the Master goes down:</p><pre>Replica 1 → New Master</pre><pre>Replica 2 → Replica</pre><p>Then a new replica can be created and connected to the new Master.</p><p>The system can continue operating.</p><p>But there is an important catch here.</p><p>In real systems, this process is not as simple as just saying:</p><p>“Master is down, make Replica 1 the Master.”</p><p>There can be something called replication lag.</p><p>The replica might not have received the latest changes from the Master before it went down.</p><p>That means some data might be slightly behind.</p><p>Handling these situations properly is one of the more complicated parts of building production systems.</p><h3>8. Putting Everything Together</h3><p>Now let’s connect everything we have learned so far.</p><p>Our system started with:</p><pre>User<br>  ↓<br>One Server<br>  ↓<br>Database</pre><p>Then we added multiple web servers:</p><pre>Users<br>  ↓<br>Load Balancer<br>  ↓<br>Multiple Web Servers<br>  ↓<br>Database</pre><p>And now, with database replication:</p><pre>Users<br>   ↓<br>Load Balancer<br>   ↓<br>Multiple Web Servers<br>   ↓<br>   ├── Read → Replica 1<br>   ├── Read → Replica 2<br>   ├── Read → Replica 3<br>   │<br>   └── Write → Master<br>                 ↓<br>          Replicates changes<br>          to all replicas</pre><p>So now the system is much more capable of handling a large number of users.</p><h3>What I Understood From This</h3><p>The main thing I understood from Database Replication is that we don’t want our entire application to depend on a single database server.</p><p>If there is only one database, it can become:</p><p>A performance bottleneck</p><p>A single point of failure</p><p>A limitation as traffic grows</p><p>Replication helps us solve these problems by keeping multiple copies of our data and distributing read traffic across them.</p><p>And just like the previous concepts, it follows the same idea I’m noticing while learning System Design:</p><p>A problem appears.</p><p>We understand why it happens.</p><p>Then we introduce something to solve that problem.</p><p>For me, that’s making System Design much easier to understand than trying to memorize all these terms separately.</p><h3>8. Caching: Making Frequently Requested Data Faster</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Q2ARF5hJylEJifkkWTBGug.png" /></figure><p>After learning about Database Replication, I came across another concept that solves a slightly different problem: <strong>Caching</strong>.</p><p>Suppose our website has some data that users request again and again.</p><p>For example, on a college website, thousands of students might open the same <strong>exam timetable</strong> or <strong>result page</strong>.</p><p>If every request goes directly to the database, the database has to do the same work again and again.</p><p>This is where caching helps.</p><h3>So, What is a Cache?</h3><p>A cache is basically a <strong>temporary storage layer</strong> where we keep data that we need frequently.</p><p>The main idea is simple:</p><p><strong>If we already have the data, why go to the database every time?</strong></p><p>Instead, we can keep a copy of that data in the cache, usually in memory, so that it can be accessed much faster.</p><p>Caching is especially useful for:</p><ul><li>Frequently requested data</li><li>Expensive computations</li><li>Data that doesn’t change very often</li></ul><h3>9. How Does Caching Actually Work?</h3><p>One common approach is called <strong>Read-Through Caching</strong>.</p><p>Suppose a user asks for some data.</p><p>The Web Server first checks the cache.</p><p>There can be two situations.</p><h3>Cache Hit</h3><p>The requested data is already present in the cache.</p><p>So we simply return it.</p><p><strong>User → Web Server → Cache → Response</strong></p><p>The database isn’t touched.</p><p>This makes the response much faster and also reduces the load on the database.</p><h3>Cache Miss</h3><p>The data isn’t present in the cache.</p><p>So now the Web Server goes to the database.</p><p><strong>User → Web Server → Cache → Database</strong></p><p>The database returns the data.</p><p>The Web Server then stores a copy of that data in the cache and sends the response to the user.</p><p>So when another user asks for the same data, we can hopefully get it directly from the cache.</p><p>That’s the basic idea behind caching.</p><h3>10. When Should We Use Caching?</h3><p>Caching isn’t something we should blindly add everywhere.</p><p>It works especially well for data that is:</p><p><strong>Read frequently but changed less frequently.</strong></p><p>For example, imagine your college website has a page showing the academic calendar.</p><p>Thousands of students might open it, but the calendar might only change a few times.</p><p>Instead of hitting the database every single time, we can keep the calendar in the cache.</p><p>But if the data changes every few seconds, caching becomes more complicated because we have to keep the cache updated.</p><p>This brings us to another important concept.</p><h3>11. What Happens When Cached Data Becomes Old?</h3><p>Imagine the database contains:</p><p><strong>Exam Date: 15 October</strong></p><p>The cache also contains:</p><p><strong>Exam Date: 15 October</strong></p><p>Now suppose the college changes the exam date to:</p><p><strong>20 October</strong></p><p>But the cache still has the old date.</p><p>A student might receive:</p><p><strong>15 October</strong></p><p>even though the database now says:</p><p><strong>20 October</strong></p><p>This is called <strong>stale data</strong>.</p><p>So we need some way to decide how long data should remain in the cache.</p><p>This is where <strong>TTL (Time To Live)</strong> comes in.</p><h3>12. TTL: How Long Should Data Stay in the Cache?</h3><p>TTL defines how long an item should remain in the cache before it expires.</p><p>For example:</p><pre>Exam Timetable → TTL = 10 minutes</pre><p>After 10 minutes, the cached data expires and the system can fetch fresh data again.</p><p>But choosing the TTL is important.</p><p>If the TTL is <strong>too short</strong>, cached data expires very frequently.</p><p>That means we get more cache misses and more requests go back to the database.</p><p>If the TTL is <strong>too long</strong>, we might serve old information to users.</p><p>So we need to choose the TTL based on how frequently the data changes and how important freshness is.</p><h3>13. Cache Consistency</h3><p>This also creates another problem.</p><p>We now have the same data in two places:</p><p><strong>Database</strong></p><p>and</p><p><strong>Cache</strong></p><p>So whenever data changes in the database, we need to think about what should happen to the cached copy.</p><p>Otherwise, the database might contain new information while the cache still contains old information.</p><p>This is why <strong>cache consistency</strong> becomes an important part of designing systems that use caching.</p><h3>14. What If the Cache Server Goes Down?</h3><p>There is another problem I came across while learning this.</p><p>What if we have only one cache server?</p><p>For example:</p><p><strong>Web Servers → One Cache Server → Database</strong></p><p>If that cache server goes down, all those requests that normally would have been served from the cache can suddenly start hitting the database.</p><p>This can put a huge amount of load on the database.</p><p>So the cache itself can become a <strong>Single Point of Failure</strong>.</p><p>One way to reduce this risk is to use multiple cache servers or a distributed cache setup.</p><p>The idea is the same as what we saw earlier with multiple web servers and database replicas:</p><p><strong>Don’t depend on a single machine for everything.</strong></p><h3>15. What Happens When the Cache Becomes Full?</h3><p>Cache usually has limited memory.</p><p>So eventually, it can become full.</p><p>Now the system has to decide:</p><p><strong>Which data should we remove to make space for new data?</strong></p><p>This is where <strong>Eviction Policies</strong> come in.</p><p>Some common ones are:</p><h3>LRU — Least Recently Used</h3><p>Remove the data that hasn’t been accessed for the longest time.</p><h3>LFU — Least Frequently Used</h3><p>Remove the data that has been accessed the fewest times.</p><h3>FIFO — First In, First Out</h3><p>Remove the data that was added first.</p><p>The exact policy depends on the application’s requirements.</p><h3>Putting It All Together</h3><p>So now our architecture is becoming a little more interesting.</p><p>We started with:</p><p><strong>Users → Server → Database</strong></p><p>Then we added multiple servers and a Load Balancer.</p><p>Then we added Database Replication.</p><p>And now we can add a Cache:</p><p><strong>Users → Load Balancer → Web Servers → Cache → Database</strong></p><p>The idea is pretty simple:</p><p><strong>Check the cache first.</strong></p><p>If the data is there, return it quickly.</p><p>If it isn’t, get it from the database and store a copy in the cache for future requests.</p><p>And this is another thing I’m starting to understand while learning System Design:</p><p>Every component we add is usually there because we have a specific problem to solve.</p><p>Here, the problem was repeated database requests and slow access to frequently used data.</p><p>The solution was <strong>Caching</strong>.</p><h3>CDN: How Websites Deliver Images, Videos and Other Static Files Faster</h3><p>In the last part, I learned about Database Replication and how having multiple database copies can help a system handle more traffic and avoid depending on a single database.</p><p>While going through the next concept, I came across something I had seen many times but never really thought about:</p><p><strong>CDN.</strong></p><p>You might have noticed websites loading images, videos, CSS, and JavaScript files very quickly, even when the actual server is located far away.</p><p>So I wanted to understand:</p><p><strong>How does that actually happen?</strong></p><h3>1. First, What Exactly Are We Trying to Deliver?</h3><p>Before understanding CDN, I first had to understand that not everything a website sends to us is the same.</p><p>There are mainly two types of content.</p><h3>Static Content</h3><p>Static content is something that usually doesn’t change depending on who is requesting it.</p><p>For example:</p><ul><li>Images</li><li>Videos</li><li>CSS files</li><li>JavaScript files</li><li>PDFs</li></ul><p>Think about an online bookstore.</p><p>The cover image of a particular book is the same for everyone.</p><p>If 10,000 people open that book’s page, the same image might be requested 10,000 times.</p><p>That is static content.</p><h3>Dynamic Content</h3><p>Dynamic content is different.</p><p>It can change depending on the user or the request.</p><p>For example, when you open your profile, the website needs to show your name, your orders, your account information, etc.</p><p>That information is usually generated or fetched by the application servers.</p><p>So the basic idea is:</p><p><strong>Static content → Can be served separately</strong></p><p><strong>Dynamic content → Usually needs the application servers</strong></p><p>And this is where CDN becomes useful.</p><h3>2. So, What Is a CDN?</h3><p>CDN stands for <strong>Content Delivery Network</strong>.</p><p>Here is how CDN works at the high-level: when a user visits a website, a CDN server closest to the user will deliver static content. Intuitively, the further users are from CDN servers, the slower the website loads. For example, if CDN servers are in San Francisco, users in Los Angeles will get content <strong>faster</strong> than users in Europe. Below figure is a great example that shows how CDN improves load time.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/826/1*XFm6uKnvsvT9A8RGuqCLcg.png" /></figure><p>In simple words, a CDN is a network of servers placed in different geographical locations.</p><p>The main purpose is to keep copies of <em>static content </em>closer to users.</p><p>Why does location matter?</p><p>Suppose your website’s main server is located in the United States.</p><p>If a user is also in the US, the distance is relatively <strong>small</strong>.</p><p>But now imagine a user accessing the same website from India.</p><p>The request has to travel a much <strong>longer</strong> distance.</p><p>More physical distance generally means more network latency.</p><p>This is where a CDN helps.</p><p>Instead of every user fetching static files from the main server, the CDN can keep copies of those files at servers closer to users.</p><p>So a user in India might get an image from a nearby CDN location instead of requesting it all the way from the origin server in the US.</p><h3>3. How Does a CDN Actually Work?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/895/1*QQewtcMVhlGgZNIK1-3KYw.png" /></figure><p>This part became much easier for me when I looked at a simple example.</p><p>Imagine two users:</p><p><strong>User A</strong> and <strong>User B</strong></p><p>Both want the same file:</p><p>image.png</p><p>The CDN doesn’t have this image yet.</p><p>So User A requests it.</p><p>The request reaches the CDN.</p><p>The CDN checks:</p><p><strong>“Do I already have image.png?”</strong></p><p>The answer is no.</p><p>So this is called a <strong>Cache Miss</strong>.</p><p>The CDN now requests the file from the original web server.</p><p>The server sends image.png back to the CDN.</p><p>The CDN stores a copy of it and also sends the image back to User A.</p><p>The flow looks like this:</p><p><strong>User A → CDN → Web Server</strong></p><p><strong>Web Server → CDN → User A</strong></p><h3>4. What Happens When User B Requests the Same Image?</h3><p>Now User B asks for:</p><p>image.png</p><p>But this time, something has changed.</p><p>The CDN already has the image.</p><p>So there is no need to contact the original web server again.</p><p>The CDN simply returns the stored copy.</p><p>This is called a <strong>Cache Hit</strong>.</p><p>So now the flow is:</p><p><strong>User B → CDN → User B</strong></p><p>The original server doesn’t need to send the image again.</p><p>This gives us two important benefits:</p><p><strong>Faster response for the user</strong></p><p>and</p><p><strong>Less work for the original server</strong></p><p>This is one of the main reasons CDNs are useful for websites with lots of static content.</p><h3>5. CDN Is Not Just About Speed</h3><p>Initially, I thought CDN was mainly about making websites faster.</p><p>But there is another important benefit.</p><p>It also reduces the amount of work the main server has to do.</p><p>Imagine thousands of users requesting the same image.</p><p>Without a CDN:</p><p><strong>1000 users → Web Server → 1000 image requests</strong></p><p>With a CDN:</p><p><strong>1000 users → CDN</strong></p><p>The CDN can serve the same cached image to those users without repeatedly asking the origin server for it.</p><p>So the main server can focus more on processing actual application requests.</p><h3>6. But How Does the CDN Know When to Remove Old Files?</h3><p>Now another question comes up.</p><p>Suppose we upload a new version of an image.</p><p>But the CDN still has the old version stored.</p><p>Users might continue receiving the old file.</p><p>So CDN caching also needs an expiration mechanism.</p><p>This is where <strong>TTL (Time To Live)</strong> comes in.</p><p>TTL basically tells the CDN how long it should keep a particular file cached.</p><p>For example:</p><p>image.png → TTL = 1 hour</p><p>After the TTL expires, the CDN can fetch a fresh copy from the origin server when needed.</p><p>But choosing the right TTL isn’t always straightforward.</p><h3>TTL too short</h3><p>The CDN keeps requesting files from the origin server more frequently.</p><p>This reduces some of the benefits of caching.</p><h3>TTL too long</h3><p>Old content might remain cached for too long.</p><p>So again, it comes down to the requirements of the application.</p><h3>7. What If We Need to Update a File Immediately?</h3><p>Sometimes we don’t want to wait for the TTL to expire.</p><p>For example, suppose we have:</p><p>image.png</p><p>The CDN has an old version cached.</p><p>We upload a new version, but users are still getting the old one.</p><p>One option is to use the CDN provider’s <strong>invalidation API</strong> to remove the old cached file.</p><p>Another common approach is <strong>URL versioning</strong>.</p><p>For example:</p><pre>image.png?v=1</pre><p>Later, when the file changes:</p><pre>image.png?v=2</pre><p>The CDN treats this as a different URL and can fetch the new version.</p><p>This is a simple idea, but it can be very useful when working with cached static files.</p><h3>8. What If the CDN Goes Down?</h3><p>There is another thing I learned while going through this.</p><p>What happens if the CDN itself becomes unavailable?</p><p>We shouldn’t design the entire application assuming that the CDN will always work.</p><p>A fallback can be kept so that static files can still be served from the original server if necessary.</p><p>Of course, this may increase the load on the origin server, but it gives the application another path to serve the content.</p><p>This is another example of the same principle I’ve been seeing throughout System Design:</p><p><strong>Don’t depend completely on a single component.</strong></p><h3>9. What Should We Cache?</h3><p>CDNs are especially useful for content such as:</p><ul><li>Images</li><li>Videos</li><li>CSS</li><li>JavaScript</li><li>Other static files</li></ul><p>But caching everything isn’t always a good idea.</p><p>There is a cost involved because CDN providers charge based on things such as data transfer and usage.</p><p>So if a file is rarely requested, putting it on a CDN may not always provide much benefit.</p><p>Again, it depends on the application.</p><h3>10. Connecting CDN With Everything We Learned So Far</h3><p>Now things are getting interesting. 😉</p><p>So far, our architecture has gone through several stages.</p><p>We started with a simple server.</p><p>Then we added multiple Web Servers.</p><p>Then a Load Balancer.</p><p>Then Database Replication.</p><p>Then a Cache.</p><p>And now we’re adding a CDN for static content.</p><p>Our architecture is starting to look something like this:</p><p><strong>User → CDN → Static Content</strong></p><p>and for application requests:</p><p><strong>User → Load Balancer → Web Servers → Cache → Database</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/806/1*DS7qoWNTuFUqzn1XkZzT5g.png" /></figure><h3>11. The Complete Request Flow</h3><p>So, putting everything together:</p><h3>If the user requests static content</h3><p>For example:</p><p>image.png</p><p>The request can go through the CDN.</p><p>If the CDN already has the file:</p><p><strong>User → CDN → Response</strong></p><p>No need to contact the Web Server.</p><p>If the CDN doesn’t have it:</p><p><strong>User → CDN → Web Server → CDN → User</strong></p><p>The CDN stores the file so future requests can be served faster.</p><h3>If the user makes a dynamic request</h3><p>For example:</p><p>“Show me my profile.”</p><p>That request needs application logic.</p><p>So it can go through:</p><p><strong>User → Load Balancer → Web Server</strong></p><p>The Web Server may then check the Cache.</p><p>If the data is not available there, it can query the appropriate database.</p><p>For example:</p><p><strong>Read → Replica Database</strong></p><p><strong>Write → Master Database</strong></p><p>And the Master continues replicating changes to the replicas.</p><h3>The System So Far</h3><p>What started as:</p><p><strong>User → One Server → Database</strong></p><p>has now evolved quite a bit:</p><p><strong>Users → CDN / Load Balancer → Multiple Web Servers → Cache → Replicated Database</strong></p><p>And honestly, this is what I find interesting about learning System Design.</p><p>Every time the system grows, a new problem appears.</p><p>More traffic?</p><p>→ Add more servers.</p><p>Multiple servers?</p><p>→ Add a Load Balancer.</p><p>Database becomes a bottleneck?</p><p>→ Add Replication.</p><p>Repeated database requests?</p><p>→ Add a Cache.</p><p>Static content is being requested from users all over the world?</p><p>→ Add a CDN.</p><p>I’m slowly realizing that System Design is basically this continuous process of <strong>finding the bottleneck and figuring out how to handle it</strong>.</p><p>And there is still a lot more to learn.</p><p>So, I think that’s enough for today.</p><p>I’ll keep sharing what I learn as I continue with System Design.</p><p><em>Thanks for reading, and see you in the next article! :)</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/proxy/1*84vn-wc1LQzJThajUUxZAg.gif" /></figure><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2b977d0035cd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How a Simple Website Scales to Millions of Users: A Beginner’s Guide to System Design (Part 1)]]></title>
            <link>https://medium.com/@sa8103339/how-a-simple-website-scales-to-millions-of-users-a-beginners-guide-to-system-design-part-1-38bca63aed67?source=rss-4917855a024d------2</link>
            <guid isPermaLink="false">https://medium.com/p/38bca63aed67</guid>
            <category><![CDATA[system-design-concepts]]></category>
            <dc:creator><![CDATA[Samir]]></dc:creator>
            <pubDate>Sun, 23 Aug 2026 15:33:09 GMT</pubDate>
            <atom:updated>2026-08-23T15:52:59.275Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ZTeEE0ukiNTitjlg.jpg" /></figure><p>Recently, I started learning System Design, and honestly, words like Load Balancer, Scaling, Database Replication, and Distributed Systems initially sounded pretty complicated to me.</p><p>So I decided to learn everything from the basics, step by step.</p><p>In this article, I’m sharing what I’ve learned so far and explaining these concepts in the same simple way that helped me understand them.</p><p>We’ll start with a very basic question: how does a simple website can slowly grow into a system capable of handling millions of users and what happens when more and more users start using it?</p><p>Let’s start from the beginning.</p><h3>1. One User, One Server</h3><p>Suppose, you create a website called:</p><p>mywebsite.com</p><p>A user opens the website from their browser. But how does the browser know where your website actually exists?</p><p>This is where DNS comes in.</p><p>DNS converts a domain name like mywebsite.com into an<strong> IP address</strong> that computers can understand.</p><p>Now you might be thinking, <strong>what is an IP Address?</strong></p><p>well , it like a home address. Just like your home has an address so people can find it, every server has an IP Address so computers can find it on the internet.</p><p>For example: 142.250.195.46</p><p>The basic flow looks like this:</p><p><strong>User → DNS → Server → Response → User</strong></p><p>The user enters the website URL, DNS finds the server’s IP address, the browser sends an HTTP request to the server, and the server sends the response back.</p><p>For a small application, this setup works perfectly.</p><p>But what happens when your website starts getting thousands or even millions of users?</p><p>That is where the real problems begin.</p><h3>2. Traffic Starts Growing</h3><p>Think about your college during exam time.</p><p>On a normal day, only a few students might use the college website or portal at the same time. The server can easily handle those requests.</p><p>But imagine the day when semester results are announced.</p><p>Suddenly, thousands of students open the website at the same time to check their results.</p><p>Everyone is sending requests to the same server.</p><p>Now the server has to handle:</p><p>=&gt; More requests</p><p>=&gt; More processing</p><p>=&gt; More database queries</p><p>=&gt; More CPU and memory usage</p><p>If the traffic becomes too high, the website starts becoming slow. Sometimes, you may even see that frustrating message:</p><p><strong>“Server is busy. Please try again later.”</strong></p><p>The same problem happens with any growing application. As more users start using it, one server may eventually struggle to handle everything.</p><p>So, the first improvement is to separate responsibilities.</p><h3>3. Separating the Web Server and Database</h3><p>In a simple setup, one server might handle both the application and the database.</p><p>Instead, we can separate them:</p><p><strong>User → Web Server → Database Server</strong></p><p>The Web Server handles user requests and application logic.</p><p>The Database Server stores and retrieves data. This is useful because both can now scale independently.</p><p>If your application gets more users, you can focus on adding more Web Servers.</p><p>If database operations become heavy, you can improve the Database side separately.</p><p>Simple change, but it makes the architecture much more flexible.</p><h3>4. SQL or NoSQL?</h3><p>Now another question comes up. Which database should we use?</p><p>There are two common categories:</p><h3>SQL Databases</h3><p>Examples:</p><p>=&gt; MySQL</p><p>=&gt; PostgreSQL</p><p>=&gt; Oracle</p><p>SQL databases store data in tables and work well when your data is structured and has relationships.</p><p>For example:</p><p>Users → Orders → Products</p><p>SQL is useful when you need structured data and complex relationships between tables.</p><h3>NoSQL Databases</h3><p>Examples:</p><p>=&gt; MongoDB</p><p>=&gt; Cassandra</p><p>=&gt; DynamoDB</p><p>NoSQL databases are more flexible and can work well with unstructured or changing data.</p><p>For example, a user’s profile might contain different types of information in a document-like structure.</p><p>The important thing I learned here is:</p><p><strong>There is no universally “best” database. The right choice depends on your application.</strong></p><h3>5. Vertical vs Horizontal Scaling</h3><p>Now imagine your website is growing and getting more users every day. At some point, one server may not be enough to handle all the traffic.</p><p>So, how do we increase the capacity?</p><p>There are mainly two ways: <strong>Vertical Scaling</strong> and <strong>Horizontal Scaling</strong>.</p><h3>Vertical Scaling</h3><p>Think about your own laptop.</p><p>If your laptop starts becoming slow while running heavy applications, one option is to upgrade it. You might add more RAM, get a better processor, or upgrade the storage.</p><p>The laptop is still the same machine, but now it has become more powerful.</p><p>That is basically what <strong>Vertical Scaling</strong> means.</p><p>Instead of adding more servers, we upgrade the existing server by giving it:</p><p>=&gt; More RAM</p><p>=&gt; More CPU</p><p>=&gt; Better hardware</p><p>For smaller applications, this can be a simple and easy solution.</p><p>But there is a problem.</p><p>Just like you cannot keep upgrading your laptop forever, you also cannot keep upgrading one server forever. Every machine has a limit.</p><p>There is another important issue too.</p><p>Imagine your entire website depends on just one powerful server. What happens if that server suddenly crashes?</p><p>Everything goes down with it.</p><p>Your website becomes unavailable because there is no other server to take over.</p><p>This is called a <strong>Single Point of Failure</strong>.</p><p>So, upgrading one server can help for some time, but for very large applications, we need another approach.</p><p>And that is where <strong>Horizontal Scaling</strong> comes in.</p><h3>Horizontal Scaling</h3><p>Now, instead of making one server more powerful, what if we simply add more servers?</p><p>Think about a group project in college.</p><p>If one student has to do the entire project alone, it can take a lot of time and become overwhelming. But if the work is divided among 4 or 5 students, everyone can handle a part of it.</p><p>The same idea applies here.</p><p>Instead of putting all the traffic on one server, we add multiple servers and divide the work between them.</p><p>For example:</p><p><strong>Users → Server 1</strong></p><p><strong>Users → Server 2</strong></p><p><strong>Users → Server 3</strong></p><p>Now, multiple servers can handle the incoming requests together instead of depending on just one server.</p><p>This is called <strong>Horizontal Scaling</strong>, or simply <strong>Scaling Out</strong>.</p><p>One big advantage is that if one server goes down, the other servers can still handle the traffic. So the entire application does not have to go down.</p><p>And if your application keeps growing, you can simply add more servers when needed.</p><p>Sounds much better, right?</p><p>But now we have another question.</p><p>If we have multiple servers, <strong>who decides which server should receive a user’s request?</strong></p><p>And this is where the <strong>Load Balancer</strong> comes in.</p><h3>6. The Load Balancer</h3><p>Now we have multiple servers, which is great.</p><p>But there is one problem.</p><p>When a user opens our website, how does the request know which server to go to?</p><p>Should it go to Server 1?</p><p>Server 2?</p><p>Or Server 3?</p><p>We need something that can manage this traffic and send requests to the right server.</p><p>This is where a <strong>Load Balancer</strong> comes in.</p><p>Think of it like a traffic police officer at a busy intersection.</p><p>When too many vehicles are coming from different directions, the traffic officer helps control and direct them so that one road does not become completely crowded.</p><p>A Load Balancer does something similar, but with user requests.</p><p>The architecture now looks like this:</p><p><strong>Users → Load Balancer → Multiple Web Servers</strong></p><p>Instead of users directly connecting to different servers, every request first goes to the Load Balancer.</p><p>The Load Balancer then checks the available servers and distributes the requests between them.</p><p>For example:</p><p><strong>User 1 → Load Balancer → Server 1</strong></p><p><strong>User 2 → Load Balancer → Server 2</strong></p><p><strong>User 3 → Load Balancer → Server 3</strong></p><p>This way, one server does not have to handle all the traffic alone.</p><h3>What if a server goes down?</h3><p>Suppose Server 2 suddenly stops working.</p><p>Without a Load Balancer, some users might still try to connect to that server and get an error.</p><p>But with a Load Balancer, it can detect that Server 2 is not available and stop sending new requests to it.</p><p>The traffic can be redirected to the healthy servers instead.</p><p>So the application can continue running.</p><p>Another advantage is that as your application grows, you can add more servers whenever needed.</p><p>For example:</p><p>Before:</p><p><strong>Load Balancer → Server 1 + Server 2 + Server 3</strong></p><p>Later, when traffic increases:</p><p><strong>Load Balancer → Server 1 + Server 2 + Server 3 + Server 4</strong></p><p>The new server can now also share the workload.</p><p>So basically, a Load Balancer helps us with two important things:</p><p>=&gt; Distributing traffic between multiple servers</p><p>=&gt; Keeping the application running even if one server fails</p><p>And that’s why Load Balancers are an important part of large-scale systems.</p><p>But even after solving the web server problem, we still have one major problem left.</p><p><strong>What happens if our Database Server goes down?</strong></p><p>And that’s what I’ll be learning next.</p><h3>7. The Journey So Far</h3><p>If we look back at where we started, the journey is actually pretty interesting.</p><p>At first, everything was simple:</p><p><strong>User → One Server</strong></p><p>One server was handling the requests, application logic, and everything else.</p><p>But as the number of users started growing, new problems started appearing.</p><p>So our system slowly evolved into this:</p><p><strong>Users → Load Balancer → Multiple Web Servers → Database</strong></p><p>And honestly, this is the point where System Design started making much more sense to me.</p><p>I realized that we don’t just randomly add things like more servers, databases, or Load Balancers.</p><p>Every new thing is added because the current system has a problem.</p><p>For example:</p><p>→ More users and too much traffic? We need more capacity.</p><p>→ One server is becoming overloaded? We can add more servers.</p><p>→ Now we have multiple servers? We need a Load Balancer to distribute the traffic.</p><p>→ What if the database becomes the bottleneck? Well, that’s the next problem we need to solve.</p><p>That’s basically how I now look at System Design.</p><p>You start with something simple. Then the application grows.</p><p>A problem appears. You find a solution.</p><p>Then as the system grows further, another problem appears.</p><p>And the system keeps evolving step by step.</p><p>For me, this way of learning makes System Design feel much less complicated. Instead of trying to memorize big terms, I’m just trying to understand one problem at a time and why a particular solution is needed.</p><h3>Final Thoughts</h3><p>One thing I’ve realized while learning System Design is that it’s not really about memorizing complicated terms.</p><p>It’s more about understanding one simple thing:</p><p><strong>What problem is my system facing right now?</strong></p><p>Then you find a solution.</p><p>As the application grows, new problems appear. You solve them one by one.</p><p>That’s the approach I’m following while learning System Design, and honestly, it has made everything feel much easier to understand.</p><h3>FINAL NOTE :</h3><p>So, this was Part 1 of my <em>System Design </em>learning journey.</p><p>I’m still learning and trying to understand these concepts from the basics, step by step. I’ll be sharing what I learn along the way in the upcoming parts.</p><p>Stay tuned for Part 2.</p><blockquote>Thanks for reading! :)</blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=38bca63aed67" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>