@@ -434,3 +434,43 @@ level on that:
434
434
user3_lookup = user_lookup_stmt.bind([user_id3])
435
435
user3_lookup.consistency_level = ConsistencyLevel.ALL
436
436
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