A high-performance Redis-compatible server written in Go, featuring both asynchronous (epoll-based) and synchronous implementations. This project demonstrates advanced Go networking concepts including non-blocking I/O, event-driven architecture, and RESP protocol parsing.
- Dual Server Implementations:
- Async Server: Linux epoll-based event loop for maximum performance
- Sync Server: Traditional goroutine-per-connection model for cross-platform compatibility
- RESP Protocol: Full Redis Serialization Protocol support
- Core Redis Commands:
PING,SET,GET,DEL,TTL - Key Expiration: Automatic cleanup of expired keys with background goroutines
- Multi-key Operations:
DELcommand supports deleting multiple keys at once - Docker Support: Containerized deployment for Linux epoll functionality
- Cross-Platform: Sync server runs natively on macOS, Windows, and Linux
- Event-driven: Uses Linux epoll for non-blocking I/O
- Single-threaded: Handles thousands of connections efficiently
- High performance: Similar to Redis, Nginx, and Node.js architecture
- Low resource usage: Minimal memory and CPU overhead
- Goroutine-based: One goroutine per client connection
- Cross-platform: Works on macOS, Windows, and Linux
- Simple architecture: Easy to understand and debug
- Good performance: Suitable for moderate loads
-
Clone and run with Docker:
git clone https://github.com/satyawaniaman/redis-golang.git cd redis-golang docker-compose up --build -
Test the server:
redis-cli -h localhost -p 6379 > PING PONG > SET mykey "Hello Redis!" OK > GET mykey "Hello Redis!"
-
Clone the repository:
git clone https://github.com/satyawaniaman/redis-golang.git cd redis-golang -
Switch to sync server (edit
main.go):// Change this line in main.go: server.RunSyncTCPServer() // instead of RunAsyncTCPServer()
-
Run the server:
go run main.go
-
Test with redis-cli:
redis-cli -h localhost -p 6379
| Command | Description | Example |
|---|---|---|
PING |
Test server connectivity | PING β PONG |
SET key value [EX seconds] |
Store a key-value pair with optional expiration | SET name "John" EX 60 β OK |
GET key |
Retrieve value by key | GET name β "John" |
DEL key [key ...] |
Delete one or more keys | DEL name age β (integer) 2 |
TTL key |
Check remaining TTL | TTL name β (integer) 45 |
- Automatic Expiration: Keys with TTL are automatically cleaned up by background goroutines
- Multi-key Operations:
DELcommand can delete multiple keys in a single operation - SET with Expiration: Use
SET key value EX secondsto set a key with immediate expiration
# Connect to the server
redis-cli -h localhost -p 6379
# Test basic commands
> PING
PONG
> SET user:1 "Alice"
OK
> SET user:2 "Bob" EX 10
OK
> GET user:1
"Alice"
> GET user:2
"Bob"
> TTL user:1
(integer) -1
> TTL user:2
(integer) 7
> DEL user:1 user:2
(integer) 2telnet localhost 6379
*1\r\n$4\r\nPING\r\n
+PONG\r\nThe project includes Docker configuration for running the high-performance async server:
- Dockerfile: Multi-stage build with Go 1.21 Alpine
- docker-compose.yml: Service configuration with port mapping
- Automatic builds: No external dependencies required
# View logs
docker-compose logs -f
# Stop the server
docker-compose down
# Rebuild after code changes
docker-compose up --buildredis_golang/
βββ main.go # Entry point (switches between async/sync)
βββ server/
β βββ async_tcp.go # Linux epoll-based server
β βββ sync_tcp.go # Cross-platform goroutine server
βββ core/
β βββ resp.go # RESP protocol parser
β βββ cmd.go # Command structures
β βββ eval.go # Command evaluation logic
β βββ comm.go # Communication wrapper
βββ test_client/
β βββ main.go # Go client for testing
βββ Dockerfile # Container configuration
βββ docker-compose.yml # Docker service setup
βββ go.mod # Go module definition
This is expected! The async server uses Linux-specific epoll. Solutions:
- Use Docker (recommended):
docker-compose up --build - Switch to sync server: Change
main.goto callserver.RunSyncTCPServer()
# Check if server is running
docker ps
# Check port accessibility
telnet localhost 6379
# View server logs
docker-compose logs- Async Server: Handles 10,000+ concurrent connections efficiently
- Sync Server: Good for moderate loads (hundreds of connections)
- Comprehensive Test Suite
- Unit tests for all core functions
- Integration tests for client-server communication
- Benchmark tests vs real Redis
- Load testing with concurrent clients
- Adding Performance Benchmarks
- RDB Snapshots: Save/load data to/from disk
- AOF (Append-Only File): Log all write operations
- Configurable persistence strategies
- Data recovery mechanisms
- Numeric Operations:
INCR,DECR,INCRBY,DECRBY - List Operations:
LPUSH,RPUSH,LPOP,RPOP,LLEN - Hash Operations:
HSET,HGET,HDEL,HKEYS,HVALS - Set Operations:
SADD,SREM,SMEMBERS,SISMEMBER
This project demonstrates:
- Network Programming: TCP servers, non-blocking I/O
- Go Concurrency: Goroutines, channels, event loops
- Protocol Implementation: RESP parsing and generation
- System Programming: Linux epoll, file descriptors
- Containerization: Docker, multi-stage builds
- Cross-platform Development: Platform-specific code handling
MIT License - feel free to use for commercial projects!
If this helped you build something cool, give it a star!β¨
Built with β€οΈ by Aman Satyawani to learn Redis like implementation in Go