Skip to content

Using a Proxy with Serverless Functions Without Timeouts

Serverless functions have strict time limits, and a proxy adds another hop. This guide explains how to plan time budgets, split jobs, retry safely and keep proxy calls inside the limit.

10 min readUpdated Nov 14, 2026By XPASS Editorial Team

Using a Proxy with Serverless Functions Without Timeouts

Serverless functions are attractive for small, bursty jobs: check a page, call an API, collect a status, then stop. They scale without servers to manage, and you pay for what runs. But they come with a strict rule that traditional servers do not have: every invocation must finish within a fixed maximum time. When you add a proxy to the path, there is one more hop, one more connection to set up, and one more place for delays. The result is often a function that works in testing and times out in production.

This guide explains how to plan the time inside a function, how to configure timeouts and retries, how to reuse connections where the platform allows it, and how to split larger jobs so that no single invocation comes near the limit. It is written for developers who use proxies for lawful work such as regional testing, monitoring of sites that allow it, or quality assurance, and it assumes you respect target-site terms and local law. The exact limits differ by platform and plan, so check your platform's documentation instead of relying on any number in a blog post.

Why serverless and proxies strain each other

Several characteristics of serverless platforms interact badly with proxy traffic.

  • Hard time limit: the platform ends the function when the maximum duration is reached. There is no graceful extension, and unfinished work is lost unless you saved it.
  • Cold starts: the first invocation after idle time must start the runtime and load your code. This takes time before your own logic runs.
  • Short-lived connections: because instances can be created and discarded, the connection to the proxy may need to be established again for many invocations, including the TCP connection, the proxy handshake and TLS to the target.
  • Limited control of the network: you may not control the outbound IP or the region of the function itself, which can add distance between the function, the proxy and the target.
  • Concurrency: bursts of simultaneous invocations can produce many parallel connections at once, which a proxy plan, a target site or your own account may handle differently from steady traffic.

None of this makes the combination a bad idea. It only means you must plan your time as a budget and design for failure.

Think in time budgets

Treat the function's maximum duration as a total budget, then divide it among the steps. A rough breakdown looks like this.

Step What consumes time Planning note
Cold start and setup Runtime start, reading settings, creating the client Keep initialisation light; do it once per instance when possible
Connect to the proxy TCP connection and proxy handshake Set a short connect timeout
Request to the target Target latency, redirects, response size Set a total timeout for each request
Processing Parsing, validating, transforming Keep it small, or move heavy work elsewhere
Saving results Writing to a database, queue or storage Reserve time for this; it is easy to forget
Safety margin Variation in all of the above Leave a clear gap before the platform limit

Say your platform limit is some number of seconds. Set an internal deadline well below it. When the deadline approaches, stop starting new work, save what you have, and return a clean response. A function that ends itself on purpose is far easier to debug than one that is cut off in the middle of a write.

Configure timeouts properly

Most HTTP libraries support several kinds of timeouts. Use them deliberately.

  1. Connect timeout: how long to wait for the connection to the proxy. A few seconds is common. If the proxy is not reachable quickly, waiting longer rarely helps.
  2. Read or response timeout: how long to wait for data once connected. This should reflect the slowest acceptable response.
  3. Total request timeout: an overall cap for the entire request, including connection, redirects and body download. This is the most important one inside a function, because it ensures one slow call cannot consume the whole budget.
  4. Function-level deadline: your own clock that you check between steps. Libraries can enforce per-request limits, but you must enforce the total.

Set each per-call timeout to fit within the remaining budget at the time of the call, not to a fixed value. If only a few seconds remain, a request with a long timeout should not even start.

Also limit redirects. Following a long chain through a proxy multiplies delay, and a loop can consume the entire budget. A small cap on the number of redirects is a low-cost protection. RFC 9110 explains how redirection status codes and the Location header work, which is useful background when you read a chain.

Reuse connections where you can

Opening a new connection for every request is wasteful, especially through a proxy, where each new connection can involve a handshake with the proxy before the request reaches the target.

  • Create the HTTP client outside the function handler so that warm instances can reuse it. A warm instance is one that the platform keeps for a while after an invocation.
  • Use keep-alive so several requests in the same invocation share a connection to the proxy when the protocol and library allow it.
  • Do not assume reuse. Instances may be recycled at any time, so your code must work correctly when the client is created fresh.
  • Keep the global client without secrets that change per request. Load credentials once from the environment and avoid logging them. For a clean pattern, read the guide on managing proxy credentials with environment variables.

Connection reuse helps latency, but it is not a license to send a constant flood of requests. Keep your rate modest and respect the target site's rules.

Retry sensibly

Retries make jobs more resilient, but inside a function they spend the same limited time you are trying to protect.

  • Retry only transient problems: connection timeouts, connection resets, and certain server errors such as 502, 503 or 504. Do not retry authentication failures like 407 or client errors like 404, because the second attempt will fail the same way.
  • Limit attempts. Two or three tries are usually enough for a single call.
  • Use backoff: wait a short time before the first retry and a longer time before the next, with a little random variation so that many invocations do not retry at the same moment.
  • Respect a Retry-After header when a server sends one with a 429 or 503 response. If the wait is longer than your remaining budget, do not wait inside the function. Give the work back to the queue instead.
  • Make operations safe to repeat. Retrying a request that creates something on the other side can cause duplicates. A simple read is safe to repeat; a write needs an idempotency design.

A plain example: a function receives one URL, checks it through a proxy, and stores the status and elapsed time. It has a 30-second internal deadline. The first attempt gets a connect timeout after 5 seconds. It waits one second, tries again with a 10-second total timeout, and succeeds. The function has used about 16 seconds, stores the result and returns. If the second attempt had also failed, the function would store a "failed, will retry" record and put the URL back on the queue, rather than keep trying until the platform ended it.

Split big jobs into small ones

The most reliable way to avoid timeouts is to make each invocation small.

  1. Break the job into units. One unit could be one URL, one small page of results, or a small batch of items that you know finishes well within the budget.
  2. Put the units on a queue, or let a scheduler trigger one function per unit. A coordinator function only creates and enqueues work; it does not do the work itself.
  3. Let each worker function process one unit, save the result, and exit.
  4. Track state outside the function, in a database or storage, so you know which units are done, failed or waiting.
  5. Retry at the unit level. A failed unit goes back to the queue with a counter, and after a limited number of attempts moves to a failure list for a human to look at.
  6. Limit concurrency. Configure the platform or queue to run only as many workers at once as your proxy plan, the target site and your account can sensibly handle.

This design also makes cost easier to predict, because the amount of data each unit transfers is small and measurable. When you plan how much traffic a job will use, the article on calculating traffic from API responses shows how to estimate it from response sizes.

Test regional behaviour inside functions

A function may run in a different region from your desktop, so test inside the real environment. If your job checks what visitors in a region see, confirm the exit country with a diagnostic endpoint from within the function, then run the actual checks. The guide on checking geo-specific redirects with a proxy describes a clean method for recording each hop and comparing regions, and it adapts well to one-unit-per-invocation designs.

Monitoring and logging

You cannot fix what you cannot see. Record the following for each invocation: a run identifier, the unit being processed, the time spent in each step, the result category such as success, connect timeout or proxy authentication failure, and the time left when the function ended. Look at percentiles over time, not just averages. If your slowest invocations are getting close to the limit, you have found a risk before it becomes a timeout. Avoid logging credentials, full proxy URLs or sensitive response content.

Set alerts on trends, such as a rising share of timeouts, rather than on a single failure. A failure on its own is normal on public networks.

Limits and things to watch for

  • Platform limits vary and can change. Always read your platform's current documentation for maximum duration, memory, concurrency and outbound networking, and design with margin.
  • Outbound network rules differ. Some platforms restrict the protocols or ports a function may use, and not every runtime supports every proxy type. Test your exact setup, including whether you use HTTP or SOCKS5 as described in RFC 1928, before relying on it.
  • The function's own region affects latency. Place functions near your proxy or your targets when the platform lets you choose.
  • Large response bodies cost time and traffic. Request only what you need, set size limits, and avoid downloading content you will not use.
  • A passing test in your development environment may not reflect production. Cold starts, shared resources and concurrency can only be seen under realistic conditions.
  • Use proxies only for lawful work, follow site terms and robots rules, and keep request rates polite. A queue and concurrency limits are helpful for this too.

Summary

Serverless functions and proxies work well together when you plan time as a budget. Set an internal deadline below the platform limit, give every call a connect timeout and a total timeout that fit the remaining time, cap redirects, and reuse clients on warm instances without relying on it. Retry only transient errors, with a few attempts, backoff and a respect for Retry-After, and make repeated operations safe. Split large jobs into small units driven by a queue, store state outside the function, limit concurrency, and monitor timing so you see trouble before it becomes a timeout. Stay within site terms and local law. When you are ready to choose a proxy for work like this, see what is on offer on the Proxy page.

Frequently asked questions

Why do proxy requests time out more often inside serverless functions?

A serverless function has a fixed maximum run time, and a proxy adds a network hop plus connection setup. Cold starts, slow target sites and retries all consume the same limited budget, so the total can pass the limit even when each step looks reasonable.

How should I set timeouts for proxy calls in a function?

Give the whole function a time budget below the platform limit, then set a connect timeout and a total request timeout per call that fit inside it. Leave a margin to return a clean response or save partial results before the platform cuts the function off.

Should I retry failed proxy requests inside the same function?

Only a small number of times and only when the remaining time budget allows it. Use short waits that increase between attempts, retry only on errors that are likely temporary, and hand longer work to a queue instead of looping until the function ends.

What is the best way to handle large jobs?

Split them into small units, such as one URL or one small batch per invocation, and drive them from a queue or scheduler. Each unit finishes well inside the limit, failures are isolated, and you can retry just the unit that failed.

About the author

Bob Kanokphon Thongdee, founder of XPASS VPN

Bob Kanokphon Thongdee

  • Founder of XPASS VPN
  • Co-founder of eSIMonline.asia
  • More than 7 years in online marketing
  • Founder of Cojourney Visa and Cojourney Travel

Need a proxy for this?

XPASS Proxy sells residential proxy traffic by the gigabyte, with the price shown before you pay.

See proxy plans

Keep reading

Proxy Guides

Managing Proxy Credentials with Environment Variables

Hardcoded proxy passwords end up in Git history, logs and screenshots. This guide shows a safer pattern using environment variables, with naming, loading, masking and rotation.

9 min read

Proxy Guides

How to Calculate Proxy Traffic from API Responses in Data Collection Jobs

Proxy traffic is measured in bytes, not requests. This guide shows how to measure API responses, add overhead and retries, and build a realistic traffic estimate.

9 min read

Proxy Guides

How to Check Geo-Specific Redirects with a Proxy

Many sites send visitors to different pages by country or language. This guide shows how to use a proxy to observe those redirects step by step and record what you find.

10 min read