● Stage 01 • Networking & Protocols
In-Memory Key-Value Store
Implement an in-memory database server. Parse the binary Redis wire protocol over raw TCP sockets, handle concurrent connections with non-blocking I/O multiplexing, and support crash recovery.
First steps
- Accept one TCP connection on port 6379.
- Read a RESP PING. Reply with +PONG\r\n.
- Check with redis-cli -p 6379 PING. Add SET and GET before concurrency.
Project Goal
- Understand socket programming and raw TCP stream processing.
- Parse a binary application-layer protocol (RESP) without third-party frameworks.
- Handle thousands of concurrent client connections using an event loop or worker pool.
- Implement in-memory data structures with expiration policies and crash recovery logging.
Skills Acquired
POSIX TCP Sockets
Wire Protocol Serialization
Non-blocking I/O (epoll/kqueue)
TCP Buffer Accumulation
Active & Passive Expiration
Append-Only File (AOF) Durability
Technical Specification
- Port: Bind raw TCP socket on 0.0.0.0:6379.
- Protocol: Full support for Redis Serialization Protocol (RESP).
- Supported Commands: PING, ECHO, SET [key] [val] [EX sec], GET [key], DEL [key], EXPIRE [key] [sec].
- Persistence: Append-Only File (appendonly.aof) replayed on startup.
Milestones
01.1
TCP Socket Server & Buffer Accumulator
Bind port 6379 with SO_REUSEADDR. Maintain a read buffer per connection to handle partial packet arrivals split across TCP segments.
01.2
RESP Stream Parser
Implement a state machine parser for RESP frames: Simple Strings (+), Errors (-), Integers (:), Bulk Strings ($), and Arrays (*).
01.3
Non-Blocking I/O Multiplexing
Set sockets to non-blocking mode (O_NONBLOCK). Use an event loop (epoll / kqueue) or worker pool to handle multiple connections without blocking.
01.4
Key Expiration (Active & Passive)
Delete expired keys during GET lookups (passive). Run a periodic timer sampling keys every 100ms to clear expired entries from memory (active).
01.5
Append-Only File (AOF) Durability
Append every write command (SET, DEL) to disk. On server reboot, read and replay the file to restore in-memory state.
Verification Commands
Install the Redis command-line tools, but run these against your own server on port 6379, not a Redis server. The first GET should return "active"; the second should return (nil).
# 1. Test basic operations with official redis-cli
redis-cli -p 6379 PING
redis-cli -p 6379 SET token_user123 "active" EX 2
redis-cli -p 6379 GET token_user123
sleep 2
redis-cli -p 6379 GET token_user123 # Returns (nil)
# 2. Run official redis-benchmark under concurrency
redis-benchmark -p 6379 -t set,get -n 10000 -c 50 -q
Acceptance Criteria
The server must pass all standard redis-cli commands. redis-benchmark must complete 10,000 operations across 50 concurrent connections with zero dropped sockets. Terminating the process with kill -9 and restarting must restore all unexpired keys.