A Redis-compatible in-memory key-value store written in Go, built from scratch as a learning project.
Anahtar started as a simple TCP key-value server and is gradually evolving toward a more complete Redis-like server, including RESP protocol support, key expiration, persistence, and performance testing.
-
TCP server
-
Concurrent client connections
-
SET,GET,DEL, andPINGcommands -
Thread-safe in-memory storage
-
Key expiration with:
EX— secondsPX— milliseconds
-
Lazy expiration
-
Background expired-key cleanup
-
RESP request parsing
-
Multiple commands over a persistent TCP connection
-
RESP response encoding
-
Compatible with
redis-clifor basic commands
- Complete RESP protocol handling
- Additional Redis data types
- Append-only persistence / WAL
- Database recovery on startup
- Concurrent load testing
- Performance profiling and optimization
- Possible store sharding for improved concurrency
TCP Connection
│
▼
RESP Reader
│
▼
[]string command
│
▼
Handler
│
▼
Store
│
▼
Handler Result
│
▼
RESP Writer
│
▼
TCP Connection
The storage layer is intentionally independent of the RESP protocol. The handler deals with command semantics, while the RESP package is responsible for protocol encoding and decoding.
PING
Returns:
PONG
SET key value
Set a key without expiration.
Expiration can also be specified:
SET key value EX 10
SET key value PX 5000
GET key
Returns the stored value if the key exists.
DEL key
Deletes a key.
Anahtar currently runs on TCP port 7777.
Using redis-cli:
redis-cli -p 7777Then:
127.0.0.1:7777> SET foo bar
OK
127.0.0.1:7777> GET foo
"bar"
127.0.0.1:7777> DEL foo
OK
127.0.0.1:7777> GET foo
(nil)
Multiple commands can be sent over the same TCP connection.
Anahtar uses the Redis Serialization Protocol (RESP) for communication.
For example:
SET foo bar
is represented as:
*3\r\n
$3\r\n
SET\r\n
$3\r\n
foo\r\n
$3\r\n
bar\r\n
Responses are encoded using RESP types such as:
+OK\r\n
for Simple Strings,
$3\r\nbar\r\n
for Bulk Strings,
-ERR message\r\n
for errors, and:
$-1\r\n
for a null Bulk String.
Keys can have an optional expiration time.
Example:
SET foo bar EX 10
The key will expire after 10 seconds.
Anahtar uses both:
- lazy expiration when a key is accessed
- a background sweeper for removing expired entries
The store is protected by a read/write mutex to allow concurrent access safely.
anahtar/
├── cmd/
├── internal/
│ ├── handler/
│ ├── resp/
│ ├── server/
│ └── store/
├── go.mod
└── README.md
The exact structure may evolve as the project grows.
The goal of Anahtar is not simply to reproduce Redis. It is primarily a learning project focused on understanding how a networked data store works internally.
The project is being developed incrementally, with each stage introducing a new systems concept:
- TCP networking
- Concurrent connections
- In-memory data structures
- Synchronization and race conditions
- Key expiration
- Network protocol design
- RESP parsing and encoding
- Persistence
- Load testing
- Performance optimization
- TCP server
- Basic command handling
- Thread-safe key-value store
-
SET/GET/DEL - Key expiration
- Background expiration cleanup
- RESP request parsing
- Persistent TCP command loop
- RESP response encoding
- Clean RESP-independent handler architecture
- Additional Redis data type
- Append-only persistence
- Startup recovery
- Load testing
- Performance profiling
- Store optimization / sharding
Anahtar means "key" in Turkish — fitting for a key-value store.