Skip to main content

Serverless API

caution

This feature is being actively developed as a technology preview under alpha and is subject to change.

Overview

For serverless environments such as Cloudflare Workers or Vercel Edge Functions, TCP support is not readily available, being based on V8 isolates. These environments strive for lightweight, ephemeral compute where long lived database connectivity and connection pooling is somewhat of an anti-pattern. Serverless environments promise low latency by serving closer to users, but database connectivity problems stand in the way of this.

To enable such environments, PolyScale supports a lightweight API that processes SQL queries via HTTP. PolyScale initiates and maintains TCP connectivity to the origin database.

Architecture

HTTP requests are accepted by PolyScale and the SQL extracted. A native TCP connection is then initiated to the database (if not already established) and pooled for later reuse.

This provides the benefit that PolyScale is closer to the serverless environment (edge function) and cache hits are served at very low latency at scale.

API

The design goal for the first iteration of the API is simplicity. It should be very simple for a developer to connect via PolyScale to their database using the HTTP API.

We plan to add additional features and compatibility with other platforms over time.

Client Library

See https://www.npmjs.com/package/@polyscale/serverless-js

import Client from "@polyscale/serverless";

const polyscale = new Client("https://serverless.aws.psedge.global", {
cacheId: "polyscale-cache-id",
username: "target-db-username",
password: "target-db-password",
database: "target-db-database",
provider: "mysql",
});

polyscale.query("SELECT 1;").then((result) => console.log(result));

Connection Pooling

The Serverless API supports connection Pooling for all databases: Postgres, MySQL, MariaDB and MS SQL Server.

As part of the alpha release, the pool size is limited to 2 connections per PolyScale Point of Presence (PoP). This value will be configurable in the future.

The timeout until stale connections are closed is 60 seconds, and like the pool size, will be configurable in the future.

Feedback

Join the Discord here to keep up to date with the latest on the HTTP API and provide thoughts and feedback.