Connection Pooling in PostgresSQL with NodeJS (Performance Numbers)

แชร์
ฝัง
  • เผยแพร่เมื่อ 8 ก.ย. 2024

ความคิดเห็น • 84

  • @hnasr
    @hnasr  3 ปีที่แล้ว +7

    If you enjoyed this video consider checking out my 13+ hours Introduction to Database Engineering course here! for more exclusive content
    husseinnasser.com/courses

  • @objectObject212
    @objectObject212 4 ปีที่แล้ว +8

    God bless you
    Hussein! :) Another great video from you.

  • @ileska7828
    @ileska7828 17 วันที่ผ่านมา

    Holy cow, this channel is insanely good

  • @prasangsinghal7887
    @prasangsinghal7887 2 ปีที่แล้ว +1

    Wow! I just found the hidden gem(channel) on TH-cam. Can't decide what to watch first. 😅

  • @bogdanRules
    @bogdanRules 3 ปีที่แล้ว +5

    the most interesting part was at the end ( just kidding ) . But I really want to know how you can reserve a client and using it for multiple queries and then return it to the pool, that would really suit my project. Awesome tutorial BTW!

  • @mahmoudadel6965
    @mahmoudadel6965 2 ปีที่แล้ว +1

    Amazing
    I am new with postgres in node and was confused about pool and client
    This video helped me a lot to understand
    Thanks Hussein,

  • @HardcoreFixation
    @HardcoreFixation 2 ปีที่แล้ว +3

    Great video, I really like your teaching style - thank you for this!

  • @Paul-qn7qv
    @Paul-qn7qv 3 ปีที่แล้ว +2

    Thank you for the performance test!

  • @TheGodSaw
    @TheGodSaw 2 ปีที่แล้ว +1

    Really great video. I used this as the basis for not using pooling for my particular data processing application. Thanks!:D

  • @kundanranjan
    @kundanranjan 4 ปีที่แล้ว +4

    Hi Hussein, thanks for your indepth videos. I want to suggest topic discussing Node js architecture like how it works as single threaded event driven runtime and how it differs from javascript running in client (browser).
    Thanks in Advance 😊

    • @hnasr
      @hnasr  4 ปีที่แล้ว +1

      I discussed it a little bit here
      th-cam.com/video/0vFgKr5bjWI/w-d-xo.html
      th-cam.com/video/JhpUch6lWMw/w-d-xo.html

  • @Dsouza10082
    @Dsouza10082 ปีที่แล้ว

    Amazing !! Thank you, direct to the point !

  • @Krishna-zh3pw
    @Krishna-zh3pw 4 ปีที่แล้ว +3

    Thanks for the video..It was really informative.

    • @hnasr
      @hnasr  4 ปีที่แล้ว +1

      Krishna thank you Krishna 🙏

  • @EddyVinck
    @EddyVinck 4 หลายเดือนก่อน

    Very helpful video! Thanks

  • @DavidNBooth
    @DavidNBooth 5 หลายเดือนก่อน

    Great video, this was super helpful!

  • @noktilux4052
    @noktilux4052 3 ปีที่แล้ว

    Excellent tutorial. This really helped out. Thank you.

  • @user-td1nr8hc7x
    @user-td1nr8hc7x ปีที่แล้ว

    This guy is legend!!

  • @mariocalcetin7602
    @mariocalcetin7602 4 ปีที่แล้ว +2

    Great Video!!!!

  • @satyajeetkumarjha1482
    @satyajeetkumarjha1482 3 ปีที่แล้ว +1

    awesome dude .

  • @valikonen
    @valikonen 3 ปีที่แล้ว +1

    I like how you stress the app

  • @yayayunicornplayz
    @yayayunicornplayz 2 ปีที่แล้ว

    Very good job sir. Thank you

  • @SuperMBARutgers2013
    @SuperMBARutgers2013 ปีที่แล้ว

    Solid

  • @user-ci9fr1uq7g
    @user-ci9fr1uq7g 9 หลายเดือนก่อน

    Great tutorial.

  • @riz_ahmeds
    @riz_ahmeds 2 หลายเดือนก่อน

    Nice explanation, specially with fetchAPI.
    Thanks @hussein

  • @nicetomeetugaming7024
    @nicetomeetugaming7024 2 ปีที่แล้ว

    This video was superb sir!

  • @nimitsavant3127
    @nimitsavant3127 2 ปีที่แล้ว

    That was amazing thank you!

  • @sumitdalal343
    @sumitdalal343 4 ปีที่แล้ว +2

    awsm.. thanks!!

  • @yamtaztiik
    @yamtaztiik 3 ปีที่แล้ว +2

    Hussein, could you please make a video about the "MIME errors" we get sometimes when we try to use the 'pg' require. Ty

  • @fazlulmohideen8783
    @fazlulmohideen8783 3 ปีที่แล้ว +1

    If you could show the DB show processlist; (something similar in PostgresSQL) also would be great.

  • @heliribeiro1027
    @heliribeiro1027 3 ปีที่แล้ว

    awesome man! Thanks.

  • @tdias25
    @tdias25 3 ปีที่แล้ว +1

    Excellent video, Hussein!
    Just one question, what do you mean by "stateless query"? is that related to transactions somehow?

    • @vishnukrishnathu4918
      @vishnukrishnathu4918 3 ปีที่แล้ว

      th-cam.com/video/nFPzI_Qg3FU/w-d-xo.html

    • @hnasr
      @hnasr  2 ปีที่แล้ว +1

      Thanks @vishnu for sharing
      Yes a stateless query is a query that carries all what the backend needs within it and doesn’t require anything to be in the server. You can restart the server and execute a stateless query and it should give you the correct result

  • @sanjayabc12345
    @sanjayabc12345 4 ปีที่แล้ว +1

    Thanks

  • @hendrasanusi7994
    @hendrasanusi7994 3 ปีที่แล้ว

    Ty for great videos, but is pool is better and client? What is different pool and client connection? And When we decide to choose one of it?

  • @vizardesign
    @vizardesign 4 ปีที่แล้ว +3

    Hussein, can you make same as this video you did but using NodeJS and MariaDB with use of JSON functions and operators it provides. Official Docs for this is terrible. Only you can do proper explanation for these things :P

    • @bobDotJS
      @bobDotJS 4 ปีที่แล้ว

      I'm having the same problem. The issue is that the client sends the database information over to the server. I need multiple different companies that all have their own databases to be able to use a single server without risking security problems. I've got it working right now but it involves creating a new connection before each query. It's very expensive with GCP. I'm trying to figure out pulling but what I don't understand is how the server knows which pool to connect the person to. I've been stuck on trying to solve this problem for about 3 months now.

  • @sagartyagi2450
    @sagartyagi2450 3 ปีที่แล้ว +2

    Awesome tutorial, just one doubt: What happens if there are multiple WRITE requests, and pool allocates one thread to each of the request and they start executing parallely? So how the Atomicity gets handled in that case? Is it DB which handles Atomicity?

    • @sagartyagi2450
      @sagartyagi2450 3 ปีที่แล้ว

      Got the answer in this one : th-cam.com/video/_95dCYv2Xv4/w-d-xo.html , thanks a lot.

  • @danangjehan9733
    @danangjehan9733 ปีที่แล้ว

    so when we using Pool instead of "client", we don't need to close the connection like ""client.end()" ???, or its already auto close connection when we using "pool". ??

  • @mdjahidulislam9205
    @mdjahidulislam9205 3 ปีที่แล้ว

    You take love

  • @wahoobeans
    @wahoobeans 6 หลายเดือนก่อน

    if you set the idleTimoutMillis to 0, when does the pool connection resource die? When the application shuts down?

  • @ItsGonnaRaiN143
    @ItsGonnaRaiN143 ปีที่แล้ว

    Cool, so .. what should be the best number of connections in a pool?

  • @TheAmeer3881
    @TheAmeer3881 4 ปีที่แล้ว +1

    You just earned yourself a s-s-subscribe. Boo yeaaahuhs 💞♥️😛😤🥺

    • @hnasr
      @hnasr  4 ปีที่แล้ว +1

      Yohooo thank you 🙏

  • @CarbonsHDTuts
    @CarbonsHDTuts 2 ปีที่แล้ว

    Thanks for this video bro. I’m new to data engineering and I’m using AWS redshift and Lambda to make an ETL. Would this connection pooling apply as best practice for my scenario too? The Lambda activates every night at 9pm and process and inserts values from a data frame into redshift tables. Appreciate any response bro. I’ve tried two connection methods; 1.) having a connection established and closed for every query, and 2.) connection established once outside the lambda handler (but still doesn’t use the pool method you showed)

  • @milon27
    @milon27 2 ปีที่แล้ว

    hi, i have an express mysql application running using pm2 . i am using connection pool with limit 10 connection. but after 3-4 days it stop working and require a pm2 restart.
    how to solve this?

  • @yashshuklaphotography
    @yashshuklaphotography ปีที่แล้ว

    Can we apply this pooling approach in warm state of aws Lambda.. what will be the bottleneck scenario in that case?

  • @wahoobeans
    @wahoobeans 6 หลายเดือนก่อน

    Secondly, a silly example and question.
    Is it the same if I open a database connection and close it versus configuring a pool of 1 resource and requesting a dedicated client and releasing the client per query?

    • @vishnukaushik1716
      @vishnukaushik1716 2 หลายเดือนก่อน

      I don't think so. Because in the first case, you would have to open DB connection for each and every API request. But in the second case, the connection is opened only once and used multiple times

  • @MSUdhayaRaj
    @MSUdhayaRaj 4 ปีที่แล้ว +2

    That was amazing.Speed of api response is high.But sometimes i am getting"The pool is probably full".How to fix it?.

    • @hnasr
      @hnasr  4 ปีที่แล้ว

      Yes response time is fast because of the eager loading instead of lazy loading. For your question Two things, make sure you are not leaking your connections and correctly returning them to the pool when your done. If you are absolutely sure you are not leaking then you may increase the max clients in your pool. 20 should be ALOT and it could serve alot of requests.

    • @MSUdhayaRaj
      @MSUdhayaRaj 4 ปีที่แล้ว

      @@hnasr i have an api,in that api i am using knex npm to get the connection.Api had multiple quries select,update, insert and delete,multiple tables are involved.when i gave the frequent request to an api getting connection pool full error.i increased pool size also.sometime table loked.

  • @IkraamDev
    @IkraamDev 3 ปีที่แล้ว

    I'm new to this. For some reason, when I put no password in the new Pool({})/new Client({}), it still connects to the database. How do I password protect my database?

  • @edwardteach2
    @edwardteach2 2 ปีที่แล้ว

    Love the hands on and the ability to see it in action in real time without reading from the holy bibles (official documentations)

  • @davidjiang7929
    @davidjiang7929 3 ปีที่แล้ว

    Is 'pg' a better library to use than 'pg-promise'?

  • @manikantanayar3257
    @manikantanayar3257 4 ปีที่แล้ว

    Hello Hussein Sir, This is Mani from India. We are planning to build a Social media app as our product without a chat functionality as of now!. Expecting bit many users as we have a sound sales channel, so in a note of scalability, what database we can go with? If we are going to use Node.js as our server-side language. If you can give a suggestion, We would be extremely grateful. Thanks in advance

  • @Meleeman011
    @Meleeman011 3 ปีที่แล้ว

    why postgresql for node and not mysql though?

  • @RahulKashyap-yv5ox
    @RahulKashyap-yv5ox 3 หลายเดือนก่อน

    Plz explain connection pooling in django

  • @SameedUsmani47
    @SameedUsmani47 3 ปีที่แล้ว

    Can we do Connection pooling for DynamoDB ?

  • @aniketmore7018
    @aniketmore7018 3 ปีที่แล้ว

    @Hussein Nasser Is the connection pool needed when NodeJS is supposedly Single Threaded ? How does this actually help in increasing performance ?

    • @simonlammes6257
      @simonlammes6257 2 ปีที่แล้ว

      Only the execution of your JavaScript is single threaded. Network Requests and other IO are handed off to separate threads so that your main thread can continue working and does not have to wait for those time consuming tasks. Therefore, the main thread might be concurrently waiting on multiple network requests. The concept of the EventLoop will give you more details.

  • @tariqmalhi9851
    @tariqmalhi9851 4 ปีที่แล้ว

    Can you give example with Oracle & php

  • @jugsma6676
    @jugsma6676 ปีที่แล้ว

    Can you share us how to use pgBouncer ?

  • @fxstreamer238
    @fxstreamer238 4 ปีที่แล้ว +1

    in the documentation they use multi query in which you have to release the clients in the pool or write done() after the query. whats the difference between that and your method which is supposed to be "single" query. I dont get it , with your method if you want to have multiple different queries cant you use multiple functions to call for the queries from the pool?

    • @fxstreamer238
      @fxstreamer238 4 ปีที่แล้ว

      const { Pool } = require('pg')
      const pool = new Pool()
      ;(async () => {
      const client = await pool.connect()
      try {
      const res = await client.query('SELECT * FROM users WHERE id = $1', [1])
      console.log(res.rows[0])
      } finally {
      // Make sure to release the client before any error handling,
      // just in case the error handling itself throws an error.
      client.release()
      }
      })().catch(err => console.log(err.stack))

    • @hnasr
      @hnasr  4 ปีที่แล้ว +1

      thanks, great question!
      It really depends on what you are trying to do. When you say multiple queries Do you mean multiple queries in the same transaction? Of multiple independent transactions with a single query each?
      Let us assume you want to do multiple queries that is in the same transaction, if you want to do that you have to do this in the same tcp socket (client object) to do that you need to “lease” a client object from the pool, execute your queries with that client object then YES release the client back to the pool With the done () method you specified. So the pool can use it again for other stuff.
      However if you dont really want to do transactions and you want to execute a single query then you dont have to lease a client from the pool, you can ask the pool to execute your query and it will pick up any client from the pool and execute your query and that is what i was using. This approach is better if you are doing a single stateless queries that are not in the same transaction because the pool will take care of the lease and release of clients for you. However it is not the way to go if you want to execute multiple queries in the same transaction.
      With first approach you will have to make sure to release the client after you are done. If you forget to do so, then the client is leaked.
      The second approach the pool manages the client for you
      Hope that helps!

    • @fxstreamer238
      @fxstreamer238 4 ปีที่แล้ว

      looks like it says that i have to connect to the pool each time (is it the same as connecting to database each time?) for each get request ? or just executes the unknown function once. Im not sure

    • @fxstreamer238
      @fxstreamer238 4 ปีที่แล้ว

      @@hnasr Thanks I guess by transaction you mean one single request ? for example a client sends a get request and in that request there will be multiple queries right? I think that makes sense that the client should be lease and released. But is the pool.connect() as expensive as the old school client.connect() ?

    • @optimiserlenergie1094
      @optimiserlenergie1094 4 ปีที่แล้ว

      FxStreamer search for SQL transaction : a set of queries that you can confirm(commit) if everything went ok or discard (rollback) if something went ko

  • @bobDotJS
    @bobDotJS 4 ปีที่แล้ว

    If you have multiple different companies which I'll have different database information to the API with each call, how do you make sure that the correct pool connection is returned to the correct end user?

    • @hnasr
      @hnasr  4 ปีที่แล้ว

      I am going to assume your API will have some sort of input which we can key off and get to the desired database

    • @bobDotJS
      @bobDotJS 3 ปีที่แล้ว

      @@hnasr That's what I'm trying to figure out in a sense. Here's the situation. My client (a VueJS Front End) sends over an object
      dbObject: { host, user, pass, schema }
      to the server. The server uses that information to connect to the DB and run a query, then return the results.
      When the user first logs in, the server runs a function named connectToDBase(host, user, pass, schema) - that creates the initial connection and pool. Then every time the user needs to get more data, a new connection is created by the client sending the same dbObject: { host, user, pass, schema } back to the server and repeating the process.
      Instead of connecting to the already existing pool, it creates a new one.
      I have 30 different people with 30 different SQL databases using the service. The database info needs to go to the server with each request. It seems that the pool isn't identified by default. I'm trying to figure out how to make sure they keep connecting to the same pool.

  • @JhonZ77
    @JhonZ77 4 ปีที่แล้ว

    Hello Hussein I hope you're well. I have a problem, I'm using docker-compose to spin up a postgres container and I have a backend app written in type script using NestJS as backen framework, all was working well until I changed a statement (I had to drop a table column); from so, I'm getting an error like 'ERROR: column r.cdstatus does not exist at character 75'. Summing up, postgres is executing the old statement.
    Do you know what I can do? I've tried deleting docker container and the docker volume attached to pgdata, but it is not working.

    • @hnasr
      @hnasr  4 ปีที่แล้ว

      Jhon Zambrano hey Jhon.
      Postgres does not execute old statements if you are getting this error that means the app is executing this statement still.
      Make sure in the app you don’t have a query that still uses this columns. I am not familiar with NestJS.
      EDIT
      If you are positive you are not using this column on the app than make sure there aren’t any stored procedures.
      Run a postgres log and see all queries going to the db

    • @JhonZ77
      @JhonZ77 4 ปีที่แล้ว

      @@hnasr with this in mind and knowing that postgres does not store statements, I figured out that NestJs does not always refresh dist directory.
      I deleted it and everything works fine, thanks man!

    • @hnasr
      @hnasr  4 ปีที่แล้ว +1

      Thanks for sharing this!

  • @mhdhabboub
    @mhdhabboub 4 ปีที่แล้ว

    Great video .. Thanks, Hussein.
    What if I have multiple organizations who access my application and I have different databases for different organizations.
    My understanding, in that case, is that I need to have some kind of logic to connect to the database based on the url? is there a way to dynamically check whether a new connection is needed before actually connect (i.e. to avoid reaching the maximum connections)? I would appreciate if you can make a video to handle such a case?

    • @hnasr
      @hnasr  4 ปีที่แล้ว +1

      Hey Mohammed, great 👍 question ,
      Well, since you are building a stateful application might as well keep track of whether you have connections to a particular database by putting it in an array or having a library like this one manage it for you.
      For you case I would imagine you would use two connection pools. One for each org, Then based on certain key the client sends, you know which pool to hit. And the pool actually control the maximum number of connections (configurable as i showed in the video) so all of that is surely possible
      Hope that helps!

  • @wesxd5292
    @wesxd5292 3 ปีที่แล้ว

    3:06 user and password: "postgres"
    Me: ik this is an example lol