Scaling
This guide covers scaling strategies for Teleportal deployments.
Single-Node Deployment
Section titled “Single-Node Deployment”Best for: Small to medium applications
- Single server instance
- In-memory or local storage
- No coordination needed
const server = new Server({ getStorage: async (ctx) => { // Your storage },});Multi-Node with PubSub
Section titled “Multi-Node with PubSub”Best for: Horizontal scaling, high availability
The purpose of the pubsub broker is to coordinate messages between server instances, so that multiple servers can operate on the same document simultaneously.
import { Server } from "teleportal/server";import { RedisPubSub } from "teleportal/pubsub/redis";
const server = new Server({ getStorage: async (ctx) => { // Shared storage backend return documentStorage; }, pubSub: new RedisPubSub({ url: "redis://localhost:6379", }), nodeId: process.env.NODE_ID,});Running Multiple Instances
Section titled “Running Multiple Instances”# Instance 1NODE_ID=node-1 PORT=3000 node server.js
# Instance 2NODE_ID=node-2 PORT=3001 node server.js
# Instance 3NODE_ID=node-3 PORT=3002 node server.jsPubSub Options
Section titled “PubSub Options”- Redis: Fast, widely supported
- NATS: Lightweight, high performance
- In-Memory: For testing (not shared across nodes)
Considerations
Section titled “Considerations”In some cases, pubsub may put pressure on the broker. This can be mitigated by:
- Co-locating users: Route users to the same server instance when possible
- Document affinity: Route documents to specific servers
- Load balancing: Use sticky sessions to keep users on the same server
Multi-Node with HTTP Load Balancer
Section titled “Multi-Node with HTTP Load Balancer”Best for: Simple scaling without PubSub
- Multiple server instances behind a load balancer
- Shared storage
- Sticky sessions recommended (route same document to same server)
// Each server instanceconst server = new Server({ getStorage: async (ctx) => { // Shared storage return documentStorage; }, // No pubSub - relies on sticky sessions});Load Balancer Configuration
Section titled “Load Balancer Configuration”- Sticky Sessions: Route same document to same server
- Health Checks: Monitor server health
- Session Affinity: Use document ID for routing
Custom Deployment Strategy
Section titled “Custom Deployment Strategy”Teleportal is a framework, so you can build custom deployment strategies:
Document Sharding
Section titled “Document Sharding”Route documents to specific servers based on document ID:
function getServerForDocument(documentId: string): string { const hash = hashDocumentId(documentId); const serverIndex = hash % serverCount; return servers[serverIndex];}Regional Deployment
Section titled “Regional Deployment”Deploy servers in different regions:
const server = new Server({ getStorage: async (ctx) => { // Regional storage return regionalStorage; }, pubSub: new RegionalPubSub({ region: "us-east-1", }),});Storage Scaling
Section titled “Storage Scaling”Separate Storage Types
Section titled “Separate Storage Types”Use different backends for different storage types:
// PostgreSQL for documentsconst documentStorage = new PostgresDocumentStorage(db);
// S3 for filesconst fileStorage = new S3FileStorage(s3Client);
// Redis for milestonesconst milestoneStorage = new RedisMilestoneStorage(redisClient);Storage Replication
Section titled “Storage Replication”Replicate storage across regions for high availability:
const storage = new ReplicatedStorage([ primaryStorage, replicaStorage1, replicaStorage2,]);Monitoring
Section titled “Monitoring”Monitor your scaled deployment:
// Per-node metricsconst metrics = await server.getMetrics();
// Aggregate metrics across nodesconst aggregatedMetrics = await aggregateMetrics(allNodes);Best Practices
Section titled “Best Practices”- Use PubSub for Multi-Node: Always use PubSub when running multiple server instances
- Shared Storage: Use shared storage backends (Redis, PostgreSQL, etc.)
- Unique Node IDs: Ensure each server instance has a unique
nodeId - Health Checks: Monitor server health and remove unhealthy instances
- Load Balancing: Use appropriate load balancing strategies
- Co-location: Co-locate users onto the same server instances when possible
Next Steps
Section titled “Next Steps”- Pub/Sub Guide - Learn about pub/sub setup
- Performance - Optimize performance
- Monitoring - Set up monitoring