This repository is a starting point for building a Redis clone in C++ as part of the
"Build Your Own Redis" Challenge. The goal of this project is to implement a simplified version of Redis that supports basic commands like PING, SET, and GET, while learning about event loops, the Redis protocol, and other key concepts.
The server runs in an event loop using epoll to handle multiple clients concurrently. To enable multithreading, multiple event loops can be run on the same CommandHandler instance. This design ensures scalability while maintaining a simple architecture.
The CommandHandler is implemented as a singleton to ensure a single source of truth for the key-value store. Write commands are protected by mutexes to ensure consistency in a multithreaded environment. The CommandHandler also manages replication and RDB file operations.
The CommandRegistry is designed to dynamically register and execute commands. This allows for easy extensibility by simply adding new commands without modifying the core logic.
The RdbParser provides an easy developer experience for loading and saving the database state. However, this presented challenges during replication, as the responses are not as straightforward. Improvements in this area are necessary to better handle replication scenarios.
The Config module is implemented using a parser and functional maps (OneArgs and NoArgs) to handle command-line arguments. This approach simplifies configuration management and ensures flexibility.
Currently, replication only supports full synchronization. A better solution is required to handle partial synchronization, replication IDs, and offsets. A MasterHandler and ReplicaHandler class can be implemented to manage the handshake process and synchronization. These classes can also handle saving the RDB file in a separate process to mimic the non-blocking nature of replication in the actual Redis system.
The current replication system blocks during synchronization. To improve this, the system should save the RDB file and send it to replicas in a non-blocking manner. This would require significant architectural changes but would bring the implementation closer to the actual Redis system.
While the CommandRegistry simplifies command registration, further optimizations can be made to improve performance and reduce overhead during command execution.
Ensure you have the following tools installed on your system:
cmake: For building the project.- A C++ compiler (e.g.,
g++orclang).
To build the project, follow these steps:
-
Clone the repository:
git clone https://github.com/your-username/codecrafters-redis-cpp.git cd codecrafters-redis-cpp -
Create a build directory and run
cmake:mkdir build cd build cmake .. make -
The compiled binary will be available in the
builddirectory.
To start the Redis server, run the following command from the project root:
./your_program.shThis will execute the server implemented in src/Server.cpp.
This project is licensed under the MIT License. See the LICENSE file for details.
This project is part of the CodeCrafters challenge. Special thanks to the CodeCrafters team for designing this educational experience.
Happy coding!