We're decoupling the Tophhie Cloud Redirect Service âī¸âđĨ
The Tophhie Cloud Redirect Service will become a micro-service in its own right, and removed from the Tophhie Cloud API.
The Tophhie Cloud API continues to grow, adding more and more endpoints. However, with that, comes an increase in the need for system resources, and additional complexity when it comes to troubleshooting one big API system.
That's why, today, we're decoupling the Tophhie Cloud Redirect Service into its own microservice.
Those that know me by now, know, I love Cloudflare. If I can host a service on Cloudflare, I will. And the Redirect Service is no exception.
What's changed?
Functionally, nothing.
The service works in the exact same way it always has...
- User goes to aka.tophhie.cloud/{shortname}
- User is then redirected to the longer URL for that specific shortname
- User can also go to aka.tophhie.cloud/index to get a full list of all redirect URLs
There... nothing has changed.
Architecturally, things have changed. Let's get into that.
The new Redirect Service Architecture
So, what actually powers the Redirect Service.
- A Cloudflare Worker
- A Cloudflare Hyperdrive connection to a MySQL database hosted in Azure
- and a Cloudflare D1 Database for logging redirect requests
The Cloudflare Worker
The worker performs all the network requests! When you make a request to aka.tophhie.cloud you'll end up hitting this worker, and the worker processes the request.
The code is quite simple for a service like this...
- Request comes in.
- We then do a check on the number of URL segments.
- This is the number of / instances there are.
- aka.tophhie.cloud/index == 1
- aka.tophhie.cloud/this/is/not/right == 4
- This is the number of / instances there are.
- We make sure there's only one segment, and return an error if there is more than one.
- The worker than initialises a connection to the MySQL database via Hyperdrive.
- We then do a switch statement...
- If the shortname is equal to "index", we perform an index request.
- The index request fetches all redirect entries within the database that are currently "published", builds a JSON response, and returns that JSON.
- If the shortname does not equal "index", we perform a redirect request.
- The redirect request does a search in the database for that specific shortname, if one is found, the worker returns a Response.redirect object with the full URL from the database, and a 302 status code.
- If the shortname is equal to "index", we perform an index request.
- In each statement, we also log the request details to the Cloudflare D1 database for tracking and monitoring, so we can check for malicious activity, or particularly popular links.
The Cloudflare Hyperdrive Connection
Previously, the Tophhie Cloud API handled all SQL connections to the MySQL server. With the introduction of Cloudflare, this is now handed off to Cloudflare's Hyperdrive.
A Zero Trust tunnel connection is configured to my Azure environment, allowing Cloudflare to securely connect to my MySQL database. Built in caching makes queries a lot quicker, and responses even faster.

For more information on Hyperdrive, look no further than the Cloudflare Docs here!
The Cloudflare D1 Database
Previously, logs were stored in Azure Table Storage. Whilst this worked, navigating the interface and querying the data was a bit cumbersome and often slow.
This data is now being stored in a Cloudflare D1 database. The interface allows for speedy, in-browser querying, and better readability.

For more information on D1, look no further than the Cloudflare Docs here!
Requests to the old Tophhie Cloud API Redirect endpoint will continue to function as usual for the short-term, until the endpoint is completely removed within the next month or so.
All requests to aka.tophhie.cloud will now be routed to the new Cloudflare Worker.
Until next time đđģ