Knowledge Hub
PlanetScale
Written by Aren Hovsepyan
Last update: 6/7/2024
Feature | PlanetScale | |
---|---|---|
Infrastructure, Underlying technology Cloud Infrastructure, database system and underlying egine | MySQL, Vitess, GCP, AWS | |
Serverless Support Serverless and Edge computing support | ||
API | uses HTTP connection, JavaScript Driver | |
Edge Functions Serverless functions that run closer to the user, reducing latency and improving performance. | Cloudflare Workers, Vercel Edge Functions, Netlify Edge Functions | |
Developer Experience | ||
Database type | OLTP | |
Data model Databases store data in models, such as Relational, Columnar or Document based | Relational, SQL | |
CLI Command Line Interface tools that allow developers to perform tasks and manage the tool via the command line. | pscale | |
Languages, APIs The languages supporting the database and API interface for inter-process communication | Any package/driver supporting MySQL, Prisma, Node.js, Go, Django, C# | |
Query language Supported query language such as SQL, MQL, PartiQL, | SQL, MySQL variation | |
ACID compliant ACID is a set of properties of database transactions intended to guarantee data validity despite errors, power failures, and other mishaps | ACID compliant | |
Self-hosting Support for self-hosted Database instance | Only Enterprise Plan members, AWS, GCP | |
Backups | Daily basis (Included in Free tier), Scheduled backups ($0.023 per GB per month) | |
Security & Compliance Offerings | ||
SOC2 Service Organization Control 2 compliance for managing customer data. | ||
Multi-factor authentication Support of the cloud-histed database for multi-factor authentication | ||
Encryption Cryptographic security at rest and on transportation level | AES Encription, TLS v1.3 | |
SSO | ||
Monitoring and analytics | ||
Log retention Query log retention usually keeps lofs of operations for debugging and replication purposes | Based on plan, Unlimited for Enterprise | |
Statistics Tools and APIs for analysing and monitoring of Database performance | Web based dashboard, read/write ratio | |
Scalability | ||
Replication and Partitioning | Database can be deployed on AWS, GCP supported regions (GDPR compliant) |
Companies like Uber and Spotify use PlanetScale because it provides the scalability and fault tolerance needed for their demanding workloads without sacrificing the simplicity and ease of use of MySQL.
PlanetScale was founded in 2018 by the co-creators and maintainers of the Vitess open-source project.
PlanetScale is a cloud-hosted and serverless database platform created on top of Vitess - a MySQL-compatible and cloud-native database. It combines many essential features of relational MySQL and the scalability of NoSQL databases. It has a built-in sharding feature that enables growing applications without explicit sharding logic.
PlanetScale allows you to set up a MySQL server in a matter of seconds. Some of the most prominent features are the automatic failover and backup capabilities. It lets a developer be ignorant about the underlying database topology. It means your application growth is not limited by the hardware resources and can automatically scale for larger throughput and handle automatic failover in the case of emergency.
Alongside excellent scalability and high availability features, it provides a neat developer experience that allows for secure separation of staging data from production and easy data replication from a certain point in time. PlanetScale’s branching feature enables you to treat the database as code and create branches of the database from a certain point in time, like in Git.
It provides two branch types - development and production.
Both branch types create an isolated copy of your database schema. The development branch is intended for development and experimental purposes, as the name suggests. It becomes handy when you want to branch off your production environment to quickly test a new feature or create a hotfix for a bug in the database.
On the other hand, production branches are highly available databases tuned for production traffic. Most importantly, they are protected from direct schema mutations and include daily backups.
Note that by default, PlanetScale includes only the schema in your branches without your data copy. However, it also offers the Data Branching feature, which automatically creates a copy of both data and schema in a separate development branch.
With the popularity of Edge networks, there is a need to adjust database interfaces to be compatible with modern Edge runtimes. It gives a huge opportunity to increase the performance and decrease the latency of your application. Nowadays, Edge computing is a complementary part of many large-scale applications. With the help of Cloudflare Workers or Vercel Edge Functions, you can easily leverage a microservice architecture that improves the developer experience and application performance. However, a traditional centralized database server, where data queries and operations are handled by one or multiple machines, does not fit the new web model. Most database servers provide a TCP interface for communication between the application and the database. This acts a huge limitation when it comes to Edge networks, where for sake of security and performance we have an isolated environment that can invoke only HTTP requests.
Thus, PlanetScale introduces an Edge-compatible driver that can communicate with PlanetScale's globally distributed infrastructure via Fetch API.
Get your hands dirty
In this example, we will create a database that stores a dictionary of Navajo code talkers. Let’s bootstrap a simple Cloudflare Workers application where you can communicate with a database to add to and manipulate existing data.
First, you need to run `wrangler init` followed by your project name:
`wrangler init <PROJECT_NAME>`
You will be asked a couple of questions related to the newly created project.
3. Choose to use TypeScript in this example.
The newly created application is going to have the following file structure
Next, you can run your workers application via the following command:
`wrangler dev`
In order to connect to your database cluster, you need to install PlanetScale Serverless driver, which is created especially for Edge environments.
`npm install @planetscale/database`
Next, add the following code in `index.ts`, where you initialize the connection to the database cluster and call the content of the `codes` table.
import { connect } from '@planetscale/database'
export interface Env {
DATABASE_HOST: string
DATABASE_USERNAME: string
DATABASE_PASSWORD: string
}
export default {
async fetch(
request: Request,
env: Env,
ctx: ExecutionContext
): Promise<Response> {
const conn = await connect({
host: env.DATABASE_HOST,
username: env.DATABASE_USERNAME,
password: env.DATABASE_PASSWORD
})
const data = await conn.execute('SELECT * FROM codes')
return new Response(JSON.stringify(data.rows, null, 2))
}
}
You can find credentials in the PlanetScale dashboard under the “Connect” button.
However, when you first run the workers endpoint, you will face an error because you have not set up the`codes` table yet.
Set up the table and insert the data
To store data in MySQL, you need to connect to your PlanetScale cluster via a CLI tool called `pscale`. You can find the installation manual in the following link.
CREATE TABLE codes (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
category VARCHAR(30) NOT NULL,
word VARCHAR(30) NOT NULL,
navajo_word VARCHAR(50) NOT NULL,
literal_translation VARCHAR(30) NOT NULL
);
INSERT INTO codes (
category, word, navajo_word, literal_translation
)
VALUES
(
'alphabet', 'A', 'WOL-LA-CHEE', 'ANT'
),
(
'alphabet', 'A', 'BE-LA-SANA', 'APPLE'
),
(
'alphabet', 'A', 'TSE-NILL', 'AXE'
),
(
'alphabet', 'B', 'NA-HASH-CHID', 'BADGER'
),
('alphabet', 'B', 'SHUSH', 'BEAR'),
(
'alphabet', 'B', 'TOISH-JEH', 'BARREL'
),
('alphabet', 'C', 'MOASI', 'CAT'),
(
'alphabet', 'C', 'TLA-GIN', 'COAL'
),
(
'alphabet', 'C', 'BA-GOSHI', 'COW'
),
('alphabet', 'D', 'BE', 'DEER'),
(
'alphabet', 'D', 'CHINDI', 'DEVIL'
),
(
'alphabet', 'D', 'LHA-CHA-EH', 'DOG'
),
('alphabet', 'E', 'AH-JAH', 'EAR'),
('alphabet', 'E', 'DZEH', 'ELK'),
('alphabet', 'E', 'AH-NAH', 'EYE'),
('alphabet', 'F', 'CHUO', 'FIR'),
(
'alphabet', 'F', 'TSA-E-DONIN-EE',
'FLY'
),
('alphabet', 'F', 'MA-E', 'FOX'),
('alphabet', 'G', 'AH-TAD', 'GIRL'),
(
'alphabet', 'G', 'KLIZZIE', 'GOAT'
),
('alphabet', 'G', 'JEHA', 'GUM'),
(
'alphabet', 'H', 'TSE-GAH', 'HAIR'
),
('alphabet', 'H', 'CHA', 'HAT'),
('alphabet', 'H', 'LIN', 'HORSE'),
('alphabet', 'I', 'TKIN', 'ICE'),
(
'alphabet', 'I', 'YEH-HES', 'ITCH'
);
After you’ve set up the table and populated it with data, you can call your worker endpoint and display the results.
The main idea behind PlanetScale is to provide developers with an easy way to scale applications without worrying about database scaling.
PlanetScale was created to solve the problems caused by existing solutions:
High costs – most companies pay more than $100 per month for database hosting, and the prices increase every year.
Inconvenient scaling – when your data grows, you need to add new servers manually or use third-party services (e.g., AWS RDS). Either way, it requires time and effort from developers and DevOps teams to manage this process properly.
Inconvenient management – most solutions require manual tuning of settings (e.g., cache parameters), which causes instability.