A Redis-compatible server built from scratch in Go to learn Redis internals: the RESP protocol, event-driven I/O, key expiry, object encoding, eviction, and AOF persistence.
- Custom encoder/decoder supporting simple strings (
+), errors (-), integers (:), bulk strings ($), and arrays (*). - Unit tests in
core/resp_test.go.
| Command | Description |
|---|---|
PING [message] |
Health check / echo |
SET key value [EX seconds] |
Set a string value, with optional expiry |
GET key |
Get a string value |
DEL key [key ...] |
Delete one or more keys |
EXPIRE key seconds |
Set a TTL on an existing key |
TTL key |
Get remaining TTL in seconds |
INCR key |
Increment an integer-encoded key |
BGREWRITEAOF |
Force a full AOF rewrite |
- Sync server (
server/sync_tcp.go) - plainnet.Listen, handles one connection fully before accepting the next (no concurrency). - Async server (
server/async_tcp.go, wired up inmain.go) - raw non-blocking sockets + akqueueevent loop (BSD/macOS) for multiplexing many client connections on a single thread.
- Passive expiry on read (checked in
Get). - Active expiry cron in the async server's main loop: every 1s, samples up to 20 keys with a TTL and deletes expired ones, repeating while at least 25% of the sample was expired (mirrors Redis's active-expire cycle).
- Values carry a type + encoding byte, mirroring Redis's
tryObjectEncoding: integer strings are encoded asint, short strings (<=44 bytes) asembstr, longer strings asraw.
- When the key count reaches
KeysLimit(default 1000), an arbitrary key is evicted before the next write.
BGREWRITEAOF(also triggered automatically after everySET) dumps the full dataset to an AOF file as RESP-encodedSETcommands.- On each new client connection, the async server replays the AOF file to rebuild state.
--host/--portCLI flags (default0.0.0.0:7379).KeysLimitandAOFFilepath are set inconfig/config.go.
go run main.go --port 7379Then connect with any RESP client, e.g.:
redis-cli -p 7379- Go 1.26+
- macOS or another BSD (the async server uses
kqueue; no Linux/epollor Windows backend yet)
core/- RESP codec, command evaluation, storage, expiry, eviction, AOFserver/- sync and async TCP server implementationsconfig/- runtime configuration