Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 

Repository files navigation

Redis Learning Repository

Redis Logo

Welcome to my Redis learning journey! This repository contains comprehensive notes and practical examples for learning Redis, the powerful in-memory data structure store.

📌 Table of Contents

🌟 What is Redis?

Redis (REmote DIctionary Server) is an open-source, in-memory data structure store that can be used as a database, cache, and message broker. It supports various data structures such as strings, hashes, lists, sets, and sorted sets with range queries.

🚀 Why Use Redis?

  • Blazing Fast: Redis stores data in memory, making it significantly faster than disk-based databases
  • Versatile: Supports multiple data structures and operations
  • Atomic Operations: Ensures data integrity with atomic operations
  • Persistence: Can persist data to disk if needed
  • Pub/Sub: Built-in publish/subscribe messaging system
  • Replication: Supports master-slave replication

🔑 Key Features

  • In-memory data storage
  • Data persistence options
  • Built-in replication
  • Lua scripting
  • Transactions
  • High availability via Redis Sentinel
  • Automatic partitioning with Redis Cluster

📊 Data Structures

Redis supports various data structures:

Data Structure Description Common Use Cases
Strings Simple key-value pairs Caching, counters
Lists Linked lists of strings Message queues, activity feeds
Sets Unordered collections of unique strings Tags, social graphs
Hashes Maps between string fields and values Storing objects, user profiles
Sorted Sets Sets with a score for ordering Leaderboards, priority queues
Streams Log-like data structure Message queues, event sourcing
Geospatial Location-based data Nearby locations, geofencing

🚀 Getting Started

Prerequisites

  • Docker (recommended) or Redis installed locally
  • Node.js (for Node.js examples)

Running Redis with Docker

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

Connecting to Redis CLI

docker exec -it redis-learning redis-cli

💻 Basic Commands

Strings

SET user:1 "John Doe"
GET user:1
INCR counter
EXPIRE user:1 3600  # Set expiration in seconds

Lists

LPUSH tasks "task1"
RPUSH tasks "task2"
LPOP tasks
LRANGE tasks 0 -1

Sets

SADD tags "redis" "database" "cache"
SMEMBERS tags
SISMEMBER tags "redis"

Hashes

HSET user:1000 name "John" age 30 email "john@example.com"
HGET user:1000 name
HGETALL user:1000

Sorted Sets

ZADD leaderboard 100 "player1" 200 "player2"
ZRANGE leaderboard 0 -1 WITHSCORES
ZREVRANGE leaderboard 0 -1 WITHSCORES

🔍 Practical Examples

Caching with Redis

// Example of caching with Node.js and Redis
const redis = require('redis');
const client = redis.createClient();

async function getCachedData(key) {
  const cachedData = await client.get(key);
  if (cachedData) return JSON.parse(cachedData);
  
  // If not in cache, fetch from database
  const data = await fetchFromDatabase(key);
  // Cache for 1 hour
  await client.setEx(key, 3600, JSON.stringify(data));
  return data;
}

Rate Limiting

// Simple rate limiter using Redis
async function isRateLimited(userId, limit = 10, windowInSeconds = 60) {
  const key = `rate_limit:${userId}`;
  const current = await client.incr(key);
  
  if (current === 1) {
    await client.expire(key, windowInSeconds);
  }
  
  return current > limit;
}

🎯 Use Cases

  • Caching: Speed up your application by caching frequently accessed data
  • Session Storage: Store user session data
  • Real-time Analytics: Track user activity in real-time
  • Message Queues: Implement background job processing
  • Leaderboards: Track and display top scores or rankings
  • Geospatial Indexing: Find locations near a point
  • Pub/Sub Messaging: Implement real-time notifications

📝 License

This project is open source and available under the MIT License.


Happy coding with Redis! 🚀 If you find this repository helpful, consider giving it a ⭐

Created with ❤️ as part of my learning journey

About

A practical guide to Redis with JavaScript examples. Learn key Redis concepts, commands, and caching strategies through clear, commented code samples

Resources

Stars

3 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages