HashiCorp Consul
medium 32 minConsul 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
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
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
criticalAnybody who reaches the port can register, read and delete.
Wrong# no acl block: everything is allowedRightacl { enabled = true default_policy = "deny"}Trusting the lock alone
criticalA frozen holder may wake up and write after the lock moved on.
Wrongif acquire("lock/job", s): storage.write(data)Rightn = acquire("lock/job", s)if n: storage.write(data, fence=n)Watch loop without limits
majorFast changes turn a blocking query into a busy loop, and an index that goes backwards is missed.
Wrongwhile True: index, v = get(index) use(v)Rightwhile 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
majorA program that resolves once keeps calling a dead copy.
WrongADDR = resolve("payments.service.consul") # at startRightaddr = 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.