Performance Optimization

Techniques to reduce latency and increase throughput for Nostr applications across clients, APIs, and workers.

LatencyThroughputCachingProfiling

Connections & Subscriptions

Batch requests, debounce filters, and reuse sockets

  • Prefer long-lived WebSocket connections.
  • Coalesce filters and de-duplicate subscriptions.
  • Exponential backoff and jitter for reconnects.

Relay Subscription Fan-out

Loading chart...
Legend: Client opens subscriptions to multiple relays and merges them in a Multiplexer before rendering the App timeline.

WebSocket Tips

Keep connections healthy and efficient

  • Use ping/pong heartbeats; detect half-open sockets quickly.
  • Compress payloads where supported; avoid excessive JSON nesting.
  • Shard subscriptions by timeline vs. DMs to avoid overfetching.

Caching & Indexes

Exploit temporal locality and derived views

Use in-memory caches for hot timelines, and precomputed indexes for common queries. Invalidate by event id and author.

Server Optimizations

Throughput on APIs and workers

  • Connection pooling and reuse; avoid per-request TLS handshakes.
  • Zero-copy parsing where possible; stream large responses.
  • Batch writes and use idempotent upserts keyed by event id.

Profiling & Observability

Find bottlenecks, not guesses

Metrics

Track p50/p95/p99 latencies per operation.

Tracing

Trace publish/subscribe across services.

Load Tests

Continuously run representative scenarios.