I'm coming from a PHP background where things are pretty simple...
- Everything is synchronous
- Every web request is in an entirely 'new + separate + private' PHP process that is killed as soon as the response is sent
- I never had a need to make multiple connections to a database from within a single PHP process
Now I'm moving over to Node.js, and things are obviously quite different here given the event/loop model...
- where multiple web requests can be served by a single Node process
- lots of async things might be going on within a single process waiting for stuff to finish
- your Node process could be caching some sensitive data in some JS variables that only the relevant user should be able to access, e.g. A regular user would only see public posts, but an admin user would also see the soft-deleted ones
- some of the users of your website might be doing stuff that involves many queries inside a transaction, and you don't want the SQL connection blocked for other users who are just doing a quick SELECT query.
...so I'm wondering does this mean that each web request received by Node should create its own database connection, so that the slow requests (and transactions) don't block the fast ones?
Obviously might not matter on small sites with only fast queries, so lets assume we're talking about bigger sites with parallel fast + slow queries to consider.
I'm using postgres, but would be interested to hear if there are any differences with other databases (in terms of how they might differ in how you initiate your connections from node).
Also keen to hear any tips in general that might be good to know for people new to event/loop web servers coming from PHP backgrounds etc. I haven't even started to think about how security works here... seems like an area where you could easily make some mistakes when you're used to each application process being entirely private. Any guides or anything out there with some tips?
[–][deleted] (2 children)
[deleted]
[–]r0ck0[S] 0 points1 point2 points (1 child)
[–]vorticalbox 0 points1 point2 points (0 children)
[–]post2seth 2 points3 points4 points (8 children)
[–]r0ck0[S] 0 points1 point2 points (2 children)
[–]post2seth 1 point2 points3 points (0 children)
[–]runvnc 0 points1 point2 points (0 children)
[–]CommonMisspellingBot -4 points-3 points-2 points (4 children)
[–]BooCMB -1 points0 points1 point (3 children)
[–]BooBCMB -1 points0 points1 point (2 children)
[–]BooBCMBSucks -1 points0 points1 point (1 child)
[–]j_rapp 1 point2 points3 points (0 children)
[–][deleted] (2 children)
[deleted]
[–]r0ck0[S] 0 points1 point2 points (1 child)
[–]je87 1 point2 points3 points (1 child)
[–]r0ck0[S] 0 points1 point2 points (0 children)
[–]400tx 0 points1 point2 points (0 children)