Client discovery through Sentinel in Redis - Time & Space Complexity
When using Redis Sentinel for client discovery, it is important to understand how the time to find the master server changes as the number of monitored servers grows.
We want to know how the discovery process scales when more servers are involved.
Analyze the time complexity of this Redis Sentinel client discovery snippet.
# Connect to Sentinel
SENTINEL masters
# Get list of masters
for each master in masters:
SENTINEL get-master-addr-by-name <master-name>
# Connect to the master
This code asks Sentinel for all masters it monitors, then queries each master's address to connect clients properly.
Look for repeated steps in the discovery process.
- Primary operation: Looping over each master to get its address.
- How many times: Once per master server monitored by Sentinel.
The number of operations grows as the number of masters increases.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 queries to get addresses |
| 100 | About 100 queries to get addresses |
| 1000 | About 1000 queries to get addresses |
Pattern observation: The work increases directly with the number of masters; doubling masters doubles the queries.
Time Complexity: O(n)
This means the time to discover all master addresses grows linearly with the number of masters monitored.
[X] Wrong: "Getting all master addresses is a single quick step regardless of how many masters exist."
[OK] Correct: Each master requires a separate query, so more masters mean more queries and more time.
Understanding how client discovery scales helps you design systems that stay responsive as they grow. This skill shows you can think about real-world performance, not just code correctness.
What if Sentinel cached all master addresses and returned them in one response? How would the time complexity change?