Senior
What were the timeout issues when interacting with [system/service] and how did you resolve them?
sobes.tech AI
Answer from AI
Timeouts when interacting with the service occurred due to high load and insufficient optimization of network requests. Specifically, the service failed to process all requests in time, leading to timeout.
To address the issue, I took the following steps:
- Implemented asynchronous request processing to unload the service.
- Configured retries with exponential backoff.
- Optimized network calls by reducing the size of transmitted data and the number of requests.
- Added monitoring and alerts for timely response to increased delays.
An example of configuring an HTTP client with timeout and retry in Java using the HttpClient library and CompletableFuture might look like this:
HttpClient client = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(5))
.build();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://example.com/api"))
.timeout(Duration.ofSeconds(10))
.build();
// Simple retry implementation
int maxRetries = 3;
for (int i = 0; i < maxRetries; i++) {
try {
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() == 200) {
// handle successful response
break;
}
} catch (IOException | InterruptedException e) {
// log and wait before retrying
Thread.sleep((long) Math.pow(2, i) * 1000);
}
}