This directory contains comprehensive examples demonstrating various features of the Redis client library.
Make sure you have Redis running locally on 127.0.0.1:6379 (default configuration) before running the examples.
# Run a specific example
deno run -A examples/basic.ts
# Or use the task
deno task exampleDemonstrates fundamental Redis operations:
- Connecting to Redis server
- String operations (GET, SET, INCR, DECR)
- Hash operations (HSET, HGET, HMGET)
- List operations (LPUSH, RPUSH, LRANGE)
- Key management (EXISTS, EXPIRE, TTL)
- Error handling
- Connection cleanup
Topics covered:
- Connection establishment
- Basic data types
- Error handling patterns
- Resource cleanup
Comprehensive Redis publish/subscribe messaging demonstration:
- Basic channel subscriptions and message publishing
- Pattern-based subscriptions with wildcards (
user:*,log:*) - Multiple subscribers and message broadcasting
- Real-time messaging simulation (chat room example)
- Dynamic subscription management (add/remove channels)
- Advanced monitoring patterns with message statistics
- Error handling and graceful connection cleanup
- Performance tracking and analytics
Topics covered:
- Publisher/Subscriber pattern fundamentals
- Pattern matching and flexible routing
- Concurrent message handling across multiple subscribers
- Real-time communication patterns
- Dynamic subscription management
- Message statistics and monitoring
- Best practices for pub/sub architectures
Demonstrates command pipelining for performance:
- Basic pipeline usage
- Performance comparisons
- Batch data processing
- Error handling in pipelines
- Transaction-like operations
Topics covered:
- Command batching
- Performance optimization
- Error handling in batches
- Atomic-like operations
Comprehensive Redis Streams functionality demonstration:
- Basic stream operations with automatic and custom IDs
- Stream range queries (forward, reverse, by ID, with limits)
- Consumer groups for distributed message processing
- Real-time event processing with blocking reads
- Stream information and monitoring commands
- Memory management with stream trimming (MAXLEN, MINID)
- Multiple consumers with task distribution
- Event sourcing patterns and best practices
- IoT sensor data simulation
- Application event processing workflow
Topics covered:
- Event streaming and message ordering
- Consumer groups and distributed processing
- Message acknowledgment and delivery guarantees
- Stream queries and data retrieval patterns
- Real-time processing with blocking operations
- Memory management and stream maintenance
- Event sourcing architectures
- IoT and time-series data handling
Explores advanced Redis features:
- Redis transactions (MULTI/EXEC)
- Optimistic locking with WATCH
- Connection pooling simulation
- Advanced data structures
- Error handling and recovery
- Health checks
Topics covered:
- ACID transactions
- Concurrent access patterns
- Connection management
- Advanced data types
Make sure Redis is installed and running:
# macOS with Homebrew
brew install redis
brew services start redis
# Ubuntu/Debian
sudo apt install redis-server
sudo systemctl start redis-server
# Docker
docker run -d -p 6379:6379 redis:latestExamples require network permissions:
# Allow network access
deno run --allow-net examples/basic.ts
# Or allow all permissions
deno run -A examples/basic.tsExamples use default Redis configuration:
- Host:
127.0.0.1 - Port:
6379 - Database:
0 - No authentication
To use different settings, modify the connection configuration in each example:
const redis = await connect({
hostname: "your-redis-host",
port: 6380,
password: "your-password",
db: 1,
});You can also use environment variables:
export REDIS_HOST=localhost
export REDIS_PORT=6379
export REDIS_PASSWORD=mypassword
deno run --allow-net --allow-env examples/basic.tsAll examples demonstrate proper error handling:
try {
const redis = await connect(config);
// ... operations
} catch (error) {
if (error instanceof ConnectionClosedError) {
console.error("Connection closed");
} else if (error instanceof AuthenticationError) {
console.error("Authentication failed");
} else {
console.error("Unexpected error:", error);
}
} finally {
redis.close();
}Always close connections when done:
const redis = await connect(config);
try {
// ... operations
} finally {
redis.close(); // Always cleanup
}Examples include timing measurements:
const start = performance.now();
await operation();
const time = performance.now() - start;
console.log(`Operation took ${time.toFixed(2)}ms`);Error: Connection refused
- Make sure Redis is running
- Check host/port configuration
- Verify firewall settings
Error: Authentication failed
- Check Redis AUTH configuration
- Verify username/password
- Ensure Redis is configured for authentication
Error: Network access is not allowed
- Run with
--allow-netflag - Or use
--allow-all/-Afor all permissions
Error: Out of memory
- Check Redis memory configuration
- Monitor Redis memory usage
- Use appropriate data expiration
- Always close connections: Use try/finally blocks
- Handle errors gracefully: Use appropriate error types
- Use pipelining: For multiple operations
- Monitor performance: Measure operation timing
- Clean up test data: Remove keys after examples
- Use connection pooling: For high-throughput applications
When adding new examples:
- Follow the existing file structure
- Include comprehensive comments
- Add error handling
- Clean up test data
- Update this README
- Test with a clean Redis instance
For more information, see the main README.md and API documentation.