Mesh LLM + iroh: Building Distributed LLM Inference
On this page 5
Distributed LLM Inference: Why Mesh LLM on iroh?
Running large language models for inference at scale presents immediate memory and throughput challenges. A 70B parameter model, quantized to FP16, requires over 140GB of VRAM. This exceeds the capacity of most single GPUs, necessitating model distribution across multiple accelerators or nodes.
Distributing a model introduces complexity: sharding the model weights, coordinating forward passes, and exchanging intermediate activations between shards. Traditional solutions often rely on centralized orchestrators, complex RPC frameworks, or cloud-specific infrastructure. These approaches introduce single points of failure, increase operational overhead, and can incur significant egress costs for inter-node communication.
Mesh LLM addresses these issues by providing a decentralized framework for distributed inference. It orchestrates the partitioning of large models across a cluster of nodes, managing the flow of tokens and intermediate tensor exchanges. This peer-to-peer approach removes the need for a central control plane during inference, distributing coordination responsibilities among the participating nodes.
iroh acts as the foundational network layer for Mesh LLM. It provides a content-addressed, peer-to-peer data transfer protocol that simplifies network topology and connectivity. iroh enables direct communication between inference nodes, facilitating efficient exchange of model weights during setup and intermediate activations during the forward pass, even across NATs without complex firewall configurations.
By combining Mesh LLM’s decentralized inference coordination with iroh’s robust peer-to-peer data layer, we achieve a resilient and efficient distributed system. iroh ensures that model shards can locate and communicate with each other directly, minimizing latency and maximizing throughput. This setup reduces reliance on centralized services, lowering infrastructure costs and improving fault tolerance.
Mesh LLM Architecture: Design & Tradeoffs
Distributed LLM inference requires efficient data movement and precise coordination across multiple machines. The Mesh LLM architecture addresses this by separating inference orchestration from data plane operations. An orchestrator component manages the high-level inference flow, while worker nodes execute model partitions and tensor computations.
iroh serves as the underlying data plane for Mesh LLM. It handles content-addressed distribution of model weights, intermediate tensors, and inference results. When a worker node requires a specific model shard or an input tensor, it requests the data by its iroh hash. iroh’s peer-to-peer capabilities then locate and transfer the data from any available peer, whether that’s another worker, the orchestrator, or a dedicated storage node.
For model distribution, Mesh LLM shards large models into smaller, manageable layers or tensor blocks. Each shard is stored in iroh as a Blob or a Collection. The orchestrator informs workers which shards are needed for a given inference task, and workers use iroh to fetch these components. This approach eliminates a central bottleneck for data transfer, spreading bandwidth use across the network. The tradeoff is increased complexity in peer discovery and initial data availability compared to a centralized file server.
An inference request initiates with the orchestrator, which decomposes the prompt into a sequence of operations and assigns them to available worker nodes. Workers fetch necessary input tensors, process their assigned model segment, and publish output tensors (e.g., activations, logits) back to iroh. The orchestrator monitors progress and aggregates final results.
use iroh::client::Client;
use iroh::bytes::BlobFormat;
async fn store_model_shard(client: &Client, data: &[u8]) -> anyhow::Result<iroh::bytes::Hash> {
let hash = client.blobs.add_bytes(data.to_vec(), BlobFormat::Raw).await?;
println!("Stored model shard with hash: {}", hash);
Ok(hash)
}
// Example output:
// Stored model shard with hash: bafkreihx53d2r362p6x4s4g4d7o6v3f2x4e6c4a6b2a4c2d4e2f4g2h4i2j4k2l4m2n4o2p4q2r4s4t2u4v2w4x2y4z2
This content-addressable storage ensures data integrity and simplifies caching. Any node can verify the data it receives against its hash. The primary architectural consideration here is balancing the overhead of P2P discovery and connection establishment against the benefits of decentralized data availability and reduced single-point-of-failure risk. This design allows for dynamic scaling of worker nodes without reconfiguring a central data store.
Building Mesh LLM Inference: Step-by-Step Implementation
Deploying a multi-node Mesh LLM inference service begins with preparing the environment and binaries. Ensure Rust and Cargo are installed. Clone the iroh and mesh-llm repositories from their respective sources to a local development machine or a build server.
git clone https://github.com/n0-computer/iroh.git
git clone https://github.com/n0-computer/mesh-llm.git
Navigate into each repository and compile the release binaries. This process builds optimized executables for the target architecture. The resulting binaries, iroh (if standalone iroh daemon is used) and mesh-llm, will be located in the target/release/ directory within their respective project folders.
cd iroh && cargo build --release
cd ../mesh-llm && cargo build --release
Initialize the coordinator node. This node manages model distribution and orchestrates inference requests across the mesh. Start the mesh-llm coordinator, specifying a local data directory for iroh state and the path to the complete LLM model. The coordinator will output its PeerId, which other nodes use to connect.
./target/release/mesh-llm coordinator \
--model-path /mnt/models/llama-7b-full.gguf \
--iroh-data-dir /var/lib/mesh-llm/coordinator
# Output will include: Coordinator PeerId: <COORDINATOR_PEER_ID>
On each worker node, start a mesh-llm worker instance. Provide the PeerId of the coordinator and the path to the model shard or full model available on that specific worker. Workers register themselves with the coordinator and await task assignments. Each worker also requires a unique iroh data directory.
./target/release/mesh-llm worker \
--coordinator-peer-id <COORDINATOR_PEER_ID> \
--model-path /mnt/models/llama-7b-shard-a.gguf \
--iroh-data-dir /var/lib/mesh-llm/worker1
After all workers connect, verify the mesh operation by submitting an inference request from a client. The client connects to the coordinator using its PeerId and sends a prompt. The coordinator distributes the request, aggregates responses from workers, and returns the final output.
./target/release/mesh-llm client \
--coordinator-peer-id <COORDINATOR_PEER_ID> \
--prompt "Explain the concept of distributed consensus in two sentences."
Monitor the logs on the coordinator and worker nodes for confirmation of task distribution and inference processing. This confirms the multi-node setup is functional and ready for production workloads.
Mesh LLM Verification: Performance & Stability
Production distributed LLM inference requires rigorous verification beyond functional tests. The system must deliver correct outputs consistently, maintain performance under load, and distribute work effectively across nodes. This validates the architecture and identifies bottlenecks before deployment.
Begin with correctness testing. Establish a baseline by running a fixed set of prompts against a single LLM instance, recording expected outputs. Then, deploy the Mesh LLM service with a minimal configuration. Send the same prompts through the distributed system and compare outputs against the baseline. Any deviation indicates a critical issue with request routing, model loading, or inference execution within the distributed setup.
Performance validation requires load generation. Tools like k6 or locust can simulate concurrent user requests. Target the Mesh LLM service endpoint with varying prompt lengths and concurrency levels. Monitor key metrics: end-to-end latency (p90, p99), requests per second (RPS) throughput, and error rates. Observe resource use on individual nodes, specifically GPU memory and compute, to identify saturation points.
For example, a k6 script can simulate requests:
// k6_test.js
import http from 'k6/http';
import { check, sleep } from 'k6';
export default function () {
const url = 'http://localhost:8080/infer'; // Your Mesh LLM API endpoint
const payload = JSON.stringify({
prompt: 'Explain the concept of distributed consensus in one sentence.',
max_tokens: 50
});
const params = {
headers: {
'Content-Type': 'application/json',
},
};
const res = http.post(url, payload, params);
check(res, { 'status is 200': (r) => r.status === 200 });
sleep(0.1);
}
Run with:
k6 run --vus 10 --duration 30s k6_test.js
Effective load balancing is verified by observing work distribution. As load increases, each node should show a proportional increase in resource use and processed requests. Monitor individual node logs or metrics (e.g., Prometheus exporters for GPU use) to ensure no single node becomes a hot spot while others remain idle. Introduce node failures during a load test to confirm the system re-routes requests and maintains availability, albeit potentially with reduced aggregate throughput. This confirms the iroh-based discovery and routing mechanisms function as expected under stress.
A common pitfall is uneven request distribution due to client-side caching or sticky sessions. Ensure the load generator distributes requests randomly or round-robin across available service instances if hitting them directly, or trusts the Mesh LLM entry point to handle distribution. The goal is to stress the internal load balancing logic.
Ship Mesh LLM: Production Readiness Checklist
Production readiness for Mesh LLM requires validating security postures, establishing observability, and refining deployment mechanics. This ensures the distributed inference service operates reliably and securely under load.
Implement strict access controls for iroh nodes. Iroh uses capability-based security; define which peers can publish models, request inferences, or retrieve results. Use iroh’s key management to provision distinct identities and apply ACLs that limit peer interactions to their minimum required scope.
Network segmentation isolates iroh peers and LLM hosts. Configure firewalls to restrict inbound and outbound traffic to only necessary ports and protocols. Store iroh private keys, model access tokens, and other sensitive data in a secrets management system, not in source control or plain text configuration files.
Collect key metrics from each LLM worker and iroh node. Monitor inference latency (p95, p99), request throughput, error rates, and resource use (GPU, CPU, memory). Expose these metrics via a standard endpoint, allowing integration with existing monitoring stacks.
$ curl http://localhost:8080/metrics
# HELP iroh_node_peers_count Number of connected peers
iroh_node_peers_count 5
# HELP llm_inference_requests_total Total number of inference requests
llm_inference_requests_total{model="llama3-8b"} 12345
# HELP llm_inference_latency_seconds Inference latency in seconds
llm_inference_latency_seconds_bucket{le="0.1",model="llama3-8b"} 12000
llm_inference_latency_seconds_bucket{le="0.5",model="llama3-8b"} 12300
Implement structured logging across all service components. Centralize logs for correlation and analysis, including unique request IDs to trace inference requests end-to-end. Configure threshold-based alerts for critical operational metrics, such as sustained high error rates or elevated inference latency, integrating with on-call notification systems. For deep visibility, instrument the distributed request path with OpenTelemetry to trace calls across iroh nodes and LLM workers.
Automate deployment pipelines using CI/CD. Externalize all environment-specific configurations, use environment variables or templated configuration files. Conduct comprehensive load testing against expected and peak traffic profiles. Validate that the service meets target QPS, latency SLAs, and resource consumption limits before promoting to production.
Spotted an error? Tell us via the corrections process — verified reports get fixed and credited.