Search code examples
javascriptnode.jsexpressload-balancingmulter

Express upload a file on load balancing?


Recently i made an app in node js with a load balancing feature. I made a server just for the db itself and other for managing request. The problem is, in the app you can upload a file with multer and in express you upload the file to that server. Its a express static.

For example i have 4 server, one for the db, 2 for the apps, and 1 for the load balancer. When the loadbalancer request to app-1 server, the file upload to app-1 server not to db server. So when i try to access the file from app-2 server the file didn't exist.

Is there any ways to solve this problem? Or better ways to use the load balancer? because im new with load balancer. Thanks


Solution

  • Where are you storing the file that has been uploaded through app-1? You can for example store it in a bucket and keep the reference in your SQL DB. So, when app-2 tries to access the file, it directly asks the DB, which is shared between your apps.

    To answer your question, you have 2 solutions:

    • maintaining a session persistence with your load balancer, so a user that requests to app-1 keeps requesting to app-1 until his session expires.
    • having a stateless backend design, meaning that you don't need sessions, so any user can send a request to any app instances you have, it will behave the same.

    I would go with solution 2, it's easier after you get your head around the stateless concept.

    Having an external bucket and not relying on your app internal memory and storage system is a good way to implement a stateless architecture.