If you want to know what is service discovery and how it works, this guide is for you. I have explained service discovery, its patterns, and its tools with practical examples.
Service discovery is a method of identifying and accessing devices and services automatically on a network. It is a common pattern followed in distributed systems and microservice architectures. Because servers in such an environment may be short-lived and relying on static IP addresses to connect with these systems is not reliable and may lead to system failures and errors.
Let’s understand service discovery with a real-world example.
Let’s say you have an application that’s deployed on multiple servers and a database cluster consisting of six servers. The application needs to connect to the database in order to store and retrieve data.
In order to make this connection, the application needs to know the network locations (IP address and port number) of the database cluster. One way to do this is to hardcode the IP address and port number in the application properties. However, this approach has a few drawbacks:
Therefore, an efficient mechanism is needed for the application to access healthy database servers at all times.
Here you can use a service discovery approach. A service discovery system is a tool that allows servers/services to register themselves and discover other services.
In our example, the database cluster nodes would register themselves with the service discovery system (service registry), and the application servers would use the service discovery system (service registry) to find the IP addresses of the database servers.
Now let’s look at what is a service registry. The key component of service discovery.
It is a centralized service registry that maintains information about available services. In most cases, it is a key-value store database. The following image shows the dashboard of a service registry of a service discovery tool called consul.
Here is how it works in our example.
The following gif shows an example of the self-registered node/service in the consul service registry.
If a database server fails, the service registry identifies it as such since it has a health check associated with each node. The service registry then marks the node as unhealthy. The agent running in the application server continuously monitors for changes in the service registry and thus identifies the node failure. As a result, the application servers remove the unhealthy database IP from their configurations. All of these changes occur within seconds.
The workflow is the same. The new database server registers itself. The application server identifies it via the service registry and adds it to its configuration.
There are two types of service discovery patterns. Let’s have a look at it.
In the client-side discovery pattern, the client, for example, an application is responsible for interacting with the service registry to get the service details.
For example, the client queries the service registry to get information about the available backend services.
In this model, the client has to implement the service discovery query logic in the application framework. For example, if an application server wants to get the available database server details, the application should implement the query using the service discovery client library to query the service registry.
In the server-side discovery pattern, the request goes to a load balancer (server) and the load balancer connects to the service registry to get the IP addresses of the healthy backend servers.
Here the server component implements the agent or library to query the service registry. One classic example of this model is Nginx load balancing using service discovery. Here the clients (service consumers) directly talk to the Nginx load balancer and the load balancer queries the service registry to get the list of backend servers to route the traffic.
The following image shows service side service discovery using consul.
If you are aware kubernetes, the ingress controller implements the service side service discovery.
To implement the concept of a service registry you need specific tools. Following are the two common open-source service discovery tools.
You can use HTTP REST API, DNS, and gRPC protocol with these service discovery tools.
If you want to get your hands on service discovery concepts, you can try setting up Nginx reverse proxy configuration using consul.
I have published a detailed hands-on guide for the same. Check out the Service Discovery Example Using Consul
Service discovery is a must-know concept for developers and devops engineers. Most of the distributed systems and microservices architectures implement service discovery.
One class example is Kubernetes. It relies heavily on service discovery using etcd datastore.
Bibin Wilson is a cloud and DevOps consultant with over 10 years of IT experience. He has extensive hands-on experience with public cloud platforms, cloud hosting, Kubernetes and OpenShift deployments in production. He has authored over 300 tech tutorials, providing valuable insights to the DevOps community. His courses on techiescamp.com offer practical guidance and real-world examples for professionals aiming to excel in cloud, DevOps, and infrastructure automation.