About
A Golang implemented Redis Server and Cluster. Go 语言实现的 Redis 服务器和分布式集群
# Godis
[中文版](https://github.com/hdt3213/godis/blob/master/README_CN.md)
`Godis` is a golang implementation of Redis Server, which intents to provide an example of writing a high concurrent
middleware using golang.
Key Features:
- Support string, list, hash, set, sorted set, bitmap
- Concurrent Core for better performance
- TTL
- Publish/Subscribe
- GEO
- AOF and AOF Rewrite
- RDB read and write
- Multi Database and `SELECT` command
- Transaction is **Atomic** and Isolated. If any errors are encountered during execution, godis will rollback the executed commands
- Replication
- Server-side Cluster which is transparent to client. You can connect to any node in the cluster to access all data in the cluster.
- Cluster metadata management based on Raft. Support dynamic expansion, rebalancing and failover.
- `MSET`, `MSETNX`, `DEL`, `Rename`, `RenameNX` command is supported and atomically executed in cluster mode, allow over multi node.
- `MULTI` Commands Transaction is supported within slot in cluster mode
If you could read Chinese, you can find more details in [My Blog](https://www.cnblogs.com/Finley/category/1598973.html).
## Get Started
You can get runnable program in the releases of this repository, which supports Linux and Darwin system.
```bash
./godis-darwin
```
```bash
./godis-linux
```
You could use redis-cli or other redis client to connect godis server, which listens on 0.0.0.0:6399 on default mode.
The program will try to read config file path from environment variable `CONFIG`.
If environment variable is not set, then the program try to read `redis.conf` in the working directory.
Please see [example.conf](./example.conf) for all configuration information.
### cluster mode
We provide node1.conf and node2.conf for demonstration. use following command line to start a two-node-cluster:
```bash
CONFIG=node1.conf ./godis-darwin &
CONFIG=node2.conf ./godis-darwin &
```
Connect to a node in the cluster to access all data in the cluster:
```cmd
redis-cli -p 6399
```
Please refer to [example.conf](./example.conf) for cluster configuration.
## Supported Commands
See: [commands.md](https://github.com/HDT3213/godis/blob/master/commands.md)
## Benchmark
Environment:
Go version:1.23
System: MacOS Monterey 12.5 M2 Air
Performance report by redis-benchmark:
```
…
```
## Read My Code
If you want to read my code in this repository, here is a simple guidance.
- project root: only the entry point
- config: config parser
- interface: some interface definitions
- lib: some utils, such as logger, sync utils and wildcard
I suggest focusing on the following directories:
- tcp: the tcp server
- redis: the redis protocol parser
- datastruct: the implements of data structures
- dict: a concurrent hash map
- list: a linked list
- lock: it is used to lock keys to ensure thread safety
- set: a hash set based on map
- sortedset: a sorted set implements based on skiplist
- database: the core of storage engine
- server.go: a standalone redis server, with multiple database
- database.go: data structure and base functions of single database
- exec.go: the gateway of database
- router.go: the command table
- keys.go: handlers for keys commands
- string.go: handlers for string commands
- list.go: handlers for list commands
- hash.go: handlers for hash commands
- set.go: handlers for set commands
- sortedset.go: handlers for sorted set commands
- pubsub.go: implements of publish / subscribe
- aof.go: implements of AOF persistence and rewrite
- geo.go: implements of geography features
- sys.go: authentication and other system function
- transaction.go: local transaction
- cluster:
- cluster.go: entrance of cluster mode
- com.go: communication within nodes
- del.go: atomic implementation of `delete` command in cluster
- keys.go: keys command
- mset.go: atomic implementation of `mset` command in cluster
- multi.go: entrance of distributed transaction
- pubsub.go: pub/sub in cluster
- rename.go: `rename` command in cluster
- tcc.go: try-commit-catch distributed transaction implementation
- aof: AOF persistence
# License
This project is licensed under the [GPL license](https://github.com/hdt3213/godis/blob/master/LICENSE).