Skip to content
KALANBLON
Show contents

Chapter 4 of 7 · 20 min

Availability and scaling

By the end of this chapter, you'll be able to

  • Turn an availability rate into downtime
  • Compute the availability of components in series and in parallel
  • Spot a single point of failure
  • Explain horizontal and automatic scaling

The “nines”

Availability is often expressed as a number of “nines”: 99.9% is called “three nines”. Each extra nine divides the allowed downtime by ten:

HOURS_PER_YEAR = 365 * 24

for availability in (0.99, 0.999, 0.9999, 0.99999):
    down_h = HOURS_PER_YEAR * (1 - availability)
    if down_h >= 1:
        print(f"{availability:.3%} → {down_h:.1f} hours of downtime per year")
    else:
        print(f"{availability:.3%} → {down_h * 60:.1f} minutes of downtime per year")
99.000% → 87.6 hours of downtime per year
99.900% → 8.8 hours of downtime per year
99.990% → 52.6 minutes of downtime per year
99.999% → 5.3 minutes of downtime per year

Series and parallel

A request goes through several components: load balancer, web servers, database. If they are in series (all required), availabilities multiply: the system is less available than its weakest link. If they are in parallel (redundant: one is enough), the system only fails if all of them fail at the same time.

def series(*availabilities):
    total = 1.0
    for a in availabilities:
        total *= a
    return total

def parallel(*availabilities):
    all_down = 1.0
    for a in availabilities:
        all_down *= 1 - a
    return 1 - all_down

server = 0.99
print(f"1 server: {server:.4%}")
print(f"2 redundant servers: {parallel(server, server):.4%}")
print(f"balancer 99.99% + 2 servers + database 99.95%: {series(0.9999, parallel(server, server), 0.9995):.4%}")
1 server: 99.0000%
2 redundant servers: 99.9900%
balancer 99.99% + 2 servers + database 99.95%: 99.9300%

Two 99% servers in parallel give 99.99%, provided their failures are independent. Yet the whole only reaches 99.93%: the single database, at 99.95%, has become the weakest link.

The database, replicated in the other zone, fails over in turn: the service carries on.

Step 5 of 5
Text description of the animation
  1. Traffic arrives at a single address.
  2. The load balancer spreads the requests and checks each server's health.
  3. A data centre goes down: its servers stop answering the probes.
  4. The load balancer sends everything to zone B; autoscaling adds servers there.
  5. The database, replicated in the other zone, fails over in turn: the service carries on.

Scaling

When the load increases, there are two options:

  • vertical scaling: a more powerful machine. Simple, but limited, and often with an interruption;
  • horizontal scaling: more identical machines behind the load balancer. Practically without limit, provided the application is stateless: no session data kept in one specific server's memory, everything is in the database or a shared cache.

Autoscaling adjusts the number of machines to a target, for example 60% CPU utilisation:

import math

TARGET = 0.60
CAPACITY_PER_SERVER = 100

def servers_needed(requests_per_s, minimum=2, maximum=20):
    need = math.ceil(requests_per_s / (CAPACITY_PER_SERVER * TARGET))
    return max(minimum, min(maximum, need))

for hour, load in [("3am", 40), ("9am", 380), ("noon", 650), ("8pm", 1500)]:
    print(f"{hour:>4}: {load:>4} req/s → {servers_needed(load)} servers")
 3am:   40 req/s → 2 servers
 9am:  380 req/s → 7 servers
noon:  650 req/s → 11 servers
 8pm: 1500 req/s → 20 servers
  • the 60% target keeps a margin to absorb a sudden rise while new machines start;
  • the minimum of 2 ensures redundancy even at night;
  • the maximum protects the bill: at 8pm, 1,500 requests per second would need 25, but the limit is reached. You need to know it, and monitor that case.

Mini-lab: how many nines for your architecture?Python 3.13

Output

Click “Run”: Python will load in your browser (about 10 MB the first time).

Your code runs in your browser, isolated from the page. · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor

Your turn

Worked exercise

Worked exercise: an online shop's availability

A shop depends in series on a load balancer (99.99%), web servers, a database (99.95%) and an external payment service (99.9%). Compute the total availability with 1, 2 then 3 web servers at 99% each in parallel, and turn each result into hours of downtime per year.

Hint 1
First compute the availability of the web server group, then multiply by the other components.
Hint 2
Hours of downtime: 8,760 × (1 − availability).
Show the solution and common mistakes
def parallel(*a):
    down = 1.0
    for x in a:
        down *= 1 - x
    return 1 - down

for n in (1, 2, 3):
    web = parallel(*[0.99] * n)
    total = 0.9999 * web * 0.9995 * 0.999
    print(f"{n} server(s): {total:.3%}, i.e. {8760 * (1 - total):.1f} h of downtime per year")
1 server(s): 98.842%, i.e. 101.5 h of downtime per year
2 server(s): 99.830%, i.e. 14.9 h of downtime per year
3 server(s): 99.840%, i.e. 14.0 h of downtime per year

The second server gains a lot; the third almost nothing. Availability is now limited by the external payment service (99.9%) and the database: adding web servers no longer helps. You could, for example, plan a second payment provider, or let customers order now and pay a little later when payment is unavailable.

Common mistakes:

  • Adding or averaging availabilities: in series, they multiply.
  • Forgetting external dependencies (payment, sending emails): they count in the availability the user experiences.

Summary

  1. 1Each nine of availability divides the allowed downtime by ten: 99.9% ≈ 8.8 hours a year.
  2. 2In series, availabilities multiply; in parallel, the system only fails if everything fails.
  3. 3A single point of failure cancels redundancy: spread over several zones and replicate the database.
  4. 4Horizontal scaling adds machines; it requires a stateless application.
  5. 5Autoscaling aims for a utilisation target, between a minimum (redundancy) and a maximum (budget).

Your progress is saved on this device. Create a free account to keep your progress on all your devices.