MongoDB open close in production (singleton or not)

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

MongoDB open close in production (singleton or not)

Sabine Manaa
Hi,

Currently, I create one single Instance of Mongo when starting the Image.

| theRoot  |
theRoot := Mongo default.
theRoot open.

and use it for all requests. In my development environment that works fine.

My question is: is this the right way and will it work for production?
Or is it better to create one Mongo instance for each request (and close it after the request)?
And, btw: what would happen if I would open many Mongo connections but never close any?
Or better one Instance per user session?

Sabine

(I posted this in Frameworks forum, possibly better here)
Reply | Threaded
Open this post in threaded view
|

Re: MongoDB open close in production (singleton or not)

Mariano Martinez Peck
Hi Sabine, take a look to Voyage, and its class VOMongoSessionPool.
I think a nice way of dealing with what you ask is using the pool and each time you need to do something with the DB, you do it inside a block to #withDatabase:

myPool withDatabase: [:database| self doSomething ]

you also have #withSession instead of #withDatabase.

btw, I guess we can move the pool class from voyage to mongo since it's decoupled.
 

Cheers, 



On Thu, Jul 18, 2013 at 7:33 AM, Sabine Knöfel <[hidden email]> wrote:
Hi,

Currently, I create one single Instance of Mongo when starting the Image.

| theRoot  |
theRoot := Mongo default.
theRoot open.

and use it for all requests. In my development environment that works fine.

My question is: is this the right way and will it work for production?
Or is it better to create one Mongo instance for each request (and close it
after the request)?
And, btw: what would happen if I would open many Mongo connections but never
close any?
Or better one Instance per user session?

Sabine

(I posted this in Frameworks forum, possibly better here)



--
View this message in context: http://forum.world.st/MongoDB-open-close-in-production-singleton-or-not-tp4699322.html
Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.




--
Mariano
http://marianopeck.wordpress.com
Reply | Threaded
Open this post in threaded view
|

Re: MongoDB open close in production (singleton or not)

NorbertHartl
In reply to this post by Sabine Manaa

Am 18.07.2013 um 12:33 schrieb Sabine Knöfel <[hidden email]>:

> Hi,
>
> Currently, I create one single Instance of Mongo when starting the Image.
>
> | theRoot  |
> theRoot := Mongo default.
> theRoot open.
>
> and use it for all requests. In my development environment that works fine.
>
> My question is: is this the right way and will it work for production?

No, certainly not. If you restart mongo db e.g. due a system update the connection from the image would be stale serving you errors.
> Or is it better to create one Mongo instance for each request (and close it
> after the request)?
That is theoretically the safest thing but very expensive. Opening a connection always takes time and consume quite some resources. So reuse is king.
> And, btw: what would happen if I would open many Mongo connections but never
> close any?
You would run out of external semaphores in the image. The image is "somewhat broken" in this regard because it cannot expand the semaphore table. As each connection uses three semaphores you are running out of semaphores after 85 requests.

> Or better one Instance per user session?
>
That can be a good idea but it doesn't solve your actual problem.

You also need to take care of concurrent requests. The line protocol to the mongo database needs to be aligned and it is binary. If more threads try to write on the same connection the connection will break. I'm not sure if this is still possible in the newest MongoTalk driver nor do I know if I managed it to release my thread safe mongo instance.

The pool Mariano is talking about sounds like a good idea. Otherwise wrap your store method with an exception handler to reopen a connection on failure.

Norbert


Reply | Threaded
Open this post in threaded view
|

Re: MongoDB open close in production (singleton or not)

Sabine Manaa
Hi Norbert and Mariano,

thanks for the important input.
I will use voyage now, just loading :-)

BTW: it is really great, getting answers in the forum so quick and
profound!!! I am very happy about that.

Sabine

On Thu, Jul 18, 2013 at 2:57 PM, Norbert Hartl [via Smalltalk]
<[hidden email]> wrote:

>
> Am 18.07.2013 um 12:33 schrieb Sabine Knöfel <[hidden email]>:
>
>> Hi,
>>
>> Currently, I create one single Instance of Mongo when starting the Image.
>>
>> | theRoot  |
>> theRoot := Mongo default.
>> theRoot open.
>>
>> and use it for all requests. In my development environment that works
>> fine.
>>
>> My question is: is this the right way and will it work for production?
>
> No, certainly not. If you restart mongo db e.g. due a system update the
> connection from the image would be stale serving you errors.
>> Or is it better to create one Mongo instance for each request (and close
>> it
>> after the request)?
> That is theoretically the safest thing but very expensive. Opening a
> connection always takes time and consume quite some resources. So reuse is
> king.
>> And, btw: what would happen if I would open many Mongo connections but
>> never
>> close any?
> You would run out of external semaphores in the image. The image is
> "somewhat broken" in this regard because it cannot expand the semaphore
> table. As each connection uses three semaphores you are running out of
> semaphores after 85 requests.
>
>> Or better one Instance per user session?
>>
> That can be a good idea but it doesn't solve your actual problem.
>
> You also need to take care of concurrent requests. The line protocol to the
> mongo database needs to be aligned and it is binary. If more threads try to
> write on the same connection the connection will break. I'm not sure if this
> is still possible in the newest MongoTalk driver nor do I know if I managed
> it to release my thread safe mongo instance.
>
> The pool Mariano is talking about sounds like a good idea. Otherwise wrap
> your store method with an exception handler to reopen a connection on
> failure.
>
> Norbert
>
>
>
>
> ________________________________
> If you reply to this email, your message will be added to the discussion
> below:
> http://forum.world.st/MongoDB-open-close-in-production-singleton-or-not-tp4699322p4699354.html
> To unsubscribe from MongoDB open close in production (singleton or not),
> click here.
> NAML
Reply | Threaded
Open this post in threaded view
|

Re: MongoDB open close in production (singleton or not)

NorbertHartl

Am 18.07.2013 um 15:06 schrieb Sabine Knöfel <[hidden email]>:

Hi Norbert and Mariano,

thanks for the important input.
I will use voyage now, just loading :-)

BTW: it is really great, getting answers in the forum so quick and
profound!!! I am very happy about that.

In my case it is pure selfishness! I think answering a question in a well-founded way is a great of learning stuff. So I'm not sure you gains more ;)

Anyway, you're welcome!

Norbert

Sabine

On Thu, Jul 18, 2013 at 2:57 PM, Norbert Hartl [via Smalltalk]
<<a href="x-msg://7541/user/SendEmail.jtp?type=node&amp;node=4699356&amp;i=0" target="_top" rel="nofollow" link="external">[hidden email]> wrote:

>
> Am 18.07.2013 um 12:33 schrieb Sabine Knöfel <[hidden email]>:
>
>> Hi,
>>
>> Currently, I create one single Instance of Mongo when starting the Image.
>>
>> | theRoot  |
>> theRoot := Mongo default.
>> theRoot open.
>>
>> and use it for all requests. In my development environment that works
>> fine.
>>
>> My question is: is this the right way and will it work for production?
>
> No, certainly not. If you restart mongo db e.g. due a system update the
> connection from the image would be stale serving you errors.
>> Or is it better to create one Mongo instance for each request (and close
>> it
>> after the request)?
> That is theoretically the safest thing but very expensive. Opening a
> connection always takes time and consume quite some resources. So reuse is
> king.
>> And, btw: what would happen if I would open many Mongo connections but
>> never
>> close any?
> You would run out of external semaphores in the image. The image is
> "somewhat broken" in this regard because it cannot expand the semaphore
> table. As each connection uses three semaphores you are running out of
> semaphores after 85 requests.
>
>> Or better one Instance per user session?
>>
> That can be a good idea but it doesn't solve your actual problem.
>
> You also need to take care of concurrent requests. The line protocol to the
> mongo database needs to be aligned and it is binary. If more threads try to
> write on the same connection the connection will break. I'm not sure if this
> is still possible in the newest MongoTalk driver nor do I know if I managed
> it to release my thread safe mongo instance.
>
> The pool Mariano is talking about sounds like a good idea. Otherwise wrap
> your store method with an exception handler to reopen a connection on
> failure.
>
> Norbert
>
>
>
>
> ________________________________
> If you reply to this email, your message will be added to the discussion
> below:
> http://forum.world.st/MongoDB-open-close-in-production-singleton-or-not-tp4699322p4699354.html
> To unsubscribe from MongoDB open close in production (singleton or not),
> click here.
> NAML


View this message in context: Re: MongoDB open close in production (singleton or not)
Sent from the Pharo Smalltalk Users mailing list archive at Nabble.com.