Skip to main content
Cloudflare Workers is a serverless platform that allows you to run your code at the edge, close to your users. Hyperdrive accelerates queries you make to existing databases, including PlanetScale.
Already created a PlanetScale Vitess database? Jump straight to integration instructions.
We’ll cover:
  • Creating a new Vitess database
  • Cluster configuration options
  • Connecting to your database

Prerequisites

Before you begin, make sure you have a PlanetScale account. After you create an account, you’ll be prompted to create a new organization, which is essentially a container for your databases, settings, and members. After creating your organization, it’s important to understand the relationship between databases, branches, and clusters.
  • Database: Your overall project (e.g., “my-ecommerce-app”)
  • Branch: Isolated database deployments that provide you with separate environments for development and testing, as well as restoring from backups - learn more about branching
  • Cluster: The underlying compute and storage infrastructure that powers each branch
PlanetScale Vitess clusters use Vitess, a database clustering system for horizontal scaling of MySQL.

Create a new database

Step 1: Navigate to database creation

1
Log in to your PlanetScale dashboard
2
Select your organization from the dropdown
3
Click “New database” button or navigate to /new

Step 2: Choose database engine

1
On the database creation form, you’ll see two engine options:
  • Vitess (MySQL-compatible)
  • Postgres (PostgreSQL-compatible)
2
Select Vitess to create a MySQL-compatible database

Step 3: Configure your database cluster

1
Database name: Enter a unique name for your database
Your database name can contain lowercase, alphanumeric characters, or underscores. We allow dashes, but don’t recommend them, as they may need to be escaped in some instances.
2
Region: Choose the primary region where your database will be hosted. For the lowest latency, select a region near you or your application’s hosting location.
3
Cluster configuration: Select your preferred cluster and storage size for your database.

Step 4: Create the database cluster

1
Review your configuration settings
2
Click “Create database” to provision your Vitess database
3
Your database will be created with a main branch by default

What happens during creation

When you create a Vitess database cluster, PlanetScale automatically:
  • Provisions a Vitess cluster in your selected region
  • Creates the initial main branch
  • Sets up monitoring and metrics collection
  • Configures backup and high availability settings

Integrate with Cloudflare Workers

Don’t have a Workers project yet? Create a Workers project from the MySQL Hyperdrive template.
Terminal
npm create cloudflare@latest -- --template=cloudflare/templates/mysql-hyperdrive-template

Step 1: Create a Hyperdrive connection

You can automatically create a connection from the PlanetScale dashboard when creating a new role, or use one of the methods below.
1
Log into the Cloudflare dashboard and navigate to “Compute & AI” > “Workers & Pages”. You should see your Worker in the list.
PlanetScale Cloudflare integration wizard - step 1
2
Select the “Storage & databases” tab, then “Hyperdrive”, and finally “Create configuration” in the top right corner.
PlanetScale Cloudflare integration wizard - step 2
3
Click “Connect to PlanetScale database” then click Next.
PlanetScale Cloudflare integration wizard - step 3
4
Click Login to PlanetScale to log in to your PlanetScale account.
PlanetScale Cloudflare integration wizard - step 4
5
The next screen will allow you to grant access to your organization, database, and branch. Start by selecting your organization from the list.
PlanetScale Cloudflare integration wizard - step 5
6
Select the database you created, the “main” branch and then click Next.
PlanetScale Cloudflare integration wizard - step 6
7
You should see a success screen with the connection binding details to add to your wrangler.json file.
PlanetScale Cloudflare integration wizard - step 7
8
You should now have metrics within your Cloudflare dashboard in the Hyperdrive page.
PlanetScale Cloudflare integration wizard - step 8

Configure Worker placement

By default, Workers run at the edge close to your users. For database-heavy workloads with multiple round trips, you can use placement hints to run your Worker closer to your PlanetScale database and reduce latency. Add a placement configuration to your wrangler.jsonc file with the region that matches your PlanetScale database region:
wrangler.json
{
  "placement": {
    "region": "aws:us-east-1"
  }
}
PlanetScale RegionPlacement Hint
AWS ap-northeast-1 (Tokyo)aws:ap-northeast-1
AWS ap-south-1 (Mumbai)aws:ap-south-1
AWS ap-southeast-1 (Singapore)aws:ap-southeast-1
AWS ap-southeast-2 (Sydney)aws:ap-southeast-2
AWS ca-central-1 (Montreal)aws:ca-central-1
AWS eu-central-1 (Frankfurt)aws:eu-central-1
AWS eu-west-1 (Dublin)aws:eu-west-1
AWS eu-west-2 (London)aws:eu-west-2
AWS sa-east-1 (Sao Paulo)aws:sa-east-1
AWS us-east-1 (N. Virginia)aws:us-east-1
AWS us-east-2 (Ohio)aws:us-east-2
AWS us-west-2 (Oregon)aws:us-west-2
GCP asia-northeast3 (Seoul)gcp:asia-northeast3
GCP europe-west1 (Belgium)gcp:europe-west1
GCP northamerica-northeast1 (Montreal)gcp:northamerica-northeast1
GCP us-central1 (Iowa)gcp:us-central1
GCP us-east4 (Virginia)gcp:us-east4
Your Worker will run in the Cloudflare data center with the lowest latency to the specified region. If your Worker connects to multiple back-end services and you’re unsure which region to specify, use "mode": "smart" for automatic placement based on measured latency:
wrangler.json
{
  "placement": {
    "mode": "smart"
  }
}

Step 2: Deploy your Worker

Run the following to deploy your Worker.
Terminal
npx wrangler deploy
For more information on using Cloudflare Workers and Hyperdrive, refer to the Cloudflare documentation.

What’s next?

Once you’re done with development, it is highly recommended that safe migrations be turned on for your main production branch to protect from accidental schema changes and enable zero-downtime deployments. When you’re ready to make more schema changes, you’ll create a new branch off of your production branch. Branching your database creates an isolated copy of your production schema so that you can easily test schema changes in development. Once you’re happy with the changes, you’ll open a deploy request. This will generate a diff showing the changes that will be deployed, making it easy for your team to review. Learn more about how PlanetScale allows you to make non-blocking schema changes to your database tables without locking or causing downtime for production databases.

Need help?

Get help from the PlanetScale Support team, or join our GitHub discussion board to see how others are using PlanetScale.