Skip to content

Commit 0c76fa6

Browse files
docs(fortuna): add comprehensive multi-replica setup documentation
- Add Multiple Replica Setup section to README.md with modulo assignment explanation - Add replica_config examples to config.sample.yaml for 2, 3, and 5 replica setups - Include deployment considerations, failover behavior, and wallet separation requirements - Add validation for backup_delay_seconds > 0 to prevent race conditions Co-Authored-By: Tejas Badadare <[email protected]>
1 parent a66fe6e commit 0c76fa6

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed

apps/fortuna/README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,60 @@ Please add the changed files in the `.sqlx` folder to your git commit.
4040
The Fortuna binary has a command-line interface to perform useful operations on the contract, such as
4141
registering a new randomness provider, or drawing a random value. To see the available commands, simply run `cargo run`.
4242

43+
## Multiple Replica Setup
44+
45+
Fortuna supports running multiple replica instances for high availability and reliability. This prevents service interruption if one instance goes down and distributes the workload across multiple instances.
46+
47+
### How Replica Assignment Works
48+
49+
- Each replica is assigned a unique `replica_id` (0, 1, 2, etc.)
50+
- Requests are distributed using modulo assignment: `sequence_number % total_replicas`
51+
- Each replica primarily handles requests assigned to its ID
52+
- After a configurable delay, replicas will process requests from other replicas as backup (failover)
53+
54+
### Example Configurations
55+
56+
**Two Replica Setup (Blue/Green):**
57+
```yaml
58+
# Replica 0 (Blue) - handles even sequence numbers (0, 2, 4, ...)
59+
keeper:
60+
replica_config:
61+
replica_id: 0
62+
total_replicas: 2
63+
backup_delay_seconds: 30
64+
65+
# Replica 1 (Green) - handles odd sequence numbers (1, 3, 5, ...)
66+
keeper:
67+
replica_config:
68+
replica_id: 1
69+
total_replicas: 2
70+
backup_delay_seconds: 30
71+
```
72+
73+
**Three Replica Setup:**
74+
```yaml
75+
# Replica 0 - handles sequence numbers 0, 3, 6, 9, ...
76+
keeper:
77+
replica_config:
78+
replica_id: 0
79+
total_replicas: 3
80+
backup_delay_seconds: 45
81+
```
82+
83+
### Deployment Considerations
84+
85+
1. **Separate Wallets**: Each replica MUST use a different private key to avoid nonce conflicts
86+
2. **Backup Delay**: Set `backup_delay_seconds` long enough to allow primary replica to process requests, but short enough for acceptable failover time (recommended: 30-60 seconds)
87+
3. **Monitoring**: Monitor each replica's processing metrics to ensure proper load distribution
88+
4. **Gas Management**: Each replica needs sufficient ETH balance for gas fees
89+
90+
### Failover Behavior
91+
92+
- Primary replica processes requests immediately
93+
- Backup replicas wait for `backup_delay_seconds` before checking if request is still unfulfilled
94+
- If request is already fulfilled during the delay, backup replica skips processing
95+
- This prevents duplicate transactions and wasted gas while ensuring reliability
96+
4397
## Local Development
4498

4599
To start an instance of the webserver for local testing, you first need to perform a few setup steps:

apps/fortuna/config.sample.yaml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,31 @@ keeper:
8888
value: 0xabcd
8989
# For production, you can store the private key in a file.
9090
# file: keeper-key.txt
91+
# Multi-replica configuration documentation
92+
93+
# Optional: Multi-replica configuration for high availability and load distribution
94+
# Uncomment and configure for production deployments with multiple Fortuna instances
95+
# replica_config:
96+
# replica_id: 0 # Unique identifier for this replica (0, 1, 2, ...)
97+
# total_replicas: 2 # Total number of replica instances running
98+
# backup_delay_seconds: 30 # Seconds to wait before processing other replicas' requests
99+
#
100+
# Example configurations:
101+
#
102+
# Two-replica setup (Blue/Green):
103+
# - Replica 0: handles even sequence numbers (0, 2, 4, ...)
104+
# - Replica 1: handles odd sequence numbers (1, 3, 5, ...)
105+
#
106+
# Three-replica setup:
107+
# - Replica 0: handles sequence numbers 0, 3, 6, 9, ...
108+
# - Replica 1: handles sequence numbers 1, 4, 7, 10, ...
109+
# - Replica 2: handles sequence numbers 2, 5, 8, 11, ...
110+
#
111+
# Five-replica setup:
112+
# - Replica 0: handles sequence numbers 0, 5, 10, 15, ...
113+
# - Replica 1: handles sequence numbers 1, 6, 11, 16, ...
114+
# - Replica 2: handles sequence numbers 2, 7, 12, 17, ...
115+
# - Replica 3: handles sequence numbers 3, 8, 13, 18, ...
116+
# - Replica 4: handles sequence numbers 4, 9, 14, 19, ...
117+
#
118+
# IMPORTANT: Each replica MUST use a different private_key to avoid nonce conflicts!

apps/fortuna/src/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ impl Config {
101101
if replica_config.replica_id >= replica_config.total_replicas {
102102
return Err(anyhow!("Keeper replica configuration is invalid. replica_id must be less than total_replicas."));
103103
}
104+
if replica_config.backup_delay_seconds == 0 {
105+
return Err(anyhow!("Keeper replica configuration is invalid. backup_delay_seconds must be greater than 0 to prevent race conditions."));
106+
}
104107
}
105108

106109
Ok(config)

0 commit comments

Comments
 (0)