Skip to content
Tutorials Center
My learning

Topics

Lesson 34 of 35 ยท Projects and Practice

Project: A Three-Tier Architecture on DigitalOcean

Design a scalable three-tier setup on DigitalOcean: a VPC, Load Balancer, web Droplets, a managed database, firewalls by tag, a cost estimate and an architecture checker.

One Droplet running everything is fine until it is not: a deploy takes the site down, a traffic spike exhausts memory, and the data lives on the same disk as the code. The classic fix is tiers: a public entrance (Load Balancer), a stateless application tier (several identical Droplets) and a data tier (a managed database) on a private network. Each tier scales and fails separately.

This is a design project. DigitalOcean is not available on this site, so you will write the plan and the Terraform skeleton yourself, and the Python stand-ins validate and price the design with sample numbers. Prices are examples.

The target

flowchart TB
  U["Visitors"] --> LB["Load Balancer (public IP, TLS)"]
  subgraph VPC["VPC 10.20.0.0/24, one region"]
    LB --> W1["web-1 (tag web)"]
    LB --> W2["web-2 (tag web)"]
    W1 --> DB["Managed PostgreSQL<br/>(private connection)"]
    W2 --> DB
    W1 --> R["Managed Redis<br/>(sessions, cache)"]
    W2 --> R
  end
  W1 -.-> S["Spaces + CDN<br/>(uploads, static files)"]
  W2 -.-> S

Only the Load Balancer is public. Web Droplets and databases live in the VPC.

Design decisions

  1. One region, one VPC. Resources in the same region and VPC talk over the private network, which is fast and free. Choose a region close to your users. Use a separate VPC per environment (production, staging).
  2. Load Balancer terminates TLS (a managed certificate, see TLS), health-checks /healthz and sends traffic to tag web (Load Balancers).
  3. Stateless web tier. Sessions in Redis, uploads in Spaces, configuration from environment variables, code from a Git tag or an image. Then any Droplet can be replaced at any time.
  4. Managed database with a standby node for failover, daily backups and trusted sources: allow connections only from your VPC or from the web tag. Never use a public database hostname in the application.
  5. Firewalls by tag. web accepts 8080 only from the Load Balancer and 22 only from your VPN or office. The database firewall accepts only the web tag (Cloud Firewalls).
  6. Reproducibility. Terraform (Terraform) for the VPC, Droplets, firewalls, balancer and database; cloud-init for first boot.
  7. Operations. Monitoring alerts on the tag, an uptime check on the public URL, backups and a restore drill (backups).

Step 1: validate the network plan

Subnets must not overlap, and every Droplet needs a private IP inside the VPC range. This stand-in checks a plan with the ipaddress module.

Python Stand-in: check VPC addressing

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Fix the dev range and decide what belongs in which network: here db-1 is on the staging address, a bug the check reveals.

Step 2: validate the architecture

Write down the design as data and check the rules you care about. This stand-in verifies that only the balancer is public, the database accepts only the web tier and there are at least two web servers.

Python Stand-in: architecture rules checker

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Step 3: estimate the cost

Price the design with example numbers, then compare "small" and "grown" scenarios.

Python Stand-in: three-tier cost estimate

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The totals are only as good as the example prices: real ones come from the pricing page. The exercise shows the shape: web nodes and the database dominate, and a Load Balancer is a small fixed cost.

Step 4: Terraform skeleton

Extend the configuration from the Terraform lesson (read-only reference):

HCL
resource "digitalocean_database_cluster" "pg" {
  name                 = "app-pg"
  engine               = "pg"
  version              = "16"
  size                 = "db-s-1vcpu-2gb"
  region               = var.region
  node_count           = 2
  private_network_uuid = digitalocean_vpc.main.id
}

resource "digitalocean_database_firewall" "pg" {
  cluster_id = digitalocean_database_cluster.pg.id
  rule {
    type  = "tag"
    value = "web"
  }
}

Run terraform plan, read it for surprises, apply to a staging project first and destroy it when you are done. Check the allowed engine versions and plan sizes in the provider documentation, because they change.

Extensions

  • Add a second web pool in another zone or region behind a global load balancer.
  • Put a CDN in front of Spaces for static files.
  • Move the web tier to App Platform or DOKS (Kubernetes) and keep the same data tier.
  • Add a read replica for reporting queries.
  • Write a runbook: what to do if a web node dies, the database fails over or the certificate expires.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why should web servers be stateless?

    State in Redis, the database and Spaces lets you add, remove and replace web nodes freely.
  2. 2. Who should be able to connect to the database?

    Trusted sources restricted to the web tag keep the data tier private.
  3. 3. What does the overlap check protect you from?

    Overlapping ranges make it ambiguous where an address lives and prevent peering.
  4. 4. Which tier is public in this design?

    The balancer is the single entrance; everything else sits in the VPC.

Summary

  • A three-tier design separates the public entrance, the stateless web tier and the managed data tier inside one VPC.
  • Firewalls by tag and database trusted sources enforce who can talk to whom.
  • Validate addressing and rules with code, estimate costs from real prices, and keep the whole thing in Terraform.
  • Test in staging and tear down what you do not need.

Next: Practice: Troubleshooting and Review