Skip to main content

Build a scalable web app on Compute Instances

20 min tutorial

What you'll build​

Two Compute Instances sharing a label, and an L4 Load Balancer that fronts both of them behind a single public IP — the pattern for scaling a web app horizontally instead of resizing a single instance.

Prerequisites​

  • A SonicInfra account with billing enabled.
  • An SSH key on your account (handy for troubleshooting, not required by the steps below).
  • Comfort with the basics from Deploy your first compute instance — this tutorial moves faster over instance creation and spends its time on the new part: labels and load balancer selectors.
Show me!

Open Compute Instances

Click Compute Instances in the sidebar.

Create the first instance

Click + Create Compute Instance.

Fill out the instance details

Fill in the fields:

  • Instance Type — pick ne.2 for this tutorial.
  • Image — pick ubuntu-24-04.
  • Labels — this is the important one. Add a label so a Load Balancer can find this instance later without naming it directly: tier: web. Any instance carrying this label will be treated as part of the same pool.

Before you create it

Compute instances are a paid resource — your organization needs billing set up before you can create one.

tip

Haven't set up billing yet? Here's how to add a payment method first.

Create the first instance

Click Create Compute Instance.

  • instanceType — ne.2 — 2 cores, 8Gi RAM. Plenty for a small web app; bump the core count for more headroom.
  • labels — tier: web is what the Load Balancer's selector will match on later — not the instance name — so it can find both instances without listing them individually.

Create a second instance

Click + Create Compute Instance again — one instance isn't a pool. This second one is what makes the Load Balancer's selector actually matter.

Create the second instance

Same Instance Type and Image as before. Give it a different Name — web-02 — but the same label: tier: web. Click Create Compute Instance.

Open L4 Load Balancers

Compute instances don't get a public IP on their own. An L4 Load Balancer allocates one and forwards traffic to a raw TCP/UDP port on whatever instances match its selector. Click L4 Load Balancers in the sidebar.

Create a load balancer

Click + Create L4 Load Balancer.

Target both instances by label

Fill in the fields:

  • Target — by default this field picks one instance by name. Click Advanced: match by any label, then set Label key to tier and Label value to web. That matches both web-01 and web-02 — and any instance you add later with the same label.
  • Ports — add one mapping: Port 80, Target Port 3000 (whatever port your app listens on inside each instance).

Create the load balancer

Click Create L4 Load Balancer.

Open the load balancer

Its row is highlighted below — click it to open the details panel.

Find the address

Find the assigned address under External IP. Traffic sent to <external-ip>:80 now gets spread across every instance matching tier: web — currently web-01 and web-02. Confirm it from your terminal:

curl http://<external-ip>

Run it a few times (or with -v and a couple of retries) and you'll see responses coming from either instance, not always the same one.

Scale out further

To add capacity, you don't touch the load balancer at all — create another Compute Instance with the same tier: web label (a web-03, using the same Instance Type, Image, and label as before) and it joins the pool automatically. Remove an instance and the selector stops sending it traffic just as automatically. The load balancer's selector, not a fixed list of instances, is what defines the pool.

Next steps​