This is a refactored Redis server implementation in Go, following Go best practices for project structure and code organization.
app/
├── main.go # Main server application
├── internal/ # Private application code
│ ├── commands/ # Command handlers
│ │ ├── interface.go # Command interface and registry
│ │ ├── basic.go # Basic commands (PING, ECHO, COMMAND)
│ │ ├── data.go # Data commands (GET, SET, INCR, KEYS, TYPE)
│ │ ├── server.go # Server commands (CONFIG, INFO, REPLCONF, PSYNC, WAIT)
│ │ ├── transaction.go # Transaction commands (MULTI, EXEC, DISCARD)
│ │ └── stream.go # Stream commands (XADD, XRANGE, XREAD)
│ ├── config/ # Configuration management
│ │ └── config.go # Configuration loading and validation
│ ├── logging/ # Centralized logging
│ │ └── logger.go # Logger implementation
│ ├── protocol/ # RESP protocol handling
│ │ └── resp.go # RESP protocol read/write functions
│ ├── server/ # Server core logic
│ │ └── server.go # Server struct and methods
│ └── transaction/ # Transaction handling
│ └── transaction.go # Transaction manager
└── pkg/ # Public packages
├── database/ # Database operations
│ ├── database.go # Core database operations
│ └── stream.go # Stream data structure operations
└── rdb/ # RDB file parsing
├── parser.go # RDB file parser
└── helpers.go # RDB parsing helpers
- Commands: Each command type is in its own file with focused responsibility
- Server Logic: Separated from command handling
- Protocol: RESP protocol handling is isolated
- Configuration: Centralized configuration management
- Logging: Consistent logging across all components
- cmd/: Application entry points following Go conventions
- internal/: Private application code that can't be imported by other projects
- pkg/: Public packages that could be reused by other projects
- Consistent error handling patterns across all components
- Proper error propagation and logging
- Graceful handling of connection failures
- Single responsibility principle applied to all modules
- Clear interfaces and abstractions
- Reduced code duplication
- Better testability through dependency injection
- Proper mutex usage for concurrent access
- Safe replica management
- Transaction isolation per connection
# From the project root
go run app/main.go [flags]
# Available flags:
# --port=6379 # Port to listen on
# --dir=/path/to/data # Data directory
# --dbfilename=dump.rdb # RDB filename
# --replicaof="host port" # Master address for replica mode
PING
- Test connectivityECHO <message>
- Echo a messageCOMMAND
- Get command info
GET <key>
- Get value by keySET <key> <value> [PX <milliseconds>]
- Set key-value with optional TTLINCR <key>
- Increment integer valueKEYS <pattern>
- Find keys matching patternTYPE <key>
- Get key type
CONFIG GET <parameter>
- Get configuration parameterINFO [section]
- Get server informationREPLCONF <subcommand> [args...]
- Replication configurationPSYNC <replid> <offset>
- Partial synchronizationWAIT <numreplicas> <timeout>
- Wait for replica acknowledgments
MULTI
- Start transactionEXEC
- Execute transactionDISCARD
- Discard transaction
XADD <key> <id> <field> <value> [field value ...]
- Add entry to streamXRANGE <key> <start> <end>
- Get range of entries from streamXREAD [BLOCK <milliseconds>] STREAMS <key> [key ...] <id> [id ...]
- Read from streams
- Automatic handshake process
- Command replication to slaves
- Offset tracking and synchronization
- RDB file transfer for full resync
- ACID transaction properties
- Command queuing during MULTI
- Atomic execution with EXEC
- Transaction rollback with DISCARD
- Time-ordered entries with unique IDs
- Range queries and blocking reads
- Automatic ID generation
- Field-value pair storage
- RDB file parsing and loading
- Support for expiration times
- Metadata and database selection
- Various encoding formats
- Comprehensive logging with different log levels
- Proper error handling with detailed error messages
- Thread-safe operations with appropriate locking
- Clean interfaces for easy testing and extension
- Consistent code style following Go conventions
- Modular design for easy maintenance and updates