{"id":31666718,"url":"https://github.com/ahmedsobhy01/redis-clone","last_synced_at":"2026-02-27T23:07:11.982Z","repository":{"id":312410120,"uuid":"1030315416","full_name":"AhmedSobhy01/redis-clone","owner":"AhmedSobhy01","description":"A lightweight Redis clone implemented in C++ that supports key-value operations and sorted sets","archived":false,"fork":false,"pushed_at":"2025-09-03T16:19:57.000Z","size":59,"stargazers_count":15,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-10-07T23:56:15.870Z","etag":null,"topics":["avl","c","clone","cpp","hashmap","hashtable","inmemory-db","redis","redis-client","redis-server","system-design"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AhmedSobhy01.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-08-01T12:38:17.000Z","updated_at":"2025-09-19T15:40:29.000Z","dependencies_parsed_at":"2025-08-30T13:10:12.678Z","dependency_job_id":"d13f338b-8147-4018-8404-bd0250ebfcea","html_url":"https://github.com/AhmedSobhy01/redis-clone","commit_stats":null,"previous_names":["ahmedsobhy01/redis-clone"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/AhmedSobhy01/redis-clone","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AhmedSobhy01%2Fredis-clone","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AhmedSobhy01%2Fredis-clone/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AhmedSobhy01%2Fredis-clone/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AhmedSobhy01%2Fredis-clone/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AhmedSobhy01","download_url":"https://codeload.github.com/AhmedSobhy01/redis-clone/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AhmedSobhy01%2Fredis-clone/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29918971,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-27T19:37:42.220Z","status":"ssl_error","status_checked_at":"2026-02-27T19:37:41.463Z","response_time":57,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["avl","c","clone","cpp","hashmap","hashtable","inmemory-db","redis","redis-client","redis-server","system-design"],"created_at":"2025-10-07T23:56:10.319Z","updated_at":"2026-02-27T23:07:11.952Z","avatar_url":"https://github.com/AhmedSobhy01.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Redis Clone\n\nA lightweight Redis server clone implemented in C++ that supports key-value operations and sorted sets.\n\n## Overview 📘\n\nThis project is a simplified implementation of Redis, featuring:\n\n-   In-memory key-value storage\n-   Binary protocol for client-server communication\n-   Support for strings, integers, and sorted sets\n-   Command processing similar to Redis\n-   Non-blocking I/O with event loop architecture\n\n## Building 🛠️\n\n### Prerequisites\n\n-   C++17 compatible compiler (g++ recommended)\n-   Linux/Unix environment\n\n### Compilation\n\n```bash\n# Build server and client\nmake\n```\n\n## Running ▶️\n\n### Server\n\n```bash\n./redis-clone\n```\n\nThe server listens on 0.0.0.0:8080 by default.\n\n### Client\n\n```bash\n./client\n```\n\n## Supported Commands 🧾\n\nThe server supports the following Redis-like commands:\n\n### Basic Commands\n\n-   `GET key` - Get the value of a key\n-   `SET key value` - Set key to value\n-   `DEL key` - Delete key\n-   `KEYS` - Get all keys\n-   `PTTL key` - Get the time to live for a key in milliseconds\n-   `PEXPIRE key milliseconds` - Set a key's time-to-live in milliseconds\n\n### Sorted Set Commands\n\n-   `ZADD key score member` - Add member to a sorted set\n-   `ZREM key member` - Remove member from a sorted set\n-   `ZSCORE key member` - Get the score of a member in a sorted set\n-   `ZQUERY key min max` - Query members within a score range\n\n### Usage Examples\n\n```\nSET mykey hello\nGET mykey\nPTTL mykey\nPEXPIRE mykey 1500\nPTTL mykey\nDEL mykey\nZADD ranks 10 alice\nZADD ranks 5 bob\nZSCORE ranks alice\nZQUERY ranks 0 10\n```\n\n## Protocol Details 📡\n\n#### Request Format\n\n```\nmsglen (4 bytes) | nargs (4 bytes) | arglen1 (4 bytes) | arg1 | ... | arglenN (4 bytes) | argN\n```\n\nWhere:\n\n-   msglen: Total message length (excluding the 4 bytes for msglen itself)\n-   nargs: Number of arguments\n-   arglenX: Length of argument X\n-   argX: Byte data for argument X\n\n#### Response Format\n\n```\nmsglen (4 bytes) | tag (1 byte) | payload\n```\n\nWhere:\n\n-   msglen: Total message length (excluding the 4 bytes for msglen itself)\n-   tag: Type of response (NIL, ERROR, STRING, INTEGER, DOUBLE, ARRAY)\n-   payload: Type-specific data format\n\n### Example Encoded Request (SET key value)\n\nArguments: [\"SET\",\"mykey\",\"hello\"]\n\n```\n[msglen= ?] | nargs=3\narglen=3  SET\narglen=5  mykey\narglen=5  hello\n```\n\nHex representation in little-endian:\n\n```\n18 00 00 00    # msglen = 24 (bytes after this field)\n03 00 00 00    # nargs\n03 00 00 00 53 45 54\n05 00 00 00 6d 79 6b 65 79\n05 00 00 00 68 65 6c 6c 6f\n```\n\n## Architecture 🧱\n\n### Core Components\n\n-   **Server**: Main server implementation handling connections\n-   **EventLoop**: Non-blocking I/O event loop using epoll()\n-   **ConnectionManager**: Manages client connections\n-   **RequestProcessor**: Processes incoming client requests\n-   **ProtocolHelper**: Handles the binary protocol encoding/decoding\n-   **Command System**: Extensible command pattern implementation\n-   **Database**: In-memory data storage with HashTable and AVL tree\n\n### Data Structures\n\n-   **ResizableHashTable**: Main key-value storage\n-   **AVLTree**: Self-balancing binary tree for sorted sets\n\n### Core Components\n\n1. **Server**:\n\n    - Central component that initializes and coordinates all other subsystems\n    - Manages the lifecycle of the application\n    - Handles signal processing and graceful shutdown\n\n2. **Event Loop**:\n\n    - Non-blocking I/O multiplexer using `epoll()`\n    - Efficiently handles multiple client connections concurrently\n    - Event-based architecture that processes read/write events as they occur\n    - Avoids thread-per-connection overhead while maintaining responsiveness\n\n3. **Connection Management**:\n\n    - Manages client connection lifecycles\n    - Handles socket I/O operations through the `Connection` class\n    - Tracks connection state (read/write/closed)\n    - Implements connection timeouts to prevent resource leaks\n    - Uses `ByteBuffer` for efficient input/output buffering\n\n4. **Protocol Layer**:\n\n    - Binary protocol implementation for efficient communication\n    - `ProtocolHelper` encapsulates serialization/deserialization logic\n    - Support for different data types (strings, integers, doubles, arrays)\n    - Request parsing and response formatting\n    - Error handling and protocol validation\n\n5. **Command Processing Pipeline**:\n\n    - `RequestProcessor` receives and routes client requests\n    - Command pattern implementation for extensibility\n    - `CommandFactory` creates appropriate command handlers\n    - Each command implemented as a separate class with consistent interface\n    - Error handling and response generation\n\n6. **Storage Engine**:\n    - In-memory data structures optimized for different access patterns\n    - `ResizableHashTable` as the main key-value store with dynamic resizing\n    - `AVLTree` for sorted set implementation with O(log n) operations\n    - Value types system for different data representations\n    - Expiration mechanism for key TTL implementation\n\n### Data Flow\n\n1. Client connection is accepted by the Server\n2. EventLoop monitors socket for read/write readiness\n3. When data is available, Connection.handleRead() buffers the incoming data\n4. RequestProcessor attempts to parse a complete request using ProtocolHelper\n5. If a complete request is available, CommandFactory creates the appropriate command\n6. Command is executed against the database, producing a Response\n7. Response is serialized by ProtocolHelper and written to the connection's output buffer\n8. EventLoop detects write readiness and Connection.handleWrite() sends the response\n9. After sending, connection returns to read mode waiting for the next request\n\n### Design Patterns\n\n-   **Command Pattern**: All operations encapsulated as Command objects with uniform interface\n-   **Factory Pattern**: CommandFactory creates appropriate Command instances\n-   **Repository Pattern**: IDatabase interface abstracts storage operations\n-   **Strategy Pattern**: Different command implementations for various operations\n\n## Attribution 📖\n\nThis implementation is based on (and adapted from) the concepts and structure presented in the book:\nBuild Your Own Redis with C/C++  \nNetwork programming, data structures, and low-level C.  \nAuthor: James Smith  \nhttps://build-your-own.org/redis/\n\nThe code here is an independent reimplementation inspired by that guide.\n\n## License 📄\n\nThis project is open source and available under the MIT License. See LICENSE file.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fahmedsobhy01%2Fredis-clone","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fahmedsobhy01%2Fredis-clone","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fahmedsobhy01%2Fredis-clone/lists"}