Skip to content

Latest commit

 

History

13 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Redis Clone in Go(ongoing project)

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.

Features implemented

RESP protocol

  • Custom encoder/decoder supporting simple strings (+), errors (-), integers (:), bulk strings ($), and arrays (*).
  • Unit tests in core/resp_test.go.

Commands

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

Two server implementations

  • Sync server (server/sync_tcp.go) - plain net.Listen, handles one connection fully before accepting the next (no concurrency).
  • Async server (server/async_tcp.go, wired up in main.go) - raw non-blocking sockets + a kqueue event loop (BSD/macOS) for multiplexing many client connections on a single thread.

Key expiry

  • 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).

Object encoding

  • Values carry a type + encoding byte, mirroring Redis's tryObjectEncoding: integer strings are encoded as int, short strings (<=44 bytes) as embstr, longer strings as raw.

Eviction

  • When the key count reaches KeysLimit (default 1000), an arbitrary key is evicted before the next write.

AOF persistence

  • BGREWRITEAOF (also triggered automatically after every SET) dumps the full dataset to an AOF file as RESP-encoded SET commands.
  • On each new client connection, the async server replays the AOF file to rebuild state.

Configuration

  • --host / --port CLI flags (default 0.0.0.0:7379).
  • KeysLimit and AOFFile path are set in config/config.go.

Getting started

go run main.go --port 7379

Then connect with any RESP client, e.g.:

redis-cli -p 7379

Requirements

  • Go 1.26+
  • macOS or another BSD (the async server uses kqueue; no Linux/epoll or Windows backend yet)

Project layout

  • core/ - RESP codec, command evaluation, storage, expiry, eviction, AOF
  • server/ - sync and async TCP server implementations
  • config/ - runtime configuration

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages