This project is a Redis-like key-value store implemented in Python. It supports fundamental Redis commands such as SET, GET, DEL, INCR, transactions using MULTI/EXEC/DISCARD, and stream operations like XADD, XRANGE, and XREAD. It also supports master-replica replication with synchronization capabilities.
The server runs a multi-threaded architecture where each connection is handled by a separate thread. The primary features include:
- Key-Value Operations: Supports basic
SET,GET,DEL, andINCRcommands. - TTL Support: Allows setting keys with an expiration time.
- Streams Support: Implements
XADD,XRANGE, andXREADcommands for handling streams. - Replication: Master-replica synchronization using
PSYNC,REPLCONF, andWAIT. - Transaction Support: Implements
MULTI,EXEC, andDISCARD. - Configurable Server: Can be started in master or replica mode.
The implementation is run using the provided your_program.sh script. You can execute it with optional arguments:
./your_program.sh --port <port> --dir <directory> --dbfilename <db_file> --replicaof "<master_host> <master_port>"Note: The --replicaof argument should be provided as a quoted string ("<master_host> <master_port>") to avoid parsing issues.
SET key1 "value1"
GET key1
> "value1"
DEL key1
> OK
INCR counter
> 1MULTI
SET key1 "value1"
INCR counter
EXEC
> OK
> 1XADD mystream * field1 value1 field2 value2
> 1681273645863-0
XRANGE mystream - +
> [["1681273645863-0", ["field1", "value1", "field2", "value2"]]]
XREAD BLOCK 5000 STREAMS mystream $
> [["mystream", [["1681273645863-1", ["field1", "value2"]]]]]To start a replica server, specify the master host and port:
./your_program.sh --replicaof "127.0.0.1 6379"- Python 3.8+
- Install dependencies (if any):
pip install -r requirements.txt
./your_program.sh --port 6379./your_program.sh --replicaof "127.0.0.1 6379"- Command Processing: Uses the Redis Serialization Protocol (RESP) to parse and execute commands.
- Replication:
- The replica connects to the master and initiates a handshake.
- The master sends a snapshot of its current state (
FULLRESYNC). - The master then streams subsequent commands (
PSYNC). - The
WAITcommand ensures write propagation to a given number of replicas before returning.
- Concurrency: Each client connection is handled by a dedicated thread, while the main thread manages global state.