Skip to content

Node.js query caching with PlanetScale Boost

Learn how to use PlanetScale Boost in a Node.js application.

Note

PlanetScale Boost is in limited beta. Reserve your spot on the waitlist today.

This guide will walk you through two methods to connect to your database using the PlanetScale Boost query caching feature when building an application with Node.js. It will start with a simple code snippet that will be used to always execute a query through the cache server, then expand into using separate connections.

Enable PlanetScale Boost on a MySQL connection

Below is sample code that will establish a connection to a PlanetScale database and perform a query.

Javascript
Copied
require('dotenv').config()
const mysql = require('mysql2/promise')

;(async () => {
  const connection = await mysql.createConnection(process.env.DATABASE_URL)
  let [rows] = await connection.query('select * from users')
})()

In order to enable a connection to use PlanetScale Boost, the @@boost_cached_queries session variable needs to be set with the following code:

Javascript
Copied
await connection.execute('SET @@boost_cached_queries = true')

Any code written after that session variable has been set will proxy requests through the Boost server. A fully updated code snippet would look like this:

Javascript
Copied
require('dotenv').config()
const mysql = require('mysql2/promise')

;(async () => {
  const connection = await mysql.createConnection(process.env.DATABASE_URL)
  connection.execute('SET @@boost_cached_queries = true')
  let [rows] = await connection.query('SELECT * FROM users')
})()

With this configuration, all queries will be run through the boosted connection, regardless of if they're actually using PlanetScale Boost. This may make it difficult to tell which queries are really being boosted. For this reason, we recommend using two separate connections: one for regular queries and one for boosted queries.

Create a separate connection

Our recommended strategy for working with PlanetScale Boost is to use two separate connections so you can be explicit when executing your queries. To do this, you'll need two separate connection objects using the mysql2 package and set the session variable on only one of them:

Javascript
Copied
require('dotenv').config()
const mysql = require('mysql2/promise')

;(async () => {
  // Create the standard connection
  const connection = await mysql.createConnection(process.env.DATABASE_URL)
  connection.execute('SET @@boost_cached_queries = true')
  let [rows] = await connection.query('SELECT * FROM users')

  // Create a Boost-enabled connection
  const boostedConnection = await mysql.createConnection(process.env.DATABASE_URL)
  await boostedConnection.execute('SET @@boost_cached_queries = true')
  let [rowsFromCache] = await connection.query('SELECT * FROM users')
})()

Need help?

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

Was this page useful?
Last updated on November 15, 2022
Help us improve this page
© 2022 PlanetScale Inc.