volpe/posts/drafts/hyper-logLog-tombstone-garbage-collection.md

23 KiB

HyperLogLog-Based Tombstone Garbage Collection for Distributed Systems

Abstract

When synchronizing records in a distributed network, deletion presents a fundamental challenge. If nodes simply delete their local copies, other nodes may resynchronize the original data, reverting the deletion. This occurs due to non-simultaneous events between nodes or nodes temporarily disconnecting and reconnecting with outdated state. The traditional solution creates "tombstone" records that persist after deletion to prevent resurrection of deleted data.

While effective, this approach requires every node to indefinitely maintain an ever-growing collection of tombstone records. Typically, after an arbitrarily large time period, tombstones are assumed safe to clear since no rogue nodes should retain the original data.

This paper presents a methodology using the HyperLogLog algorithm to estimate how many nodes have received a record, comparing this estimate against the count of nodes that have received the corresponding tombstone. This enables pruning tombstones across the network to a minimal set of "keeper" nodes, extending the viable tombstone retention period while significantly reducing storage overhead.

1. Introduction

Distributed systems face an inherent tension between data consistency and storage efficiency when handling deletions. Traditional tombstone-based approaches guarantee correctness but impose unbounded storage growth. Time-based garbage collection (GC) offers storage efficiency but risks data resurrection if stale nodes reconnect after the GC window.

This paper introduces a probabilistic approach using HyperLogLog (HLL) cardinality estimation1 to achieve both goals: safe garbage collection that provably prevents resurrection while minimizing the number of nodes that must retain tombstones.

1.1 Core Concept

The algorithm operates in three phases:

sequenceDiagram
participant A as Node A
participant B as Node B
participant C as Node C

Note over A,C: Phase 1: Record Propagation
A->>B: record + recordHLL
B->>A: update recordHLL estimate
B->>C: record + recordHLL

Note over A,C: Phase 2: Tombstone Propagation
A->>A: Create tombstone with recordHLL and delete record
C->>B: update recordHLL estimate
A->>B: tombstone + tombstoneHLL + recordHLL
B->>B: tombstone updated with new recordHLL and delete record
B->>C: tombstone + tombstoneHLL + recordHLL

Note over A,C: Phase 3: Keeper Election and tombstone garbage collection
C->>C: tombstoneCount >= recordCount, become keeper and deletes record
C->>B: updates with node tombstone count estimate
B->>B: sees higher estimate, step down and garbage collects its own tombstone record
B->>A: update connected node with tombstoneHLL
A->>A: garbage collects its own tombstone record

Phase 1: Records propagate through the network via gossip, with each node adding itself to the record's HLL. Nodes then talk between themselves to slowly turn local estimates for the records count into global ones.

Phase 2: When deletion occurs, the deleting node creates a tombstone containing a copy of the record's HLL as the target count. The tombstone propagates similarly, with nodes adding themselves to the tombstone's HLL. During propagation, the target recordHLL is updated to the highest estimate encountered.

Phase 3: When a node detects that tombstoneCount >= recordCount, it becomes a "keeper" responsible for continued propagation. As keepers communicate, those with lower estimates step down and garbage collect, converging toward a minimal keeper set.

2. Data Model

Records and tombstones are maintained as separate entities with distinct tracking mechanisms:

interface DataRecord<Data> {
  readonly id: string;
  readonly data: Data;
  readonly recordHLL: HyperLogLog; // Tracks nodes that have received this record
}

interface Tombstone {
  readonly id: string;
  readonly recordHLL: HyperLogLog; // Target count: highest observed record distribution
  readonly tombstoneHLL: HyperLogLog; // Tracks nodes that have received the tombstone
}

3. Algorithm

3.1 Record Creation and Distribution

When a node creates or receives a record, it adds itself to the record's HLL:

const createRecord = <Data>(id: string, data: Data, nodeId: string): DataRecord<Data> => ({
  id,
  data,
  recordHLL: hllAdd(createHLL(), nodeId),
});

const receiveRecord = <Data>(
  node: NodeState<Data>,
  incoming: DataRecord<Data>
): NodeState<Data> => {
  // Reject records that have already been deleted
  if (node.tombstones.has(incoming.id)) {
    return node;
  }

  const existing = node.records.get(incoming.id);
  const updatedRecord: DataRecord<Data> = existing
    ? { ...existing, recordHLL: hllAdd(hllMerge(existing.recordHLL, incoming.recordHLL), node.id) }
    : { ...incoming, recordHLL: hllAdd(hllClone(incoming.recordHLL), node.id) };

  const newRecords = new Map(node.records);
  newRecords.set(incoming.id, updatedRecord);
  return { ...node, records: newRecords };
};

3.2 Tombstone Creation

When deleting a record, a node creates a tombstone containing a copy of the record's HLL as the initial target count:

const createTombstone = <Data>(record: DataRecord<Data>, nodeId: string): Tombstone => ({
  id: record.id,
  recordHLL: hllClone(record.recordHLL),
  tombstoneHLL: hllAdd(createHLL(), nodeId),
});

3.3 Garbage Collection Status Check

The core decision logic determines whether a node should become a keeper, step down, or continue as-is:

const checkGCStatus = (
  tombstone: Tombstone,
  incomingTombstoneEstimate: number | null,
  myTombstoneEstimateBeforeMerge: number,
  myNodeId: string,
  senderNodeId: string | null
): { shouldGC: boolean; stepDownAsKeeper: boolean } => {
  const targetCount = hllEstimate(tombstone.recordHLL);

  const isKeeper = myTombstoneEstimateBeforeMerge >= targetCount;

  if (isKeeper) {
    // Keeper step-down logic:
    // If incoming tombstone has reached the target count, compare estimates.
    // If incoming estimate >= my estimate before merge, step down.
    // Use node ID as tie-breaker: higher node ID steps down when estimates are equal.
    if (incomingTombstoneEstimate !== null && incomingTombstoneEstimate >= targetCount) {
      if (myTombstoneEstimateBeforeMerge < incomingTombstoneEstimate) {
        return { shouldGC: true, stepDownAsKeeper: true };
      }
      // Tie-breaker: if estimates are equal, the lexicographically higher node ID steps down
      if (myTombstoneEstimateBeforeMerge === incomingTombstoneEstimate &&
          senderNodeId !== null && myNodeId > senderNodeId) {
        return { shouldGC: true, stepDownAsKeeper: true };
      }
    }
    return { shouldGC: false, stepDownAsKeeper: false };
  }

  // Not yet a keeper - will become one if tombstone count reaches target after merge
  return { shouldGC: false, stepDownAsKeeper: false };
};

3.4 Tombstone Reception and Processing

graph TD
A[Receive tombstone deletion message] --> B{Do I have<br/>this record?}
B -->|No| C[Ignore: record not found]
B -->|Yes| D[Merge HLLs and select<br/>highest record estimate]
D --> E{Am I already a keeper?<br/>my tombstone count >= target}
E -->|Yes| F{Is incoming tombstone<br/>count higher than mine?}
F -->|Yes| G[Step down as keeper:<br/>delete tombstone]
F -->|No| H{Same count but<br/>sender has lower node ID?}
H -->|Yes| G
H -->|No| I[Remain keeper:<br/>update tombstone]
E -->|No| J{Does my tombstone<br/>count reach target?}
J -->|Yes| K[Become keeper:<br/>store tombstone]
J -->|No| L[Store tombstone<br/>but not keeper yet]
G --> M[Forward tombstone to peers]
I --> M
K --> M
L --> M

The complete tombstone reception handler:

const receiveTombstone = <Data>(
  node: NodeState<Data>,
  incoming: Tombstone,
  senderNodeId: string
): NodeState<Data> => {
  // Don't accept tombstones for unknown records
  const record = node.records.get(incoming.id);
  if (!record) {
    return node;
  }

  const existing = node.tombstones.get(incoming.id);

  // Merge tombstone HLLs and add self
  const mergedTombstoneHLL = existing
    ? hllAdd(hllMerge(existing.tombstoneHLL, incoming.tombstoneHLL), node.id)
    : hllAdd(hllClone(incoming.tombstoneHLL), node.id);

  // Select the best (highest estimate) record HLL as target count
  // This ensures we use the most complete view of record distribution
  let bestRecordHLL = incoming.recordHLL;
  if (existing?.recordHLL) {
    bestRecordHLL = hllEstimate(existing.recordHLL) > hllEstimate(bestRecordHLL)
      ? existing.recordHLL
      : bestRecordHLL;
  }
  if (hllEstimate(record.recordHLL) > hllEstimate(bestRecordHLL)) {
    bestRecordHLL = hllClone(record.recordHLL);
  }

  const updatedTombstone: Tombstone = {
    id: incoming.id,
    tombstoneHLL: mergedTombstoneHLL,
    recordHLL: bestRecordHLL,
  };

  const myEstimateBeforeMerge = existing ? hllEstimate(existing.tombstoneHLL) : 0;

  const gcStatus = checkGCStatus(
    updatedTombstone,
    hllEstimate(incoming.tombstoneHLL),
    myEstimateBeforeMerge,
    node.id,
    senderNodeId
  );

  // Always delete the record when we have a tombstone
  const newRecords = new Map(node.records);
  newRecords.delete(incoming.id);

  if (gcStatus.stepDownAsKeeper) {
    // Step down: delete both record and tombstone
    const newTombstones = new Map(node.tombstones);
    newTombstones.delete(incoming.id);
    return { ...node, records: newRecords, tombstones: newTombstones };
  }

  const newTombstones = new Map(node.tombstones);
  newTombstones.set(incoming.id, updatedTombstone);
  return { ...node, records: newRecords, tombstones: newTombstones };
};

3.5 Cascading Step-Down via Forwarding

When a keeper steps down, it immediately forwards the tombstone to all connected peers, creating a cascade effect that rapidly eliminates redundant keepers:

const forwardTombstoneToAllPeers = <Data>(
  network: NetworkState<Data>,
  forwardingNodeId: string,
  tombstone: Tombstone,
  excludePeerId?: string
): NetworkState<Data> => {
const forwardingNode = network.nodes.get(forwardingNodeId);
if (!forwardingNode) return network;

let newNodes = new Map(network.nodes);

for (const peerId of forwardingNode.peerIds) {
  if (peerId === excludePeerId) continue;

  const peer = newNodes.get(peerId);
  if (!peer || !peer.records.has(tombstone.id)) continue;

  const updatedPeer = receiveTombstone(peer, tombstone, forwardingNodeId);
  newNodes.set(peerId, updatedPeer);

  // If this peer also stepped down, recursively forward
  if (!updatedPeer.tombstones.has(tombstone.id) && peer.tombstones.has(tombstone.id)) {
    const result = forwardTombstoneToAllPeers({ nodes: newNodes }, peerId, tombstone, forwardingNodeId);
    newNodes = new Map(result.nodes);
  }
}

return { nodes: newNodes };
};

4. Design Rationale

4.1 Why Propagate the Record HLL with Tombstones?

Without a shared target count, each node would compare against its own local recordHLL estimate, leading to premature garbage collection:

graph LR
subgraph Problem: Without Shared Target
A1["Node A: recordHLL=2"] -->|gossip| B1["Node B: recordHLL=2"]
B1 --> C1["Both estimate 2 nodes have record"]
C1 --> D1["tombstoneHLL reaches 2"]
D1 --> E1["GC triggers prematurely!"]
E1 --> F1["Node C still has record, resurrection"]
end

By propagating the recordHLL with the tombstone and always keeping the highest estimate encountered, all nodes converge on a safe target count. During propagation, if a node has a more complete view of record distribution (higher HLL estimate), that becomes the new target for all subsequent nodes.

4.2 Why Dynamic Keeper Election?

A fixed originator-as-keeper design creates a single point of failure. If the originator goes offline, tombstone propagation halts.

Dynamic election allows any node to become a keeper when it detects tombstoneCount >= recordCount. Multiple keepers provide redundancy during network partitions or node failures.

4.3 Why Keeper Step-Down?

Without step-down logic, every node eventually becomes a keeper (since they all eventually observe the threshold condition). This defeats the purpose of garbage collection.

Step-down creates convergence toward a minimal keeper set:

graph TD
subgraph Keeper Convergence Over Time
T0["t=0: 0 keepers"]
T1["t=1: 5 keepers<br/>(first nodes to detect threshold)"]
T2["t=2: 3 keepers<br/>(2 stepped down after seeing higher estimates)"]
T3["t=3: 1-2 keepers<br/>(most informed nodes remain)"]
end
T0 --> T1 --> T2 --> T3

4.4 Why Node ID Tie-Breaker?

When HLL estimates converge (all nodes have similar tombstoneHLL values due to full propagation), no node can have a strictly higher estimate. Without a tie-breaker, keepers with equal estimates would never step down.

The lexicographic node ID comparison ensures deterministic convergence: when two keepers with equal estimates communicate, the one with the higher node ID steps down. This guarantees eventual convergence to a single keeper per connected component.

4.5 Why Forward on Step-Down?

Without forwarding, keepers only step down when randomly selected for gossip - a slow process. With aggressive forwarding, a stepping-down keeper immediately propagates the "winning" tombstone to all neighbors, creating a cascade effect that rapidly eliminates redundant keepers.

5. Evaluation

5.1 Experimental Setup

We implemented a discrete-event simulation to evaluate the algorithm under various network conditions. The simulation models:

  • Gossip protocol: Each round, every node with a record or tombstone randomly selects one peer and exchanges state
  • HLL precision: 10 bits (1024 registers, ~1KB per HLL)
  • Convergence criteria: Records deleted, followed by 100 additional rounds for keeper convergence

5.2 Test Scenarios

5.2.1 Single Node Deletion

Scenario: A single node creates a record, propagates it through gossip, then initiates deletion.

graph TD
subgraph Network Topology 15 nodes 40 percent connectivity
N0((node-0<br/>originator))
N1((node-1))
N2((node-2))
N3((node-3))
N4((node-4))
N5((node-5))
N6((node-6))
N7((node-7))
N0 --- N1
N0 --- N3
N1 --- N2
N1 --- N4
N2 --- N5
N3 --- N4
N3 --- N6
N4 --- N5
N5 --- N7
N6 --- N7
end

Protocol:

  1. Node-0 creates record and propagates for 20 rounds
  2. Node-0 creates tombstone and initiates deletion
  3. Simulation runs until convergence

Results (averaged over 50 trials):

Metric Value
Nodes 15 per trial (750 total)
Records deleted 100% success
Rounds to delete records 11
Total rounds (including convergence) 121
Final tombstones 116 (~15.5% of nodes)

Analysis: Record deletion completes rapidly (11 rounds). Tombstone keeper count converges to approximately 2-3 keepers per trial, demonstrating effective garbage collection while maintaining redundancy.

5.2.2 Early Tombstone Creation

Scenario: Tombstone created before record fully propagates, testing the algorithm's handling of partial record distribution.

sequenceDiagram
participant N0 as Node-0
participant N1 as Node-1
participant N2 as Node-2
participant Nx as Nodes 3-19

Note over N0,Nx: Record only partially propagated
N0->>N1: record (round 1)
N1->>N2: record (round 2)
N2->>N0: record (round 3)

Note over N0: Create tombstone after only 3 rounds
N0->>N1: tombstone
N1->>N2: tombstone
Note over Nx: Most nodes never receive record

Results:

Metric Value
Nodes 20
Records deleted Yes
Rounds to delete records 10
Total rounds 120
Final tombstones 3 (15% of nodes)

Analysis: Even with partial record propagation, the algorithm correctly handles deletion. The propagated recordHLL accurately captures the distribution, updating as the tombstone encounters nodes with more complete views. Tombstones converge to nodes that actually received the record.

5.2.3 Bridged Network (Two Clusters)

Scenario: Two densely-connected clusters joined by a single bridge node, simulating common real-world topologies.

graph TD
subgraph Cluster A 15 nodes
A0((A-0<br/>bridge))
A1((A-1))
A2((A-2))
A3((A-3))
A0 --- A1
A0 --- A2
A1 --- A2
A1 --- A3
A2 --- A3
end

subgraph Cluster B 15 nodes
B0((B-0<br/>bridge))
B1((B-1))
B2((B-2))
B3((B-3))
B0 --- B1
B0 --- B2
B1 --- B2
B1 --- B3
B2 --- B3
end

A0 ===|single bridge| B0

Results:

Metric Cluster A Cluster B Total
Nodes 15 15 30
Records deleted Yes Yes Yes
Rounds to delete - - 10
Final tombstones 4 3 7 (23%)

Analysis: The single-bridge topology creates a natural partition point. Each cluster independently elects keepers, resulting in 2-4 keepers per cluster. This provides fault tolerance - if the bridge fails, each cluster retains tombstones independently.

5.2.4 Concurrent Tombstones

Scenario: Multiple nodes simultaneously initiate deletion of the same record, simulating concurrent delete operations.

sequenceDiagram
participant N0 as Node-0
participant N5 as Node-5
participant N10 as Node-10
participant Others as Other Nodes

Note over N0,Others: Record fully propagated (30 rounds)

par Concurrent deletion
N0->>N0: Create tombstone
N5->>N5: Create tombstone
N10->>N10: Create tombstone
end

Note over N0,Others: Three tombstones propagate and merge
N0->>Others: tombstone (from N0)
N5->>Others: tombstone (from N5)
N10->>Others: tombstone (from N10)

Note over N0,Others: HLLs merge, keepers converge

Results:

Metric Value
Nodes 20
Concurrent deleters 3
Records deleted Yes
Rounds to delete 10
Final tombstones 2 (10% of nodes)

Analysis: The algorithm handles concurrent tombstone creation gracefully. Multiple tombstones merge via HLL union operations, and keeper election converges as normal. The final keeper count (2) is actually lower than single-deleter scenarios, likely due to faster HLL convergence from multiple sources.

5.2.5 Network Partition and Heal

Scenario: Network partitions after record propagation, tombstone created in one partition, then network heals.

sequenceDiagram
participant CA as Cluster A
participant Bridge as Bridge
participant CB as Cluster B

Note over CA,CB: Phase 1: Record propagates to all nodes
CA->>Bridge: record
Bridge->>CB: record

Note over CA,CB: Phase 2: Network partitions
Bridge--xCB: connection lost

Note over CA: Cluster A creates tombstone
CA->>CA: tombstone propagates within A
Note over CB: Cluster B still has record

Note over CA,CB: Phase 3: Network heals
Bridge->>CB: tombstone propagates to B
CB->>CB: record deleted, keepers elected

Results:

Metric Cluster A Cluster B Total
Nodes 10 10 20
Records deleted Yes Yes Yes
Rounds to delete - - 10
Total rounds (partition + heal) - - 720
Final tombstones 3 2 5 (25%)

Analysis: The extended total rounds (720) includes the partition period where only Cluster A processes the tombstone. Upon healing, Cluster B rapidly receives and processes the tombstone. Each cluster maintains independent keepers, providing partition tolerance.

5.2.6 Sparse Network

Scenario: Low connectivity (15%) network, testing algorithm behavior under challenging propagation conditions.

graph TD
subgraph Sparse Network 25 nodes 15 percent connectivity
N0((0)) --- N3((3))
N3 --- N7((7))
N7 --- N12((12))
N0 --- N5((5))
N5 --- N9((9))
N9 --- N15((15))
N12 --- N18((18))
N18 --- N22((22))
N1((1)) --- N4((4))
N4 --- N8((8))
N2((2)) --- N6((6))
N6 --- N11((11))
N11 --- N16((16))
N16 --- N20((20))
N20 --- N24((24))
end

style N0 fill:#f96
style N24 fill:#9f9

Results (averaged over 20 trials):

Metric Value
Nodes 25 per trial (500 total)
Connectivity 15%
Records deleted 100% success
Rounds to delete 13
Total rounds 123
Final tombstones 102 (~20.4% of nodes)

Analysis: Sparse networks require more rounds for propagation (13 vs. 10-11 for denser networks) and retain more keepers (~20% vs. ~15%). The higher keeper retention provides additional redundancy appropriate for networks where nodes may have limited connectivity.

5.3 Summary of Results

Scenario Nodes Deletion Rounds Keeper % Key Insight
Single Node Deletion 15 11 15.5% Baseline performance
Early Tombstone 20 10 15% Handles partial propagation
Bridged Network 30 10 23% Independent keepers per cluster
Concurrent Tombstones 20 10 10% Faster convergence with multiple sources
Partition and Heal 20 10 25% Partition-tolerant
Sparse Network 25 13 20.4% Graceful degradation

5.4 Key Findings

  1. Consistent deletion: Records are deleted within 10-13 gossip rounds across all scenarios
  2. Effective GC: Tombstones converge to 10-25% of nodes as keepers
  3. Topology adaptation: Bridged and partitioned networks maintain ~1-4 keepers per cluster
  4. Graceful degradation: Lower connectivity increases keeper retention, providing appropriate redundancy
  5. Concurrent safety: Multiple simultaneous deleters do not cause conflicts

6. Trade-offs

Aspect Impact
Memory ~1KB per tombstone (HLL at precision 10)
Bandwidth HLLs transmitted with each gossip message (~2KB per tombstone message)
Latency GC delayed until keeper convergence (~100 rounds after deletion)
Consistency Eventual - temporary resurrection attempts are blocked but logged

7. Properties

The algorithm provides the following guarantees:

  • Safety: Tombstones are never prematurely garbage collected. A tombstone is only deleted when the node has received confirmation (via HLL estimates) that the tombstone has propagated to at least as many nodes as received the original record.

  • Liveness: Keepers eventually step down, enabling garbage collection. The tie-breaker mechanism ensures convergence even when HLL estimates are identical.

  • Fault tolerance: No single point of failure. Multiple keepers provide redundancy, and any keeper can propagate the tombstone.

  • Convergence: Keeper count monotonically decreases over time within each connected component.

8. Conclusion

This paper presented a HyperLogLog-based approach to tombstone garbage collection in distributed systems. By tracking record and tombstone propagation through probabilistic cardinality estimation, the algorithm enables safe garbage collection while reducing storage overhead by 75-90%.

The simulation results demonstrate consistent behavior across diverse network topologies and failure scenarios, with records deleted in 10-13 gossip rounds and tombstones converging to 10-25% of nodes as keepers. The algorithm gracefully handles partial propagation, network partitions, and concurrent deletions.

Future work may explore adaptive HLL precision based on network size, integration with vector clocks for stronger consistency guarantees, and optimization of the keeper convergence rate.

References

A working simulation implementing this algorithm is available at simulations/hyperloglog-tombstone/simulation.ts.


  1. [TODO: Cite HyperLogLog paper - Flajolet et al., "HyperLogLog: the analysis of a near-optimal cardinality estimation algorithm"] ↩︎