# What is service discovery?

* A key problem in deployment is getting services to be able to find each-other
* A database might be at 10.1.1.1:6543 while the webserver is at 10.1.1.2:8080

### Level 1 Service Descovery: Baiscs

* Everything is manually configured

<figure><img src="https://3885248957-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoE4wMO1dMVDOGDjh0En7%2Fuploads%2FrN7iPUeYHdniTGgAW3vI%2Fimage.png?alt=media&#x26;token=c0ab9686-26c0-446e-ba2e-4f8b21c4860a" alt=""><figcaption></figcaption></figure>

<figure><img src="https://3885248957-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoE4wMO1dMVDOGDjh0En7%2Fuploads%2F8TQMCHeUKAUV3oQVTNdh%2Fimage.png?alt=media&#x26;token=7a5bd909-eba7-49f8-8dff-a773cbab494c" alt=""><figcaption></figcaption></figure>

{% hint style="warning" %}
You'll know you should start caring about service discovery when you start seeing one of the following:

1. You want "zero downtime deployments" or to use other, more complex deploy strategies
2. You have more than a couple of microservices
3. You are deploying to several environments (dev, staging, prod) and it's getting unwieldy
   {% endhint %}

### Lever 2: Service Discovery via hash table

* A straightforward approach: Store the service IPs in a hash table&#x20;

<figure><img src="https://3885248957-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoE4wMO1dMVDOGDjh0En7%2Fuploads%2FSlvl7SSWBE7TvE6zyLD2%2Fimage.png?alt=media&#x26;token=ca0f4eee-3d01-49a5-b778-0fed3c93a4d6" alt=""><figcaption></figcaption></figure>

* The hash table in question in the config is here:&#x20;

<figure><img src="https://3885248957-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoE4wMO1dMVDOGDjh0En7%2Fuploads%2FvOz4OhBacbPg2mtiZJvz%2Fimage.png?alt=media&#x26;token=c8178db8-d2f9-4646-b696-8238c9551646" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
In simple term:

* All you need to do is update your frontend to set the keys in the hash table to be the forntend's IP
* Then make the backend do the same for the backend
  {% endhint %}

#### Problems with this approach

* It's complex
* It's error prone
* It requires to write custom config files

### Level 3 Service Discovery via DNS

* The idea for DNS is just to map hostnames to IPs. This is the industry standard
* It would be ideal if we could connect to "<http://frontend>" from our reverse proxy and having DNS respond with the IP for the correct versions of the frontend

<figure><img src="https://3885248957-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoE4wMO1dMVDOGDjh0En7%2Fuploads%2FgkMko2TAqqeuEccO4Kxw%2Fimage.png?alt=media&#x26;token=1d373c5b-8b4b-4de4-9f26-c1088e08377d" alt=""><figcaption></figcaption></figure>

* So instead of the first example where you use the IP
* You would use `mongo` where `mongo` is a key in the key:value pairs

<figure><img src="https://3885248957-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FoE4wMO1dMVDOGDjh0En7%2Fuploads%2FReOa2UeiRRUQWNFh1jGI%2Fimage.png?alt=media&#x26;token=d4333b19-d7aa-48e7-b52d-fb7eaa98aa67" alt=""><figcaption></figcaption></figure>
