Retrofit 2 — Customize Network Timeouts

Retrofit uses some default network timeout settings, which might not fit your use case. If your app needs to be extra snappy, or in the other direction, if your server is slow, you might want to change those timeout settings. In this tutorial you'll learn how you can customize all three special network timeouts settings.

Retrofit Series Overview

Customizing Timeouts

By default, Retrofit uses the following timeouts:

  • Connection timeout: ten seconds
  • Read timeout: ten seconds
  • Write timeout: ten seconds

We'll explain what these mean in detail in a later section.

How to Configure Timeout Settings via OkHttp

As you probably guessed, the customization of network connection timeouts on Retrofit can't be done directly. Instead, you have to configure OkHttp, Retrofit's network layer, to customize the connection timeouts:

OkHttpClient okHttpClient = new OkHttpClient.Builder()  
        .connectTimeout(1, TimeUnit.MINUTES)
        .readTimeout(30, TimeUnit.SECONDS)
        .writeTimeout(15, TimeUnit.SECONDS)

Retrofit.Builder builder = new Retrofit.Builder()  

The code above sets the connection timeout to one minute, the read timeout to 30 seconds, and the write timeout to 15 seconds. Of course you don't have to change all three at the same time and can just modify whatever subset you want to modify. Let's look at each timeout in more detail.

Connection Timeout

The connection timeout is likely the most interesting timeout. It is the time that lasts from starting the request to a completed TCP handshake with the server. In other words, if Retrofit couldn't establish a connection to the server within the set connection timeout limit, it'll count the request as failed.

For example, increasing the default timeout of ten seconds to a higher value could make sense if you know your users are in conditions with bad Internet connection.

Read Timeout

The read timeout starts to pay attention once the connection is established and then watches how fast (or slow) every byte gets transferred. If the time between two bytes gets larger than the read timeout, it'll count the request as failed. The counter resets after every incoming byte. Thus, if your response has 120 bytes and it takes five seconds between each byte, the read timeout will not be triggered and the response will take ten minutes to be completed.

Again, this is not only limited to the server. Slow read times can be caused by the Internet connection as well.

Write Timeout

Finally, the write timeout is the opposite direction of the read timeout. It checks how fast you can send bytes to the server. Of course, it also gets reset after every successful byte. However, if sending a single byte takes longer than the configured write timeout limit, Retrofit will count the request as failed.


In this tutorial you've learned what kind of network timeout Retrofit offers in conjunction with OkHttp. You've also seen what they actually mean and how you can customize those timeout values.

Do you have further questions on this topic or about Retrofit in general? Just let us know on Twitter @futurestud_io or leave a comment below.

Enjoy coding & make it rock!

Explore the Library

Find interesting tutorials and solutions for your problems.