I have react webapp to make HTTP calls using fetch API.
Calls go to HTTP API gateway which is attached to a lambda function. Lambda fn is nothing but contains my entire node/express app (backend). Please note I have only one big lambda fn with node/express code.
Currently it works fine but problem comes when I hit certain APIs, certain APIs might take more than 30 seconds. In such case, API gateway shows timeout and sends internal server error
msg to the client.
Yes, I know there are several options to over come timeout issue.
-
Use Websocket API gateway. I don’t know if this is right solution as I have to make changes in React (Front-end) code. Basically connect to socket and then wait for response etc. I don’t want to make such changes in the existing implementation.
-
Use step function: People claim using step fn, 30 seconds timeout issues can be avoided but HOW? I don’t seem to find any right example. Can some pls help to understand this concept how step fn works with API gateway and lambda. Will this even resolve the problem that I face ?
Or pls tell me any other feasible solution if you have.
2
Answers
API Gateway enforces a strict timeout limit of 30 seconds, within which responses must be returned.
To ensure compliance with this constraint, it is essential to streamline your responses and minimize processing times.
Well-designed APIs typically deliver responses within milliseconds to a few seconds, as prolonged response times can indicate suboptimal API practices.
For requests requiring extensive processing or longer execution times, consider decoupling the computational tasks.
Employ an asynchronous processing model where the API immediately returns a success status, while a backend worker or service processes the request separately.
If you have any retrieval operations implement caching mechanisms to expedite data access and diminish response times. Caching frequently requested data can significantly improve performance for GET requests.
If the constraints of API Gateway do not align with your requirements, Consider alternatives like EC2 with a reverse proxy or ELB for flexibility. One such option is to deploy a lightweight EC2 instance configured as a reverse proxy or utilize an Elastic Load Balancer (ELB) to interface with your backend.
Tasks that are too demanding for traditional API models, offload them to a worker-based architecture or leverage AWS Batch and EC2 for more resource-intensive processing.
Hope this helps.
You can try with Application Load Balance, it can interactive with lambda function and set timeout as you wanted