
Agent Swarms at Scale: Horizontal Scaling
Master the infrastructure of mass agency. Learn how to scale your agent backends horizontally and how to handle thousands of concurrent autonomous workflows.
Horizontal Scaling for Agent Backends
Building an agent for 1 user is a logic problem. Building an agent for 1,000,000 users is an Infrastructure Problem. Unlike a standard web server where most requests are millisecond-length and "Stateless," an agent session is long-running, state-heavy, and CPU-intensive.
In this lesson, we will move from a single "App Server" to a Distributed Cluster capable of handling massive agent swarms.
1. The Scaling Bottleneck: VRAM vs CPU
As we saw in Module 12.4, agents are resource-hungry.
- Standard App: 1 server can handle 10,000 users.
- Agent App: 1 server might only handle 50-100 users if they are all calling local models or running complex tool containers (Module 7).
The Solution: We must scale Horizontally by adding more "Worker Nodes" instead of one giant "Supercomputer."
2. Stateless APIs vs Stateful Workers
In a production scale-out:
- The Gateway (FastAPI): Stays stateless. It receives the request and a
thread_id. - The Result: It doesn't run the agent. It pushes a "Task" to a Message Queue (Redis or RabbitMQ).
- The Workers: A fleet of 50 worker containers listen to the queue, "Pull" a task, load the state from the DB, run one step of the LangGraph, and push the update back.
graph LR
User --> Gateway[API Gateway]
Gateway --> Queue[Message Queue]
Queue --> W1[Worker 1]
Queue --> W2[Worker 2]
Queue --> W3[Worker 3]
W1,W2,W3 --> DB[(Shared Postgres State)]
3. Load Balancing Strategy
When using WebSockets or SSE for real-time agents, you have a "Sticky Session" problem.
- The Problem: If a user is connected to Server A, and the agent moves to Server B, the stream will break.
- The Solution: Distributed Pub/Sub.
- Use Redis Pub/Sub. Server B (the worker) publishes a message to a Redis channel named
thread_123. Server A (the gateway) listens to that channel and forwards the data to the user's browser.
4. Cold Starts and Auto-Scaling
Agent traffic is "Burstier" than web traffic.
- Auto-Scaling Rule: "If the Message Queue has > 50 pending tasks, launch 10 more worker containers."
- Serverless Fallback: For sudden spikes, use AWS Lambda or Google Cloud Run to handle the overflow, even if it costs more per-token.
5. Scaling the Database (Postgres)
When 1,000,000 agents are updating their state every few seconds, your Checkpoints table will become a bottleneck.
Optimization:
- Partitioning: Split the table by
user_idso that queries only scan a small subset of the data. - Short-Term vs Archive: Move old
thread_ids(older than 7 days) out of the main database and into cold storage (S3).
6. Real-World Case: Scaling a "Coding Assistant"
If you have 10,000 developers using your coding agent, you cannot run 10,000 Docker containers on one machine.
- You use Kubernetes (K8s) Pods.
- Each pods has a "Task Life"—it wakes up, clones a repo, performs an edit, and dies.
- This ensures that a "Zombie Agent" loop doesn't consume your cluster's resources forever.
Summary and Mental Model
Think of Scaling like Operating a Call Center.
- The Gateway is the receptionist.
- The Message Queue is the "Hold Music."
- The Workers are the agents at their desks.
- If too many people call, you hire more agents (Auto-scaling).
The goal is to ensure the "Wait time" (Latency) remains constant as the "Volume" (Users) goes up.
Exercise: Infrastructure Planning
- Capacity Planning: A single worker container can handle 4 concurrent agents. You expect 1,000 concurrent users on launch day.
- How many worker containers do you need?
- The "Wait": Why is a Message Queue better for a "Long-running Researcher Agent" than a direct HTTP call?
- Stability: What happens to your system if the Redis server crashes?
- (Hint: Where is the state stored? Can the agents "Resume" once Redis is back?) Ready for the data layer? Next lesson: Distributed State Management.