Out of memory 540 issues

We are using layer0 for relatively big next.js project. Our codebase has grown significantly, same as number of pages in the app. We are seeing increasing number of out-of-memory 540 crashes on prod, which we cannot fix or control. I’ve been digging the reasons for that behaviour, here’s what I’ve found:

  1. Layer0 uses serverless build for next.js(and all other frameworks layer0 supports are built on the same approach). This type of build creates set of pages, which are independent of each other, having all the modules for the app per each page bundle
  2. In ideal serverless world, there should be spawned a worker per page type, to serve only one page type. In the same time, express server should not .listen on some port, instead utilising AWS handler object.
  3. Layer0 seems to have implemented a “hybrid” approach, where serverless build is used, but in the same time there’s a express server which is listening on a port and dynamically requiring the serverless page module.
  4. Such hybrid approach consumes enormous amount of RAM, because next serverless is not built for such usage. Each page module has duplicated dependencies and creates a lot of runtime memory cache which cannot be cleared.

Are there any options to get rid of intermediate express server and use “pure” serverless lambdas to serve the requests?

Thanks.

@arturkin,

Are you using the @layer0/next connector or a custom one for your project? If you are on Layer0 4.13.0+, I would recommend trying the server build as serverless targets have been deprecated in Next.js 12: Next.js | Layer0 Documentation

To answer your question about a pure serverless lambda, we currently aren’t able to separate lambdas per page and dispatch requests accordingly.