504 - Gateway timeout #
- As the http message description says if the Gateway doesn’t receive the response after a stipulated time, the http-request is timed out and it responds the same to the caller.
- If the the service is behind gateway/proxy-service it leads to 504-Gateway timeout

- Use-case
- Assume the default gateway timeout is set to 2 seconds.
- Now, consider a scenario where the service is taking longer than 2 seconds to process request in which case the gateway server is timed out and the api-client receives time out.
- Note that the app-server may still be processing the request, but it’s futile because the api-client already received 504 timeout.
- How to fix 504?
- Improve API Response Times
- Example: If the Gateway timeout is 2 Seconds, ensure the specific endpoint responds <= 2 seconds.
- Note: In the above image, the gateway component is not necessarily always an AzureGateway/KONG/Gravitee etc but could be a proxy/intermediate-service that could respond with 504 status code when the http-client is timed out.
503 - Service Unavailable #
- Use-case
- When all the application-server worker threads are occupied and no new threads are available to serve any new requests then the service rejects all the new incoming requests with 503-service unavailable.
- How to fix 503?
- Ensure that the app-server threads are released quickly and capable to handle the incoming load by scaling up horizontally with instances.
- For SpringBoot Apps
- Use DeferredResult to free up application worker threads
- Break down the complex request processing flow and assign the tasks to threads in thread pools.
- Use @Async in spring boot having dedicated thread-pools for specific category of tasks say “db-inserts” pool having threads that will do all database inserts.