Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Best practice #76

Closed
pjebs opened this issue Jan 13, 2018 · 6 comments
Closed

Best practice #76

pjebs opened this issue Jan 13, 2018 · 6 comments
Assignees
Labels

Comments

@pjebs
Copy link

pjebs commented Jan 13, 2018

I notice there is a session, err := mgo.Dial(MONGODB_DIAL_URL) function and also Clone and Copy.

If we are using MongoDB for a web app backend, I'm wondering what the best practice is out of these options.

  1. Call Dial when we need to access database and close as soon as possible (usually with defer within same function).

  2. Call Dial at start of request and close at end of request cycle after servicing request.

  • Obviously if we must do multiple queries per request, we use the same reference to session during
    that request.
  1. Call Dial external to request cycle (i.e. when app starts up), store the reference in a global variable and keep using it independent of request and usually never close connection and just let the library do connection pooling.

(Obviously For option 2, an improvement is to dial lazily but that's obvious to readers).
Assuming that we may need to do multiple database queries per request, how exactly do we use clone and/or copy?

@pjebs
Copy link
Author

pjebs commented Jan 13, 2018

Can you please provide guidance (since you are the only authority on the matter) on how and when to use clone and copy.

@pjebs
Copy link
Author

pjebs commented Jan 13, 2018

This is a copy of: https://github.com/go-mgo/mgo/issues/287 which the original author did not answer. Since the original author has now retired, I guess you are the highest power. It would be great if a higher power answered it because I noticed there are 100's of people asking the same question on forums and many are linking to my original issue.

Thank you for your time.

@pjebs
Copy link
Author

pjebs commented Jan 13, 2018

Does this package do connection pooling similar to database/sql?

@domodwyer
Copy link

Hi @pjebs

You're not the first to ask this! I'd suggest using session.Copy() to automatically open a new socket to the Mongo server if needed, or reuse an existing socket if available. This means your application will "warm up" to the number of connections needed for the load in the first few minutes of operation.

You could use Clone() which uses the same underlying socket, but past a certain level of concurrency your application will be bound by contention caused by sharing the single socket. Depending on your workload this might not be an issue, but it's likely going to be a nasty surprise!

Calling Dial each time involves opening a connection, performing any authentication (not cheap) and then closing it when you're done, having to do it all again for the next request - this is a lot of round trips and very expensive for both the client and server - you should avoid this method.

I've added an example to the documentation - this should make it into the master branch / godoc.org soon.

I hope this helps, does it answer all your questions?

Dom

@pjebs
Copy link
Author

pjebs commented Jan 14, 2018

thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants