- Simple Python Redis — v1 A custom, multithreaded Redis-compatible server built from scratch using Python. Version: v1
This project implements a functional subset of the Redis server, supporting the RESP (Redis Serialization Protocol), concurrent client handling, and data structures like Streams and Lists.
- Overview This project is a deep dive into backend engineering and systems design. It mimics the behavior of a real Redis instance, allowing standard Redis clients (like redis-cli) to connect, store data, and perform simple operations.
It demonstrates:
Socket Programming: Handling raw TCP connections.
Concurrency: Managing multiple clients simultaneously using threading.
Protocol Design: Parsing and encoding the RESP format.
Medium Logic: Implementing blocking operations and monotonic stream IDs.
- Features (v1) ✅ Core Commands PING: Health check for the server.
SET / GET: Key-value storage with PX (millisecond) expiry support.
INCR: Atomic integer incrementing.
TYPE: Identifies the underlying data structure (string, list, or stream).
✅ List & Blocking Operations LPUSH / RPUSH: Add elements to the head or tail of a list.
LPOP / LRANGE: Retrieve and view list elements.
BLPOP: Blocking pop operation that waits until data is available.
✅ Stream Processing XADD: Append entries to a stream with auto-generated (*) or custom IDs.
XRANGE: Query a range of entries from a stream.
XREAD: Synchronous or Blocking read from one or more streams.
✅ Transactions MULTI / EXEC / DISCARD: Standard Redis transaction block. Commands are queued and executed atomically.
- Architecture The engine is built around three core pillars:
The Global Data Store: A thread-safe dictionary managing strings, lists, and RedisStream objects.
The Command Processor: A robust parser that translates RESP arrays into executable logic.
The Condition Manager: Uses threading.Condition to handle the signaling required for blocking commands like BLPOP and XREAD.
- Build & Run Requirements Python 3.8+
No external dependencies (Standard Library only!)
Run the Server Bash python redis_server.py Connect with Redis CLI Bash redis-cli -p 6379 127.0.0.1:6379> SET name shinaayl PX 5000 OK 127.0.0.1:6379> GET name "shinaayl"
- Limitations (v1) In-Memory Only: No persistence (RDB/AOF) yet; data is lost on restart.
Memory Management: Does not yet implement LRU (Least Recently Used) eviction policies.
Security: No AUTH or password protection.
Single-threaded Core Logic: While networking is multithreaded, the data store access uses a global lock via LIST_CONDITION.
- Example Session & Pro-Tips Stream ID Generation A key feature of this implementation is handling the complex ID logic of Redis Streams.
Bash
127.0.0.1:6379> XADD mystream * sensor-id 1234 temperature 22.5 "1714772800000-0"
127.0.0.1:6379> XADD mystream 0-1 speed 50 (error) ERR The ID specified in XADD must be greater than 0-0
-
Pro-Tip: When testing Streams, use XADD . The server will automatically generate a millisecond-based timestamp for you. If you provide a timestamp but use * for the sequence number (e.g., XADD mystream 1000- ...), the server logic automatically increments the sequence number for that specific millisecond!
-
Purpose Understanding Network Protocols (TCP/RESP).
Learning how Blocking I/O and thread synchronization work.
Practicing the implementation of complex, stateful data structures.
- Future Improvements (v2) Persistence: Implement AOF (Append Only File) to save data to disk.
Pub/Sub: Add PUBLISH and SUBSCRIBE for real-time messaging.
Sorted Sets: Implement the ZSET type using Skip Lists.
Cluster Support: Basic replication between master and slave instances.