- This example uses an older version of Fastly CLI. Check this article for an up-to-date version.
In this post, we will write a simple application which will run on Fastly Compute@Edge. The application will access Upstash Redis to keep track of page views.
Motivation
Edge Computing is one of the most exciting trends in recent years. CDN services like Cloudflare and Fastly started to enable users to run their applications on their edge infrastructure. This helps developers to build globally distributed, high performance applications.
Compute@Edge is the serverless platform from Fastly. You can write your code in Rust, AssemblyScript, Javascript and run in Fastly’s edge network. Fastly Compute functions are stateless. This means developers should keep their application state at an external storage. But Fastly uses WebAssembly as the runtime for the functions and it does not allow TCP connections. Upstash Redis® is a perfect solution for Fastly Compute functions with REST API, global replication and serverless pricing.
Project Setup
Create a database in the Upstash Console. Prefer Global Database for low latency from edge locations.
Install fastly cli:
brew install fastly/tap/fastly
Configure your CLI with your Fastly account:
fastly configure
Create a folder and init your project by running fastly compute init
inside the folder:
Install upstash-redis and flight-path:
npm install @upstash/redis flight-path
Update your webpack.config.js adding these plugins:
Update your fastly.toml file:
You need to create a Fastly Compute Service and paste your Fastly Service Id above.
Also you need to add the Upstash REST URL as a backend to your Fastly Compute Service. Fastly Compute requires any external network connection to be registered as a backend. On services screen, click Edit Configuration
> Clone version x to edit
> Origins
> Create a host
After adding the host, click to edit icon to add a name for the host. Set the name as upstash-db
which will be equal to the backend
option while we are configuring the upstash-redis client. Remove the https://
part from the url. Finally click on Activate
button on top right corner to activate the configuration with the current deployment.
Local_server.backends.upstash-db
is required for you to run your function locally. Replace url with the REST url of your Upstash Database.
Implementation
src/index.js is the implementation of the serverless function. Edit the code as below:
You need to copy and paste REST URL and token from the Upstash console
We simply connect to Upstash using upstash-redis client. We increment a counter and return the response.
Note that you can not use other Redis® clients as they use TCP connection which is not supported by Fastly Compute.
While authenticating with auth()
, you need to add backend: upstash-db
as an extra request option. This is required as Fastly Compute requires any external network connection to be registered as a backend.
Run Locally
You can run the function locally by:
fastly compute serve
Build and Deploy
Build and deploy your function with:
fastly compute publish
Your application will be built and deployed by Fastly. The command will log the url so you can test if it is working:
https://horribly-organic-spider.edgecompute.app
Closing Words
In this article we have created a very simple application which runs on Fastly Compute at Edge using Upstash. We love to improve Upstash and our content by your feedback. Let us know your thoughts on Twitter or Discord.