Skip to content

An implemention of the Redis server in C++ with basic commands, memory store, rdb persistence and a "working" replication

Notifications You must be signed in to change notification settings

Zendovo/redis-cpp

Repository files navigation

Build Your Own Redis - C++ Implementation

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.

Implementation Details

Server

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.

CommandHandler

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.

CommandRegistry

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.

RDB Parser

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.

Config

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.

Challenges and Future Improvements

Replication

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.

Non-Blocking Replication

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.

Command Execution

While the CommandRegistry simplifies command registration, further optimizations can be made to improve performance and reduce overhead during command execution.

Getting Started

Prerequisites

Ensure you have the following tools installed on your system:

  • cmake: For building the project.
  • A C++ compiler (e.g., g++ or clang).

Building the Project

To build the project, follow these steps:

  1. Clone the repository:

    git clone https://github.com/your-username/codecrafters-redis-cpp.git
    cd codecrafters-redis-cpp
  2. Create a build directory and run cmake:

    mkdir build
    cd build
    cmake ..
    make
  3. The compiled binary will be available in the build directory.

Running the Redis Server

To start the Redis server, run the following command from the project root:

./your_program.sh

This will execute the server implemented in src/Server.cpp.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Acknowledgments

This project is part of the CodeCrafters challenge. Special thanks to the CodeCrafters team for designing this educational experience.

Happy coding!

About

An implemention of the Redis server in C++ with basic commands, memory store, rdb persistence and a "working" replication

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •