Skip to content

Commit ee01020

Browse files
DOCSP-38442 Update contention factor instructions (#7399) (#7430)
* Merge fixes * Added missing include
1 parent 6a81d05 commit ee01020

File tree

5 files changed

+88
-38
lines changed

5 files changed

+88
-38
lines changed

source/core/queryable-encryption/fundamentals/encrypt-and-query.txt

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -210,18 +210,21 @@ Add the ``queries`` property to the previous example schema to make the
210210

211211
.. _qe-contention:
212212

213-
Configure Contention Factor
214-
~~~~~~~~~~~~~~~~~~~~~~~~~~~
213+
Contention
214+
----------
215215

216-
Include the ``contention`` property on queryable fields to prefer either
217-
find performance, or write and update performance.
216+
.. include:: /includes/queryable-encryption/qe-csfle-contention.rst
218217

219-
.. include:: /includes/fact-qe-csfle-contention.rst
218+
Adjusting the Contention Factor
219+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
220+
221+
You can optionally include the ``contention`` property on queryable fields to
222+
change the contention factor from its default value of ``8``. Before you modify
223+
the contention factor, consider the following points:
224+
225+
.. include:: /includes/queryable-encryption/qe-csfle-setting-contention.rst
220226

221-
Example
222-
+++++++
223227

224-
.. include:: /includes/example-qe-csfle-contention.rst
225228
.. _qe-query-types:
226229

227230
Query Types

source/core/queryable-encryption/reference/limitations.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@ Contention Factor
3636
-----------------
3737

3838
Contention factor is a setting that helps tune performance based on the
39-
number of concurrent connections.
39+
number of concurrent operations. When unset, contention uses a default value of
40+
``8``, which provides high performance for most workloads.
4041

4142
You can set the contention factor only when specifying a field for encryption.
42-
Once you specify a field for encryption, the contention factor is immutable. If
43-
you don't specify the contention factor, it uses the default value of ``4``.
43+
Once you specify a field for encryption, the contention factor is immutable.
44+
45+
For more information, see :ref:`Configuring contention factor <qe-contention>`.
4446

4547
Manual Metadata Collection Compaction
4648
-------------------------------------
Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,16 @@
1-
Inserting the same field/value pair into multiple documents in close
2-
succession can cause conflicts that delay insert operations.
1+
Concurrent write operations, such as inserting the same field/value pair into
2+
multiple documents in close succession, can cause contention: conflicts that
3+
delay operations.
34

4-
MongoDB tracks the occurrences of each field/value pair in an
5+
With {+qe+}, MongoDB tracks the occurrences of each field/value pair in an
56
encrypted collection using an internal counter. The contention factor
67
partitions this counter, similar to an array. This minimizes issues with
78
incrementing the counter when using ``insert``, ``update``, or ``findAndModify`` to add or modify an encrypted field
89
with the same field/value pair in close succession. ``contention = 0``
9-
creates an array with one element
10-
at index 0. ``contention = 4`` creates an array with 5 elements at
11-
indexes 0-4. MongoDB increments a random array element during insert. If
12-
unset, ``contention`` defaults to 8.
10+
creates an array with one element at index 0. ``contention = 4`` creates an
11+
array with 5 elements at indexes 0-4. MongoDB increments a random array element
12+
during insert.
1313

14-
High contention improves the performance of insert and update operations on low cardinality fields, but decreases find performance.
15-
16-
Consider increasing ``contention`` above the default value of 8 only if:
17-
18-
- The field has low cardinality or low selectivity. A ``state`` field
19-
may have 50 values, but if 99% of the data points use ``{state: NY}``,
20-
that pair is likely to cause contention.
21-
22-
- Write and update operations frequently modify the field. Since high
23-
contention values sacrifice find performance in favor of write and
24-
update operations, the benefit of a high contention factor for a
25-
rarely updated field is unlikely to outweigh the drawback.
26-
27-
Consider decreasing ``contention`` if:
28-
29-
- The field is high cardinality and contains entirely unique values,
30-
such as a credit card number.
31-
32-
- The field is often queried, but never or rarely updated. In this
33-
case, find performance is preferable to write and update performance.
14+
When unset, ``contention`` defaults to ``8``, which provides high performance
15+
for most workloads. Higher contention improves the performance of insert and
16+
update operations on low cardinality fields, but decreases find performance.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Concurrent write operations, such as inserting the same field/value pair into
2+
multiple documents in close succession, can cause contention: conflicts that
3+
delay operations.
4+
5+
With {+qe+}, MongoDB tracks the occurrences of each field/value pair in an
6+
encrypted collection using an internal counter. The contention factor
7+
partitions this counter, similar to an array. This minimizes issues with
8+
incrementing the counter when using ``insert``, ``update``, or ``findAndModify`` to add or modify an encrypted field
9+
with the same field/value pair in close succession. ``contention = 0``
10+
creates an array with one element at index 0. ``contention = 4`` creates an
11+
array with 5 elements at indexes 0-4. MongoDB increments a random array element
12+
during insert.
13+
14+
When unset, ``contention`` defaults to ``8``, which provides high performance
15+
for most workloads. Higher contention improves the performance of insert and
16+
update operations on low cardinality fields, but decreases find performance.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
Consider increasing ``contention`` above the default value of ``8`` only if the
2+
field has frequent concurrent write operations. Since high contention values
3+
sacrifice find performance in favor of insert and update operations, the
4+
benefit of a high contention factor for a rarely updated field is unlikely to
5+
outweigh the drawback.
6+
7+
Consider decreasing ``contention`` if a field is often queried, but
8+
rarely written. In this case, find performance is preferable to write and
9+
update performance.
10+
11+
You can calculate contention factor for a field by using a formula where:
12+
13+
- ``ω`` is the number of concurrent write operations on the field in a short
14+
time, such as 30ms. If unknown, you can use the server's number of virtual
15+
cores.
16+
- ``valinserts`` is the number of unique field/value pairs inserted since last
17+
performing :ref:`metadata compaction <qe-metadata-compaction>`.
18+
- ``ω``:sup:`` is ``ω/valinserts`` rounded up to the nearest integer. For a
19+
workload of 100 operations with 1000 recent values, ``100/1000 = 0.1``,
20+
which rounds up to ``1``.
21+
22+
A reasonable contention factor, ``cf``, is the result of the following
23+
formula, rounded up to the nearest positive integer:
24+
25+
````:sup:`` ``· (ω``:sup:`` ``− 1)) / 0.2``
26+
27+
For example, if there are 100 concurrent write operations on a field in 30ms,
28+
then ``ω = 100``. If there are 50 recent unique values for that field, then
29+
``ω``:sup:`` ``= 100/50 = 2``. This results in ``cf = (2·1)/0.2 = 10``.
30+
31+
.. warning::
32+
33+
Don't set the contention factor on properties of the data itself, such as
34+
the frequency of field/value pairs (:term:`cardinality`). Only set the contention factor based on your workload.
35+
36+
Consider a case
37+
where ``ω = 100`` and ``valinserts = 1000``, resulting in ``ω``:sup:`` ``=
38+
100/1000 = 0.1 ≈ 1`` and ``cf = (1·0)/0.2 = 0 ≈ 1``. 20 of
39+
the values appear very frequently, so you set ``contention = 3`` instead. An
40+
attacker with access to multiple database snapshots can infer that the high
41+
setting indicates frequent field/value pairs. In this case, leaving
42+
``contention`` unset so that it defaults to ``8`` would prevent the attacker
43+
from having that information.
44+
45+
For thorough information on contention and its cryptographic implications, see
46+
"Section 9: Guidelines" in MongoDB's `Queryable Encryption Technical Paper <https://www.mongodb.com/collateral/queryable-encryption-technical-paper>`_

0 commit comments

Comments
 (0)