Bird
Raised Fist0
Microservicessystem_design~10 mins

Service discovery concept in Microservices - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to register a service instance with the discovery server.

Microservices
discovery_client.register_service('[1]', '10.0.0.1', 8080)
Drag options to blanks, or click blank then click option'
Aservice_id
Bservice_name
Cservice_port
Dservice_address
Attempts:
3 left
💡 Hint
Common Mistakes
Using IP address or port as the service identifier.
Confusing service ID with service name.
2fill in blank
medium

Complete the code to discover the address of a service instance.

Microservices
address = discovery_client.get_service_address('[1]')
Drag options to blanks, or click blank then click option'
Ainstance_id
Bservice_port
Cservice_name
Dnode_id
Attempts:
3 left
💡 Hint
Common Mistakes
Using instance ID instead of service name.
Using port number or node ID which are not keys for discovery.
3fill in blank
hard

Fix the error in the service registration code to include health check.

Microservices
discovery_client.register_service('payment-service', '10.0.0.5', 9090, health_check='[1]')
Drag options to blanks, or click blank then click option'
Ahttp://10.0.0.5:9090/health
B10.0.0.5:9090
C/health
Dhealth
Attempts:
3 left
💡 Hint
Common Mistakes
Providing only a path or IP without protocol.
Using a keyword instead of a URL.
4fill in blank
hard

Fill both blanks to create a dictionary of service instances filtered by status.

Microservices
healthy_services = {id: info for id, info in services.items() if info['[1]'] == '[2]'}
Drag options to blanks, or click blank then click option'
Astatus
Bhealthy
Caddress
Dactive
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'address' as a key for filtering.
Using 'active' instead of 'healthy' as status value.
5fill in blank
hard

Fill all three blanks to implement a simple client-side load balancer picking a random healthy instance.

Microservices
import random

instances = discovery_client.get_service_instances('[1]')
healthy = [i for i in instances if i['[2]'] == '[3]']
selected = random.choice(healthy)
Drag options to blanks, or click blank then click option'
Aorder-service
Bstatus
Chealthy
Daddress
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong service name.
Filtering by wrong key or value.
Not filtering before choosing randomly.

Practice

(1/5)
1. What is the main purpose of service discovery in a microservices architecture?
easy
A. To help services find and communicate with each other automatically
B. To store user data securely
C. To manage database transactions
D. To handle user authentication

Solution

  1. Step 1: Understand the role of service discovery

    Service discovery allows microservices to locate each other dynamically without hardcoding addresses.
  2. Step 2: Identify the correct purpose

    It is not about data storage, transactions, or authentication but about service communication.
  3. Final Answer:

    To help services find and communicate with each other automatically -> Option A
  4. Quick Check:

    Service discovery = automatic service location [OK]
Hint: Service discovery = finding services automatically [OK]
Common Mistakes:
  • Confusing service discovery with data storage
  • Thinking it manages user authentication
  • Assuming it handles database transactions
2. Which of the following is a common component used in service discovery for microservices?
easy
A. Load balancer
B. Service registry
C. API gateway
D. Database shard

Solution

  1. Step 1: Identify components related to service discovery

    A service registry keeps track of available service instances and their locations.
  2. Step 2: Differentiate from other components

    Load balancers distribute traffic, API gateways manage requests, and database shards split data, but none perform service discovery.
  3. Final Answer:

    Service registry -> Option B
  4. Quick Check:

    Service registry = key for service discovery [OK]
Hint: Service registry stores service locations [OK]
Common Mistakes:
  • Confusing load balancer with service registry
  • Mixing API gateway with service discovery
  • Thinking database shards help find services
3. Consider this simplified service discovery flow:
1. Service A queries the registry for Service B's address.
2. Registry returns Service B's current IP and port.
3. Service A connects to Service B using the returned address.
4. Service B processes the request and responds.

What happens if Service B changes its IP but the registry is not updated?
medium
A. Service B will notify Service A directly
B. Service A will automatically find the new IP
C. The registry will redirect Service A to the new IP
D. Service A will connect to the old IP and fail

Solution

  1. Step 1: Analyze the flow when registry is outdated

    If the registry has an old IP, Service A uses that wrong address to connect.
  2. Step 2: Understand consequences of stale registry data

    Service A cannot find Service B at the old IP, so connection fails; no automatic update or redirection occurs.
  3. Final Answer:

    Service A will connect to the old IP and fail -> Option D
  4. Quick Check:

    Stale registry = failed connection [OK]
Hint: Outdated registry causes failed connections [OK]
Common Mistakes:
  • Assuming automatic IP update without registry refresh
  • Thinking services notify each other directly
  • Believing registry redirects requests automatically
4. A developer notices that service discovery is failing because services cannot find each other. The registry is running, but services do not register themselves. What is the most likely cause?
medium
A. The registry database is full
B. Network latency is too high
C. Services are not sending heartbeat or registration requests to the registry
D. Services are using incorrect API versions

Solution

  1. Step 1: Identify why services are missing in registry

    Services must actively register or send heartbeats to the registry to be discoverable.
  2. Step 2: Eliminate other causes

    Full database or network latency might cause delays but not complete absence; API version mismatch affects communication, not registration.
  3. Final Answer:

    Services are not sending heartbeat or registration requests to the registry -> Option C
  4. Quick Check:

    Missing registration = discovery failure [OK]
Hint: Services must register to be discoverable [OK]
Common Mistakes:
  • Blaming network latency for missing registrations
  • Assuming registry storage limits cause missing services
  • Confusing API version issues with registration problems
5. In a large microservices system with many instances starting and stopping frequently, which service discovery approach best supports scalability and fault tolerance?
hard
A. Using a centralized service registry with periodic health checks and automatic deregistration
B. Hardcoding service IPs in each microservice configuration
C. Using DNS-based service discovery without health checks
D. Relying on client-side caching of service addresses without updates

Solution

  1. Step 1: Evaluate scalability and fault tolerance needs

    Frequent changes require dynamic updates and health checks to avoid stale info and failures.
  2. Step 2: Compare approaches

    Centralized registry with health checks keeps accurate service info; hardcoding or caching causes stale data; DNS without health checks misses failures.
  3. Final Answer:

    Using a centralized service registry with periodic health checks and automatic deregistration -> Option A
  4. Quick Check:

    Dynamic registry + health checks = scalable, fault tolerant [OK]
Hint: Dynamic registry with health checks scales best [OK]
Common Mistakes:
  • Hardcoding IPs causing poor scalability
  • Ignoring health checks leading to stale data
  • Relying on caching without updates