TL;DR: I built an app that picks a random Irish tune each day and uses AI to generate an image for it. The app uses a bunch of new tech from Cloudflare, and in this post I dig into what "the edge" means.
These days I'm getting more and more fascinated by "the edge", a buzzword you may have been hearing lately in the web development world.
If you're not familiar, here's the gist: Instead of running a central server and routing all requests to/from that server, now you can run your computations in whichever data center is closest to the user.
As a result there is much less latency. Previously if the user was in Tokyo and the server was in Stockholm, each request had to travel 277ms each way. Even if the rest of your app was instant, the user would have to wait a minimum of 544ms before seeing anything.
Now, if your user's request is handled on the edge, perhaps even by a data center in their city, they might get a response in 10ms.
This might seem like an insignificant difference but you will definitely notice it while you are browsing the web.
The problem is that up until now, servers have been moving to the edge, but databases have not. That means any data requests might still have to go to a central database in, say, Virginia. Even if you make your app server fast, your database is holding everything up.
Things are starting to happen in this area; Planetscale is doing global MySQL read replicas but they look somewhat cumbersome to configure and you still have to manually route your requests.
Then Cloudflare D1 came along. Distributed SQLite running on the edge globally. It's still in alpha but it is absolutely amazing. Super easy to set up, the
wrangler CLI is excellent, and instead of needing to configure complicated IAM rules like Amazon's RDS, you access it via a simple
env.DB binding in your app.
A bonus is that for development you can run D1 locally with the
wrangler dev --local flag, since SQLite is simply a file on your file system.
Add R2 - global object storage - to the mix and now you have everything your app needs running on the edge.
That means no more laggy round trips to slow down your app. Everything runs close to the user.
Anyway, this is a long-winded way of explaining that I wanted to try all this stuff out. So I came up with an idea to take a random Irish tune each day and ask DALL-E to generate an image for it via the OpenAI API.
I'm storing metadata in D1 like the tune name, the date, and its ID on The Session. I'm storing the image data itself in R2.
The code is all open sourced here.
Here's the first image it came up with, for a tune called "The Eavesdropper". A little disturbing if I'm honest!
Oh, and an AI called ChatGPT came up with the "AI-rish" pun. Strange times we are living in!