Skip to content

feat(lib): cache blockhash in redis (5s TTL)#361

Merged
dev-jodee merged 3 commits intosolana-foundation:release/2.2.0from
raushan728:feat/blockhash-caching
Mar 3, 2026
Merged

feat(lib): cache blockhash in redis (5s TTL)#361
dev-jodee merged 3 commits intosolana-foundation:release/2.2.0from
raushan728:feat/blockhash-caching

Conversation

@raushan728
Copy link
Contributor

Adds a 5-second Redis cache for getLatestBlockhash to reduce RPC load on high-traffic endpoints.

  • Changes: get_blockhash and transfer_transaction now use cached blockhash.
  • Safety: Gracefully falls back to direct RPC call on Redis miss or error.
  • Tests: Added unit tests verifying fallback behavior when cache is disabled.

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Feb 26, 2026

Greptile Summary

Adds Redis caching for getLatestBlockhash with a 5-second TTL to reduce RPC load on high-traffic endpoints. The implementation follows the existing cache patterns in the codebase with graceful fallback to direct RPC calls when cache is unavailable or errors occur.

Key Changes:

  • CacheUtil::get_or_fetch_latest_blockhash() provides cached blockhash with automatic fallback
  • get_blockhash and transfer_transaction endpoints now use the cached implementation
  • Cache failures are logged but don't fail requests
  • Unit tests verify fallback behavior when cache is disabled

Notes:

  • Other blockhash-fetching code paths (e.g., bundle operations in bundle/helper.rs:224, transaction utilities in transaction/versioned_transaction.rs:299) continue to call RPC directly
  • Under very high concurrency, multiple simultaneous requests during cache expiration may create a brief burst of RPC calls

Confidence Score: 4/5

  • Safe to merge with solid implementation and proper fallback handling
  • Well-structured caching implementation following existing patterns with graceful error handling. One minor performance consideration around cache stampede under very high concurrency, but this is a common tradeoff in caching systems and doesn't affect correctness.
  • No files require special attention

Important Files Changed

Filename Overview
crates/lib/src/cache.rs Added blockhash caching with 5s TTL, graceful fallback to RPC on cache errors
crates/lib/src/rpc_server/method/get_blockhash.rs Replaced direct RPC call with cached blockhash lookup
crates/lib/src/rpc_server/method/transfer_transaction.rs Updated deprecated endpoint to use cached blockhash

Sequence Diagram

sequenceDiagram
    participant Client
    participant GetBlockhash as get_blockhash endpoint
    participant CacheUtil
    participant Redis
    participant RPC as Solana RPC

    Client->>GetBlockhash: Request blockhash
    GetBlockhash->>CacheUtil: get_or_fetch_latest_blockhash()
    
    alt Cache enabled
        CacheUtil->>Redis: GET kora:blockhash
        alt Cache hit
            Redis-->>CacheUtil: Return cached hash
            CacheUtil-->>GetBlockhash: Return hash
        else Cache miss
            Redis-->>CacheUtil: None
            CacheUtil->>RPC: get_latest_blockhash_with_commitment(confirmed)
            RPC-->>CacheUtil: Fresh blockhash
            CacheUtil->>Redis: SET kora:blockhash (TTL: 5s)
            CacheUtil-->>GetBlockhash: Return hash
        end
    else Cache disabled/error
        CacheUtil->>RPC: get_latest_blockhash_with_commitment(confirmed)
        RPC-->>CacheUtil: Fresh blockhash
        CacheUtil-->>GetBlockhash: Return hash
    end
    
    GetBlockhash-->>Client: Return blockhash
Loading

Last reviewed commit: f0e8939

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

3 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

@raushan728 raushan728 force-pushed the feat/blockhash-caching branch from f0e8939 to c8509a1 Compare February 26, 2026 08:17
@dev-jodee
Copy link
Contributor

@raushan728 if you can fix the ci issue, rest looks good

@dev-jodee dev-jodee self-requested a review February 27, 2026 21:06
dev-jodee
dev-jodee previously approved these changes Feb 27, 2026
Copy link
Contributor

@dev-jodee dev-jodee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@raushan728
Copy link
Contributor Author

raushan728 commented Feb 28, 2026

hi @dev-jodee is current soluntion is good or should i use #[allow(clippy::needless_borrow)] ?

@dev-jodee
Copy link
Contributor

@raushan728 the fix is to get a reference of get_config like so

let config = &get_config()?;

@raushan728 raushan728 force-pushed the feat/blockhash-caching branch from b511f59 to bd04aad Compare March 3, 2026 08:51
@raushan728
Copy link
Contributor Author

@dev-jodee done

@github-actions
Copy link

github-actions bot commented Mar 3, 2026

✅ Fork external live tests passed.

fork-external-live-pass:bd04aad5bed631fa1d127eb6dbd93e64bcc2aa70
run: https://github.com/solana-foundation/kora/actions/runs/22633482663

@dev-jodee dev-jodee merged commit f8d27f8 into solana-foundation:release/2.2.0 Mar 3, 2026
11 of 12 checks passed
@raushan728 raushan728 deleted the feat/blockhash-caching branch March 4, 2026 08:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants