Search code examples
node.jsreactjsmongodbarchitecturemern

Architecture question - The need for a separate backend running Mongo, Express & Node in MERN or MEVN project


Are my assumptions correct here, is there a different need for a separate backend, or can a monolithic (hopefully simpler) solution accomplish the same functionality perhaps at the cost of not scaling as well?

Looking at existing MERN or MEVN solutions, they always seem to involve two node processes where the front end process is running the client framework and backend processes DB requests using Node, Express & Mongo. This seems like a good solution performance wise when balancing across at least two servers. For my solution, where performance is not the issue, I've wondered what is the need for a separate backend. Why not just use try/catch with async/await in the front end to get to the DB data instead of an api call to a separate backend. Then once I started trying to design my more monolithic solution, I realized there is a problem that the separate backend actually solves. Trying to avoid DB concurrency issues, I realized the separate backend solution actually lowers the need for logical transactions since node is single threaded and only processes one request/response at a time.


Solution

  • So if I understand correctly, you're asking why do you need a backend that talks to the DB (and potential other backend services) instead of calling the DB from the front-end directly.

    The answer is the following:

    • If you run this application on your local machine only (front-end and DB), then I can see why you wouldn't need a backend.
    • If this application is exposed in Internet, then your DB will hijacked in a matter of minutes maybe.

    Security is the main concern here, everything that runs on client-side (JS stuff) can be seen pretty easily by the user -- this includes endpoints, passwords, etc. Not to mention that your business logic is fully exposed to the attackers.

    For that reason, the backend plays a very important role in protecting the access to the DB, rate-limiting and resource usage capping, and many others.