Database

How to Seed Redis with Realistic Mock Data

The Problem

Redis is a key-value store, not a relational database, so seeding it feels fundamentally different. There are no tables to INSERT into, no SQL files to import. Instead, you have to decide on key naming conventions, choose the right data structures (hashes, sorted sets, JSON), and write custom scripts to populate them. Most developers skip this step entirely and test against an empty Redis, missing caching bugs, leaderboard edge cases, and session handling issues.

When you do try to seed Redis, you end up writing dozens of HSET and ZADD commands with fake data that does not reflect your actual application's patterns.

The Solution: MockHero API + Redis Pipeline

Use MockHero to generate realistic structured data, then map it to your Redis data structures. MockHero does not know about Redis, but it does not need to: it generates the data, you decide how to store it. The combination of MockHero's realistic data with Redis pipelines gives you a fast, repeatable seeding process.

Quick Setup

curl -X POST https://api.mockhero.dev/api/v1/generate \
  -H "x-api-key: mh_your_api_key" \
  -H "Content-Type: application/json" \
  -d '{
  "tables": [
    {
      "name": "users",
      "count": 100,
      "fields": [
        { "name": "id", "type": "uuid" },
        { "name": "username", "type": "username" },
        { "name": "email", "type": "email" },
        { "name": "score", "type": "integer", "params": { "min": 0, "max": 10000 } },
        { "name": "last_active", "type": "datetime" }
      ]
    },
    {
      "name": "sessions",
      "count": 100,
      "fields": [
        { "name": "session_id", "type": "uuid" },
        { "name": "user_id", "type": "ref", "params": { "ref": "users.id" } },
        { "name": "ip_address", "type": "ipv4" },
        { "name": "user_agent", "type": "user_agent" },
        { "name": "created_at", "type": "datetime" }
      ]
    }
  ],
  "format": "json"
}'

Step-by-Step Guide

1. Install dependencies

npm install redis

2. Get your MockHero API key

Sign up at mockhero.dev/sign-up and copy your API key.

3. Write the seed script

Create seed-redis.mjs:

import { createClient } from "redis";

const redis = createClient({ url: process.env.REDIS_URL });
await redis.connect();

const res = await fetch("https://api.mockhero.dev/api/v1/generate", {
  method: "POST",
  headers: {
    "x-api-key": process.env.MOCKHERO_API_KEY,
    "Content-Type": "application/json",
  },
  body: JSON.stringify({
    tables: [
      {
        name: "users",
        count: 100,
        fields: [
          { name: "id", type: "uuid" },
          { name: "username", type: "username" },
          { name: "email", type: "email" },
          { name: "score", type: "integer", params: { min: 0, max: 10000 } },
          { name: "last_active", type: "datetime" },
        ],
      },
      {
        name: "sessions",
        count: 100,
        fields: [
          { name: "session_id", type: "uuid" },
          { name: "user_id", type: "ref", params: { ref: "users.id" } },
          { name: "ip_address", type: "ipv4" },
          { name: "user_agent", type: "user_agent" },
          { name: "created_at", type: "datetime" },
        ],
      },
    ],
    format: "json",
  }),
});

const { data } = await res.json();

// Store users as hashes and build a leaderboard sorted set
const pipeline = redis.multi();
for (const u of data.users) {
  pipeline.hSet(`user:${u.id}`, {
    username: u.username,
    email: u.email,
    score: String(u.score),
    last_active: u.last_active,
  });
  pipeline.zAdd("leaderboard", { score: u.score, value: u.id });
}

// Store sessions as hashes with TTL
for (const s of data.sessions) {
  pipeline.hSet(`session:${s.session_id}`, {
    user_id: s.user_id,
    ip_address: s.ip_address,
    user_agent: s.user_agent,
    created_at: s.created_at,
  });
  pipeline.expire(`session:${s.session_id}`, 86400);
}

await pipeline.exec();
console.log("Seeded", data.users.length, "users and", data.sessions.length, "sessions into Redis");
await redis.quit();

4. Run the script

node seed-redis.mjs

5. Verify with the Redis CLI

redis-cli ZREVRANGE leaderboard 0 9 WITHSCORES
redis-cli HGETALL user:<some-uuid>

Why MockHero vs Manual Redis Seeding

  • Structured generation, flexible storage — MockHero generates the data shape you need; you decide the Redis data structures.
  • Pipeline-friendly — batch all inserts into a single Redis pipeline for near-instant seeding.
  • Realistic variety — 100 unique usernames, emails, and scores instead of user1, user2, user3.

Get Started

Seed your Redis instance with production-realistic data. Sign up free at mockhero.dev and get 1,000 rows per month.

M

MockHero Team

Guides and tutorials for generating realistic test data with the MockHero API.

Start generating test data for free

1,000 rows/month on the free tier. No credit card required.

Get Your API Key

Related Articles