How to set timeout in Retrofit library?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Retrofit is a powerful type-safe HTTP client for Android and Java, used to manage web service API calls. Setting a timeout in Retrofit is crucial to ensure that your application remains responsive by handling cases when a network request takes too long to complete. Here’s how you can set timeouts in the Retrofit library.
Understanding Timeouts in APIs
Timeouts in an API signify the maximum amount of time to wait for a network operation to complete. If this time elapses without completion, the operation is aborted. Timeout configurations in Retrofit are accomplished via the HTTP client it uses, commonly OkHttpClient.
In Retrofit, there are three types of timeouts:
- Connect Timeout: The time required to connect to a server.
- Read Timeout: The time required to read received data.
- Write Timeout: The time applied when sending data to the server.
Setting Up Timeouts in Retrofit
To configure timeouts, you need to customize the OkHttpClient and set it in the Retrofit builder. Here’s a step-by-step guide:
Step 1: Add Dependencies
Ensure your build.gradle file includes the necessary dependencies for Retrofit and OkHttp:
Step 2: Configure OkHttpClient
The OkHttpClient can be configured with specific timeout values.
In this example, each timeout is set to 30 seconds. You can adjust these values as needed.
Step 3: Build Retrofit Instance with Custom Client
Integrate the customized OkHttpClient with Retrofit:
Step 4: Service Declaration and API Calls
Declare your API service interface which outlines your HTTP operations.
Step 5: Execute Requests with Configured Timeout
Finally, use your configured Retrofit instance to create and execute network requests:
Timeout Configuration Table
| Timeout Type | Description | Default Value | Custom Configuration (Example) |
| Connect Timeout | Time allowed to establish a connection | 10 seconds | 30 seconds |
| Read Timeout | Time allowed to read data | 10 seconds | 30 seconds |
| Write Timeout | Time allowed to write data | 10 seconds | 30 seconds |
Additional Considerations
- Exception Handling: Timeouts will result in a
SocketTimeoutException. Implement robust error handling to ensure the application handles these exceptions gracefully. - Environmental Factors: Network conditions widely influence optimal timeout settings. Consider varying timeouts based on the operating environment (development, testing, production).
- Performance Implications: Higher timeout values may result in unresponsive applications if the network is slow or unresponsive. Carefully balance between acceptable wait times and user experience.
By configuring timeouts wisely, developers enhance the robustness and reliability of their application’s network communications, addressing varied network performances gracefully.
Related reading
- How to set timeout in Retrofit library?
- How to set up an OAuth2 Authentication Provider with AWS API Gateway?
- How to specify a prefix to a service exposed with an ingress
- How to specify all ports in Security group - CloudFormation
- How to set timer in android?
- How to set top-left alignment for UILabel for iOS application?
- How to specify api docs url for swagger ui in spring boot open api v3?
- How to specify Proxy Pass in kubernetes

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.