# `SuperCache.Cluster.Replicator`
[🔗](https://github.com/ohhi-vn/super_cache/blob/main/lib/cluster/replicator.ex#L1)

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`.

# `apply_op`

```elixir
@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`

```elixir
@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`

```elixir
@spec push_partition(non_neg_integer(), node()) :: :ok
```

Push all records for `partition_idx` to `target_node` in batches.

# `replicate`

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

# `replicate_batch`

```elixir
@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.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
