Chain State Synchronization

Real-time chain monitoring and cache synchronization through WebSocket subscriptions.

Table of Contents


Overview

Prism supports WebSocket connections to upstream RPC providers for real-time chain state synchronization. Each configured upstream can optionally provide a WebSocket URL (wss://...) to enable push-based block notifications.

┌─────────────────────────────────────────────────────────┐
│                   WebSocket Flow                         │
├─────────────────────────────────────────────────────────┤
│                                                          │
│  1. Connect to wss://upstream.provider.com              │
│  2. Subscribe: eth_subscribe("newHeads")                │
│  3. Receive: Block header notifications                 │
│  4. Process:                                             │
│     ├─ Update chain tip                                 │
│     ├─ Detect reorgs                                    │
│     ├─ Fetch full block data                            │
│     └─ Cache block, transactions, receipts              │
│                                                          │
└─────────────────────────────────────────────────────────┘

Key Components

  1. WebSocketHandler: Manages WebSocket connections and subscriptions

  2. ReorgManager: Detects and handles chain reorganizations

  3. CacheManager: Proactively caches incoming block data

  4. WebSocketFailureTracker: Tracks connection health and prevents endless retries


Why Use WebSocket

WebSocket connections provide several critical advantages over HTTP-only polling:

1. Cache Accuracy

Problem without WebSocket:

  • Cache may serve stale data until health check detects new blocks

  • Health check intervals (default: 60s) create latency gaps

  • Cache invalidation delayed during reorgs

Solution with WebSocket:

  • Instant notification of new blocks (sub-second)

  • Immediate cache updates

  • Real-time reorg detection

2. Reorg Detection

HTTP-only detection:

WebSocket detection:

3. Reduced Upstream Load

HTTP polling:

  • Periodic eth_blockNumber calls every 60 seconds

  • Redundant if no new blocks

  • Wasted API calls during low activity

WebSocket subscription:

  • Single persistent connection

  • Push notifications only when blocks arrive

  • ~60× fewer API calls (1 connection vs 60 polls/hour)

4. Lower Latency

Metric
HTTP-only
With WebSocket

New block detection

0-60s

< 1s

Reorg detection

0-60s

< 1s

Cache update latency

60s avg

< 2s

API call overhead

High

Low


Configuration

Enable WebSocket by adding wss_url to upstream provider configuration:

Basic Configuration

Multiple Upstreams with WebSocket

Result: Prism subscribes to all three upstreams simultaneously for redundancy.

WebSocket-Only Upstream

Some providers offer WebSocket-only endpoints for specific use cases:

URL Format Requirements

Valid WebSocket URLs:

  • ws://localhost:8545 (insecure, local development)

  • wss://mainnet.infura.io/ws/v3/KEY (secure, production)

  • wss://eth-mainnet.g.alchemy.com/v2/KEY (secure, production)

Invalid URLs (rejected at startup):

  • http://provider.com ❌ (must start with ws:// or wss://)

  • https://provider.com ❌ (HTTPS is for HTTP RPC, not WebSocket)

  • `` (empty string) ❌

  • (whitespace only) ❌


WebSocket Subscriptions

Prism uses the standard Ethereum eth_subscribe method to receive real-time block headers.

Subscription Flow

1. Connection Establishment

Log output:

2. Subscription Request

Prism sends the eth_subscribe JSON-RPC call:

Purpose: Subscribe to new block header notifications.

3. Subscription Confirmation

Provider responds with a subscription ID:

Log output:

4. Block Notifications

Provider pushes new block headers as they arrive:

Log output:

Subscription Lifecycle


Real-Time Chain Tip Updates

When a new block notification arrives, Prism immediately updates its internal chain state.

Update Process

Step 1: Extract Block Data

Step 2: Update Chain Tip

Log output:

Step 3: Fetch Full Block Data

Spawn background task to fetch complete block:

Log output:

Benefits

  1. Proactive Caching: Next request for block 18498639 is a cache hit

  2. Lower Latency: No upstream fetch needed

  3. Reduced Load: Fewer on-demand upstream calls

  4. Fresher Data: Cache updated within 2 seconds of block arrival


Reorg Detection

WebSocket enables real-time detection of chain reorganizations.

Detection Scenarios

Scenario 1: Same Height, Different Hash

Timeline:

Detection:

Log output:

Action Taken:

  1. Calculate divergence point (where chains split)

  2. Invalidate cache from divergence to tip

  3. Update chain state with new canonical hash

  4. Continue normal operation

Scenario 2: Rollback Detection

Timeline:

This happens when:

  • Chain underwent a deep reorg

  • Health checker detected it before WebSocket reconnected

  • Provider rolled back state manually (e.g., debug_setHead)

Detection:

Log output:

Reorg Coalescing

During "reorg storms" (multiple rapid reorgs), Prism batches updates to prevent cache thrashing.

Problem Without Coalescing

Solution: Coalescing Window

Behavior:

Log output:

Result: Reduced cache invalidation and upstream fetches during reorg storms.

Finality Boundaries

Reorgs respect finality checkpoints:

Configuration:

Invalidation Rules:

  • Finalized blocks (≤ 900): NEVER invalidated

  • Safe blocks (901-988): Only invalidated for deep reorgs

  • Unsafe blocks (989-1000): Always invalidated during reorg


Failure Handling & Reconnection

WebSocket connections can fail for various reasons. Prism handles failures gracefully with automatic reconnection.

Failure Scenarios

1. Connection Refused

Cause: Provider endpoint unavailable or incorrect URL

Error:

2. Method Not Allowed (405)

Cause: WebSocket protocol not supported at this endpoint

Error:

3. Forbidden (403)

Cause: Authentication failed or WebSocket not enabled in plan

Error:

4. Protocol Mismatch (200 OK)

Cause: Server returned HTTP response instead of WebSocket handshake

Error:

5. Subscription Failure

Cause: eth_subscribe method not supported or rejected

Error:

6. Connection Drop

Cause: Network interruption, provider restart, or idle timeout

Detection:

Reconnection Logic

Prism automatically reconnects with exponential backoff:

Reconnection Timeline:

Log output (successful reconnection):


Backoff Strategies

Prism implements a failure tracker to prevent endless retry loops.

WebSocketFailureTracker

Tracks consecutive failures and stops retrying after threshold:

Failure Thresholds

Normal Failures (1-2)

Behavior: Retry with backoff

Log output:

Threshold Reached (3)

Behavior: Stop retrying temporarily (5 minutes)

Log output:

Effect: No reconnection attempts for 5 minutes (300 seconds)

Permanent Failure (6+)

Behavior: Stop retrying permanently

Log output:

Effect: WebSocket disabled for this upstream until restart

Failure Reset

After 5 minutes (300 seconds), failure count resets:

Result: Retry attempts resume automatically

Configuration

Default thresholds are hardcoded but can be modified in source:

Tuning Recommendations:

  • Aggressive retry: max_consecutive_failures = 5 (more retries)

  • Conservative retry: max_consecutive_failures = 2 (fewer retries)

  • Faster reset: failure_reset_duration = 60 (1 minute cooldown)

  • Slower reset: failure_reset_duration = 600 (10 minute cooldown)


Monitoring WebSocket Health

Logs

Prism emits structured logs for WebSocket lifecycle events:

Connection Events

Block Processing

Disconnection Events

Error Events

Metrics

WebSocket health is tracked in Prometheus metrics:

Connection Status

Failure Tracking

Block Processing

Reorg Detection

Grafana Dashboards

WebSocket Health Panel:

WebSocket Latency Panel:

Alerting

Alert: WebSocket Connection Down

Alert: High Failure Rate

Alert: Permanent Failure


When to Use WebSocket vs HTTP-Only

Use WebSocket When

1. Real-Time Applications

  • DeFi frontends with live data updates

  • Block explorers showing latest blocks

  • Transaction monitoring systems

  • MEV bots requiring instant block notifications

2. Cache Accuracy Critical

  • Financial applications

  • Analytics platforms

  • Data pipelines requiring canonical chain data

  • Applications sensitive to reorgs

3. High Query Volume

  • Applications making frequent eth_getLogs calls

  • Heavy caching workloads

  • Reduced upstream API costs important

4. Reorg-Sensitive Workloads

  • Cross-chain bridges

  • Smart contract indexers

  • Data integrity verification systems

Use HTTP-Only When

1. Development/Testing

  • Local development without WebSocket support

  • Testing environments

  • Proof-of-concept applications

2. Simplified Deployment

  • Don't want to manage WebSocket connections

  • Firewall restrictions on WebSocket protocols

  • Simpler architecture preferred

3. Low Query Volume

  • Occasional API calls

  • Background processing jobs

  • Cache hit rate already high

4. Provider Limitations

  • WebSocket not available in plan

  • WebSocket endpoint unreliable

  • Cost optimization (WebSocket often premium)

Hybrid Configuration

Run some upstreams with WebSocket, others without:

Benefits:

  • Real-time updates from primary

  • Cost savings on backup/tertiary

  • Failover to HTTP if WebSocket unavailable


Troubleshooting

Issue: WebSocket Won't Connect

Symptoms:

Causes:

  1. Invalid WebSocket URL

  2. Firewall blocking WebSocket port

  3. Provider endpoint down

  4. Authentication failure

Solutions:

1. Verify URL format:

2. Test connection manually:

3. Check firewall rules:

4. Verify API key:

  • Check key is valid and not expired

  • Verify WebSocket is enabled in provider plan

  • Test with different API key

Issue: WebSocket Connects But No Blocks

Symptoms:

Causes:

  1. No new blocks being mined (testnets)

  2. Subscription not active

  3. Provider not sending notifications

Solutions:

1. Check if chain is producing blocks:

2. Enable debug logging:

Look for messages:

3. Test with different provider:

  • Try Alchemy if Infura has issues

  • Use multiple providers for redundancy

Issue: Frequent Reconnections

Symptoms:

Causes:

  1. Provider imposing connection time limits

  2. Network instability

  3. Idle timeout

Solutions:

1. Check provider documentation:

  • Some providers limit WebSocket connection duration

  • May require periodic reconnection

2. Monitor network stability:

3. Increase idle timeout (if provider supports):

  • Some providers accept keepalive messages

  • Prism currently doesn't send keepalives (future enhancement)

Issue: High Failure Count

Symptoms:

Causes:

  1. Provider WebSocket endpoint unreliable

  2. Incorrect configuration

  3. Network issues

  4. Provider rate limiting WebSocket connections

Solutions:

1. Verify endpoint is working:

2. Switch to different upstream:

3. Check rate limits:

  • Some providers limit WebSocket connections

  • May require higher-tier plan

Issue: WebSocket Marked Permanently Failed

Symptoms:

Causes:

  1. Provider doesn't support WebSocket

  2. Endpoint permanently unavailable

  3. Configuration error

Solutions:

1. Disable WebSocket for this upstream:

2. Restart Prism to reset failure tracker:

3. Verify provider documentation:

  • Confirm WebSocket support

  • Check correct WebSocket URL format

  • Verify plan includes WebSocket access

Issue: Cache Not Updating in Real-Time

Symptoms:

  • WebSocket connected successfully

  • Blocks received via WebSocket

  • Cache still stale

Causes:

  1. Block processing failing

  2. Cache full (LRU eviction)

  3. Reorg invalidating cache frequently

Solutions:

1. Check block processing logs:

Should see:

2. Increase cache sizes:

3. Monitor cache evictions:

If evictions high, increase cache sizes.

Issue: Reorg Not Detected

Symptoms:

  • Chain reorganized

  • Prism still serving old data

  • No reorg logs

Causes:

  1. WebSocket missed reorg event

  2. Both old and new blocks have same height/hash (rare)

  3. Health checker interval too long

Solutions:

1. Check WebSocket is receiving blocks:

2. Decrease health check interval:

3. Enable multiple upstreams:

  • Multiple WebSocket connections increase reorg detection probability

  • Cross-validate between upstreams

4. Monitor reorg metrics:


Summary

WebSocket subscriptions provide significant benefits for Prism:

Key Advantages:

  • Real-time updates: Sub-second block notifications

  • 🎯 Cache accuracy: Immediate reorg detection and invalidation

  • 💰 Cost savings: ~60× fewer API calls vs. polling

  • 📊 Better observability: Detailed metrics and logs

Best Practices:

  1. Enable WebSocket on at least one upstream for real-time updates

  2. Use multiple WebSocket upstreams for redundancy

  3. Monitor connection health via Prometheus metrics

  4. Configure alerts for permanent failures

  5. Test WebSocket endpoints before deploying to production

  6. Have HTTP-only fallback upstreams

Production Checklist:


Next: Learn about Caching System or Monitoring.

Last updated