Welcome to Redis From Scratch β a minimal Redis clone built to understand the internals of Redis. This project walks through the fundamental components of a Redis server, implementing features like in-memory storage, key-value operations, persistence through RDB files, and client-server communication using the RESP protocol.
- Key-Value Store: Support for basic
SET,GET, andDELcommands. - Persistence: Implements Redis RDB file parsing to load data at startup.
- RESP Protocol: Handles Redis Serialization Protocol (RESP) to communicate with clients.
- Multiple Data Types: Support for strings, lists, and expiration settings.
- Command Handling: Processes common Redis commands like
PING,ECHO, andKEYS. - Error Handling: Gracefully handles unknown commands and file-related issues.
βββ src
β βββ main/java
β β βββ Main.java # Entry point for the server
β β βββ ClientHandler.java # Handles client requests
β β βββ RDBParser.java # Parses RDB files
β β βββ Cache.java # Manages Cache
β β βββ Config.java # Configuration management
βββ README.md # Project documentation
- Java 11+
- Maven
-
Clone the Repository
git clone https://github.com/Lavdeep-Singh/redis-from-scratch.git cd redis-from-scratch -
Build the Project
javac -d out src/main/java/*.java -
Run the Server
java -cp out src/main/java/Main.java
-
Test with Redis CLI
redis-cli -p 6379 > PING > SET key value > GET key
You can modify the server's configuration using the Config class:
- Port: Default is
6379. - Data Directory: Specify the path for RDB files.
- DB Filename: Set the default RDB filename to load at startup.
-
PING:
> PING +PONG -
SET/GET:
> SET name "Codecrafters" > GET name "Codecrafters" -
KEYS:
> KEYS * 1) "name"
The project includes a custom RDB parser that reads and decodes binary Redis dump files. If the file does not exist, the server treats the database as empty.
The parser supports multiple length encoding schemes, including special markers for 32-bit and 64-bit lengths.
Contributions are welcome! Feel free to fork the repo, submit issues, or create pull requests.
- Fork the repo
- Create a new branch
- Commit your changes
- Open a pull request
This project is licensed under the MIT License.
- Inspired by Redis.
- Thanks to Codecrafters.io for the challenge idea.
Happy hacking! π