Are there downside to make all my backend code in one serverless Netlify function?

I want to move my already developed backend into serverless functions. What are the best practice in this case?

Depends what your backend code is…

Some questions you need to ask yourself:

  1. Can your backend code finish executing within 10 seconds (or 26 if you get the limit raised)?
  2. Is the 1 GB RAM limit enough for it?
  3. Can you write your backend only in JavaScript (or Go)?
  4. Can you work with your backend only sending responses in JSON?

If all of this is yes, then you can make this work. But depending on the current setup, the migration might not be easy.

1 Like

Hello hrishikesh and thanks for your kind response.
Sorry I was too vague.

My backend answer YES to all your 4 points. It is an API that respond only in JSON.

I just need to test it but:

  1. It should finish every call in less than 10 seconds.
  2. 1GB should be enough.
  3. It is written in Typescript and compiled in Nodejs Javascript.
  4. It answer only in JSON.

My question now is:

Can be a problem if I fit all my backend CRUD inside only 1 function called “/.netlify/funcitons/api/” and handle the routes inside this function? There will be downside, maybe in performance?

Thank you!

Yes, you can do that and yes, it will have some downside in performance. In most cases more code = more execution time especially is you’re going to use conditions to route.

When you have it all in separate functions, the server has to execute just that one piece of code, but if you handle the routes, I’m assuming you’ll be using conditions. If yes, then the server will have to check for each condition and execute accordingly. This would be a major problem for the last condition in a series as the server will have to check for each of the previous one.

So in short, I think, it’s better to keep the code and complexities at a minimum in a serverless function.

I see. Thanks for the clarification.

I want to know about it. How can handle it.

What exactly do you want to achieve @Smith?