Home Redis
Post
Cancel

Redis

Overview

Redis (Remote Dictionary Server) is an in-memory data structure store. It functions as a database, cache, and message broker, with support for various data structures like strings, hashes, lists, sets, and more. Renowned for its speed and efficiency, Redis is ideal for scenarios requiring fast operations, such as caching, session management, and real-time analytics.

Key Features

  • Data Structures: Supports strings, lists, sets, sorted sets, hashes, bitmaps, hyperloglogs, and geospatial indexes.
  • Performance: High-speed operations, supporting millions of requests per second.
  • Transactions: Atomic execution of a group of commands.
  • Pub/Sub: Implements Publisher/Subscriber messaging paradigm.
  • Persistence: Offers options like RDB snapshots and AOF (Append Only File) logs.
  • Lua Scripting: Advanced scripting capabilities for complex operations.
  • Clustering: Supports distributed data storage for scalability and high availability.

Available Operations

  • Strings: Basic operations like SET, GET, INCR, DECR.
  • Lists: Ordered collections of strings with operations like LPUSH, RPUSH, LPOP, RPOP, LRANGE.
  • Sets: Unordered collection of strings. Operations include SADD, SMEMBERS, SINTER.
  • Sorted Sets: Sets ordered by a score, useful for leaderboards. Commands include ZADD, ZRANGE.
  • Hashes: Store, retrieve, and manage hash tables with HSET, HGETALL, etc.
  • Pub/Sub: Messaging with PUBLISH, SUBSCRIBE.
  • Transactions: MULTI, EXEC for atomic command execution.

Code Examples

Python Code Examples

Connection to Redis

1
2
3
4
import redis

# Connect to Redis
redis_client = redis.Redis(host='localhost', port=6379, db=0)

Working with Strings

1
2
3
4
5
# Set a string
redis_client.set('hello', 'world')

# Get a string
print(redis_client.get('hello'))

List Operations

1
2
3
4
5
# Push to list
redis_client.lpush('mylist', 'hello')

# Pop from list
print(redis_client.rpop('mylist'))

Set Operations

1
2
3
4
5
# Add to set
redis_client.sadd('myset', 'a', 'b')

# Get all members of the set
print(redis_client.smembers('myset'))

Transactions

1
2
3
4
5
# Transaction using pipeline
pipeline = redis_client.pipeline()
pipeline.set('foo', 'bar')
pipeline.incr('baz')
pipeline.execute()

Node.js Code Examples

Connection to Redis

1
2
3
4
5
6
7
8
9
const redis = require('redis');

// Connect to Redis
const client = redis.createClient({
  host: 'localhost',
  port: 6379
});

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

Working with Strings

1
2
3
4
5
6
7
// Set a string
client.set('hello', 'world', redis.print);

// Get a string
client.get('hello', (err, reply) => {
  console.log(reply);
});

List Operations

1
2
3
4
5
6
7
// Push to list
client.lpush('mylist', 'hello', redis.print);

// Pop from list
client.rpop('mylist', (err, reply) => {
  console.log(reply);
});

Set Operations

1
2
3
4
5
6
7
8
9
// Add to set
client.sadd('myset', 'a', 'b', redis.print);

// Get all members of the set
client.smembers('myset', (err, replies) => {
  replies.forEach((reply, i) => {
    console.log(`    ${i}: ${reply}`);
  });
});

Transactions

1
2
3
4
5
6
7
// Transaction using multi
const multi = client.multi();
multi.set('foo', 'bar');
multi.incr('baz');
multi.exec((err, replies) => {
  console.log(replies);
});
This post is licensed under CC BY 4.0 by the author.