Skip to content

A Redis server implementation built from scratch in Python (Codecrafters challenge). Supports basic commands like PING, ECHO, SET/GET, RPUSH/LPUSH, LPOP, BLPOP, and more.

Notifications You must be signed in to change notification settings

ttheghost/codecrafters-redis-python

Repository files navigation

progress-banner

My Redis Clone - CodeCrafters Challenge

This repository contains my implementation of a Redis-like server, built from scratch in Python as part of the CodeCrafters "Build Your Own Redis" challenge.

About The Project

This project is a fully asynchronous server that communicates using the Redis Serialization Protocol (RESP). It is built using Python's asyncio library to handle multiple concurrent clients efficiently. The goal is to replicate the functionality of Redis, one feature at a time.

Current Progress & Implemented Commands

The server currently supports a wide range of Redis commands, indicating progress through several stages of the CodeCrafters challenge. Below is a comprehensive list of all supported commands.

Core & Connection Commands

  • PING: Responds with PONG. A basic command to test if the connection is alive.
  • ECHO: Returns the provided message. Used for debugging and client testing.

Server & Configuration Commands

  • INFO: Provides information and statistics about the server, primarily for replication.
  • CONFIG GET: Retrieves server configuration parameters.

Key-Value & Data Type Commands

  • SET: Sets a key-value pair. Supports the PX option for setting an expiry time in milliseconds.
  • GET: Retrieves the value for a given key.
  • KEYS: Returns all keys matching a given pattern.
  • TYPE: Reports the data type of a key.

Transaction Commands

  • MULTI: Marks the start of a transaction block. Subsequent commands are queued for atomic execution.
  • EXEC: Executes all commands queued in a transaction.
  • DISCARD: Flushes all commands queued in a transaction, aborting it.

Replication Commands

  • REPLCONF: Used by replicas to configure the replication stream and acknowledge processed data.
  • PSYNC: Used by replicas to initiate synchronization with the master. Supports both full and partial resynchronization.
  • WAIT: Blocks until a specified number of replicas acknowledge a write command, ensuring write durability.

Pub/Sub Commands

  • SUBSCRIBE: Subscribes the client to one or more channels.
  • UNSUBSCRIBE: Unsubscribes the client from channels.
  • PUBLISH: Posts a message to a channel, which is then sent to all subscribers.

Stream Commands

  • XADD: Adds a new entry to a stream data structure.
  • XRANGE: Returns a range of entries from a stream.
  • XREAD: Blocks and waits for new entries in one or more streams, supporting a BLOCK timeout.

Geospatial Commands

  • GEOADD: Adds a geospatial item (latitude, longitude, name) to a sorted set.
  • GEODIST: Returns the distance between two members in a geo set.

How to Run

You can run the server in either master or replica mode.

Running the Master Server

To start the primary server instance, run the following command:

python -m app.main --port 6379

Running a Replica Server

To start a replica that connects to a master running on localhost:6379, run the following command in a new terminal:

python -m app.main --port 6380 --replicaof "localhost 6379"

You can now connect to either server using a standard Redis client.

Integrated CLI for Fast Testing

This project includes a custom command-line interface (CLI) for quick and easy testing. It provides a simple way to send commands directly to the server without needing an external tool.

To use the custom CLI with the master server, run:

python -m cli.main

If you are running a replica on a different port (e.g., 6380), you can connect the CLI to it with:

python -m cli.main --port 6380

About

A Redis server implementation built from scratch in Python (Codecrafters challenge). Supports basic commands like PING, ECHO, SET/GET, RPUSH/LPUSH, LPOP, BLPOP, and more.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •