Skip to content

Commit 540adc7

Browse files
DOCSP-38442 Update contention factor guidance (#7399)
* Initial structural changes * Subscript/superscript rendering check * Parsing fix * Syntax fixes * Additional clarification * Syntax and wording * Line break * Wording change * Wording change * Updated wording * Updated wording * Updated wording * Fixed wording * Changed chapter to section * Apply suggestions from code review Internal review feedback Co-authored-by: Jeff Allen <[email protected]> * Line break * Monospace --------- Co-authored-by: Jeff Allen <[email protected]>
1 parent 0a58c05 commit 540adc7

File tree

4 files changed

+70
-43
lines changed

4 files changed

+70
-43
lines changed

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,20 @@ the storage space requirements.
5555

5656
.. _qe-contention:
5757

58-
Configure Contention Factor
59-
~~~~~~~~~~~~~~~~~~~~~~~~~~~
60-
61-
Include the ``contention`` property on queryable fields to prefer either
62-
find performance, or write and update performance.
58+
Contention
59+
----------
6360

6461
.. include:: /includes/queryable-encryption/qe-csfle-contention.rst
6562

63+
Adjusting the Contention Factor
64+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
65+
66+
You can optionally include the ``contention`` property on queryable fields to
67+
change the contention factor from its default value of ``8``. Before you modify
68+
the contention factor, consider the following points:
69+
70+
.. include:: /includes/queryable-encryption/qe-csfle-setting-contention.rst
71+
6672
.. _qe-query-types:
6773

6874
Supported Query Types and Behavior

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,11 @@ Contention Factor
3333
-----------------
3434

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

3839
You can set the contention factor only when specifying a field for encryption.
39-
Once you specify a field for encryption, the contention factor is immutable. If
40-
you don't specify the contention factor, it uses the default value of
41-
``8``.
40+
Once you specify a field for encryption, the contention factor is immutable.
4241

4342
For more information, see :ref:`Configuring contention factor <qe-contention>`.
4443

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +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
15-
on low cardinality fields, but decreases find performance.
16-
17-
Consider increasing ``contention`` above the default value of 8 only if:
18-
19-
- The field has low cardinality or low selectivity. A ``state`` field
20-
may have 50 values, but if 99% of the data points use ``{state: NY}``,
21-
that pair is likely to cause contention.
22-
23-
- Write and update operations frequently modify the field. Since high
24-
contention values sacrifice find performance in favor of write and
25-
update operations, the benefit of a high contention factor for a
26-
rarely updated field is unlikely to outweigh the drawback.
27-
28-
Some other examples of low cardinality fields are credit card types,
29-
gender, and ethnicity.
30-
31-
Consider decreasing ``contention`` if:
32-
33-
- The field is high cardinality and contains entirely unique values,
34-
such as a credit card number.
35-
36-
- The field is often queried, but never or rarely updated. In this
37-
case, find performance is preferable to write and update performance.
38-
39-
Some other examples of high cardinality fields are mobile phone numbers,
40-
social security numbers, full names, and addresses.
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)