Ankita.eth
GithubContact
  • About Ankita
  • experience
    • TECHNOLOGIES
    • Frontend
      • Javascript
      • React
      • NextJS
      • HTML & CSS
      • UI Libraries & Frameworks
        • Tailwind CSS
        • Comprehensive Guide to UI Libraries and Frameworks
    • Backend
      • Node.js
      • Express.js
    • Database
      • Mongodb, Mongoose
      • PostgresSQl
      • MySQL
    • Packege Mangers
      • NPM-Node Packege Manager
      • Yarn
      • Yarn 2 (Berry)
      • PNPM
      • BUN
      • Commands cheatsheet
    • API Providers
      • Alchemy
      • Telegram Bot
      • CoinMarket
      • Thirdweb
      • Infura
      • Moralis
    • DevOps/Infrastructure
      • Docker
      • Kubernetes
      • CI/CD
      • Docker Swam
    • Protocols
      • ERCs & EIPs
        • ERC-20
        • ERC-721
        • ERC-1155
        • ERC-4337
        • ERC-6551
        • ERC-777
        • ERC-3643
        • EIP-7702
        • ERC-7715
        • ERC-7739
        • EIP-6780
        • EIP-5792
        • ERC-4626
        • EIP-1559
        • ERC-404
        • ERC-3643
        • ERC-223
    • Web3 Toolkits
      • Foundry
      • Hardhat
      • RemixIDE
    • Messaging/Caching
      • Kafka
      • Redis
      • Sendgrid
    • Blockchain
      • Solana
      • Ethereum
      • Polygon & Zero knowldge Proof
      • Bitcoin
      • Solidity
    • Deployment Platforms
      • AWS
      • Vercel
      • Heroku, Render
      • Domain setup
  • SDKs
    • Google Cloud SDK
    • AWS SDK
    • Firebase SDK
  • EOF EVM Object Format
  • Articles
    • Medium Articles
    • 🌐 My Work
  • 📞 Get in Touch
Powered by GitBook
On this page
  • Redis: In-Memory Data Structure Store 🚀
  • Architecture 🏗️
  • Key Concepts 🗝️
  • Installation and Setup 🛠️
  • Basic Redis Commands 💻
  • Using Redis with Node.js 🟢
  • Redis Data Structures 📊
  • Redis Pub/Sub 📡
  • Redis Transactions 🔒
  • Deployment 🚀

Was this helpful?

  1. experience
  2. Messaging/Caching

Redis

PreviousKafkaNextSendgrid

Last updated 8 months ago

Was this helpful?

Redis: In-Memory Data Structure Store 🚀

Redis (Remote Dictionary Server) is an open-source, in-memory data structure store that can be used as a database, cache, message broker, and queue. It's known for its high performance and versatility.

Architecture 🏗️

Redis follows a client-server architecture:

Key Concepts 🗝️

  • In-memory data storage

  • Key-value data model

  • Data structures: Strings, Lists, Sets, Hashes, Sorted Sets

  • Pub/Sub messaging

  • Transactions

  • Lua scripting

Installation and Setup 🛠️

To install Redis on Ubuntu:

sudo apt update
sudo apt install redis-server

To start Redis server:

sudo systemctl start redis-server

Basic Redis Commands 💻

Here are some basic Redis commands:

SET key value
GET key
DEL key
EXISTS key
INCR key
EXPIRE key seconds

Using Redis with Node.js 🟢

First, install the Redis client for Node.js:

npm install redis

Here's a simple example of using Redis with Node.js:

const redis = require('redis');
const client = redis.createClient();

client.on('error', (err) => console.log('Redis Client Error', err));

async function main() {
    await client.connect();

    // Set a key
    await client.set('user:1', 'John Doe');

    // Get a key
    const value = await client.get('user:1');
    console.log(value); // Output: John Doe

    // Close the connection
    await client.quit();
}

main();

Redis Data Structures 📊

1. Strings

await client.set('mykey', 'Hello, Redis!');
const value = await client.get('mykey');
console.log(value); // Output: Hello, Redis!

2. Lists

await client.lPush('mylist', 'world');
await client.lPush('mylist', 'hello');
const list = await client.lRange('mylist', 0, -1);
console.log(list); // Output: [ 'hello', 'world' ]

3. Sets

await client.sAdd('myset', 'member1');
await client.sAdd('myset', 'member2');
const members = await client.sMembers('myset');
console.log(members); // Output: [ 'member1', 'member2' ]

4. Hashes

await client.hSet('user:1000', 'name', 'John Doe');
await client.hSet('user:1000', 'email', 'john@example.com');
const user = await client.hGetAll('user:1000');
console.log(user); // Output: { name: 'John Doe', email: 'john@example.com' }

Redis Pub/Sub 📡

Redis Pub/Sub implementation:

const publisher = redis.createClient();
const subscriber = publisher.duplicate();

await Promise.all([publisher.connect(), subscriber.connect()]);

await subscriber.subscribe('news', (message) => {
    console.log(message); // 'Hello world!'
});

await publisher.publish('news', 'Hello world!');

Redis Transactions 🔒

Example of a Redis transaction:

const multi = client.multi();
multi.set('key1', 'value1');
multi.set('key2', 'value2');
const results = await multi.exec();
console.log(results); // Output: ['OK', 'OK']

Deployment 🚀

For production deployment, consider using Redis Cloud or setting up a Redis cluster for high availability and scalability. Always ensure proper security measures are in place, such as authentication and encryption.

Remember to configure Redis for persistence if you need data to survive server restarts. You can use RDB snapshots or AOF (Append Only File) for this purpose.

With its speed, versatility, and rich feature set, Redis is an excellent choice for various use cases in modern application development, from caching to real-time analytics and more.