Building a Url Shortner with Fastly
Learn how to build a URL shortener using Fastly's edge computing capabilities. In this blog post we're going to build a simple URL shortener using Fastly's edge computing capabilities. This will allow us to create a service that can handle URL shortening at the edge, providing low latency and high availability. Fastly is a cloud platform that provides edge computing capabilities, allowing developers to run applications closer to users. This reduces latency and improves performance by processing requests at the edge of the network, rather than relying on centralized servers. I wanted to do this project in Rust, many other edge computing platforms support Rust; CloudFlare, Vercel and Netlify to name a few. My primary reason for choosing Fastly was that, that I hadn't used it before and wanted to try it out. The same project could have been completely built using any of the other platforms, but I wanted to see how Fastly compared. A URL shortener is a service that takes a long URL and converts it into a shorter, more manageable link. This is useful for sharing links on social media, in emails, or anywhere else where space is limited. By building this service on Fastly, we can take advantage of its edge computing capabilities to ensure fast response times and high availability. For the initial version we will hard code a few URLs to shorten, but in a future post we will look at how to store these in a database and allow users to create their own shortened URLs. For this to be effective we really need a very short short domain but any domain will do for the purposes of this example. To build our URL shortener, we'll use Rust as the programming language and Fastly's Compute@Edge platform. This will allow us to write a lightweight service that can handle URL shortening requests efficiently. Download fastly CLI from Fastly CLI Install the downloaded package: Get a personal API token from the Fastly dashboard. Authenticate with Fastly: Initialise a new Fastly service: Deploy the service: Say yes to create a new service. All other defaults should be fine. We don't need any backends for this project. You should see a domain listed in the output, something like Now we can add the logic to handle URL shortening. Open the Some things to note here: Build and deploy deploy the service again to apply the changes: Now when you visit In this post, we've built a simple URL shortener using Fastly's edge computing capabilities. We've seen how to set up a Fastly service, write Rust code to handle URL shortening, and deploy the service to Fastly's edge network. If you are happy to use hard coded URLs then you can stop here, but in a future post we will look at how to store these URLs in a database and allow users to create their own shortened URLs. This will involve using Fastly's edge computing capabilities to handle database interactions and dynamically generate shortened URLs based on user input.What is Fastly?
Why Fastly?
Why Build a URL Shortener?
Getting Started
Prerequisites
1. Install Fastly CLI
wget https://github.com/fastly/cli/releases/download/v11.2.0/fastly_11.2.0_linux_amd64.deb
sudo dpkg -i fastly_11.2.0_linux_amd64.deb
fastly profile create
2. Create a New Fastly Service
fastly compute init --from=https://github.com/fastly/compute-starter-kit-rust-default
fastly compute build
fastly compute deploy
repeatedly-calm-marlin.edgecompute.app
. If all is good you should be able to go to this address in a web browser and see the default web site from the starter kit.Add redirection logic
src/main.rs
file , find the code to match the request path in the main function, and add the "abc123" path to the match statement:
// Pattern match on the path...
match req.get_path() {
// If request is to the `/` path...
"/" => {
// Send a default synthetic response.
Ok(Response::from_status(StatusCode::OK)
.with_content_type(mime::TEXT_HTML_UTF_8)
.with_body(include_str!("welcome-to-compute.html")))
}
"/abc123" => {
Ok(Response::from_status(StatusCode::PERMANENT_REDIRECT)
.with_header(header::LOCATION, "https://rustmanchester.co.uk/")
.with_header(header::CACHE_CONTROL, "no-cache, no-store, max-age=0, must-revalidate")
.with_header(header::PRAGMA, "no-cache")
.with_header(header::EXPIRES, "0")
.with_body_text_plain("You have been redirected to the root path.\n"))
}
// Catch all other requests and return a 404.
_ => Ok(Response::from_status(StatusCode::NOT_FOUND)
.with_body_text_plain("The page you requested could not be found\n")),
}
Cache-Control
, Pragma
, and Expires
headers to ensure that the redirect is not cached by browsers or proxies. This is important for URL shorteners, as we want to ensure that users always get the latest redirect. Without this the browser will cache the redirect and testing any future changes to the code will become difficult. This also allows us to add analytics to the redirect in the future without worrying about cached redirects.fastly compute build
fastly compute deploy
https://<your domain here>/abc123
, you should be redirected to https://rustmanchester.co.uk/
(Or whatever you changed it to).What next?