NOTE:

Do not read it, Ai generate slop. Just add for testing links working or not, will update the actual things i did later.

Software engineering is often about managing constraints. When you are building an MVP, your constraints are time and complexity. You stick everything in one box.

But when you start growing, the constraints shift. Suddenly, your constraint is physics.

Specifically, the physics of a single hard drive and a single CPU trying to answer thousands of questions at once.

Recently, I hit this wall. My Postgres database was crying for help. Here is how I deconstructed the problem and implemented a scalable solution using Read Replicas and a custom SQL-based Load Balancer.


1. The Single Node Problem

Let’s start at the beginning. In the simplest architecture, you have one Application Server and one Database.

codeMermaid

Application ---> [ Postgres (Primary) ]

Every time a user logs in, loads a feed, or updates their profile, the Application sends a query to Postgres. Postgres does the math, writes to the disk, and sends the answer back.

This works perfectly... until it doesn't.

As traffic grows, two things happen:

  1. CPU Saturation: The database spends all its time processing queries.
  2. I/O Blocking: The disk can only write and read so fast.

If you add more CPU and RAM (Vertical Scaling), you are just delaying the inevitable. You will eventually hit the limit of the largest machine money can buy. We need a way to spread the load horizontally.

2. Analyzing the Traffic (The 80/20 Rule)

To solve this, we have to look at what the database is actually doing.