RoadToChain Logo
RoadToChain
T3/M3.3/Why Redis exists in Web3 systems
intermediate 12m read

Why Redis exists in Web3 systems

How direct IPFS gateway lookups cause massive latencies, and why caching immutable CIDs in Redis drops feed render times to 80ms.

#redis #caching #ipfs #performance
Redis Cache Proxy Flow for Immutable IPFS Content
Decentralized IPFS gateways have highDHT-lookup latencies (3-5s). Caching immutable, content-addressed CIDs in Redis bypasses DHT lookups for subsequent loads (80ms).

Decentralized storage solutions (like IPFS, Arweave, and Filecoin) are great for censorship-resistance, but they are terrible for real-time latency.

When Socio3 V2 loaded a user's feed, it had to fetch the metadata JSON file for every post:

  1. Read the IPFS hash from the blockchain (QmMetadata...).
  2. Request the file from Pinata's IPFS gateway:
    gateway.pinata.cloud
    text
    GET https://gateway.pinata.cloud/ipfs/QmMetadata...
  3. Wait. And wait.

On average, a public IPFS gateway takes 3 to 5 seconds to locate, fetch, and return a single pinned JSON file. If your home feed displays 15 posts, your app is forced to execute 15 parallel network queries, freezing feed rendering for several seconds.

This is why we introduced Upstash Redis as our backend caching layer.


1. The Gateway Latency Problem

IPFS is a peer-to-peer network. When you query a gateway for a file CID:

  • The gateway must search the Distributed Hash Table (DHT) to locate nodes pinning that specific file.
  • It establishes connections, downloads block fragments, verifies hashes, and streams the payload.
  • Even with private Pinata gateways, cold-start retrieval times are rarely under 1.5 seconds.

2. Caching Immutable Content (The Web3 Cheat Code)

In standard Web2 databases (like PostgreSQL), caching is notoriously difficult because data is mutable (it changes). You must implement complex cache invalidation rules when records update.

In Web3, IPFS storage is immutable (content-addressed). The content of a CID hash can never change:

SmartAccount.sol
text
QmMetadataXyZ... will ALWAYS return the exact same JSON.

This means we can cache IPFS metadata in Redis forever (or with a high TTL) without worrying about data drift or cache invalidation.

SmartAccount.sol
text
               ┌──▶ [Cache Hit] ──▶ Upstash Redis ──(80ms)──▶ Render Feed

Fetch metadata ┼──▶ [Cache Miss] ─▶ Pinata IPFS ──(4s)──▶ Cache in Redis ──▶ Render

3. Implementing the Redis Cache Proxy

In Socio3, we routes all metadata requests through a cached backend Express proxy endpoint (GET /api/ipfs/:hash):

index.js
javascript
// backend/routes/ipfs.js
const express = require('express');
const { Redis } = require('@upstash/redis');
const axios = require('axios');
 
const router = express.Router();
const redis = new Redis({
  url: process.env.UPSTASH_REDIS_REST_URL,
  token: process.env.UPSTASH_REDIS_REST_TOKEN,
});
 
router.get('/ipfs/:hash', async (req, res) => {
  const { hash } = req.params;
  const cacheKey = `ipfs:${hash}`;
 
  try {
    // 1. Try reading from Upstash Redis
    const cachedData = await redis.get(cacheKey);
    if (cachedData) {
      return res.status(200).json(cachedData);
    }
 
    // 2. Cache Miss: Fetch from Pinata IPFS Gateway
    const response = await axios.get(`${process.env.VITE_PINATA_GATEWAY_URL}${hash}`);
    const metadata = response.data;
 
    // 3. Store in Redis with a 7-day TTL (time-to-live)
    await redis.set(cacheKey, JSON.stringify(metadata), { ex: 604800 });
 
    return res.status(200).json(metadata);
  } catch (error) {
    return res.status(500).json({ error: "Failed to retrieve IPFS metadata" });
  }
});

4. The Performance Impact

By wrapping Pinata's gateway with Upstash Redis, the feed load time dropped from 4,200ms to 80ms—a 98% decrease in latency.


// Reality Check

Never let your client-side React app fetch IPFS CIDs directly from public gateways. Set up a cached proxy endpoint on your API server. It protects your Pinata gateway credentials, speeds up user load times, and saves backend resource costs.

— Production Engineering Principle

System Design Challenge
Think Active

If IPFS CIDs are immutable, why do we set a 7-day TTL instead of caching them permanently (forever) in Redis? Hint: Think about memory limits and database maintenance costs.

[ Think Before Continuing ]

Was this lesson helpful?

Let us know what you think of this specification. (submitting anonymously)