Middle
How do you localize a problem with a slow handle? What to do with a falling external dependency?
sobes.tech AI
Answer from AI
To localize the problem with the braking handle (narrow point), you should:
- Measure the execution time of each stage or function to identify where the delay occurs.
- Use code profiling (e.g., cProfile module in Python) to identify the "heavy" sections.
- Check logs and metrics of external dependencies to understand when and why they fail.
What to do with a failing external dependency:
- Implement error handling and retries with exponential backoff.
- Use timeouts to avoid blocking the main process.
- Consider caching data to reduce the frequency of requests.
- If possible, replace or duplicate the dependency to improve fault tolerance.
Example of a simple retry with timeout in Python:
import time
import requests
def fetch_with_retry(url, retries=3, delay=2):
for attempt in range(retries):
try:
response = requests.get(url, timeout=5)
response.raise_for_status()
return response.text
except requests.RequestException:
if attempt < retries - 1:
time.sleep(delay)
else:
raise