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.
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.
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.
PING: Responds withPONG. A basic command to test if the connection is alive.ECHO: Returns the provided message. Used for debugging and client testing.
INFO: Provides information and statistics about the server, primarily for replication.CONFIG GET: Retrieves server configuration parameters.
SET: Sets a key-value pair. Supports thePXoption 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.
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.
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.
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.
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 aBLOCKtimeout.
GEOADD: Adds a geospatial item (latitude, longitude, name) to a sorted set.GEODIST: Returns the distance between two members in a geo set.
You can run the server in either master or replica mode.
To start the primary server instance, run the following command:
python -m app.main --port 6379
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.
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