Consistency, LWT, Retries and Shard-Aware Routing

Article · 7:00

Consistency levels carried over, lightweight transactions in this engine, driver retry and speculative execution, and shard-aware routing verified under load.

Consistency is identical to Cassandra, including the arithmetic. Levels, R + W > RF, LOCAL_QUORUM for multi-datacentre, last-write-wins by timestamp with the same clock-skew danger. If you have done the Cassandra course you already know this module's first half, and the only thing worth re-stating is that NTP remains a correctness requirement - the resolution rule is the same and so is the silent data loss.

Lightweight transactions work and are implemented with Paxos, as in Cassandra. Two points specific to here. First, ScyllaDB's implementation has historically had different performance characteristics and some version-dependent behaviour, so measure yours rather than assuming the Cassandra numbers. Second, the general advice is unchanged: use them for genuine uniqueness, never in a hot path, and read the [applied] column.

Driver behaviour is where this module earns its place, because two things here are specific to ScyllaDB and both are measurable.

Shard-aware routing, introduced in Module 1, needs verifying under realistic load rather than at idle. The mechanism: ScyllaDB advertises a shard-aware port and tells the driver how shards are allocated, and the driver selects a source port such that the connection lands on the shard that owns the key. If the driver is not shard-aware, or the shard-aware port is blocked by a firewall or a proxy, or connections pass through a load balancer that rewrites source ports, the routing silently degrades to token-aware only - every request then pays an inter-shard hop, and nothing reports it except the latency and the per-shard connection distribution.

That last point is the practical one: a proxy or NAT between your application and ScyllaDB breaks shard awareness, and this is a real deployment consideration rather than a theoretical one.

Retries and speculative execution carry the same correctness rules as Cassandra - only idempotent statements may be retried, a retried counter double-counts, a retry policy that downgrades consistency has invalidated your arithmetic. What differs is the tail-latency picture: with no GC pauses, a slow request is more likely to be a genuinely saturated shard or a compaction stall, and speculative execution against a saturated shard sends more work to a component that is already the bottleneck. So speculative execution is more clearly a trade here: it helps with an unlucky request and hurts with a hot shard, and you need the per-shard metrics to tell which you have.

Laboratory exercise 3 - routing and retries, measured.

Build a game event service and run at least 200 concurrent clients.

Verify shard-aware routing properly. Record the per-shard CQL connection distribution with a shard-aware driver, then with a plain Cassandra driver, then with the shard-aware driver behind a TCP proxy, and compare the distributions and the p99 for all three. The proxy case is the one that matters operationally.

Build the consistency table as in Cassandra - miss rates and latencies across combinations - and confirm the arithmetic behaves identically.

Demonstrate the clock skew data loss again here, briefly, to confirm it is the same risk.

Implement a username registration with IF NOT EXISTS, measure its latency against a plain insert in this engine, and run it concurrently for the same username showing exactly one success.

Then the speculative execution experiment, which is the module's distinctive one: measure p99 with and without speculative execution in two scenarios - one node artificially slow (where it should help) and one shard saturated by a hot partition (where it should hurt). Capture the per-shard metrics for both and explain the difference.

Deliverable: the service, the three-way routing comparison including the proxy case, the consistency table, the clock skew confirmation, the LWT measurements, and the speculative execution experiment with per-shard metrics and an explanation of both outcomes.

-- ==================================================================
-- CONSISTENCY: identical to Cassandra. Including the arithmetic.
-- ==================================================================
CONSISTENCY LOCAL_QUORUM;
--   RF=3:  W=QUORUM + R=QUORUM = 4 > 3  -> strong
--          W=ONE    + R=ONE    = 2 NOT > 3 -> a read can miss
--   Multi-DC: LOCAL_QUORUM, for the same reason as Cassandra.
--
-- Last-write-wins by timestamp, per column, with the same danger:
SELECT WRITETIME(kind), kind FROM events_by_player_hour
WHERE player_id = 7f3c... AND day = '2026-11-30' AND hour = 14 LIMIT 1;
--
-- *** NTP IS STILL A CORRECTNESS REQUIREMENT. *** The resolution
-- rule is the same, and so is the silent data loss from a fast
-- clock. Nothing about the C++ engine changes this.

-- ==================================================================
-- LIGHTWEIGHT TRANSACTIONS: Paxos, as in Cassandra.
-- ==================================================================
INSERT INTO players (player_id, username, email)
VALUES (uuid(), 'ada', 'ada@example.test')
IF NOT EXISTS;
--   [applied]
--        True
--   Read the [applied] column. A driver that ignores it treats a
--   refused write as a success.

UPDATE players SET tier = 'gold' WHERE player_id = 7f3c...
IF tier = 'silver';

CONSISTENCY SERIAL;
SELECT * FROM players WHERE player_id = 7f3c...;
--
--   ScyllaDB's LWT implementation has historically had DIFFERENT
--   PERFORMANCE CHARACTERISTICS and some version-dependent
--   behaviour. *** MEASURE YOURS rather than assuming the Cassandra
--   numbers. *** The advice is unchanged: genuine uniqueness only,
--   never in a hot path.

-- ==================================================================
-- *** SHARD-AWARE ROUTING, VERIFIED UNDER LOAD. *** The module's
-- *** distinctive content.
-- ==================================================================
-- THE MECHANISM: ScyllaDB advertises a SHARD-AWARE PORT (19042) and
-- tells the driver how shards are allocated. The driver then selects
-- a SOURCE PORT such that the connection lands on the shard that
-- owns the key.
--
--   curl -s http://localhost:9180/metrics \
--     | grep scylla_transport_cql_connections
--   scylla_transport_cql_connections{shard="0"} 51
--   scylla_transport_cql_connections{shard="1"} 49
--   scylla_transport_cql_connections{shard="2"} 50
--   scylla_transport_cql_connections{shard="3"} 50
--   -> EVEN. Shard-aware is working.
--
--   scylla_transport_cql_connections{shard="0"} 200
--   scylla_transport_cql_connections{shard="1"} 0
--   ...
--   -> ALL ON SHARD 0. NOT shard-aware. Every request now pays an
--      INTER-SHARD HOP, and NOTHING REPORTS IT except the latency.

-- *** THE THREE WAYS IT SILENTLY DEGRADES: ***
--   1. the driver is not shard-aware (a plain Cassandra driver)
--   2. port 19042 is BLOCKED by a firewall or security group
--   3. *** A PROXY, NAT OR LOAD BALANCER REWRITES THE SOURCE PORT ***
--
-- Number 3 is the real deployment consideration, and it is why the
-- lab tests it. A service mesh sidecar, a TCP load balancer in front
-- of the cluster, or NAT between the application and the database
-- ALL BREAK SHARD AWARENESS - and the system still works, just
-- slower, with no error anywhere. Connect directly to the nodes.
--
--   The shard-aware drivers:
--     python  scylla-driver       (not cassandra-driver)
--     go      scylladb/gocql      (not gocql/gocql)
--     java    java-driver-core 4.x scylla variant
--     rust    scylla
--     c++     scylla-cpp-driver
--     node    scylladb/nodejs-driver
--
--   Measure all three cases: shard-aware direct, plain driver
--   direct, shard-aware through a proxy. Record p99 and the
--   connection distribution for each.

-- ==================================================================
-- RETRIES: the same correctness rules.
-- ==================================================================
--   * only IDEMPOTENT statements may be retried
--   * a retried COUNTER double-counts
--   * a retried LWT may report the wrong [applied]
--   * a retry policy that DOWNGRADES CONSISTENCY has invalidated
--     your R + W > RF arithmetic
--   Mark idempotence explicitly; the default is usually false, which
--   means your safe inserts are NOT being retried.

-- ==================================================================
-- SPECULATIVE EXECUTION: the trade is CLEARER here. Both ways.
-- ==================================================================
-- With NO GC PAUSES, a slow request is more likely to be a
-- GENUINELY SATURATED SHARD or a compaction stall than an unlucky
-- pause. And:
--
--   *** SPECULATIVE EXECUTION AGAINST A SATURATED SHARD SENDS MORE
--   *** WORK TO THE COMPONENT THAT IS ALREADY THE BOTTLENECK. ***
--
-- So it is a real trade, measurable both ways - which is the
-- experiment:
--
--   SCENARIO A: one NODE artificially slow (throttled disk, or a
--               competing process)
--     speculative execution OFF    p99  ___ ms
--     speculative execution ON     p99  ___ ms   <- should IMPROVE
--
--   SCENARIO B: one SHARD saturated by a hot partition
--     speculative execution OFF    p99  ___ ms
--     speculative execution ON     p99  ___ ms   <- should WORSEN
--
--   And the per-shard metrics tell you WHICH you have:
--   curl -s http://localhost:9180/metrics | grep -E \
--     'scylla_reactor_utilization|scylla_scheduler_queue_length'
--   -> one shard near 1.0 with a growing queue  = scenario B
--   -> all shards similar, one NODE slow        = scenario A
--
-- You cannot choose a speculative execution policy correctly without
-- the per-shard view. That is the module's conclusion.
--
--   ConstantSpeculativeExecutionPolicy(delay = measured p99,
--                                      maxExecutions = 2)
--   and ONLY for idempotent statements, for the same reason as
--   retries.

Discussion

No questions on this topic yet. Sign in to ask the first one.