In this tutorial, we’ll build a serverless website uptime monitoring application using Cloudflare Workers. Our application will periodically check websites’ availability, track response times, and store the results in a serverless database. We’ll use the HONC stack: Hono (a lightweight web framework), OpenTelemetry (for observability), and Cloudflare’s suite of serverless products. Overview of the Application Our uptime monitor will:
- Monitor multiple websites simultaneously
- Allow configurable check intervals per website
- Track response times and HTTP status codes
- Calculate uptime percentages
- Provide a RESTful API for managing monitored sites
- Include a simple web interface to view results
The Technology Stack
Let’s break down the key technologies we’re using:
- Hono: A lightweight web framework specifically designed for edge computing platforms like Cloudflare Workers
- D1: Cloudflare’s serverless SQL database, perfect for storing our monitoring results
- Drizzle ORM: A TypeScript-first ORM that provides type safety and a great developer experience
- Durable Objects: Cloudflare’s solution for maintaining state and coordinating our monitoring schedules
Setting Up the Database Schema
First, let’s define our database schema using Drizzle ORM. We need two main tables: one for websites and another for uptime checks.
Building the Monitor Class (Durables Object)
The Monitor class is the core engine of our uptime monitoring system.
What makes it special is that it’s implemented as a Cloudflare Durable Object, which means it maintains its state across multiple executions.
Each website gets its own Monitor instance that persists in memory, keeping track of its own timer and check schedule.
The scheduleChecks
method sets up an interval timer based on the website’s configured check frequency, while performCheck
does the actual work of making HTTP requests to the target site, measuring response times, and storing results.
This design ensures reliable, consistent monitoring even at scale. If a website is configured to be checked every 60 seconds, the Monitor instance will maintain that schedule without requiring external coordination.
Creating the API Endpoints
We use Hono to create our REST API endpoints. The interesting part about these endpoints is how they integrate with both D1 (for data persistence) and Durable Objects (for monitoring). When you create a new website through the POST endpoint, it doesn’t just store the website data - it also creates a new Monitor instance for that site. This is done by generating a unique Durable Object ID based on the website’s ID, ensuring a 1:1 relationship between websites and their monitors. The GET endpoints are designed to be efficient, using Drizzle ORM’s type-safe queries to fetch exactly the data needed without overfetching. Here are the main routes we’ll implement:
Calculating Uptime Percentages
We also implement an endpoint to calculate uptime percentages for any given website. The uptime percentage calculation is more sophisticated than it might appear at first glance. Rather than just storing a running total, it calculates uptime dynamically based on historical check data. This allows for flexible time windows - you can calculate uptime for the last day, week, or month without needing to store separate aggregates. The calculation uses SQL’s date handling capabilities to filter checks within the specified time period, then simply divides the number of successful checks by total checks. What’s clever about this approach is that it automatically accounts for varying check intervals - a site checked every minute will have more data points than one checked every hour, but both will give accurate uptime percentages. The results are rounded to two decimal places to provide meaningful precision without false accuracy.
The Web Interface
Finally, we create a simple web interface to view monitored websites and their uptime percentages.
The frontend of the uptime monitor is built using Hono’s JSX rendering capabilities, featuring two main components: a Layout
component and a WebsiteList
component.
The Layout
component serves as a base template, providing a clean, flexible structure with CSS-in-JS styling that ensures full viewport height and proper padding.
The WebsiteList
component is where the main UI lives, displaying monitored websites in a responsive grid layout.
If you want to read more about how to build a frontend in combination with Hono, check out the this guide.
Conclusion
This serverless uptime monitor demonstrates the power of Cloudflare’s edge computing platform. By leveraging Durable Objects for state management, D1 for data storage, and Hono for routing, we’ve created a scalable and efficient monitoring solution. Some potential improvements could include:
- Adding authentication for API endpoints
- Implementing real-time updates using WebSocket connections
- Creating more detailed reporting and analytics
- Adding notification systems for downtime alerts
- Implementing custom check parameters (timeout, expected status codes, etc.)
- Furthermore a more robust approach might be to use a cron trigger that runs every few seconds, looks for records that haven’t had a recent uptime check (according to their interval) and then making the check, such that when the Worker is redeployed, it doesn’t miss any existing checks.
The complete source code shows how we can build complex applications using serverless technologies while maintaining good practices like type safety, proper error handling, and clean architecture.