Cloud Native / Data / Contributed

How to Migrate to PlanetScale’s Serverless Database

14 Oct 2021 1:00pm, by
Lee Robinson
Lee Robinson is a developer, writer, and creator who works closely with Vercel customers and helps grow the Next.js community through education and training. Before joining Vercel, he scaled web development at a $10B retailer. In his free time, he enjoys playing music, photography/videography and has his own YouTube channel with over 16,000 subscribers.

As a developer, I’m always trying to learn new technologies through experimentation and building. I’ve recently been interested in serverless databases, which promise faster deployment, higher scalability, and improved developer experience. After testing multiple offerings, I decided to move my personal website from using Firebase and Redis to PlanetScale, a new serverless database platform built on MySQL and Vitess, an open-source platform built to power YouTube.

Here’s why I chose PlanetScale:

  • Database Branching: I’m able to use my database with the same mental model as Git. For each change to my database schema, I open a deploy request. Then, I can merge those changes back into my main database branch.
  • Prisma Support: In combination with Prisma, it’s so easy to handle database migrations.
  • Connectionless: Since PlanetScale is serverless, it’s built to withstand thousands of simultaneous connections. You can almost consider this connectionless as I don’t need to worry about pooling or other common objections.
  • Deploy in 10 Seconds: After testing 13 databases, PlanetScale was the fastest to deploy. After using it for a few weeks and monitoring performance, I’m seeing APIs resolve in ~150ms on average (see results below).

Firebase and Redis

My site previously used Google Firebase and Redis for real-time blog post views and my guestbook. The primary decision for choosing these technologies was to learn. Both Firebase and Redis (through Upstash) are easy to get started, scale without thinking, and work great in serverless environments. However, I wanted to move to something SQL-based (either MySQL or PostgreSQL) for a new learning experience.

Rebuilding SQL

I found myself writing JavaScript in places where SQL has the functionality built-in. For example, I used Firebase to fetch JSON object views and then summed the values together to find the total number of views.

Same thing with sorting. Before I was using JavaScript sort and now I can use ORDER BY. Although Firebase does have something similar, I did not use it.

SQL is boring, proven technology. It’s been around for years and will continue to be around for many more. I’ve used it before in prior jobs, but still felt I could understand it better. I also enjoy using PostgreSQL (and recommend Supabase) and strongly considered that solution as well.

I’m also a strong believer in using tools I recommend. Without actually getting my hands dirty in code and running an application in production, it’s hard to confidently recommend a product to others. After using the PlanetScale Vercel Integration, I was blown away. I could deploy an entire full-stack application in just a few clicks. And as I mentioned at the start, database migrations aligned with my mental model.

Migrating Existing Data

There may be better ways to do this, but my hacky solution to migrate data into PlanetScale was as follows:

  • Export JSON data from my Firebase Realtime Database
  • Export JSON data from my Redis cluster using HVALS and TablePlus
  • Create a new database branch in PlanetScale for the schema migration adding the tables
  • Create two new API routes, which loaded the JSON data and INSERT INTO MySQL
  • Verify on the database branch the APIs correctly process and migrate the data
  • Create a deploy request with the new schema changes and merge it into main
  • Finally, hit the APIs to migrate the JSON data into main
  • Done!

Here are the two scripts I used, for reference.

Here’s my PlanetScale schema for tracking post views and guestbook entries.

Results

I’ve been monitoring the performance of my production APIs connecting to PlanetScale with Checkly. Checkly allows me to set alerts if there’s downtime or if performance decreases below my accepted threshold. To date, I’ve seen latency of ~150ms for my Next.js API Routes, deployed as serverless functions on Vercel in us-east.

PlanetScale performance summary

As you can tell in the graph below, this has yielded significantly faster response times than my previous Firebase implementation (note when I made the switch). Furthermore, having one service instead of two cleaned up the code and required fewer environment variables to connect to each service. 

PlanetScale response times per location.

The New Stack is a wholly owned subsidiary of Insight Partners, an investor in the following companies mentioned in this article: PlanetScale.

Feature image via Pixabay.

A newsletter digest of the week’s most important stories & analyses.