Capstone - Global Event Feed

Article · 7:00

Time-bucketed partitions, one table per query, consistency experiments and a failure drill with measured results.

The system. A global event feed: users generate events, events are read back per user, per kind and globally, counts are maintained per day, and old data expires. Deployed across two datacentres with RF=3 in each.

Three invariants must hold:

  1. Every event written at LOCAL_QUORUM is readable at LOCAL_QUORUM in the same datacentre - demonstrated by measurement, not assumed.
  2. The denormalised copies agree: an event present in one query's table is present in all of them, with a detection query and a repair procedure.
  3. No partition exceeds 100 MB or 100,000 rows, at any point, including for the heaviest user - verified by nodetool tablehistograms, not by calculation.

Invariant 3 is this capstone's distinguishing requirement and the one most submissions get wrong, because the bucket size was chosen from an average write rate rather than from the heaviest user's.

What is assessed. 20% knowledge checks, 35% laboratory portfolio, 15% troubleshooting practical, 30% capstone. Three things carry the capstone.

A query list and a schema demonstrably derived from it, with bounded partitions. The numbered query list comes first, each query mapped to its table and its partition key, and the partition size distribution measured for the heaviest key. A schema with a plausible-looking bucket and no measurement has not demonstrated invariant 3.

Measured consistency and performance against a declared baseline. Declare it first: topology, replication, consistency levels per operation, dataset, workload mix, and p50/p95/p99. Include the consistency-level table from Module 3 - miss rates and latencies across combinations - because in Cassandra the consistency choice is part of the design, and showing you measured it rather than copied it is the point.

A defensible account of failure modes, operational limits and cost. Three things must appear specifically: the gc_grace_seconds and repair-interval relationship, with your chosen values and why; the clock synchronisation requirement, stated as a correctness requirement; and the compaction strategy, chosen with a reason and with its disk headroom in the capacity plan.

Deliverables: architecture diagram with datacentres, racks and replication; the numbered query list; the schema with each query mapped and the partition bound argued; source code including the driver policies and copy maintenance; reproducible test dataset; benchmark report including the consistency table; security review; recovery runbook with measured RPO and RTO from an actual node replacement; capacity and cost plan; and a short design defence.

-- ================= Invariants, as runnable checks ==================

-- 1. LOCAL_QUORUM write is readable at LOCAL_QUORUM. MEASURED.
--    Not assumed - a loop of 10,000 write-then-read pairs, counting
--    misses. Expected: 0. Then the same at ONE/ONE, which will NOT
--    be 0, so you have both numbers.
CONSISTENCY LOCAL_QUORUM;

-- 2. THE DENORMALISED COPIES AGREE. A detection query, scheduled:
--    for a sample of event_ids from events_by_id, confirm the row
--    exists in events_by_user_day and events_by_kind_day with the
--    same payload.
SELECT event_id, user_id, day, kind FROM events_by_id
WHERE event_id = ...;
SELECT event_id FROM events_by_user_day
WHERE user_id = ... AND day = ... AND event_at = ...;
SELECT event_id FROM events_by_kind_day
WHERE kind = ... AND day = ... AND event_at = ...;
--    Any missing -> the REPAIR PROCEDURE: re-derive from
--    events_by_id (the designated source of truth for repair) and
--    re-insert into the others. SAY WHICH TABLE IS AUTHORITATIVE -
--    without that, repair has no direction.

-- 3. *** NO PARTITION EXCEEDS 100 MB OR 100,000 ROWS *** - at any
--    point, INCLUDING FOR THE HEAVIEST USER.
--      nodetool tablehistograms feed.events_by_user_day
--      -> Partition Size Max, Cell Count Max
--      nodetool toppartitions feed events_by_user_day 10000
--
--    *** THE MISTAKE MOST SUBMISSIONS MAKE: choosing the bucket from
--    *** the AVERAGE write rate. ***
--      average user:  50 events/day   -> 1 day buckets: 20 KB. Fine.
--      heaviest user: 400,000/day     -> 1 day buckets: 160 MB. FAILS.
--
--    So size the bucket from the HEAVIEST key, or shard it:
CREATE TABLE events_by_user_hour (
    user_id uuid, day date, hour int,
    event_at timestamp, event_id timeuuid, kind text, payload text,
    PRIMARY KEY ((user_id, day, hour), event_at, event_id)
) WITH CLUSTERING ORDER BY (event_at DESC, event_id DESC)
  AND compaction = {'class':'TimeWindowCompactionStrategy',
                    'compaction_window_unit':'HOURS',
                    'compaction_window_size':6}
  AND default_time_to_live = 2592000
  AND gc_grace_seconds = 86400;      -- 1 day. See the contract below.
--    heaviest user, hour buckets: ~17,000 rows, ~7 MB. Passes.
--    Cost: "last 50 events" may now read 2-3 partitions instead of
--    1. Measure it; that is the trade, and it is the right one.

-- Or a SHARD in the partition key, for an unknown worst case:
--    PRIMARY KEY ((user_id, day, shard), event_at, event_id)
--    shard = hash(event_id) % 4   -> read all 4 and merge

-- ================= Declare the baseline BEFORE measuring ===========
--
--   Topology:    2 DCs x 3 nodes, 3 racks each, RF {dc1:3, dc2:3}
--   system_auth: RF {dc1:3, dc2:3}  <- fixed, and repaired
--   Consistency: writes LOCAL_QUORUM, reads LOCAL_QUORUM
--                counters LOCAL_ONE (approximate by design - stated)
--                username registration SERIAL/LWT
--   Compaction:  TWCS, 6-hour windows, TTL 30 days
--   gc_grace:    86400 (1 day)
--   REPAIR:      -pr -full, EVERY NODE, EVERY 12 HOURS, scheduled by
--                Reaper, WITH ALERTING ON RECENCY
--                *** gc_grace (24 h) > repair interval (12 h). THE
--                *** CONTRACT HOLDS, and this is why that number. ***
--   Dataset:     5,000,000 users, 2,000,000,000 events
--   Workload:    80% reads, 20% writes
--
--   Claim:
--     write one event (3 tables, logged batch) p99 <  15 ms
--     read a user's last 50                    p99 <  10 ms
--     read a kind's last 50 for a day          p99 <  20 ms
--     day count per kind                       p99 <   5 ms
--     node replacement RTO                     < 4 h, MEASURED
--     writes continue with 1 node down         yes, at LOCAL_QUORUM
--     writes continue with 2 nodes down        NO at LOCAL_QUORUM.
--                                              State it; do not
--                                              discover it.
--
--   And THE CONSISTENCY TABLE from Module 3 - measured, not copied:
--     W / R              miss rate   write p99   read p99
--     ONE / ONE             ___ %      ___ ms     ___ ms
--     QUORUM / ONE          ___ %      ___ ms     ___ ms
--     LOCAL_QUORUM / LOCAL_QUORUM  0 %  ___ ms     ___ ms
--     ALL / ONE             ___ %      ___ ms     ___ ms
--   Showing you MEASURED the consistency choice rather than copied
--   it is the point.

-- ================= Verification reads =============================
CONSISTENCY ALL;
SELECT COUNT(*) FROM events_by_user_hour
WHERE user_id = 7f3c... AND day = '2026-11-30' AND hour = 14;
--   CONSISTENCY ALL makes every replica answer, so a divergent one
--   shows up rather than being hidden by quorum.

-- ================= Failure modes ==================================
--   1 node down (RF=3)         LOCAL_QUORUM writes and reads CONTINUE.
--                              Hints accumulate. Latency rises.
--   2 nodes down (RF=3)        LOCAL_QUORUM FAILS. ONE still works,
--                              with no consistency guarantee. Decide
--                              in advance whether to degrade.
--   node down > 3 h            PAST THE HINT WINDOW. It has
--                              permanently missed writes until a
--                              repair.
--   repair falls behind        gc_grace_seconds expires without the
--                              delete having propagated -> DELETED
--                              DATA RETURNS. Alert on repair recency;
--                              it is not optional.
--   CLOCK SKEW                 SILENT DATA LOSS by last-write-wins.
--                              NTP is a CORRECTNESS requirement.
--                              Alert on offset.
--   unbounded partition        compaction pauses, heap pressure,
--                              latency variance, eventual failure.
--                              Invariant 3, measured not calculated.
--   tombstone accumulation     a query that times out reading ZERO
--                              rows. TWCS avoids it for TTL data;
--                              null-vs-unset avoids the silent kind.
--   multi-partition batch      one coordinator serialising
--                              cluster-wide writes. Slower than
--                              parallel. Only batch the copies of
--                              ONE logical write.
--   counter retried            DOUBLE COUNTED. Counters are
--                              approximate under failure, by design.
--                              Say so, or derive the exact number.
--   pending compactions grow   SSTables accumulate, reads slow, disk
--                              fills. One of the most important
--                              metrics.
--   dropped MUTATIONs          WRITES LOST. A data problem.
--   system_auth at RF=1        a one-node outage becomes a TOTAL
--                              AUTHENTICATION outage.
--   wrong node replacement     the ring believes data exists where
--                              it does not, and nodetool status
--                              looks fine.
--
-- Verify after EVERY node replacement and repair with the three
-- checks above, read at CONSISTENCY ALL. "All nodes UN" is not
-- verification.

Where to go from here

Longer reading about this subject, written alongside the course.

Course guide How to work through this course Why three nodes from day one, the two experiments that matter, what to unlearn, and what to skip. 4 min read

Discussion

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