Skip to content

MuhammadFaizan19/cc-redis-lite

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

44 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Redis-lite Key-Value Store Implementation

Overview

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.

How It Works

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, and INCR commands.
  • TTL Support: Allows setting keys with an expiration time.
  • Streams Support: Implements XADD, XRANGE, and XREAD commands for handling streams.
  • Replication: Master-replica synchronization using PSYNC, REPLCONF, and WAIT.
  • Transaction Support: Implements MULTI, EXEC, and DISCARD.
  • Configurable Server: Can be started in master or replica mode.

Example Usage

Running the Program

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.

Example Commands

Basic Commands

SET key1 "value1"
GET key1
> "value1"

DEL key1
> OK

INCR counter
> 1

Transactions

MULTI
SET key1 "value1"
INCR counter
EXEC
> OK
> 1

Stream Operations

XADD 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"]]]]]

Replication Setup

To start a replica server, specify the master host and port:

./your_program.sh --replicaof "127.0.0.1 6379"

Installation & Setup

Prerequisites

  • Python 3.8+
  • Install dependencies (if any):
    pip install -r requirements.txt

Running the Server

Start in Master Mode

./your_program.sh --port 6379

Start in Replica Mode

./your_program.sh --replicaof "127.0.0.1 6379"

Internals

  • 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 WAIT command 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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published