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.
Install Docker: First, make sure you have Docker installed on your operating system. You can download and install Docker from the official Docker website.
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.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 yourpasswordThis command creates and starts a container named "my-redis" using the Redis image. The container will run in the background.
Verify Redis is Running: Open a terminal or command prompt and run the following command to verify the Redis container is running.
docker psYou 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.

Architecture Overview

Prerequisites
Install php_redis.dll on Windows
Find your PHP version:
Run this in Command Prompt or PowerShell:
php -vLook for something like:
PHP 8.2.12 (cli) …Note if it is Thread Safe (TS) or Non-Thread Safe (NTS).
Download php_redis.dll:
You can download the PHP Redis extension from the official PECL page:
The PHP Redis extension can also be downloaded directly from the official GitHub releases page at https://github.com/phpredis/phpredis/releases. Download the version that matches your PHP version and thread-safety (TS/NTS) setting.
Install the DLL:
Extract the .dll file.
Copy it into your PHP ext directory (usually C:\xampp\php\ext or C:\php\ext).
Edit php.ini:
Open your php.ini (often in C:\xampp\php or C:\php).
Add this line at the bottom:
extension=php_redis.dllThen save and close.
Restart Your Server:
If you're using XAMPP:
Open the XAMPP Control Panel
Stop and start Apache
Or, restart any PHP service you’re using.
Verify Redis Extension is Loaded:
Run:
php -mYou should see redis in the list of loaded modules.Or, use:
php -i | findstr redisYou should see info about the redis extension.
Get a Gemini API Key
Prepare a Google Account: Make sure you're signed in with a Google account.
Visit Google AI Studio: Go to aistudio.google.com and sign in.
Access the API Key Section: Use the left sidebar to navigate to API Keys or click Get API Key.
Create an API Key:
Choose to create the key in a new project (recommended) or an existing GCP project.
Click Create API Key after selecting your option.
Copy and Store the Key:
Once generated, copy the API key immediately.
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:
Check fuzzy cache for similar prompts
If found → Store response in Redis with job ID key
Return job ID to frontend (appears like normal processing)
Cache Miss Path:
No similar prompt found
Create job object with unique ID
Push job to Redis queue for background worker processing
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