Nov 09, 2023
Posted by
James Blackwood-Sewell
We ❤️ Cloudflare at Timescale, and while we don’t think serverless makes sense for databases, it’s absolutely amazing for stateless edge functions—and nothing does those better than Cloudflare Workers. Recently, Cloudflare launched its Hyperdrive product, which promises to turn your existing regional databases into globally distributed ones. We couldn’t pass up the opportunity to partner with them so we could make Hypertables on Hyperdrive a thing.
Timescale’s hypertables work exactly like PostgreSQL tables, but they automatically partition your data for a much-welcome performance boost.
Hyperdrive is essentially an edge proxy for PostgreSQL databases. It does smart caching, connection management, and persistence at the edge, which cuts down on the amount of traffic you’re sending from your remote Worker to your database.
Timescale has been supported from day one (and is the only supported database for time series and analytics), and we are already busy talking about internal use cases for low-cost edge connections sending data back to a central Timescale database.
To complement the brief example already in the Hyperdrive documentation (which assumes you already have a Cloudflare project and some basic knowledge of how things work), we have written a full tutorial describing the deployment of an edge app that uses Timescale for persistence, allowing readings to be ingested and queried from Workers via a Serverless REST API, which is defined by TypeScript code. You might look to this kind of architecture if you have remote locations that are recording readings (maybe an IoT use-case) and you want to be able to persist them to your database but also query them when needed.
You might think that this sounds like a bit of work, but it’s not. The Cloudflare Workers developer experience is second to none when you’re using JavaScript (or, in our case, TypeScript). It’s three commands (and two file edits) from an empty directory to deploy, with npm used to scaffold and the wrangler tool npm installs used to create a Hyperdrive instance and then deploy your code to the edge. You can be none to done in three minutes flat.
And why use Timescale for this tutorial rather than another PostgreSQL variant? Because Timescale is faster for time-series data and large PostgreSQL datasets. You can see some benchmarks versus Amazon RDS here if you’re interested.
By leveraging Cloudflare's Hyperdrive and Timescale's performant time-series database, you can easily cut down the traffic from your remote Worker to your database and then ingest and query that data faster with Timescale.
The tutorial provided shows how simple it is to deploy such an architecture, with Timescale providing speed and scalability advantages over vanilla PostgreSQL. Start speeding up your workflow today: you can try Timescale out for 30 days, free of charge.