Back to Blog
Error-Code Fixes

Twilio Webhook Returning 502 504: How to Fix Timeouts

Twilio gives your webhook 15 seconds to respond. If it times out you get 502 or 504 errors. Here is how to fix slow webhooks before they break your app.

DA
Danial A
Senior Twilio Consultant, Telphi Consulting
June 21, 2026
6 min read
Twilio
Debugging
Troubleshooting
Twilio Webhook Returning 502 504: How to Fix Timeouts

Twilio requires your webhook endpoint to return an HTTP response within 15 seconds of receiving the request: if your server takes longer than 15 seconds to respond, Twilio logs an 11200 error (HTTP Retrieval Failure) in the Debugger and the call or message is handled as if no TwiML was returned, typically resulting in an error message being played to the caller or the message being dropped. Webhook timeouts are almost always caused by slow synchronous operations in the webhook handler: database queries, external API calls, or computation that should be deferred to a background process. The fix is to make the webhook handler return a valid TwiML response immediately and perform slow operations asynchronously.

Why Twilio Requires Fast Webhook Responses

Twilio's 15-second webhook timeout exists because the Twilio platform holds an active call or message session open while waiting for your webhook to respond: if the timeout were longer, a single slow server would tie up Twilio's internal resources for an extended period per request, degrading service for all customers. For voice calls, the 15-second timeout is particularly critical because the caller is listening to silence during the webhook processing time: any response that takes more than 2 to 3 seconds produces a perceptible delay that degrades the call experience even if the webhook eventually returns within 15 seconds. Twilio's Debugger logs the exact response time for each webhook call in the event detail view under the Request Duration field: response times above 3,000 milliseconds are shown as warnings, and any response that did not arrive within 15,000 milliseconds is shown as a timeout error with the 11200 error code. The 15-second limit applies to the time from when Twilio sends the HTTP request to when it receives the first byte of the HTTP response: this means your server must have the response headers sent within 15 seconds, not just the body.

How to Diagnose a Slow Webhook

Find the webhook timeout in the Twilio Debugger under Monitor, then Debugger: the error entry will show 11200 (HTTP Retrieval Failure) or a long Request Duration value (close to 15,000 milliseconds), and the detail view will show the request Twilio sent to your server and confirm the response was either slow or never received. Add timing instrumentation to your webhook handler by logging the elapsed time at each major processing step (database query start and end, external API call start and end, TwiML generation start and end): the step with the largest elapsed time is the bottleneck that needs to be moved to a background process. Test your webhook endpoint's response time directly using curl with the timing flag (curl -w @curl-format.txt -o /dev/null -s https://yourwebhook.example.com/voice) by creating a curl-format.txt file that prints time_total: the result gives you the server response time without Twilio's network overhead, helping you isolate whether the slowness is in your server or in the network path between Twilio and your server. If your server responds quickly in direct tests but slowly when Twilio calls it, the issue may be in the network path (for example, a cold start if using serverless functions, or a DNS resolution delay): check your serverless function's cold start time and implement a keep-warm ping to prevent cold starts during production traffic.

How to Fix Webhook Timeouts

Restructure your webhook handler to follow this pattern: immediately generate a minimal TwiML response (a Say verb playing a brief hold message, or a Pause verb), return that response within 200 milliseconds, and then perform all slow operations (database lookups, CRM queries, AI inference, external API calls) asynchronously in a background job that updates the call flow using the Twilio REST API's Calls resource to redirect the call to new TwiML when ready. For database queries that are genuinely required to generate the correct TwiML, optimize them with indexes, implement caching (Redis or Memcached) for data that does not change between webhook calls, and ensure your database connection pool is sized correctly so webhook handlers do not wait for a connection. Move any external API calls in your webhook handler to a pre-computation layer: for example, if you need a customer's name to personalize a voice greeting, cache customer records in your application's memory or Redis at login time rather than fetching them from the CRM on each webhook call. For serverless deployments (AWS Lambda, Google Cloud Functions, Azure Functions), implement a keep-warm strategy by scheduling a lightweight ping every 5 minutes to prevent cold starts: a Lambda function that takes 3 seconds to cold-start plus 1 second for processing will hit the Twilio timeout in low-traffic environments where cold starts are frequent.

Preventing Timeouts in Production

Set a server-side timeout on all outbound HTTP calls made from your webhook handler that is shorter than Twilio's 15-second limit: use a 5-second timeout for any external API call made synchronously within the handler, which ensures your handler fails fast and returns a fallback TwiML response rather than waiting past the Twilio timeout limit. Implement a fallback TwiML response for every webhook handler that fires when any operation exceeds its timeout: for voice webhooks, the fallback TwiML should play an apology message and redirect to a human agent; for message webhooks, the fallback should log the failure and queue the message for a retry. Monitor your webhook response times using your server's application performance monitoring tool (New Relic, Datadog, or similar) and set an alert when the 95th percentile response time exceeds 3 seconds: catching slow webhooks before they reach 15 seconds gives you time to investigate and fix the bottleneck before it causes actual Twilio timeouts. Conduct load testing on your webhook endpoint at the peak requests-per-second you expect during your busiest traffic period: a webhook that responds in 1 second at low load may take 12 seconds at peak load due to database connection pool exhaustion or thread pool saturation, and load testing reveals this before production does.

Conclusion

Webhook timeouts are always caused by slow synchronous operations in the handler: the fix is to return a TwiML response immediately and defer all slow work to background processes, keeping the webhook handler fast regardless of load. If your Twilio webhooks are timing out in production, contact our team and we will diagnose the bottleneck and implement the fix within the hour.

Share this article:
0 views

Ready to Transform Your Business Communications?

Get a free consultation with our VoIP experts and discover how we can help you save costs, improve efficiency, and scale your business.

Comments (0)

Join the discussion and share your thoughts (AI-moderated for quality)

Protected by AI moderation

Be the first to comment

No comments yet. Share your thoughts below.