Redis

Getting Started with Redis and Redis Insight

What is Redis

Redis is an in-memory key-value store often used for caching and real-time processing. It supports various data structures and is known for its speed and flexibility.

Install Redis on Docker

To install Redis using Docker, follow the steps below.

  1. Install Docker: First, make sure you have Docker installed on your operating system. You can download and install Docker from the official Docker website.

  2. Pull the Redis Image: Open a terminal or command prompt and run the following command to pull the Redis Docker image: docker pull redis.This downloads the official Redis image from Docker Hub.

  3. Run Redis Container: Once the Redis image is downloaded, you can start a Redis container using the following commands. You can also change the mapped port if needed by replacing 6379:6379 with your preferred port. Redis uses port 6379 by default: # Run Redis with no password (accessible via any network interface)

    docker run --name my-redis -p 6379:6379 -d redis

    # Run Redis with password (local access only)

    docker run --name my-redis -p 127.0.0.1:6379:6379 -d redis redis-server --requirepass yourpassword

    This command creates and starts a container named "my-redis" using the Redis image. The container will run in the background.

  4. Verify Redis is Running: Open a terminal or command prompt and run the following command to verify the Redis container is running. docker ps You should see a container named “my-redis” running.

What is Redis Insight

RedisInsight is a desktop GUI for Redis. Download it at: https://redis.io/downloads/#Redis_Insight

Sample Use Case

LLM ChatBot (Gemini + Redis)

LLM ChatBot (Gemini + Redis) is an AI-powered chatbot application designed with a Vue.js frontend and PHP backend. It integrates Google's Gemini API to provide intelligent natural language responses and uses Redis to efficiently queue and manage chatbot requests.

LLM ChatBot

Architecture Overview

Prerequisites

Install php_redis.dll on Windows

  1. Find your PHP version:

    1. Run this in Command Prompt or PowerShell: php -v

    2. Look for something like: PHP 8.2.12 (cli) …

    3. Note if it is Thread Safe (TS) or Non-Thread Safe (NTS).

  2. Download php_redis.dll:

  3. Install the DLL:

    1. Extract the .dll file.

    2. Copy it into your PHP ext directory (usually C:\xampp\php\ext or C:\php\ext).

  4. Edit php.ini:

    1. Open your php.ini (often in C:\xampp\php or C:\php).

    2. Add this line at the bottom: extension=php_redis.dll

    3. Then save and close.

  5. Restart Your Server:

    • If you're using XAMPP:

      1. Open the XAMPP Control Panel

      2. Stop and start Apache

    • Or, restart any PHP service you’re using.

  6. Verify Redis Extension is Loaded:

    • Run: php -m You should see redis in the list of loaded modules.

    • Or, use: php -i | findstr redis

      You should see info about the redis extension.

Get a Gemini API Key

  1. Prepare a Google Account: Make sure you're signed in with a Google account.

  2. Visit Google AI Studio: Go to aistudio.google.com and sign in.

  3. Access the API Key Section: Use the left sidebar to navigate to API Keys or click Get API Key.

  4. Create an API Key:

    1. Choose to create the key in a new project (recommended) or an existing GCP project.

    2. Click Create API Key after selecting your option.

  5. Copy and Store the Key:

    1. Once generated, copy the API key immediately.

    2. Store it securely.

Environment Configuration

Create a .env file:

Backend Code

FuzzyCacheManager.php

Intelligent response caching with fallback strategies

Similarity Calculation Algorithm

Text Normalization

Keyword Extraction

chat.php

Redis Setup and Connection

The script connects to a Redis server running locally on the default port 6379. Redis serves as both a cache storage and job queue system.

Response Caching (Cache Hit Scenario)

When a similar prompt is found in the cache:

  • Stores the cached response with a key pattern chatbot_response:{job_id}

  • This allows the frontend to retrieve the response using the same polling mechanism it would use for new requests

  • The job ID maintains consistency with the normal workflow

Job Queue System (Cache Miss Scenario)

When no similar response exists:

  • Uses Redis list data structure as a job queue

  • lPush adds the job to the left side of the list chatbot_queue

  • This creates a FIFO (First In, First Out) queue for background processing

Redis Data Flow

  • Cache Hit Path:

    1. Check fuzzy cache for similar prompts

    2. If found → Store response in Redis with job ID key

    3. Return job ID to frontend (appears like normal processing)

  • Cache Miss Path:

    1. No similar prompt found

    2. Create job object with unique ID

    3. Push job to Redis queue for background worker processing

    4. Return job ID to frontend

worker.php

Initialization Phase

Job Processing Loop

The main processing loop implements the following workflow:

Job Retrieval

  • Uses blocking pop operation from Redis queue

  • Implements 1-second polling interval when queue is empty

  • Ensures graceful handling of queue downtime

Fuzzy Cache Lookup

  • Searches for semantically similar previous responses

  • Returns match type, similarity score, and cached response

  • Avoids duplicate API calls for similar queries

API Integration

When no cache hit occurs, the worker:

  • Sends HTTP POST request with JSON payload

  • Handles API errors and network failures

  • Parses response and extracts generated text

Store Value in Redis

Response Caching

  • Stores new responses with 1-hour expiration

  • Enables fuzzy matching for future similar queries

  • Maintains cache efficiency and relevance

get_response.php

Redis Setup and Connection

Check if response exists

Frontend

Frontend Architecture

Core Frontend Components

Vue.js Template Structure

Vue.js Reactive State Management

Asynchronous Communication Flow

Message Submission

Job Queue Submission

Polling Implementation

Last updated