SDK 2.0.0.1, Connection Pooling & Connection Timeouts

@envitraux

Cool, that is pretty much what the CouchbaseHelper class looks like now: https://github.com/couchbase/couchbase-net-client/commit/f421cc0d9fb61260293c9cbbbaaa8b42a0b1f995

Note that the client is open source, so you can always send a pull request if you want to improve on something. It may not get excepted, but it will get the conversation going towards a better solution.

Thanks!

-Jeff

Hi Jeff,

So, if i was correctly watching this discussion you say its not good to open and close bucket as its written in the most of CB examples for .NET:

using(var mybucket=MyClaster.OpenBucket()){....some routine...}

?

You should definitely not be following the examples - whilst they show the correct order for doing things, and are functionally correct, the overhead for each OpenBucket call is very highā€¦

I also found that when you call OpenBucket it seems to automatically open the minimum number of connections as defined on the pooling settingsā€¦

What basically seems to happen is that the number of connections grows with each call to OpenBucket, and when you call CloseBucket it takes around 30 minutes for the unused connections to closeā€¦

Thanks for the answer!

What would be the best solution then? Keep one bucket per app? or per user session (in case of web app)?

I started with things as they are in the documentation, then I tried with one per page request and that still gave me performance issues.

One per session might be ok, but I would start with just a single bucket per application (which is the way I am trying now).

As I understand it the SDK should manage its own connection pool internally, if you find you have lots of simultaneous requests you may want to adjust the connection pool properties on the bucket settings within the web.config.

See also Using Couchbase Buckets pattern

Is the SDK client written in C#?

Yes, you can find it on GitHub - here:

Thatā€™s actually not correct! When you open a bucket, only a single reference is cached internally by the Cluster object. You can verify this by opening the same bucket twice and comparing the instances; they should be the same.

Also, when you open the bucket, it will create the ConnectionPool.MinSize connections, whatever that value is. By default itā€™s 1, which is probably not ideal for high-performance scenarios. You can increase the MaxSize, which defaults to 2, and then client will lazily add connections up to that value. In many cases, keeping a low min and bit higher max is ideal, because the client can tune itself. This is really something that requires measuring though to get right.

The reason itā€™s taking 30 minutes, is that you probably have references hanging around to do reference counting done internally - i.e. you probably opened the bucket with n+1 threads and only closed it n times. This is a feature to keep one thread from disposing of the client while another is using it. The ClusterHelper will fix this, but I am thinking their needs to be a way for the caller to control this.

Yes, as @k_maynard said, those are to show the correct order of steps, but not necessarily the best way to use the client in practice. This is especially true in a server environment, such as ASP.NET.

Using a singleton wrapper such as @k_maynard posted above or using the ClusterHelper ( NCBC-786: Add GetBucket(nm), GetBucket(nm, pw), RemoveBucket(nm) to Cā€¦ Ā· couchbase/couchbase-net-client@f421cc0 Ā· GitHub) are probably the best way to use the client within an ASP.NET application without creating and tearing down the client after every use.

Thanks!

-Jeff

I checked many times to make sure each bucket was closed, but it was hard to track. Maybe having the ability to access the current count would help with the debugging - or being able to add a name or reference string when calling open bucket (which could then be includes in the list of buckets not closed)ā€¦

Ok, to follow up conversation. Solution proposed by @k_maynard would keep 1 bucket opened per application. How good is this solution in terms of well crowded sites? Whould not it be better to keep 1 opened bucket per user session and close it when user leaves the site?

In case of single bucket per app solution, do i need to put max connection to some reasonable big value?

This should be fine. If you opened one bucket per session then you would end up using way to many connections. The client is thread safe, so itā€™s intended to be shared by many threads, such as in a ASP.NET environment.

It depends upon the amount of traffic you are serving. If you start getting timed out responses on the client-side (ResponseStatus.ClientFailure), then bumping up the MaxSize should help there, maybe to 5 or 10 or even higher.

Check this one, More aboutā€¦global.asax

Darren