Skip to content

A lightweight, Redis-compatible key-value cache server implemented in Go that supports basic Redis commands and the RESP (Redis Serialization Protocol).

Notifications You must be signed in to change notification settings

ratishjain12/kv-cache

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Redis-like Key-Value Cache Server

A lightweight, Redis-compatible key-value cache server implemented in Go that supports basic Redis commands and the RESP (Redis Serialization Protocol).

Architecture

image

Core Components

  1. Main Server (main.go)

    • TCP server listening on port 6379 (Redis default)
    • Handles client connections and command routing
    • Implements the main event loop for processing client requests
  2. Command Handler (handler.go)

    • Maps Redis commands to handler functions
    • Manages in-memory data structures with thread-safe operations
    • Implements command validation and error handling
  3. RESP Protocol Implementation (resp.go)

    • Complete RESP (Redis Serialization Protocol) parser and serializer
    • Supports all RESP data types: strings, bulk strings, arrays, integers, errors, and null values
    • Handles both reading from and writing to client connections
  4. AOF Persistence (aof.go)

    • Append Only File implementation for data durability
    • Logs every write operation to disk for crash recovery
    • Thread-safe operations with automatic disk syncing
    • Replays commands on server restart to restore state

Data Storage

  • String Storage: Thread-safe map for key-value pairs using sync.RWMutex
  • Hash Storage: Nested map structure for hash operations with concurrent access protection
  • AOF Persistence: Append Only File logging for data durability and crash recovery

Thread Safety

  • Uses sync.RWMutex for concurrent read/write operations
  • Multiple readers can access data simultaneously
  • Writers get exclusive access to prevent data races

Supported Commands

Basic Commands

  • PING - Health check command
    • PING - Returns "PONG"
    • PING <message> - Returns the provided message

String Commands

  • SET - Set a key-value pair

    • SET <key> <value> - Stores key-value pair
    • Returns "OK" on success
  • GET - Retrieve a value by key

    • GET <key> - Returns the value for the given key
    • Returns null if key doesn't exist

Hash Commands

  • HSET - Set a field in a hash

    • HSET <key> <field> <value> - Sets field-value pair in hash
    • Returns "OK" on success
  • HGET - Get a field from a hash

    • HGET <key> <field> - Returns the value for the field
    • Returns null if key or field doesn't exist
  • HGETALL - Get all fields and values from a hash

    • HGETALL <key> - Returns array of field-value pairs
    • Returns null if key doesn't exist

System Commands

  • COMMAND - Returns empty array (basic implementation)

Installation & Usage

Prerequisites

  • Go 1.25.2 or later

Running the Server

  1. Clone or navigate to the project directory:

    cd /path/to/redis
  2. Run the server:

    go run .
  3. The server will start listening on port 6379:

    listening on port 6379
    

Testing with Redis CLI

You can test the server using the official Redis CLI:

# Connect to the server
redis-cli -p 6379

# Test basic commands
127.0.0.1:6379> PING
PONG

127.0.0.1:6379> SET mykey "Hello World"
OK

127.0.0.1:6379> GET mykey
"Hello World"

127.0.0.1:6379> HSET user:1 name "John"
OK

127.0.0.1:6379> HGET user:1 name
"John"

127.0.0.1:6379> HGETALL user:1
1) "name"
2) "John"

Protocol Support

This implementation fully supports the RESP protocol, making it compatible with standard Redis clients. The protocol handles:

  • Simple Strings: +OK\r\n
  • Bulk Strings: $5\r\nhello\r\n
  • Arrays: *2\r\n$3\r\nGET\r\n$3\r\nkey\r\n
  • Integers: :1000\r\n
  • Errors: -ERR error message\r\n
  • Null Values: $-1\r\n

Persistence (AOF)

This implementation includes Append Only File (AOF) persistence for data durability:

How AOF Works

  • Command Logging: Every write operation (SET, HSET) is logged to disk immediately
  • Append-Only: Commands are always appended to the end of the AOF file, never modified
  • Crash Recovery: On server restart, all logged commands are replayed to restore exact state
  • Thread-Safe: Uses mutex protection for concurrent access to AOF file
  • Auto-Sync: Background goroutine syncs data to disk every second

AOF Benefits

  • High Durability: Minimal data loss even on unexpected crashes
  • Human Readable: AOF files contain actual Redis commands for debugging
  • Incremental: Only new commands are appended, efficient storage
  • Exact Recovery: Reconstructs the exact state before crash

AOF File Format

The AOF file stores commands in RESP format, making it compatible with Redis clients:

*3
$3
SET
$3
key
$5
value
*4
$4
HSET
$4
user
$4
name
$4
John

Project Structure

redis/
├── main.go          # Main server entry point
├── handler.go       # Command handlers and data storage
├── resp.go          # RESP protocol implementation
├── aof.go           # AOF persistence implementation
├── go.mod           # Go module definition
└── README.md        # This file

Development

This project serves as a learning implementation of Redis-like functionality and demonstrates:

  • TCP server programming in Go
  • Protocol implementation (RESP)
  • Concurrent data access patterns
  • Command pattern implementation
  • Error handling and validation

About

A lightweight, Redis-compatible key-value cache server implemented in Go that supports basic Redis commands and the RESP (Redis Serialization Protocol).

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages