Performance
This guide covers performance optimization strategies for Teleportal applications.
Rate Limiting
Section titled “Rate Limiting”Rate limiting prevents abuse and ensures fair resource usage:
const server = new Server({ rateLimitConfig: { maxMessages: 100, windowMs: 1000, trackBy: "user", },});See Rate Limiting Guide for details.
VirtualStorage for Write Buffering
Section titled “VirtualStorage for Write Buffering”VirtualStorage adds batching and buffering to any DocumentStorage implementation, improving write performance:
import { VirtualStorage } from "teleportal/storage";
// Wrap any existing storage with batchingconst batchedStorage = new VirtualStorage(existingDocumentStorage, { batchMaxSize: 100, // Batch every 100 updates batchWaitMs: 2000, // Or every 2 seconds});
// Use as normal DocumentStorageawait batchedStorage.handleUpdate("doc1", update);const doc = await batchedStorage.getDocument("doc1"); // Flushes pending writesBenefits:
- Faster writes: Updates are buffered and batched
- Reduced I/O: Fewer database calls
- Read consistency: Pending writes are flushed on reads
When to use:
- High-frequency collaborative updates
- Slow storage backends (remote DBs, object storage)
- Write-heavy applications
Storage Optimization
Section titled “Storage Optimization”Use Appropriate Storage Backend
Section titled “Use Appropriate Storage Backend”Choose storage based on your needs:
- Redis: Fast, in-memory, good for high-frequency updates
- PostgreSQL: Reliable, transactional, good for complex queries
- S3: Scalable, cost-effective, good for large files
Indexed Keys vs Key Scanning
Section titled “Indexed Keys vs Key Scanning”For unstorage-based storage:
const { documentStorage } = createUnstorage(storage, { scanKeys: false, // Use indexed keys (better for Redis, Memcached) // or scanKeys: true, // Use key scanning (better for PostgreSQL, MySQL)});Connection Optimization
Section titled “Connection Optimization”Connection Reuse
Section titled “Connection Reuse”Multiple providers can share the same connection:
const connection = new WebSocketConnection({ url: "wss://..." });
const provider1 = new Provider({ client: connection, document: "doc-1" });const provider2 = new Provider({ client: connection, document: "doc-2" });Document Switching
Section titled “Document Switching”Use switchDocument() to efficiently switch documents:
// Reuses connection instead of creating a new oneconst newProvider = provider.switchDocument({ document: "new-doc" });Message Batching
Section titled “Message Batching”Batch multiple messages together to reduce network overhead:
import { getBatchingTransform } from "teleportal/transports";
const batchedTransport = getBatchingTransform({ maxBatchSize: 10, maxBatchDelay: 100,});Monitoring
Section titled “Monitoring”Monitor performance using built-in metrics:
const metrics = await server.getMetrics();const status = await server.getStatus();Key metrics to watch:
teleportal_message_duration_seconds: Message processing timeteleportal_storage_operation_duration_seconds: Storage operation timeteleportal_sessions_active: Active session countteleportal_clients_active: Active client count
Next Steps
Section titled “Next Steps”- Scaling - Learn about scaling strategies
- Monitoring - Set up monitoring