Replication Policies¶
Replication policies provide high-level patterns for common replication topologies, managing the underlying replication tasks automatically.
Patterns Overview¶
| Pattern | Topology | Use Case |
|---|---|---|
| Chain | A → B → C | Write to head, read from tail |
| Group | A ↔ B ↔ C | Multi-master sync |
| Fanout | A → B, C, D | Event distribution |
Chain Replication¶
Linear replication where each node replicates to the next.
┌───────┐ ┌───────┐ ┌───────┐
│ Node A│ ──► │ Node B│ ──► │ Node C│
│ (head)│ │ │ │ (tail)│
└───────┘ └───────┘ └───────┘
write read
Use cases: - Strong consistency with read scaling - Disaster recovery chains
curl -X POST http://localhost:8080/_policies \
-H "Content-Type: application/json" \
-d '{
"name": "my_chain",
"pattern": "chain",
"nodes": [
"http://nodeA:8080",
"http://nodeB:8080",
"http://nodeC:8080"
],
"database": "mydb",
"mode": "continuous"
}'
curl -X POST http://localhost:8080/_policies/my_chain/_enable
Group Replication (Multi-Master)¶
Bidirectional replication between all members.
Use cases: - Multi-region active-active - High availability clusters
Fanout Replication¶
One source replicates to multiple targets.
┌───────┐
┌──► │ Rep 1 │
│ └───────┘
┌───────┐│ ┌───────┐
│ Source│├──► │ Rep 2 │
└───────┘│ └───────┘
│ ┌───────┐
└──► │ Rep 3 │
└───────┘
Use cases: - Event distribution - Read replicas - Analytics pipelines
Policy Management¶
List Policies¶
Get Policy Details¶
Enable/Disable¶
curl -X POST http://localhost:8080/_policies/my_chain/_enable
curl -X POST http://localhost:8080/_policies/my_chain/_disable
Check Status¶
Delete Policy¶
Filtering¶
Apply filters to policies:
barrel_rep_policy:create(<<"filtered_sync">>, #{
pattern => group,
members => [<<"db1">>, <<"http://remote:8080/db1">>],
mode => continuous,
filter => #{
paths => [<<"users/#">>, <<"orders/#">>],
query => #{where => [{path, [<<"active">>], true}]}
}
}).
Modes¶
| Mode | Description |
|---|---|
continuous |
Keep replicating indefinitely |
one_shot |
Replicate once then stop |
Sync Writes¶
Wait for replication to complete before returning:
{ok, _} = barrel_docdb:put_doc(<<"mydb">>, Doc, #{
replicate => sync,
wait_for => [<<"http://nodeC:8080/db/mydb">>]
}).
Best Practices¶
- Start with simple topologies - Add complexity only when needed
- Monitor policy status - Check for failed tasks
- Use filters - Don't replicate more data than necessary
- Consider network partitions - Policies handle reconnection automatically
- Test failover - Ensure your topology handles node failures