mirror of
https://github.com/redis/go-redis.git
synced 2025-07-22 10:01:50 +03:00
4.0 KiB
4.0 KiB
Redis Command Processing
This document describes how commands are processed in the Redis client, including the command pipeline, error handling, and various command execution modes.
Command Interface
The core of command processing is the Cmder
interface:
type Cmder interface {
Name() string // Command name (e.g., "set", "get")
FullName() string // Full command name (e.g., "cluster info")
Args() []interface{} // Command arguments
String() string // String representation of command and response
readTimeout() *time.Duration
readReply(rd *proto.Reader) error
SetErr(error)
Err() error
}
Command Processing Pipeline
1. Command Creation
- Commands are created using factory functions (e.g.,
NewCmd
,NewStatusCmd
) - Each command type implements the
Cmder
interface - Commands can specify read timeouts and key positions
2. Command Execution
The execution flow:
- Command validation
- Connection acquisition from pool
- Command writing to Redis
- Response reading
- Error handling and retries
3. Error Handling
- Network errors trigger retries based on configuration
- Redis errors are returned directly
- Timeout handling with configurable backoff
Command Execution Modes
1. Single Command
err := client.Process(ctx, cmd)
2. Pipeline
pipe := client.Pipeline()
pipe.Process(ctx, cmd1)
pipe.Process(ctx, cmd2)
cmds, err := pipe.Exec(ctx)
3. Transaction Pipeline
pipe := client.TxPipeline()
pipe.Process(ctx, cmd1)
pipe.Process(ctx, cmd2)
cmds, err := pipe.Exec(ctx)
Command Types
1. Basic Commands
- String commands (SET, GET)
- Hash commands (HGET, HSET)
- List commands (LPUSH, RPOP)
- Set commands (SADD, SMEMBERS)
- Sorted Set commands (ZADD, ZRANGE)
2. Advanced Commands
- Scripting (EVAL, EVALSHA)
- Pub/Sub (SUBSCRIBE, PUBLISH)
- Transactions (MULTI, EXEC)
- Cluster commands (CLUSTER INFO)
3. Specialized Commands
- Search commands (FT.SEARCH)
- JSON commands (JSON.SET, JSON.GET)
- Time Series commands (TS.ADD, TS.RANGE)
- Probabilistic data structures (BF.ADD, CF.ADD)
Command Processing in Different Clients
1. Standalone Client
- Direct command execution
- Connection pooling
- Automatic retries
2. Cluster Client
- Command routing based on key slots
- MOVED/ASK redirection handling
- Cross-slot command batching
3. Ring Client
- Command sharding based on key hashing
- Consistent hashing for node selection
- Parallel command execution
Best Practices
-
Command Batching
- Use pipelines for multiple commands
- Batch related commands together
- Consider transaction pipelines for atomic operations
-
Error Handling
- Check command errors after execution
- Handle network errors appropriately
- Use retries for transient failures
-
Performance
- Use appropriate command types
- Leverage pipelining for bulk operations
- Monitor command execution times
-
Resource Management
- Close connections properly
- Use context for timeouts
- Monitor connection pool usage
Common Issues and Solutions
-
Timeout Handling
- Configure appropriate timeouts
- Use context for cancellation
- Implement retry strategies
-
Connection Issues
- Monitor connection pool health
- Handle connection failures gracefully
- Implement proper cleanup
-
Command Errors
- Validate commands before execution
- Handle Redis-specific errors
- Implement proper error recovery
Monitoring and Debugging
-
Command Monitoring
- Use SLOWLOG for performance analysis
- Monitor command execution times
- Track error rates
-
Client Information
- Monitor client connections
- Track command usage patterns
- Analyze performance bottlenecks
Future Improvements
-
Command Processing
- Enhanced error handling
- Improved retry mechanisms
- Better connection management
-
Performance
- Optimized command batching
- Enhanced pipelining
- Better resource utilization