TrainDis is a high-performance, Redis-compatible in-memory database implemented in C++. It provides key-value storage with support for multiple data types, persistence mechanisms (AOF and RDB), and master-slave replication.
- Multiple Data Types: Supports strings, hashes, and sorted sets (zsets)
- Redis Protocol: Compatible with RESP (Redis Serialization Protocol)
- Persistence Options:
- AOF (Append-Only File) with three modes: Always, EverySec, and None
- RDB (Redis Database) snapshotting
- Master-Slave Replication: Support for data replication between nodes
- Thread-Safe Operations: Concurrency-safe data structures
- Configurable Settings: Load configuration from file or command line
- Memory Efficient: Optimized memory usage with expiration support
- Server: Main server implementation using epoll for I/O multiplexing
- KV Storage: In-memory key-value storage supporting different data types
- AOF Manager: Handles Append-Only File persistence with various sync modes
- RDB Manager: Manages database snapshots in RDB format
- RESP Parser: Handles Redis Serialization Protocol for client communication
- Config Loader: Parses configuration from files or command-line arguments
- Strings: Simple key-value pairs with expiration support
- Hashes: Key-value maps stored under a single key
- Sorted Sets (ZSets): Collections of unique members ordered by scores, implemented with skip lists
The server can be configured via command-line arguments or configuration files:
./train_set [--port <port>] [--bind <ip>] [--config <file>]port: Server port (default: 6379)bind_address: IP address to bind to (default: 127.0.0.1)aof.enabled: Enable AOF persistence (true/false)aof.mode: AOF sync mode (always/everysec/none)aof.dir: Directory for AOF fileaof.filename: AOF file namerdb.enabled: Enable RDB persistence (true/false)rdb.dir: Directory for RDB filerdb.filename: RDB file namereplicate.enabled: Enable replication (true/false)replicate.master_host: Master host for replicationreplicate.master_port: Master port for replication
SET key value [EX seconds]- Set a key-value pairGET key- Get value by keyDEL key1 [key2 ...]- Delete one or more keysEXISTS key- Check if key existsTTL key- Get time to live for a keyEXPIRE key seconds- Set expiration time
HSET key field value- Set field in hashHGET key field- Get field value from hashHDEL key field1 [field2 ...]- Delete fields from hashHEXISTS key field- Check if field exists in hashHLEN key- Get number of fields in hashHGETALL key- Get all fields and values in hash
ZADD key score member- Add member to sorted setZREM key member [member ...]- Remove members from sorted setZRANGE key start stop- Get range of members by rankZSCORE key member- Get score of member in sorted set
SAVE- Save data to RDB fileBGSAVE- Save data to RDB file in backgroundBGREWRITEAOF- Rewrite AOF file in background
PING- Ping the serverINFO- Get server informationFLUSHALL- Remove all keysKEYS *- List keys matching pattern
- C++17 compatible compiler
- CMake 3.15 or higher
- POSIX-compliant system (Linux/macOS)
mkdir build
cd build
cmake ..
make# Using a config file
./train_set --config ../conf/always.conf
# Using command line arguments
./train_set --port 6380 --bind 127.0.0.1The repository includes several example configuration files:
always.conf: AOF enabled with "always" sync modeeverysec.conf: AOF enabled with "every second" sync modenone.conf: AOF disabledmaster.conf: Master node configurationreplica.conf: Replica node configuration
- Efficient AOF Implementation: Multiple sync modes for balancing performance and durability
- Batch Writes: AOF supports configurable batch sizes to improve write performance
- Skip Lists: Sorted sets implemented with skip lists for efficient range operations
- Memory Pre-allocation: AOF files can be pre-allocated to reduce fragmentation
- Background Operations: AOF rewriting and RDB saving run in background threads
TrainDis supports master-slave replication:
- Slaves can connect to masters to receive data updates
- Initial synchronization happens via RDB dump
- Incremental updates are sent as commands over the replication channel
The project includes comprehensive unit tests for all major components:
- AOF functionality tests
- RDB persistence tests
- Key-value storage tests
- Configuration loading tests
- RESP protocol parsing tests