No history yet

Infrastructure Components

Handling the Crowd

When a website or app becomes popular, a single server can't handle all the user requests. It gets overwhelmed, just like a single cashier during a holiday sale. The system slows down and can even crash. To prevent this, we use a load balancer.

A load balancer is like a traffic cop for your servers. It sits in front of your servers and distributes incoming requests across them. This ensures that no single server gets overloaded. If one server goes down, the load balancer simply stops sending traffic to it, routing requests to the healthy servers instead. This improves both scalability and reliability.

There are several ways a load balancer can decide where to send traffic. The simplest is Round Robin, which just cycles through the servers in order. Another common method is Least Connections, where the load balancer sends the new request to the server that is currently handling the fewest active connections. The choice of strategy depends on the specific needs of the system.

Speeding Things Up with Caching

Accessing data from a database can be slow, especially if the same data is requested over and over. Caching is a technique to speed this up by storing a copy of frequently accessed data in a location that is much faster to access, called a cache.

Think of a librarian. Instead of running to the back stacks for the most popular new book every time someone asks for it, they keep a few copies right at the front desk. This makes the process much quicker for everyone. A cache does the same for your application. When a request for data comes in, the system checks the cache first. If the data is there (a "cache hit"), it's returned immediately. If not (a "cache miss"), the system retrieves it from the database and usually stores a copy in the cache for next time.

Caches can be implemented at different levels. A browser can cache website assets on your computer, a Content Delivery Network (CDN) can cache content in locations geographically closer to users, and an application server can have its own cache for database queries. While caching dramatically improves performance, it also introduces complexity. The hardest part is often figuring out when to remove or update stale data from the cache, a problem known as "cache invalidation."

Working Together, Separately

In a complex system, different parts, or services, need to communicate with each other. If they communicate directly, they become tightly coupled. If one service fails or slows down, it can cause a chain reaction and bring down other services that depend on it.

A message queue solves this by decoupling services. It acts as an intermediary, a sort of shared to-do list. One service, the producer, adds a message (a task to be done) to the queue. Another service, the consumer, picks up that message and processes it whenever it's ready. The producer doesn't need to know anything about the consumer, or even if it's currently online.

This asynchronous communication makes the system more resilient. If the consumer service crashes, the messages simply wait safely in the queue until it comes back online. It also improves scalability. If the producer is creating messages faster than a single consumer can process them, you can simply add more consumers to work through the queue in parallel.

Growing Your Database

As an application handles more users and more data, its database is often the first component to feel the strain. A single database server can only handle so much. When it reaches its limit, you need a strategy to scale it. The two most common strategies are replication and sharding.

Replication involves creating copies of your database. You have one primary (or master) database that handles all the write operations (like creating new users or posts). This primary database then replicates those changes to one or more secondary (or replica) databases. The replicas can handle all the read operations.

Replication works well for applications with many more reads than writes, but it doesn't solve the problem of a primary database growing too large.

For that, we use sharding. Sharding, or partitioning, involves splitting your data horizontally across multiple databases. Each database, called a shard, holds a distinct subset of the data. For example, you could put users with last names A-M on one shard and users N-Z on another. When a request comes in, the application knows which shard to query based on the data it's looking for.

Sharding allows a database to scale both reads and writes almost infinitely, but it's significantly more complex than replication. It requires changes to the application logic and makes tasks like running queries across all your data more difficult. The choice between them is a classic example of the trade-off analysis we covered earlier.

Now, let's test your knowledge of these core infrastructure components.

Quiz Questions 1/5

An e-commerce website is experiencing a massive surge in traffic, causing its single web server to frequently crash. What is the most effective initial step to improve the site's reliability and handle the increased load?

Quiz Questions 2/5

A load balancer receives a new request and routes it to the server with the fewest active connections. This strategy is known as ______.

Understanding these components—load balancers, caches, message queues, and database scaling techniques—is fundamental to building systems that can grow and adapt without failing under pressure.