Cache Layer and Performance Optimization

← Back to SHAMap and NodeStore: Data Persistence and State Management


Introduction

Database queries are fundamentally slow compared to memory access:

Memory access:     1-10 microseconds
Cache miss latency: 1-10 milliseconds
Ratio:             1000x slower

With millions of nodes and transactions processing at ledger-close speeds (every 3-5 seconds), any system that naively queries the database for every node access will fail.

The NodeStore's Cache Layer solves this through a multi-tier strategy that keeps hot data in memory while safely delegating cold data to disk.

The Performance Problem

Scenario: Synchronizing from the Network

A new node joins XRPL and must catch up to current ledger. This requires:

  1. Fetching missing ledgers (blocks of transactions)

  2. For each ledger, fetching all state nodes

  3. Verifying each node's hash

  4. Storing nodes to disk

Naive Approach (No Cache):

Clearly infeasible.

With Caching (90% Hit Rate):

Still slow, but realistic with parallel processing.

The difference between possible and impossible is caching.

TaggedCache Architecture

The NodeStore's primary cache is the TaggedCache:

Purpose:

Structure:

Cache Tiers:

NodeStore implements a two-tier caching strategy:

Fetch Algorithm:

Cache Insertion Strategy

When Objects Enter Cache:

Dummy Objects:

Special marker objects prevent wasted lookups:

Benefit of Dummies:

Prevents thundering herd of repeated failed lookups.

Cache Eviction

Cache capacity is limited. When full, old objects must be evicted.

Eviction Triggers:

LRU (Least Recently Used) Eviction:

Age-Based Eviction:

Configuration Parameters:

Impact of Configuration:

Cache Performance Metrics

NodeStore tracks cache effectiveness:

Example Metrics:

Synchronization Optimization

During network synchronization, special techniques optimize caching:

Prefetching

Batch Loading

Deferred Reads

Working Set Management

Different phases of operation have different access patterns:

During Normal Operation (Steady State)

During Synchronization

After Sync Completion

Thread Safety

Cache must be safe for concurrent access:

Concurrency Properties:

Advanced Caching: Full Below Optimization

During synchronization, special tracking prevents redundant work:

The Problem:

The Solution: Full Below Generation Counter

Benefit:

Summary

Key Concepts:

  1. Multi-Tier Caching: Memory + disk strategy balances performance and capacity

  2. LRU Eviction: Keeps frequently-accessed data, evicts cold data

  3. Dummy Markers: Prevent repeated failed lookups

  4. Metrics Tracking: Monitor cache effectiveness

  5. Synchronization Optimization: Prefetch and batch loading

  6. Full Below Cache: Avoid redundant traversal during sync

  7. Thread Safety: Shared locks for multiple readers

Performance Impact:

The Cache Layer transforms NodeStore from theoretical to practical. Database queries are unavoidable, but caching hides their latency, allowing XRPL to operate at microsecond efficiency despite millisecond database performance.

Last updated