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.
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.
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.
- From the dashboard, create two droplets in two or more regions.
- All the droplets should have the same specifications in terms of CPU, RAM, and storage for uniform performance.
- SSH into each droplet and install the necessary software for your app stack (Node.js and Next.js).
- 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.
For our e-commerce website to remain available during outages, we need to ensure our database is set up with high availability.
- Set up a managed MySQL database service with high availability enabled. This service will automatically handle failover and replication.
- Enable the High Availability (HA) and Standby configuration during the database creation process.
- 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 firewall rules to allow access to the MySQL database only from your droplets.
- Test the database connectivity from both droplets to ensure the app can read/write to the MySQL database successfully.
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.
- Log in to the DigitalOcean Dashboard.
- 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.
- Frontend configuration: Set up the frontend to listen on port 80 or 443 (for HTTP/HTTPS).
- 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.
- 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.
- 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.
VPC peering allows secure communication between your frontend droplets and the managed database by using private IPs instead of public IPs.
- 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).
- 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.
- Update the Firewalls of the droplets to allow outbound traffic to the MySQL instance.
- Similarly, configure the database’s trusted sources section to accept incoming connections from the droplets.
- 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.
Now that the infrastructure is in place, we can deploy the e-commerce app on the droplets.
- SSH into each droplet and clone your e-commerce app repository, or upload your app code to the droplets.
- Ensure that Node.js, npm (or yarn), and other dependencies required by your Next.js and Node.js app are installed.
- Configure environment variables for both the frontend and backend. These might include API keys, database credentials, etc.
- Use PM2 or any other process manager to start both the Next.js app (frontend) and the Node.js app (backend).
- Ensure the application is accessible via the load balancer’s DNS name.
- 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.
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