Part 8 of 11
Finding a service and spreading the load
Before a client can call a service, it must find the service's address. This is service discovery. The simplest approach is to hard-code an IP address and port. That breaks if the server dies, moves, or gets overloaded, and someone must reconfigure clients by hand.
Usually many copies of a service run, for availability and to handle load. Spreading requests across them is load balancing.
| Option | How it works | Watch out for |
|---|---|---|
| Hardware load balancer | A device in the datacenter. Clients connect to one address, and it sends each request to a healthy server, moving traffic away from failed ones | Special hardware to buy and run |
| Software load balancer | The same idea, as a program on an ordinary machine | One more system to run |
| DNS | One domain name maps to many IP addresses, and the client picks one | DNS changes spread slowly and are cached, so clients may use old addresses when servers change often |
| Service discovery system | A central registry. Each copy registers its host, port, and extra details (like which data shard it owns or which datacenter it is in) and sends regular heartbeats. Clients ask the registry for endpoints and connect directly | A central system to run |
| Service mesh | Combines software load balancing and discovery. A small load balancer runs next to both client and server, either as a library inside the process or as a "sidecar" container. The client talks to its local balancer, which talks to the server's local balancer | More moving parts |
A discovery registry suits environments where copies come and go fast, and its extra details allow smarter load balancing. A mesh brings two big benefits:
- Encryption (TLS) is handled by the balancers, so apps avoid managing certificates.
- You see a lot: which service calls which, how often calls fail, and how much load each service has.
The right choice depends on your needs. Fast-changing container platforms like Kubernetes often use a mesh. Databases and message systems may need their own special balancers. Simple setups often do best with a software load balancer.
Data encoding and evolution for RPC
Clients and servers must be deployable on their own. Unlike databases, services allow a simplifying assumption: update all the servers first, and the clients after. Then you need backward compatibility on requests (new servers read old requests) and forward compatibility on responses (old clients read new responses).
How compatible an RPC is depends on its encoding:
- gRPC uses protobuf, and Avro RPC uses Avro, so each follows its format's rules.
- RESTful APIs usually send JSON responses, and JSON or form-style (URL-encoded) requests. Adding optional request parameters and new response fields usually keeps compatibility.
When a service is used by other organizations, you cannot make them upgrade. So compatibility must last a long time, maybe forever. If you must make a breaking change, you often end up running several API versions side by side.
There is no agreed standard for API versions. Common choices:
- A version number in the URL.
- A version number in the HTTP
Acceptheader. - If clients use API keys, the server stores the version each client chose, and the client can change it in an admin page.