This project is a basic implementation of Redis, developed to deepen understanding of systems programming, network communication, and socket programming. It features core elements like a simple in-memory key-value store, command parsing, client handling with multiplexing, and a graceful shutdown mechanism. The project includes a server component and a client component to communicate with the server, resembling the structure of Redis commands like SET, GET, and DEL.
- Key-Value Store: Set, get, and delete key-value pairs in an in-memory store.
- Multiplexing: Efficiently handle multiple client connections using the
select()system call. - Command Parsing: Supports basic Redis commands such as
SET,GET, andDEL. - Graceful Shutdown: A special
NUKEcommand to gracefully shut down the server. - User-Friendly Prompt: Displays a prompt for client interactions similar to the Redis CLI.
basic-redis-implementation/
├── include/
│ ├── client_handler.h
│ ├── key_value_store.h
│ └── server.h
├── src/
│ ├── client_handler.c
│ ├── key_value_store.c
│ ├── main.c
│ └── server.c
├── build/
│ ├── main.o
│ ├── key_value_store.o
│ ├── client_handler.o
│ └── server.o
├── Makefile
├── README.md
├── .gitignore
└── LICENSEinclude/: Contains header files for the server, key-value store, and client handler.src/: Contains the source code files for the main program, key-value store functionality, server logic, and client handler logic.build/: Contains the compiled object files.Makefile: Defines how the project is compiled and linked.README.md: This file, containing the documentation for the project..gitignore: Specifies files to ignore in Git.LICENSE: The license for this project, if applicable.
- The server starts by binding to a specified port and IP address.
- Listens for incoming connections from clients.
- Uses
select()for handling multiple clients.
- Clients connect by specifying the server's IP and port.
- Communication is established through sockets.
- Commands like
SET,GET,DEL, andNUKEare parsed. - Key-value operations are performed accordingly.
- SET: Stores a key-value pair in memory.
- GET: Retrieves the value associated with a key.
- DEL: Deletes a key-value pair.
- The
NUKEcommand shuts down the server safely.
- The server uses
select()to efficiently manage multiple clients.
#include "server.h"
int main() {
start_server(); // Starts the server
return 0;
}#include "server.h"
void start_server() {
// Initialize server, bind socket, listen, and handle clients using select().
}#include "client_handler.h"
void handle_client(int client_socket) {
// Parse the command and call functions from key_value_store.c
}#include "key_value_store.h"
void set_key_value(char *key, char *value) {
// Store key-value pair in memory
}
char *get_value(char *key) {
// Retrieve value for the given key
}
void delete_key(char *key) {
// Delete the key-value pair
}./server./clientSET key value
GET key
DEL key
NUKE- The
NUKEcommand shuts down the server.
- GCC compiler
- Make (for easy builds)
-
Clone the repository:
git clone <repository_url> cd basic-redis-implementation
-
Run
maketo build the project:make
-
Run the server:
./server
-
Run the client in another terminal:
./client
-
Use commands to interact (
SET,GET,DEL,NUKE).
X - https://x.com/ritikpaltech/
This project is licensed under the MIT License.