Skip to content

Commit c46930d

Browse files
authored
[Docs] Add Mooncake HA redis backend deployment example (#8058)
1 parent 5b3dd38 commit c46930d

6 files changed

Lines changed: 459 additions & 114 deletions

File tree

docs/features/global_cache_pooling.md

Lines changed: 86 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ Ready-to-use example scripts are available in [examples/cache_storage/](../../..
4848
|--------|----------|-------------|
4949
| `run.sh` | Multi-Instance | Two standalone instances sharing cache |
5050
| `run_03b_pd_storage.sh` | PD Disaggregation | P+D instances with global cache pooling |
51-
| `run_ha.sh` | High Availability | etcd + multi-master leader election, verifies failover after killing the leader |
51+
| `run_ha.sh` | High Availability (etcd) | etcd + multi-master leader election, verifies failover after killing the leader |
52+
| `run_ha_redis.sh` | High Availability (redis) | single redis + multi-master leader election, verifies failover after killing the leader |
5253

5354
## Prerequisites
5455

@@ -287,14 +288,19 @@ curl -X POST "http://0.0.0.0:52700/v1/chat/completions" \
287288

288289
### Scenario 3: High-Availability (HA) Deployment
289290

290-
A single master is a single point of failure; if it crashes, cluster operations pause. For production, use the **etcd + multi-master** mode: multiple `mooncake_master` instances perform leader election through etcd. When the leader fails, a standby is automatically re-elected, transparently to clients.
291+
A single master is a single point of failure; if it crashes, cluster operations pause. For production, run multiple `mooncake_master` instances that perform leader election through a coordination backend. When the leader fails, a standby is automatically re-elected, transparently to clients.
292+
293+
Two coordination backends are supported:
294+
295+
- **etcd** (`run_ha.sh`): a 3-node etcd cluster does election and metadata storage.
296+
- **redis** (`run_ha_redis.sh`): a single redis instance does lease-based election. Use this to avoid introducing etcd as an extra component.
291297

292298
**Architecture:**
293299

294300
```
295301
┌──────────────────────────────────────┐
296-
etcd cluster (3 nodes)
297-
│ leader election / metadata store
302+
coordination backend (etcd / redis)
303+
│ leader election (master_view)
298304
└───────────────────┬──────────────────┘
299305
│ election (master_view)
300306
┌─────────────────────┼─────────────────────┐
@@ -304,47 +310,30 @@ A single master is a single point of failure; if it crashes, cluster operations
304310
│ rpc:8081 │ │ rpc:8082 │ │ rpc:8083 │
305311
│ (leader) │ │ (standby) │ │ (standby) │
306312
└──────┬──────┘ └─────────────┘ └─────────────┘
307-
│ FastDeploy clients discover the current leader via etcd
313+
│ FastDeploy clients discover the current leader via the backend
308314
┌──────┴───────┐
309315
▼ ▼
310316
server_0 server_1
311317
```
312318

313-
#### Prerequisites
314-
315-
**1. Install etcd**
316-
317-
Download and extract etcd (v3.5.30 in this example), then add `etcd` / `etcdctl` to `PATH`:
318-
319-
```bash
320-
ETCD_VER=v3.5.30
321-
curl -L https://github.com/etcd-io/etcd/releases/download/${ETCD_VER}/etcd-${ETCD_VER}-linux-amd64.tar.gz \
322-
-o etcd-${ETCD_VER}-linux-amd64.tar.gz
323-
tar -xzf etcd-${ETCD_VER}-linux-amd64.tar.gz
324-
export PATH=$PWD/etcd-${ETCD_VER}-linux-amd64:$PATH
325-
etcd --version
326-
```
319+
#### Build Mooncake from source
327320

328-
**2. Build Mooncake from source (with etcd support)**
321+
HA mode requires Mooncake built with the matching backend enabled:
329322

330-
HA mode requires Mooncake built with etcd support (`-DSTORE_USE_ETCD=ON -DUSE_ETCD=ON`). Install dependencies first, then build:
323+
- etcd: `-DSTORE_USE_ETCD=ON -DUSE_ETCD=ON`
324+
- redis: `-DSTORE_USE_REDIS=ON -DUSE_REDIS=ON` (build dependency: `libhiredis-dev`)
331325

332326
```bash
333-
# Download the source
334327
git clone https://github.com/kvcache-ai/Mooncake.git
335328
cd Mooncake
336-
337-
# Install system & third-party dependencies
338329
bash dependencies.sh
339330

340-
# Build C++ components (including mooncake_master, with etcd enabled)
341331
mkdir -p build && cd build
342-
cmake .. -DSTORE_USE_ETCD=ON -DUSE_ETCD=ON
332+
cmake .. -DSTORE_USE_ETCD=ON -DUSE_ETCD=ON # add -DSTORE_USE_REDIS=ON -DUSE_REDIS=ON for redis
343333
make -j
344334
sudo make install
345335
cd ..
346336

347-
# Build and install the Python wheel
348337
./scripts/build_wheel.sh
349338
pip install mooncake-wheel/dist/*.whl
350339
```
@@ -358,9 +347,24 @@ export CU13_BUILD=1
358347
pip install mooncake-wheel/dist/mooncake_transfer_engine_cuda13-*.whl
359348
```
360349

361-
#### HA Client Configuration
350+
#### Option A: etcd backend (`run_ha.sh`)
351+
352+
**1. Install etcd**
353+
354+
Download and extract etcd (v3.5.30 in this example), then add `etcd` / `etcdctl` to `PATH`:
355+
356+
```bash
357+
ETCD_VER=v3.5.30
358+
curl -L https://github.com/etcd-io/etcd/releases/download/${ETCD_VER}/etcd-${ETCD_VER}-linux-amd64.tar.gz \
359+
-o etcd-${ETCD_VER}-linux-amd64.tar.gz
360+
tar -xzf etcd-${ETCD_VER}-linux-amd64.tar.gz
361+
export PATH=$PWD/etcd-${ETCD_VER}-linux-amd64:$PATH
362+
etcd --version
363+
```
364+
365+
**2. Client configuration** (`ha_mooncake_config.json`)
362366

363-
In HA mode, both `metadata_server` and `master_server_addr` use the `etcd://` prefix pointing to the etcd cluster; clients discover the current leader through etcd (`ha_mooncake_config.json`):
367+
Both `metadata_server` and `master_server_addr` use the `etcd://` prefix; clients discover the current leader through etcd:
364368

365369
```json
366370
{
@@ -373,45 +377,77 @@ In HA mode, both `metadata_server` and `master_server_addr` use the `etcd://` pr
373377
}
374378
```
375379

376-
#### One-Command Launch & Failover Verification
380+
**3. Run**
381+
382+
```bash
383+
cd examples/cache_storage
384+
bash run_ha.sh
385+
```
377386

378-
A single self-contained script `examples/cache_storage/run_ha.sh` handles the whole flow — it starts the etcd cluster and the HA master cluster inline (each via a 3-iteration loop), so no separate `start_*.sh` is needed.
387+
The script starts a 3-node etcd cluster (client ports 12379/22379/32379), 3 HA masters (rpc 8081/8082/8083), and 2 FastDeploy instances; the leader address is written to the etcd key `mooncake-store/mooncake_cluster/master_view`.
379388

380-
Run directly:
389+
Inspect the current leader manually:
390+
391+
```bash
392+
etcdctl --endpoints=http://127.0.0.1:12379,http://127.0.0.1:22379,http://127.0.0.1:32379 \
393+
get "mooncake-store/mooncake_cluster/master_view" --print-value-only
394+
```
395+
396+
#### Option B: redis backend (`run_ha_redis.sh`)
397+
398+
**1. Client configuration** (`ha_redis_mooncake_config.json`)
399+
400+
Both `metadata_server` and `master_server_addr` use the `redis://` prefix pointing to the single redis instance:
401+
402+
```json
403+
{
404+
"metadata_server": "redis://127.0.0.1:6399",
405+
"global_segment_size": 1000000000,
406+
"local_buffer_size": 134217728,
407+
"protocol": "rdma",
408+
"rdma_devices": "",
409+
"master_server_addr": "redis://127.0.0.1:6399"
410+
}
411+
```
412+
413+
**2. Run**
381414

382415
```bash
383416
cd examples/cache_storage
384-
bash run_ha.sh
417+
bash run_ha_redis.sh
385418
```
386419

387-
What `run_ha.sh` does:
420+
The script starts a single redis instance (port 6399), 3 HA masters (rpc 8081/8082/8083) launched with `--ha_backend_type redis --ha_backend_connstring redis://127.0.0.1:6399`, and 2 FastDeploy instances. The master_view is a redis HASH at `mooncake-store/{mooncake_cluster}/master_view`.
421+
422+
Inspect the current leader manually:
423+
424+
```bash
425+
redis-cli -p 6399 hget "mooncake-store/{mooncake_cluster}/master_view" leader_address
426+
```
427+
428+
#### What the HA scripts verify
429+
430+
Both scripts run the same flow and verify failover:
388431

389-
1. **Start the etcd cluster**: a loop launches 3 etcd nodes (client ports 12379/22379/32379) forming a raft cluster, after a port check.
390-
2. **Start 3 HA masters**: a loop launches 3 `mooncake_master` (rpc 8081/8082/8083, metrics 9091/9092/9093), each with `--enable_ha --etcd_endpoints ... --rpc_port ...`, electing one leader via etcd. The leader address is written to the etcd key `mooncake-store/mooncake_cluster/master_view`.
391-
3. **Start 2 FastDeploy instances**, both joining the same cache pool with `--kvcache-storage-backend mooncake`.
392-
4. **Verify pooling (before failover)**: warm up prompt **A** on `server_0`, then send the same prompt to `server_1`, which should hit the global cache.
393-
5. **Kill the leader**: the script reads the current leader's `rpc_port` from etcd, `kill -9`s that process, triggering re-election.
394-
6. **Verify pooling (after failover)**: once etcd's `master_view` is updated to the new leader, warm up a **brand-new** prompt **B** (never sent before) on `server_0`, then reuse it on `server_1`. Using a fresh prompt ensures the hit on `server_1` can only come from the new leader's global pool, rather than stale local cache from step 4.
432+
1. Start the coordination backend (etcd cluster / single redis).
433+
2. Start 3 HA masters; one is elected leader and published to `master_view`.
434+
3. Start 2 FastDeploy instances, both joining the same cache pool with `--kvcache-storage-backend mooncake`.
435+
4. **Before failover**: warm up prompt **A** on `server_0`, then send the same prompt to `server_1`, which should hit the global cache.
436+
5. **Kill the leader**: read the current leader's `rpc_port` from the backend and `kill -9` it, triggering re-election.
437+
6. **After failover**: once `master_view` updates to the new leader, warm up a **brand-new** prompt **B** on `server_0`, then reuse it on `server_1`. Using a fresh prompt ensures the hit can only come from the new leader's global pool, not stale local cache from step 4.
395438

396-
> Check the election state manually:
397-
>
398-
> ```bash
399-
> # Current leader (rpc_address:rpc_port)
400-
> etcdctl --endpoints=http://127.0.0.1:12379,http://127.0.0.1:22379,http://127.0.0.1:32379 \
401-
> get "mooncake-store/mooncake_cluster/master_view" --print-value-only
402-
> ```
403-
>
404-
> Per-master roles can be seen in `log_master_1` / `log_master_2` / `log_master_3` (`role=leader` / `role=standby`), and etcd logs in `log_etcd_1` / `log_etcd_2` / `log_etcd_3`.
439+
Per-master roles can be seen in `log_master_1` / `log_master_2` / `log_master_3` (`role=leader` / `role=standby`).
405440

406441
#### Key HA Master Parameters
407442

408443
| Parameter | Description |
409444
|-----------|-------------|
410445
| `--enable_ha` | Enable HA mode |
446+
| `--ha_backend_type` | Coordination backend: `etcd` (default) or `redis` |
411447
| `--etcd_endpoints` | etcd endpoints, semicolon separated (when `ha_backend_type=etcd`) |
448+
| `--ha_backend_connstring` | Backend connection string, e.g. `redis://127.0.0.1:6399` (when `ha_backend_type=redis`) |
412449
| `--rpc_address` / `--rpc_port` | This master's reachable RPC address and port (must be unique per instance) |
413450
| `--cluster_id` | Cluster identifier; masters in the same cluster must match |
414-
| `--root_fs_dir` | Storage root directory in HA mode (unique per instance) |
415451

416452
## FastDeploy Parameters for Mooncake
417453

0 commit comments

Comments
 (0)