How to set connection timeout with OkHttp
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
OkHttp lets you control several different timeout phases, and connection timeout is only one of them. If the goal is specifically "how long should the client wait while establishing the TCP connection," use connectTimeout. If the goal is broader request control, you may also need readTimeout, writeTimeout, or even callTimeout.
Set The Connection Timeout On The Client Builder
This tells OkHttp how long to wait while connecting to the remote host. If the connection cannot be established in that period, the call fails with a timeout-related IOException.
Other Timeouts Are Different
It is common to confuse the timeout types.
- '
connectTimeoutlimits connection establishment' - '
readTimeoutlimits inactivity while reading the response' - '
writeTimeoutlimits inactivity while sending the request body' - '
callTimeoutlimits the entire call from start to finish'
A more complete client often looks like this:
That is often a better production configuration than tuning connection timeout alone.
Per-Call Customization Uses newBuilder()
If most requests should use one timeout but a specific call needs another, clone the base client.
This is preferable to mutating shared global client state.
A Minimal Request Example
If the remote host is slow to accept the connection, connectTimeout is the setting that governs how long this waits.
Pick Sensible Values
There is no universal correct timeout. Shorter values improve responsiveness on failures but can hurt users on slower or high-latency networks. Longer values reduce false timeouts but make failures feel slower.
The right value depends on:
- mobile versus server environment
- internal service call versus public internet call
- whether retries exist higher up the stack
- how much latency the user can tolerate
That is why timeout tuning is part of system behavior, not just syntax.
Android And Shared Clients
In Android apps, it is common to create one shared OkHttpClient and reuse it. Reusing the client is good because OkHttp manages connection pooling and internal resources efficiently. Setting the timeout once on a shared client is therefore the usual pattern.
Handling Timeouts Cleanly
Timeouts are normal failure modes, not exceptional in the "should never happen" sense. Your code should surface them clearly and decide whether to retry, show an error, or degrade gracefully.
For user-facing apps, a timeout should normally produce a message or retry path rather than a silent failure.
Common Pitfalls
The biggest mistake is changing connectTimeout when the real problem is a slow response body, which is governed by readTimeout instead. Another is using one overly aggressive timeout value for every environment. Developers also sometimes create a new OkHttpClient for every request, which defeats connection pooling benefits. Finally, catching generic Exception without distinguishing network timeout failures makes diagnostics harder than they need to be.
Summary
- Use
connectTimeoutto control how long OkHttp waits to establish a connection. - Do not confuse it with read, write, or full-call timeouts.
- Configure timeouts on
OkHttpClient.Builder. - Use
newBuilder()when one request class needs a different timeout profile. - Pick timeout values based on network context and user experience, not just habit.

