Skip to content
Tutorials Center
My learning

Topics

Lesson 34 of 37 ยท Projects and Practice

Project: Build a Service Registry Simulator

Build a small Consul-style service registry in Python with registration, health checks, TTLs, round-robin discovery and prepared-query failover, then test it.

The best way to understand a tool is to build a toy version of it. In this project you write a tiny in-memory service registry that behaves like the heart of Consul: services register with health checks, unhealthy instances disappear from answers, TTL checks expire and a lookup can fail over to another datacenter. The code is a clearly labelled stand-in (real Consul is a distributed system with Raft and gossip), but every concept maps to something you met earlier in this course.

You will build it in four steps, each runnable. Run each block in order; later ones repeat earlier code so every block works alone.

Step 1: register and deregister

A registry maps a service name to its instances. Each instance has an ID, an address, a port and optional tags and metadata, like a Consul service definition.

Python Step 1: the registry core

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

services() mirrors consul catalog services: names with their tags.

Step 2: health checks

Consul only returns instances whose checks pass (?passing, or DNS by default). We model three check types. A TTL check must be refreshed in time, otherwise it goes critical (the application "pings" Consul); an HTTP-like check is a function the registry calls. Time is simulated with an explicit clock so output is deterministic.

Python Step 2: checks and the clock

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

This matches the behavior from the health checks chapter: a critical instance is skipped by discovery, and TTL checks put the burden of reporting on the service.

Step 3: discovery with round robin and tags

Clients ask for a service, optionally filtered by tag, and get one instance at a time. A simple round-robin counter spreads the load.

Python Step 3: lookups

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A tag filter like this is what web.v1.service.consul does in DNS.

Step 4: datacenters and failover

Wrap several registries (one per datacenter) in a federation object and add a prepared-query-style lookup: local first, then the nearest datacenters in order.

Python Step 4: failover across datacenters

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Extend the project

Ideas, in rising difficulty:

  1. Add deregister critical after: remove an instance that has been critical for longer than a limit, as Consul's deregister_critical_service_after does.
  2. Add weights: instances with higher weight are picked more often.
  3. Add a watch: blocking_query(index) that returns only when the registry changes, using an integer version counter like Consul's X-Consul-Index.
  4. Add ACL checks: require a token with service:read before healthy() answers.
  5. Replace the in-memory dictionary with a file and write a command-line wrapper.

Then compare with the real thing: Consul adds Raft replication, gossip failure detection, a DNS and HTTP interface, sessions and many more check types.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. In the simulator, what makes a TTL-checked instance disappear from lookups?

    A missed TTL turns the check critical, and discovery returns only passing instances.
  2. 2. Which real Consul feature does the Federation.query method imitate?

    It tries the local datacenter and then the nearest N datacenters.
  3. 3. Why is a counter used in pick()?

    A round-robin counter picks the next instance each call.
  4. 4. What part of real Consul does an in-memory dictionary NOT provide?

    Raft replication and gossip failure detection make Consul a distributed system, not just a table.

Summary

  • A service registry is a table of instances plus health, filtered at lookup time.
  • TTL checks move the reporting duty to the service; probe checks are run by the registry.
  • Tag filters and round robin give simple discovery; datacenter order plus fallbacks give failover.
  • Real Consul adds consensus, gossip, DNS/HTTP interfaces, sessions and ACLs on top of the same model.

Next: Project: An ACL Rule Checker