SuperCache.Cluster.Replicator (SuperCache v1.7.0)

Copy Markdown View Source

Applies replicated writes on the local node and handles bulk partition transfers when a new node joins or a full sync is requested.

Replication Modes

  • :async — Fire-and-forget via a lightweight worker pool (Task.Supervisor). Eliminates per-operation spawn/1 overhead.
  • :sync — Adaptive quorum writes. Returns :ok once a strict majority of replicas acknowledge, avoiding waits for slow stragglers.
  • :strong — Write-Ahead Log (WAL) based consistency. Replaces heavy 3PC with fast local write + async replication + majority ack (~200µs vs ~1500µs).

Every call to replicate/3 increments the appropriate replicate_<mode> counter in SuperCache.Cluster.Metrics and records a latency sample so the results are visible in SuperCache.Cluster.Stats.api/0.

Async replication semantics

An :async write returns after the local apply and a supervised handoff to the replication worker pool. Workers retry transient replica failures with bounded backoff, controlled by :async_max_retries (default 3) and :async_retry_backoff_ms (default 50) under the :super_cache, :replication application environment. Failures are logged and counted in replication/async_failures; deliveries that still fail after the retry budget are counted in replication/async_drops. A full or unavailable pool also increments async_drops immediately and the write still succeeds locally. Neither failure path blocks the caller, and no task-result message is left in the caller's mailbox.

Summary

Functions

Apply a replicated operation on this node.

Apply a batch of replicated operations on this node.

Push all records for partition_idx to target_node in bounded batches.

Replicate a batch of operations to all replicas in a single :erpc call.

Functions

apply_op(partition_idx, op_name, op_arg)

@spec apply_op(non_neg_integer(), atom(), any()) :: :ok

Apply a replicated operation on this node.

Called via :erpc from the primary or worker pool — do NOT call directly.

apply_op_batch(partition_idx, op_name, op_args)

@spec apply_op_batch(non_neg_integer(), atom(), [any()]) :: :ok

Apply a batch of replicated operations on this node.

Called via :erpc from the primary — do NOT call directly.

push_partition(partition_idx, target_node)

@spec push_partition(non_neg_integer(), node()) ::
  :ok | {:error, {:push_failed, non_neg_integer(), non_neg_integer()}}

Push all records for partition_idx to target_node in bounded batches.

Records are streamed from ETS in @batch_size chunks and each chunk is retried with bounded backoff. Successful batches are kept even when a later batch fails, and the error reports the exact failed and total record counts.

replicate(partition_idx, op_name, op_arg \\ nil)

@spec replicate(non_neg_integer(), atom(), any()) :: :ok | {:error, term()}

replicate_batch(partition_idx, op_name, op_args)

@spec replicate_batch(non_neg_integer(), atom(), [any()]) :: :ok | {:error, term()}

Replicate a batch of operations to all replicas in a single :erpc call.

Dramatically reduces network overhead for bulk writes by sending all operations in one message instead of one message per operation.