AlgoViz

HashiCorp Consul

medium 32 min

Consul keeps a live list of services and their health across many machines, answers where a service is over DNS and HTTP, stores small settings and locks, and can decide which service may call which through a service mesh.

See it run

Register a service, look it up, lose a machine and a leader, take a lock and pass the mesh.

Operations

Register

A new copy of payments reports in through its agent.

Step 1 of 4

One copy

Datacenter dc1webcallerLocal agentDNS, HTTPServer 1leaderServer 2followerServer 3followerpayments 1passingpayments 2not started

Answer for payments

10.0.1.7

payments 1 runs and is listed. payments 2 is not there yet.

Step 1: One copy

def on_start(agent, service):    agent.register(service, status="critical")    leader.append(service)        # Raft log    wait_for_majority()    while True:        status = run_check(service)        if status != last:            leader.append(status)        sleep(interval)
Time
one check interval
What you need
an agent on the machine

Say that a new copy starts as critical: it must prove itself before it gets traffic.

In the real world

The same hashicorp consul doing real jobs. Pick one and step through it.

The health address of payments also tests the database.

Step 1 of 3

Slow database

Datacenter dc1webcallerLocal agentDNS, HTTPServer 1leaderServer 2followerServer 3followerpayments 1timing outpayments 2timing out

Answer for payments

10.0.1.7 · 10.0.2.4

The database becomes slow. Both copies answer their checks too late.

Step 1: Slow database

Common mistakes

The bugs interviewers watch for, with the fix beside each.

  • ACLs left off

    critical

    Anybody who reaches the port can register, read and delete.

    Wrong
    # no acl block: everything is allowed
    Right
    acl {  enabled        = true  default_policy = "deny"}
  • Trusting the lock alone

    critical

    A frozen holder may wake up and write after the lock moved on.

    Wrong
    if acquire("lock/job", s):    storage.write(data)
    Right
    n = acquire("lock/job", s)if n:    storage.write(data, fence=n)
  • Watch loop without limits

    major

    Fast changes turn a blocking query into a busy loop, and an index that goes backwards is missed.

    Wrong
    while True:    index, v = get(index)    use(v)
    Right
    while True:    limiter.wait()                 # burst of 2, then slow    new, v = get(index)    index = 0 if new < index else max(new, 1)    use(v)
  • Addresses kept for ever

    major

    A program that resolves once keeps calling a dead copy.

    Wrong
    ADDR = resolve("payments.service.consul")  # at start
    Right
    addr = resolve("payments.service.consul")  # per new connectioncall(addr, timeout=1, retries=1)

Interview tips

  • How to explain it

    • Agents register and check.
    • Servers agree with Raft.
    • Callers ask by name.
  • Memory trick

    • Raft for the few, gossip for the many.
    • Stale to find, consistent to lock.
    • Critical hides, warning stays.
  • Reach for it when

    • VMs and clusters share names.
    • Several datacenters.
    • Rules by service identity.
  • Be careful with

    • Defaults: ACLs and TLS are off.
    • Deep health checks.
    • The licence if you resell.