Bypassing AWS API gateway timeout
An API gateway is an API management tool that sits between a client and a collection of backend services, with the help API gateway you can trigger your AWS lambda, but here is a catch lambda has a max timeout of 15min so you can do processing for 15mins only but in most of the cases this is not the problem, the real problem is with API gateway where the timeout is only 30sec, so if your lambda is taking more than 30sec to process you won’t get any response back from API gateway.
Problem
Similar kind of the problem I face while developing an image processing lambda, I was given a task to create an image processing lambda that can process a large number of image files, following were the main task that i want my lambda to perform.
- Take all the URLs and create a set of those links.
- Download each image from the given URL and keep a copy in S3.
- Process each URL and create 5 different copies using the sharp module.
- Do extra processing like remove bg if asked by the user.
For 10–20 images it was working fine with API gateway but as the number of images increased and that too higher resolution images it was giving timeout.
Solution
In order to optimize the above approach, I did 2 things.
Increasing space complexity
In the computer science world, there is always a time-space trade-off by increasing space complexity we can save our processing time, the same thing I used here and after processing the URL I saved processed URLs to the database and next time if there is any request with the same URL we can reuse the previously saved S3 URL, this also helped us in reducing the number of remove bg request which was a 3rd party paid service.
By introducing web-sockets
WebSocket is a bidirectional communication protocol that can send the data from the client to the server or from the server to the client.
Sockets don’t have any timeout so we took advantage of this point and created a socket connection between our App server and client, now whenever the client sends some processing request to lambda response for it is first sent to the App server and from the App server, we make a socket call and send a response to our frontend.
Conclusion
Sockets are not only helpful where we want a continuous connection between our server and client, but can also help us in such situations where we have a long processing time.
I hope you understood the concept of how to use web sockets with AWS lambda if you have any suggestions you can drop a comment. See you soon and happy coding until then! ❤