Uptime monitoring: why you should alert only after consecutive failures
How to build an availability monitor that catches real outages without false alarms: consecutive failures, detection time, states and a C# example.
In short
A single failed check doesn't mean the site is down: network blips, DNS timeouts and deploys cause isolated failures. A reliable monitor alerts only after N consecutive failures (or failures from several locations at once), notifies once on the transition to "down", and sends another notice when the service recovers. Worst-case detection time is roughly interval × N + timeout.
The false-alarm problem
The simplest possible monitor makes a request every minute and sends an alert if it fails. In practice it alerts far too often: a dropped packet, a DNS timeout, a restart during a deploy or a momentary glitch on the monitoring server itself all produce isolated failures that resolve on their own.
The effect is well known in operations: when most alerts require no action, people start ignoring them, and the real alert gets lost. Google's SRE book sums up the criterion: every alert should be actionable and point to a real problem for users.
Consecutive failures: the simplest threshold that works
Instead of alerting on the first failure, the monitor counts failures in a row and only alerts when the counter reaches a threshold N. Any success resets the counter. Isolated failures never reach the threshold; a real outage reaches it within a few cycles.
The same idea appears in established tools: in Prometheus, the for clause of an alerting rule requires the condition to hold for a period before the alert fires.
How long it takes to detect an outage
The cost of the threshold is delay. In the worst case the outage starts right after a successful check, and the time until the alert is approximately interval × N + timeout.
| Interval | Failures (N) | Timeout | Worst-case detection |
|---|---|---|---|
| 60 s | 1 | 10 s | ~1 min 10 s, with many false positives |
| 60 s | 3 | 10 s | ~3 min 10 s |
| 30 s | 3 | 10 s | ~1 min 40 s |
| 300 s | 2 | 10 s | ~10 min 10 s |
A shorter interval compensates for a larger N without increasing detection time. For most sites, checking every 30–60 seconds with N = 3 is a good balance.
Think in states, not checks
Another source of noise is alerting on every failed check once the threshold has been reached. A site that is down for an hour shouldn't produce 60 messages. Modeling the monitor as a small state machine solves this:
- Up: checks passing, counter at zero.
- Suspect: one or more failures, below the threshold. No alert.
- Down: threshold reached. One alert on entering this state, and none while it lasts.
- Recovered: the first successful check after "down". Sends a recovery notice with the outage duration and returns to "up".
A C# example
A minimal implementation with HttpClient. An HttpClient timeout surfaces as a TaskCanceledException; the when filter distinguishes it from a cancellation requested by the application itself.
public sealed class SiteMonitor
{
private readonly HttpClient _http = new() { Timeout = TimeSpan.FromSeconds(10) };
private readonly Uri _url;
private readonly int _threshold;
private readonly Func<string, Task> _notify;
private int _consecutiveFailures;
private DateTimeOffset? _downSince;
public SiteMonitor(Uri url, int threshold, Func<string, Task> notify)
{
_url = url;
_threshold = threshold;
_notify = notify;
}
public async Task CheckAsync(CancellationToken ct)
{
bool healthy;
try
{
using var response = await _http.GetAsync(_url, ct);
healthy = response.IsSuccessStatusCode;
}
catch (HttpRequestException)
{
healthy = false;
}
catch (TaskCanceledException) when (!ct.IsCancellationRequested)
{
healthy = false; // timeout
}
if (healthy)
{
if (_downSince is { } since)
await _notify($"{_url} is responding again after {DateTimeOffset.UtcNow - since:hh\\:mm\\:ss} down.");
_consecutiveFailures = 0;
_downSince = null;
return;
}
_consecutiveFailures++;
if (_downSince is null && _consecutiveFailures >= _threshold)
{
_downSince = DateTimeOffset.UtcNow;
await _notify($"{_url} is down after {_consecutiveFailures} consecutive failures.");
}
}
}
The HttpClient is reused across checks; creating one instance per request can exhaust available sockets in long-running monitors.
What counts as a failure
An HTTP 200 doesn't guarantee the site works. A few extra checks catch failures the status code hides:
- Expected content: look for text that only appears on the correct page, to catch error pages served with status 200.
- Response time: treat a response above a limit as a failure. A site that takes 20 seconds to respond is, in practice, down. Sudden slowness in Rails applications often comes from N+1 queries or missing indexes.
- TLS certificate: alert days in advance when the certificate is close to expiring.
Check from more than one place
If the monitoring server loses its connection, every site appears down at the same time. Two measures prevent this: run the monitor outside the infrastructure it watches, and check from two or more locations, alerting only when the majority agrees. An "all sites went down at once" alert almost always points to a problem with the monitor itself.
Related writing
All writing-
Software Engineering · · 3 min read
N+1 queries in Rails: how to find them, fix them and keep them from coming back
What the N+1 problem is in Active Record, when to use includes, preload or eager_load, and how strict_loading stops regressions.
-
Software Engineering · · 3 min read
PostgreSQL indexes: B-tree, GIN and partial indexes, validated with EXPLAIN ANALYZE
How to choose between B-tree, GIN, composite and partial indexes in PostgreSQL, read the plan with EXPLAIN ANALYZE, and create indexes without locking production.
-
AI Engineering · · 4 min read
Local RAG with Ollama: how retrieval-augmented generation works
What RAG is, how each stage works (chunking, embeddings, retrieval and generation), and how to build a local, private version with Ollama and Python.