Hello to everyone. I’m using Couchbase lite in Xamarin.Forms UWP application. During database replication process I noticed that for each data chunk replicator creates new thread. I don’t have any particular issues with this, but it looks like something goes wrong when you see hundreds of threads creation in debug console:
Is it by design or maybe I’m doing something wrong? Could someone please explain how replicator works under the hood in terms of threading?
That’s too broad of a question to answer in a simple forum post. Modern applications and libraries basically do not control the threading situation. I have also noticed the thread spam in these logs, but Couchbase Lite .NET does not directly create any threads. Like the majority of .NET programs out there the Task Parallel Library is used, which abstracts away the actual creation of threads. Non Xamarin implementations use the same source, but without the threading weirdness so my first impression is that for some reason the .NET runtime is choosing to do it this way. Maybe CBL .NET is not breaking up its work into reasonable pieces, but there has not been a lot of effort put into figuring out why things happen this way since it is not something we control directly.
Thank you for your response. Let me get this straight, I didn’t mean that Couchbase Lite creates threads by itself. Of Course, Couchbase Lite consumes threads via TPL (from thread pool). But the reason of thread creating is because Couchbase REQUESTS new thread from thread pool for each data chunk.
Let’s imaging I have data base with 100 documents with similar simple structure, and as I can see, during replication process I have 100 threads for each document (of course, it’s not unique threads, it can be 10-20 threads from thread pool which are reused, depends on the environment).
I know that under the hood Couchbase lite relies on web-sokect protocol for replication process, maybe the reason is here? I can’t grab together the whole picture in my head
There is no such action as “requesting a thread.” There is only queuing a unit of work to be consumed by the TPL. This happens a lot in Couchbase Lite, and on desktop platforms (for example) there is no log spam like this, and there is no difference on our side between desktop and mobile for this area. So I don’t really know what to tell you about this, other than it is weird but it’s never been a priority to figure out why.
Yes, your definition “queuing a unit of work” sounds much better. My apologies. That what I meant.
Thank you for the explanation. It’s shame that it’s not a priority for investigation.
Just one more question. My assumption was that it happens because of data base structure. For example, currently I download hundred simple objects with the same structure, but which are stored like separate documents. Would it be better to load ONE object with hundred nested objects? Maybe you have some best practice\guideline how to deal with Couchbase data structure?