Edge Database Benchmarks
Published: 2024-06-08 15:28:14

Many database services are out there to solve your problems. However, you might be overwhelmed with all the options available(Was it only me?🥲). I found a database benchmark tool that Vercel built. This may help you make your database decisions. In this post, I'll go over the basic terminologies, my benchmark results, and some insights. Hopefully, it'll be able to give you a general idea about performance.

## Terminologies
Let's sort out all the terms we need to understand before moving on to the main point.

### CDN(Content Delivery Network)
CDN caches static contents such as HTML, CSS, and media files distributed to servers around users so that users can receive the contents faster.

### Edge Computing
Instead of running a distant server, you place multiple servers near the users to run computations. It reduces long-distance client-server communications.

### Edge Database
We just said edge computing came out to reduce client-server communications. It will lose its main purpose if the database is far from the edge servers since most edge computations require database access. You can put data and computing around users altogether. That's where people came up with the idea of an edge database.

## Distributed SQLite
- SQLite is fast so the server can read and write SQLite quickly.
- Vendors like Cloudflare D1, Turso, and fly.io offer distributed SQLite.
- Since they can be distributed, they align with edge databases and can be placed next to edge functions for fast access times.
- Turso stores the replicas of all databases in multiple regions by default.

## What's the point of Edge Database?


![Turso's Selling point](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kkheocvr32go0wzr195w.png)
- Turso's Selling point

You can consider edge databases when you want your users to get consistent latency wherever they are. This is more beneficial when your application needs to serve multiple regions(or countries). I'm planning to build travel apps and that's how I got more interested in Edge Database. The following is the real-life scenario I could think of.

![An American guy in Paris](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/08tlulov600yivqdmfjz.jpg)

Let's say you're American and traveling to Europe at the moment. When you open a mobile app, it loads slowly since your data should be in America. To solve this, the application can store your data around your current location(Europe) now and data access time will be reduced. 

Note that using SQLite as a distributed database is not a matured technology and [many people are still discussing(arguing).](https://news.ycombinator.com/item?id=39975596).


## Benchmarks
I'll use a database benchmark tool [a database benchmark tool](https://github.com/vercel-labs/function-database-latency) to see how well each database provider performs. Note that I'm testing this in South Korea, so I'm far away from the original database enough to test the performance(US East). I set the test options in the following manner.

![Test options](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wzk708ajc4d1kb0q4olq.png)

- Location option: Global function (Edge), Regional function(Edge / US East), and Serverless function (Node / US East)
- Waterfall: 5 sequential queries
- The number of samples for each location: 50

⚠️ Testing 50 requests might not be enough, but at least it gives you a general idea of the performance. Plus I set the Waterfall option to '5 sequential queries' to ensure real-life querying scenarios because each query usually depends on the result of the preceding queries(data dependency).

### Supabase with Drizzle ORM

- Latency distribution(processing time)
![Processing time of Supabase with Drizzle ORM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k377a5y59ypqaiibdqvv.png)

- Latency distribution(end-to-end)
![End-to-end latency of Supabase with Drizzle ORM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/92n9n36350li02nl0uqd.png)

- Supabase only runs on node, not on edge.
- Its processing time is ~30ms, and end-to-end access time is ~250ms. The end-to-end access time increases since the database lives in US East region.


### Neon with Drizzle ORM

- Latency distribution(processing time)

![Processing time of Neon with Drizzle ORM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/toquz5ooe9fr7ih38i7u.png)


- Latency distribution(end-to-end)

![End-to-end latency of Neon with Drizzle ORM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jfgtyey64vsjxfcpqdfu.png)

- Global edge takes an extremely longer time than regional edge/node.


### PlanetScale with Drizzle ORM

- Latency distribution(processing time)
![Processing time of PlanetScale with Drizzle ORM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m66h5ol65jg0nvbhtehq.png)

- Latency distribution(end-to-end)
![End-to-end latency of PlanetScale with Drizzle ORM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gtpqkwtdi02zqzwzwanu.png)

- Global edge takes an extremely longer time than regional edge/node.

### Upstash with Drizzle ORM

- Latency distribution(processing time)
![Processing time of Upstash with Drizzle ORM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w18cvpifi1wlg5sro9bi.png)

- Latency distribution(end-to-end)
![End-to-end latency of Upstash with Drizzle ORM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lgnjqlkly33mq3vd0ygf.png)

- Global edge still takes longer time than regional edge/node


### Turso with Drizzle ORM

- Latency distribution(processing time)
![Processing time of Turso with Drizzle ORM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wdkfav54s6mt2db0hiyc.png)

- Latency distribution(end-to-end)
![End-to-end latency of Turso with Drizzle ORM](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dyok11ygl54braxzmves.png)

- Global edge still takes about the same time as edge/node.

## Test Results in Korea

- Actually, there was no point in using edge databases in Korea.
- Apparently, it's better to access data on the origin server.

Since this result is significantly different from [Turso's test result](https://turso.tech/blog/vercel-benchmarks-show-turso-has-low-latencies-everywhere-what-the-data-edge-is-good-for-7407579d4c88), I asked my friend in France to run Upstage and Turso tests there. Here's what she sent me.

### Turso with Drizzle ORM in France

- Latency distribution(processing time)
![Processing time of Turso with Drizzle ORM in Lille, France](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/iu6k1wb9fqs6yapls175.png)

- Latency distribution(end-to-end)
![End-to-end latency of Turso with Drizzle ORM in Lille, France](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bej6a0hsfufrub34u64y.png)

- The real-life latency of the global edge is about 2 times lower than the others.

## Upstash with Drizzle ORM in France

- Latency distribution(processing time)
![Processing time of Upstash with Drizzle ORM in Lille, France](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8skqfx7van08a1h2kdhc.png)


- Latency distribution(end-to-end)
![End-to-end time of Upstash with Drizzle ORM in Lille, France](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cvhqqeqre8n6ib2yicba.png)

- The real-life latency of the global edge is about the same as the others.

## Conclusion
- Turso's edge database approach didn't work in South Korea. The rest of them either.
- Turso worked significantly better in France and Brazil.
- I assume this difference came from the fact that the closest edge location to Korea is Japan, while the other two countries have their own edge locations.
- Database Services other than Turso didn't work with Global Edge. While this means default Turso replicas outperform the others, [PlanetScale can improve performance if you set up replicas in other 15 regions(enterprise plan).](https://planetscale.com/docs/concepts/replicas). [Supabase supports 12 cross-regional read replicas configuration](https://supabase.com/docs/guides/database/replication).
Unfortunately, Neon only supports read replicas in the same region.

## References
- [Vercel's database benchmark tool](https://github.com/vercel-labs/function-database-latency)
- [Turso's benchmark result comparison between US and Brazil](https://turso.tech/blog/vercel-benchmarks-show-turso-has-low-latencies-everywhere-what-the-data-edge-is-good-for-7407579d4c88)
- [Why a company migrated from PlanetScale to Turso](https://www.openstatus.dev/blog/migration-planetscale-to-turso)