Skip to content

Commit c4066a5

Browse files
authored
Merge pull request scylladb#34 from riptano/python-1077
PYTHON-1077: Improve the documentation about enabling and using a speculative execution policy
2 parents 8cdd0e6 + ad517f7 commit c4066a5

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

docs/getting_started.rst

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,3 +434,43 @@ level on that:
434434
user3_lookup = user_lookup_stmt.bind([user_id3])
435435
user3_lookup.consistency_level = ConsistencyLevel.ALL
436436
user3 = session.execute(user3_lookup)
437+
438+
Speculative Execution
439+
^^^^^^^^^^^^^^^^^^^^^
440+
441+
Speculative execution is a way to minimize latency by preemptively executing several
442+
instances of the same query against different nodes. For more details about this
443+
technique, see `Speculative Execution with DataStax Drivers <https://docs.datastax.com/en/devapp/doc/devapp/driversSpeculativeRetry.html>`_.
444+
445+
To enable speculative execution:
446+
447+
* Configure a :class:`~.policies.SpeculativeExecutionPolicy` with the ExecutionProfile
448+
* Mark your query as idempotent, which mean it can be applied multiple
449+
times without changing the result of the initial application.
450+
See `Query Idempotence <https://docs.datastax.com/en/devapp/doc/devapp/driversQueryIdempotence.html>`_ for more details.
451+
452+
453+
Example:
454+
455+
.. code-block:: python
456+
457+
from cassandra.cluster import Cluster, ExecutionProfile, EXEC_PROFILE_DEFAULT
458+
from cassandra.policies import ConstantSpeculativeExecutionPolicy
459+
from cassandra.query import SimpleStatement
460+
461+
# Configure the speculative execution policy
462+
ep = ExecutionProfile(
463+
speculative_execution_policy=ConstantSpeculativeExecutionPolicy(delay=.5, max_attempts=10)
464+
)
465+
cluster = Cluster(..., execution_profiles={EXEC_PROFILE_DEFAULT: ep})
466+
session = cluster.connect()
467+
468+
# Mark the query idempotent
469+
query = SimpleStatement(
470+
"UPDATE my_table SET list_col = [1] WHERE pk = 1",
471+
is_idempotent=True
472+
)
473+
474+
# Execute. A new query will be sent to the server every 0.5 second
475+
# until we receive a response, for a max number attempts of 10.
476+
session.execute(query)

0 commit comments

Comments
 (0)