Tutorial

Deploying a Highly Available E-commerce Website on DigitalOcean using GLB and VPC Peering

Published on November 19, 2024

Sr Solutions Architect

Deploying a Highly Available E-commerce Website on DigitalOcean using GLB and VPC Peering

Introduction

In this article, we will walk through the process of deploying a highly available e-commerce website on DigitalOcean using a Global Load Balancer (GLB) and VPC peering to ensure seamless connectivity between distributed resources. The website, built with Next.js for the front end and Node.js for the backend, will have a scalable infrastructure with load balancing and high availability.

We’ll use two droplets in Frankfurt and San Francisco behind the GLB, with a MySQL-managed database configured for high availability in Singapore. The setup also involves VPC peering to allow secure communication between the front-end servers and the database.

Here’s a video demonstrating how to deploy a Global Load Balancer (GLB) and implement VPC peering for a website with services deployed across multiple regions.

Prerequisites

Before you begin, ensure that you have the following:

  • A DigitalOcean Account.
  • Basic knowledge of cloud networking, VPC, and load-balancing concepts.
  • Multiple Droplets (virtual machines) created in separate regions of your preference for High Availability.
  • A Managed MySQL Database set up for High Availability (using a Standby node) in the region of your preference.
  • Basic familiarity with deploying a Next.js and Node.js application.

Architecture Diagram

Architecture_Diagram

Step 1 — Deploy Droplets

To create redundancy and minimize latency for users in different regions, we have deployed two droplets, one in Frankfurt & another in the San Francisco region.

Create Droplets

  1. From the dashboard, create two droplets in two or more regions.
  2. All the droplets should have the same specifications in terms of CPU, RAM, and storage for uniform performance.

Install Required Software

  1. SSH into each droplet and install the necessary software for your app stack (Node.js and Next.js).
  2. Ensure the droplets have your e-commerce app deployed. This can involve pulling the code from a Git repository, setting up the environment variables, and starting the app using a process manager like PM2.

Step 2 — Configure MySQL Managed Database with High Availability (HA)

For our e-commerce website to remain available during outages, we need to ensure our database is set up with high availability.

Create MySQL Managed DB

  1. Set up a managed MySQL database service with high availability enabled. This service will automatically handle failover and replication.

Enable High Availability

  1. Enable the High Availability (HA) and Standby configuration during the database creation process.
  2. Additionally, you can configure a RO (read-only replica) MySQL node in a different availability zone to promote it as primary in case an entire region is impacted.

Configure Security

  1. Configure firewall rules to allow access to the MySQL database only from your droplets.

Test Connectivity

  1. Test the database connectivity from both droplets to ensure the app can read/write to the MySQL database successfully.

Step 3 — Set Up the Global Load Balancer (GLB)

A Global Load Balancer (GLB) is crucial for distributing traffic evenly across multiple regions, ensuring that users from different parts of the world get the best performance.

  1. Log in to the DigitalOcean Dashboard.
  2. Create a Global Load Balancer: Navigate to the load balancer section and create a new GLB. Configure it to distribute traffic between your two droplets in Frankfurt and San Francisco.
  3. Frontend configuration: Set up the frontend to listen on port 80 or 443 (for HTTP/HTTPS).
  4. Health checks: Enable health checks to ensure that the GLB only routes traffic to healthy droplets. Since our application runs on port 3000, configure the health check to listen on the same port.
  5. Configure load balancing rules: Define rules to balance the incoming traffic across the two droplets based on factors such as least connection or round-robin method.
  6. DNS Configuration: Set up DNS records to point your domain to the GLB’s IP address.

By the end of this step, your GLB should be active and distributing traffic between your Frankfurt and San Francisco droplets.

Step 4 — Set Up VPC Peering Between Droplets and Managed DB

VPC peering allows secure communication between your frontend droplets and the managed database by using private IPs instead of public IPs.

Create VPC Peering

  1. From your cloud provider’s dashboard, navigate to the VPC section and create a VPC peering connection between the VPCs of the droplets (Frankfurt and San Francisco in our case) and the managed MySQL database (Singapore).

Update Routing Tables

  1. Once the VPC peering connection is established, update the routing tables for both the droplets and the database VPCs to allow traffic to route through the peering connection.

Firewalls

  1. Update the Firewalls of the droplets to allow outbound traffic to the MySQL instance.
  2. Similarly, configure the database’s trusted sources section to accept incoming connections from the droplets.

Test the Connection

  1. From both droplets, test the connection to the database to ensure that the VPC peering is set up correctly and that data flows securely between the frontend and backend. You can use telnet to privately connect to the DB.

Step 5 — Deploy the E-commerce App Stack (Next.js + Node.js)

Now that the infrastructure is in place, we can deploy the e-commerce app on the droplets.

Clone the Application

  1. SSH into each droplet and clone your e-commerce app repository, or upload your app code to the droplets.

Install Dependencies

  1. Ensure that Node.js, npm (or yarn), and other dependencies required by your Next.js and Node.js app are installed.

Set Up Environment Variables

  1. Configure environment variables for both the frontend and backend. These might include API keys, database credentials, etc.

Start the Application

  1. Use PM2 or any other process manager to start both the Next.js app (frontend) and the Node.js app (backend).
  2. Ensure the application is accessible via the load balancer’s DNS name.

Verify Application Functionality

  1. Test the application in each region (Frankfurt and San Francisco) to ensure that users are being routed correctly via the Global Load Balancer, and verify that both frontend and backend components are working as expected.

Conclusion

In this article, we successfully deployed an e-commerce website using a Global Load Balancer and VPC peering. By leveraging cloud services like a managed MySQL database with high availability and setting up multiple droplets in different regions, we ensured high availability and low latency for our global user base. With VPC peering between the frontend droplets and the database, we secured communication while maintaining a scalable infrastructure. With these steps, you can build a resilient and performant architecture for any globally distributed e-commerce application.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about our products

About the authors
Default avatar

Sr Solutions Architect

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
Leave a comment


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Jump in! Follow our easy steps to deploy Hugging Face HUGS on DigitalOcean GPU Droplets and bring your AI models to life.

Join the Tech Talk
Success! Thank you! Please check your email for further details.

Please complete your information!

Become a contributor for community

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

DigitalOcean Documentation

Full documentation for every DigitalOcean product.

Resources for startups and SMBs

The Wave has everything you need to know about building a business, from raising funding to marketing your product.

Get our newsletter

Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.

New accounts only. By submitting your email you agree to our Privacy Policy

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.