<!-- 
RSS generated by JIRA (9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66) at Wed Feb 07 21:48:56 UTC 2024

It is possible to restrict the fields that are returned in this document by specifying the 'field' parameter in your request.
For example, to request only the issue key and summary append 'field=key&field=summary' to the URL of your request.
-->
<rss version="0.92" >
<channel>
    <title>MongoDB Jira</title>
    <link>https://jira.mongodb.org</link>
    <description>This file is an XML representation of an issue</description>
    <language>en-us</language>    <build-info>
        <version>9.7.1</version>
        <build-number>970001</build-number>
        <build-date>13-04-2023</build-date>
    </build-info>


<item>
            <title>[CSHARP-4661] The driver could possibly use less threads for background operations</title>
                <link>https://jira.mongodb.org/browse/CSHARP-4661</link>
                <project id="10041" key="CSHARP">C# Driver</project>
                    <description>&lt;p&gt;Hi!&lt;/p&gt;

&lt;p&gt;Currently, MongoDB driver starts multiple separate threads for background activities:&lt;br/&gt;
connection pool maintenance, roundtrip monitor and heartbeat. When an application is using multiple instances of MongoDB, especially replica sets, the number of these threads grows quickly; in one of the recent dumps I&apos;ve been looking at when debugging another issue, I encountered 162 threads dedicated to MongoDB (of ~300 threads in total for a loaded web app backend).&lt;/p&gt;

&lt;p&gt;I understand why the driver does not use built-in thread pool &amp;#8211; it can be starved by application tasks and the driver itself could starve it when there is a spike of new servers &amp;#8211; having dedicated threads solves this issue. But the current numbers of threads that mostly sit there waiting for something, occupying stack space still looks suboptimal to me. I undestand that there is nothing wrong with threads from a functional standpoint, but still hope that you would consider reducing the number of threads used for background operations, by either combining multiple tasks into one thread, using some kind of task queue or any other option that you see fit.&lt;/p&gt;</description>
                <environment></environment>
        <key id="2351494">CSHARP-4661</key>
            <summary>The driver could possibly use less threads for background operations</summary>
                <type id="4" iconUrl="https://jira.mongodb.org/secure/viewavatar?size=xsmall&amp;avatarId=14710&amp;avatarType=issuetype">Improvement</type>
                                            <priority id="3" iconUrl="https://jira.mongodb.org/images/icons/priorities/major.svg">Major - P3</priority>
                        <status id="6" iconUrl="https://jira.mongodb.org/images/icons/statuses/closed.png" description="The issue is considered finished, the resolution is correct. Issues which are closed can be reopened.">Closed</status>
                    <statusCategory id="3" key="done" colorName="success"/>
                                    <resolution id="13202">Works as Designed</resolution>
                                        <assignee username="boris.dogadov@mongodb.com">Boris Dogadov</assignee>
                                    <reporter username="onyxmaster">Aristarkh Zagorodnikov</reporter>
                        <labels>
                    </labels>
                <created>Fri, 26 May 2023 13:13:25 +0000</created>
                <updated>Fri, 27 Oct 2023 13:24:37 +0000</updated>
                            <resolved>Fri, 16 Jun 2023 16:12:40 +0000</resolved>
                                                                                        <votes>0</votes>
                                    <watches>3</watches>
                                                                                                                <comments>
                            <comment id="5505682" author="JIRAUSER1257742" created="Fri, 16 Jun 2023 16:12:27 +0000"  >&lt;p&gt;Thank you &lt;a href=&quot;https://jira.mongodb.org/secure/ViewProfile.jspa?name=onyxmaster&quot; class=&quot;user-hover&quot; rel=&quot;onyxmaster&quot;&gt;onyxmaster&lt;/a&gt; for your question!&lt;/p&gt;</comment>
                            <comment id="5497687" author="onyxmaster" created="Wed, 14 Jun 2023 08:12:18 +0000"  >&lt;p&gt;Boris, thank you for the response.&lt;br/&gt;
I understand that our use case significantly differs from the common ones, so the impact of optimizations in this area probably won&apos;t justify the effort required. Since it does not pose any real performance problems for us, we will just keep this in mind as &quot;this is ok&quot; when profiling our application.&lt;br/&gt;
Again, thank you for taking a look into this&lt;/p&gt;</comment>
                            <comment id="5496606" author="JIRAUSER1257742" created="Tue, 13 Jun 2023 19:56:14 +0000"  >&lt;p&gt;Thanks for the additional info &lt;a href=&quot;https://jira.mongodb.org/secure/ViewProfile.jspa?name=onyxmaster&quot; class=&quot;user-hover&quot; rel=&quot;onyxmaster&quot;&gt;onyxmaster&lt;/a&gt;&#160;&lt;/p&gt;

&lt;p&gt;In this case the amount of threads in expected.&lt;br/&gt;
As you&apos;ve mentioned, driver tries to balance between starvation (and overall performance) and resource consumption for the most common use cases.&lt;br/&gt;
For more rare cases as this one, the application architecture needs to account for resources consumed by the driver.&lt;br/&gt;
Unfortunately currently we don&apos;t have any optimizations planned in this area.&lt;/p&gt;</comment>
                            <comment id="5478434" author="onyxmaster" created="Tue, 6 Jun 2023 15:49:56 +0000"  >&lt;p&gt;I investigated it a bit further. It&apos;s a large application that legitimately connects to multiple replica sets, using different combination of settings to some of them, which increases the amount of active pools. So yes, it&apos;s connected to 54-56 servers (different ServerIds).&lt;/p&gt;</comment>
                            <comment id="5473372" author="onyxmaster" created="Sun, 4 Jun 2023 15:38:24 +0000"  >&lt;p&gt;Boris, thanks for looking into this. I hope to get back with more details next week.&lt;/p&gt;</comment>
                            <comment id="5462187" author="JIRAUSER1257742" created="Tue, 30 May 2023 20:44:26 +0000"  >&lt;p&gt;Hi &lt;a href=&quot;https://jira.mongodb.org/secure/ViewProfile.jspa?name=onyxmaster&quot; class=&quot;user-hover&quot; rel=&quot;onyxmaster&quot;&gt;onyxmaster&lt;/a&gt;&#160;&lt;/p&gt;

&lt;p&gt;162 threads is not an expected behaviour. As you noticed, driver uses 3 threads for every server, for example replica set with 5 nodes will result in 15 internal threads.&lt;br/&gt;
Few possible reasons for observing such high amount of threads:&lt;/p&gt;
&lt;ul&gt;
	&lt;li&gt;Using multiple MongoClient as opposed to sharing a single instance (&lt;a href=&quot;https://www.mongodb.com/docs/drivers/csharp/current/fundamentals/connection/connect/&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;reuse instance recommendation&lt;/a&gt;)&lt;/li&gt;
	&lt;li&gt;Not properly disposing MongoClient instance (proper disposing is done via &lt;a href=&quot;https://github.com/mongodb/mongo-csharp-driver/blob/master/tests/MongoDB.Driver.TestHelpers/DisposableMongoClient.cs#L247&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;MongoDB.Driver.ClusterRegistry.Instance.UnregisterAndDisposeCluster(mongoClient.Cluster&lt;/a&gt;)&lt;/li&gt;
	&lt;li&gt;External threads, that are using the driver&lt;/li&gt;
&lt;/ul&gt;


&lt;p&gt;If the none of those points is the root-cause, it would be very helpful to receive a simple self-contain repo, that demonstrates the unbounded threads amount used by the driver.&lt;br/&gt;
Thanks.&lt;/p&gt;</comment>
                            <comment id="5454826" author="dbeng-pm-bot" created="Fri, 26 May 2023 13:13:27 +0000"  >&lt;p&gt;Hi &lt;a href=&quot;https://jira.mongodb.org/secure/ViewProfile.jspa?name=onyxmaster&quot; class=&quot;user-hover&quot; rel=&quot;onyxmaster&quot;&gt;onyxmaster&lt;/a&gt;, thank you for reporting this issue! The team will look into it and get back to you soon. &lt;/p&gt;</comment>
                    </comments>
                    <attachments>
                    </attachments>
                <subtasks>
                    </subtasks>
                <customfields>
                                                                                                                                                                                                                                                                                                                                                                    <customfield id="customfield_15850" key="com.atlassian.jira.plugins.jira-development-integration-plugin:devsummary">
                        <customfieldname>Development</customfieldname>
                        <customfieldvalues>
                            
                        </customfieldvalues>
                    </customfield>
                                                                                            <customfield id="customfield_14266" key="com.atlassian.jira.plugin.system.customfieldtypes:textarea">
                        <customfieldname>Documentation Changes Summary</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>&lt;p&gt;1.  What would you like to communicate to the user about this feature?&lt;br/&gt;
2.  Would you like the user to see examples of the syntax and/or executable code and its output?&lt;br/&gt;
3.  Which versions of the driver/connector does this apply to?&lt;/p&gt;</customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            <customfield id="customfield_12550" key="com.pyxis.greenhopper.jira:gh-lexo-rank">
                        <customfieldname>Rank</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>2|i1t9kw:</customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                <customfield id="customfield_10558" key="com.pyxis.greenhopper.jira:gh-global-rank">
                        <customfieldname>Rank (Obsolete)</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>9223372036854775807</customfieldvalue>
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            </customfields>
    </item>
</channel>
</rss>