AlgoViz

Defining high availability

easy 25 min

Availability is the share of time, or of requests, that a system works for its users.

An availability calculator

Read it, run it, change a line and run it again.

Loading editor…

Outages are cut to the window and overlapping ones are merged, so two alerts for one outage are not counted twice.

The same in TypeScript
type Outage = { start: number; end: number }; // minutes since the window beganconst allowedDowntime = (target: number, periodMin: number): number => (periodMin * (100 - target)) / 100;function format(min: number): string {  if (min >= 1440) return `${(min / 1440).toFixed(2)} days`;  if (min >= 60) return `${(min / 60).toFixed(2)} hours`;  return min >= 1 ? `${min.toFixed(2)} minutes` : `${(min * 60).toFixed(1)} seconds`;}class AvailabilityMonitor {  private outages: Outage[] = [];  constructor(private target: number) {}  record(o: Outage): void {    this.outages.push(o);  }  metrics(total: number) {    const merged: Outage[] = [];    for (const o of [...this.outages].sort((a, b) => a.start - b.start)) {      const last = merged[merged.length - 1];      if (last && o.start <= last.end) last.end = Math.max(last.end, o.end);      else merged.push({ ...o });    }    const down = merged.reduce((s, o) => s + o.end - o.start, 0);    const n = merged.length;    const uptime = (100 * (total - down)) / total;    return {      uptime,      mttr: n ? down / n : 0,      mtbf: n ? (total - down) / n : Infinity, // divide by the number of failures, not by n - 1      met: uptime >= this.target,      gap: this.target - uptime,    };  }}const monitor = new AvailabilityMonitor(99.9);monitor.record({ start: 1000, end: 1030 });console.log(format(allowedDowntime(99.99, 365 * 1440)), monitor.metrics(43200));
The same in Java
import java.time.*;import java.util.*;public class AvailabilityCalculator {    record Incident(Instant start, Instant end) {        Duration duration() { return Duration.between(start, end); }    }    record Metrics(double uptime, Duration mttr, Duration mtbf, int count) {}    static Duration allowed(double target, Duration period) {        return Duration.ofMillis(Math.round(period.toMillis() * (100 - target) / 100));    }    /** Incidents are expected not to overlap. */    static Metrics metrics(List<Incident> incidents, Duration period) {        Duration down = incidents.stream().map(Incident::duration).reduce(Duration.ZERO, Duration::plus);        int n = incidents.size();        Duration up = period.minus(down);        return new Metrics(            100.0 * up.toMillis() / period.toMillis(),            n == 0 ? Duration.ZERO : down.dividedBy(n),            n == 0 ? period : up.dividedBy(n),            n);    }    public static void main(String[] args) {        Instant t = Instant.parse("2026-01-01T00:00:00Z");        var list = List.of(new Incident(t, t.plusSeconds(1800)));        System.out.println(allowed(99.99, Duration.ofDays(365)));        System.out.println(metrics(list, Duration.ofDays(30)));    }}
The same in Go
package mainimport (	"fmt"	"sync"	"time")type Incident struct{ Start, End time.Time }type Tracker struct {	mu        sync.RWMutex	target    float64	incidents []Incident}func Allowed(target float64, period time.Duration) time.Duration {	return time.Duration(float64(period) * (100 - target) / 100)}func (t *Tracker) Record(i Incident) {	t.mu.Lock()	defer t.mu.Unlock()	t.incidents = append(t.incidents, i)}// Availability clips every incident to the window, so one that began earlier still counts.func (t *Tracker) Availability(start, end time.Time) float64 {	t.mu.RLock()	defer t.mu.RUnlock()	var down time.Duration	for _, i := range t.incidents {		a, b := i.Start, i.End		if a.Before(start) {			a = start		}		if b.After(end) {			b = end		}		if b.After(a) {			down += b.Sub(a)		}	}	total := end.Sub(start)	return 100 * float64(total-down) / float64(total)}func main() {	start := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)	t := &Tracker{target: 99.9}	t.Record(Incident{start.Add(-10 * time.Minute), start.Add(20 * time.Minute)})	a := t.Availability(start, start.AddDate(0, 0, 30))	fmt.Printf("%.4f%% meets=%v allowed=%v\n", a, a >= t.target, Allowed(99.99, 365*24*time.Hour))}