All articles
Switch to dark mode
Translate

Day 2/60: Two-Tier Architecture – Separating Web and Database Servers

Frengky Soritua ManurungFrengky Soritua Manurung
··8 min read
Day 2/60: Two-Tier Architecture – Separating Web and Database Servers

Share this article


Introduction

When building an application for the first time, the most natural (and most common) approach is to put everything on a single server: the web server, the application, and the database all living together on the same machine.

This approach is fast and practical for getting started. But once the application starts receiving real traffic, this setup quickly reveals its weaknesses. In today's article, we'll explore why separating the Web Server from the Database Server is a crucial step in the evolution of system architecture, and how to do it safely.


Why Combining Web Server and Database on One Machine Is Risky

When the Web Server and Database run on the same machine, two major risks emerge:

a. Resource Contention

Both the web server and the database compete for the same CPU, RAM, and disk I/O to perform well. The problem is, their resource profiles are different:

  • The web server needs responsive CPU to handle many concurrent requests.
  • The database needs large amounts of RAM for query caching and fast disk I/O for reading and writing data.

When both compete for the same limited resources, one—or both—will suffer degraded performance. A common real-world scenario: when a heavy database query runs, the website's response time slows down because CPU and memory are being consumed by the database process.

b. Single Point of Failure

This is the more dangerous issue. If that single server goes down—whether from a crash, resource exhaustion, or scheduled maintenance—the entire system goes down with it. The website becomes unreachable, and the database becomes inaccessible at the same time. There's no redundancy at all.

Imagine needing to restart the server for an OS update. That means your application and your data go offline simultaneously.


Steps to Separate the Database into a Dedicated Server

The solution is to implement a Two-Tier Architecture: splitting the Web Server and Database Server into two separate machines. Here's a general outline of the process:

  • Provision a new server dedicated to the database (this could be a separate VM, a cloud instance, or a managed database service).
  • Migrate the data from the old database to the new server, typically using backup-restore tools (such as mysqldump, pg_dump, or a snapshot).
  • Update the connection configuration on the application/web server side, changing the database host from localhost to the new database server's IP address or hostname.
  • Test the connection and performance to make sure the application can still read and write data normally.
  • Set up separate monitoring for each server, since there are now two points whose health needs to be watched independently.

With this separation, each server can be scaled independently. For example, if website traffic increases, you can simply scale up the Web Server without touching the Database Server, and vice versa.


Understanding Network Latency Between Servers

Once separated, the Web Server and Database Server communicate over the network instead of through a local connection within the same machine (localhost). This introduces an important concept: network latency.

Latency is the time it takes for data to travel from one point to another across a network. When the Web Server needs to fetch data from the Database Server, there's now additional travel time compared to when both were on the same machine.

Several factors influence this latency:

  • Physical distance between servers (servers in the same region are typically faster).
  • Internal network quality of the cloud provider.
  • Number of round trips between the application and the database (the more repeated small queries there are, the more the accumulated latency is felt).

Because of this, a best practice is to keep the Web Server and Database Server within the same region or network zone, so that latency stays minimal even though the two are now physically and logically separated.


Security Perspective: Placing the Database in a Private VPC/Subnet

Separating the servers also opens the door to strengthening system security, specifically by placing the Database Server inside a private network (Private VPC or Private Subnet).

The concept is simple: the database doesn't need to be directly accessible from the internet. The only thing that needs to reach it is the Web Server, which sits within the same internal network.

The diagram below illustrates this setup:

The Web Server sits in a public subnet and is reachable from the internet. The Database Server sits in a private subnet with no public IP, and is only reachable internally from the Web Server. Any direct access attempt from the internet to the database is blocked.

Benefits of this approach:

  • Reduces the attack surface — since the database has no public IP, attackers from outside cannot directly attempt to exploit it.
  • Tighter access control — only traffic from the Web Server (over the private network) is allowed to reach the database, typically enforced through security groups or firewall rules.
  • Follows the principle of least privilege — each system component only has the minimum access necessary to perform its function.

With this architecture, even though the Web Server sits in the public network so users can reach it, the database remains "hidden" and can only be accessed from within the system's internal network.


Conclusion

Separating the Web Server and Database Server isn't just an architectural trend—it's a fundamental requirement for building a system that is more stable, more secure, and easier to scale. Here's a summary:

AspectBefore (Single Server)After (Two-Tier)
ResourcesCompeting for CPU/RAM/I/OSeparated and independent
FailureOne point of failure, everything goes downMore resilient to partial failure
ScalingHard to scale independentlyCan scale each component as needed
SecurityDatabase is exposed along with the web serverDatabase can be hidden in a private network
Trade-offIntroduces network latency that needs to be managed

Other Articles