A Redis-compatible server implemented from scratch in Python as part of the CodeCrafters "Build Your Own Redis" challenge.
The goal was to go beyond implementing individual commands and understand the systems behind Redis: the RESP protocol, TCP connections, concurrency, transactions, persistence, replication, blocking operations, and data structures.
- TCP server with concurrent client connections
- RESP protocol parsing and response encoding
- Command dispatch and request handling
- Multiple clients handled concurrently using Python threads
- Key expiration with
PX
PINGECHOSETGETINCRTYPEKEYS
LPUSHRPUSHLPOPBLPOPLLENLRANGE
XADDXRANGEXREAD
MULTIEXECDISCARDWATCHUNWATCH
The implementation tracks key versions to detect modifications between WATCH and EXEC.
- Primary/replica server roles
PSYNC- Replication offsets
- Replica acknowledgements
WAIT- Command propagation to replicas
SUBSCRIBEUNSUBSCRIBEPUBLISH
ZADDZRANKZRANGEZCARDZSCOREZREM
GEOADDGEOPOSGEODISTGEOSEARCH
Geospatial coordinates are encoded using an integer grid and bit interleaving, similar to the approach used by Redis for geospatial indexing.
AUTHACL- User/password management
- Per-connection authentication state
The server is intentionally implemented with a small number of primitives rather than relying on Redis-specific libraries.
At a high level:
Client
│
│ TCP
▼
RESP parser
│
▼
Command dispatcher
│
├── Strings
├── Lists
├── Streams
├── Transactions
├── Pub/Sub
├── Sorted Sets
├── Geospatial
├── Replication
└── ACL / Authentication
│
▼
In-memory data store
Concurrency is handled with Python threads, locks, conditions, and per-connection state. Conditions are also used for blocking operations such as BLPOP.
The server entry point is:
app/main.pyRun it with:
./your_program.shThe default Redis port is 6379.
You can then connect with the Redis CLI:
redis-cli -p 6379For example:
SET name Vedant
GET name
This project was built against the CodeCrafters Redis challenge and completed all available stages.
The original challenge is available at:
https://codecrafters.io/challenges/redis
I wanted to understand Redis as a system rather than only use it as a dependency.
Building the server from scratch made concepts such as wire protocols, concurrent connections, transactions, replication, blocking operations, and data-structure implementations much more concrete.