Container DNS and service discovery in Docker - Time & Space Complexity
When containers communicate, they often use DNS to find each other by name. Understanding how the time to resolve these names grows helps us see how well service discovery scales.
We want to know: How does the time to find a container by name change as the number of containers grows?
Analyze the time complexity of the following Docker Compose service discovery setup.
version: '3'
services:
web:
image: nginx
depends_on:
- api
networks:
- appnet
api:
image: myapi
networks:
- appnet
networks:
appnet:
driver: bridge
This setup lets the web service find the api service by its DNS name within the appnet network.
Look at what happens when a container tries to resolve another container's name:
- Primary operation: DNS lookup queries sent to the embedded DNS server.
- How many times: Once per service name resolution, but the DNS server may check its list of container names.
The dominant work is the DNS server searching its list of known containers to match the requested name.
As the number of containers (n) in the network grows, the DNS server must search through more names.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The number of checks grows directly with the number of containers. More containers mean more work to find a name.
Time Complexity: O(n)
This means the time to resolve a container name grows linearly as the number of containers increases.
[X] Wrong: "DNS lookups always take the same time no matter how many containers exist."
[OK] Correct: The DNS server must search through its list of container names, so more containers mean more work and longer lookup times.
Understanding how service discovery scales helps you design systems that stay fast as they grow. This skill shows you can think about real-world system behavior, not just code.
"What if the DNS server used a hash map instead of a list to store container names? How would the time complexity change?"