Giter Club home page Giter Club logo

go-redis-pool's Introduction

go-redis-pool

Build Status Go Report Card Coverage Status GitHub release GitHub release date LICENSE GoDoc

go-redis-pool was designed to implement the read/write split in Redis master-slave mode, and easy way to sharding the data.

Installation

go-redis-pool requires a Go version with Modules support and uses import versioning. So please make sure to initialize a Go module before installing go-redis-pool:

go mod init github.com/my/repo
go get github.com/bitleak/go-redis-pool/v3

Quick Start

API documentation and examples are available via godoc

Setup The Master-Slave Pool

pool, err := pool.NewHA(&pool.HAConfig{
    Master: "127.0.0.1:6379",
    Slaves: []string{
        "127.0.0.1:6380",
        "127.0.0.1:6381",
    },
    Password: "", // set master password
    ReadonlyPassword: "", // use password if no set
})

pool.Set(ctx, "foo", "bar", 0)

The read-only commands would go throught slaves and write commands would into the master instance. We use the Round-Robin as default when determing which slave to serve the readonly request, and currently supports:

  • RoundRobin (default)
  • Random
  • Weight

For example, we change the distribution type to Weight:

pool, err := pool.NewHA(&pool.HAConfig{
    Master: "127.0.0.1:6379",
    Slaves: []string{
        "127.0.0.1:6380",  // default weight is 100 if missing
        "127.0.0.1:6381:200", // weight is 200
        "127.0.0.1:6382:300", // weigght is 300
    },
    PollType: pool.PollByWeight,
})

The first slave would serve 1/6 reqeusts, and second slave would serve 2/6, last one would serve 3/6.

Auto Eject The Failure Host
pool, err := pool.NewHA(&pool.HAConfig{
    Master: "127.0.0.1:6379",
    Slaves: []string{
        "127.0.0.1:6380",  // default weight is 100 if missing
        "127.0.0.1:6381:200", // weight is 200
        "127.0.0.1:6382:300", // weigght is 300
    },
    AutoEjectHost: true,
    ServerFailureLimit: 3,
    ServerRetryTimeout: 5 * time.Second,
    MinServerNum: 2,
})

The pool would evict the host if reached ServerFailureLimit times of failure and retry the host after ServerRetryTimeout. The MinServerNum was used to avoid evicting too many and would overrun other alive servers.

Setup The Sharding Pool

pool, err := pool.NewShard(&pool.ShardConfig{
    Shards: []*HAConfig {
        // shard 1
        &pool.HAConfig{
            Master: "127.0.0.1:6379",
            Slaves: []string{
                "127.0.0.1:6380",
                "127.0.0.1:6381",
            },
            DistributeType: pool.DistributeByModular,
            HashFn: hashkit.Xxh3,
        },
        // shard 2
        &pool.HAConfig{
            Master: "127.0.0.1:6382",
            Slaves: []string{
                "127.0.0.1:6383",
            },
            DistributeType: pool.DistributeByModular,
            HashFn: hashkit.Xxh3,
        },
    },
})

pool.Set(ctx, "foo", "bar", 0)

Shard pool use the CRC32 as default hash function when sharding the key, you can overwrite the HashFn in config if wants to use other sharding hash function. The distribution type supports ketama and modular, default is modular.

Test

$ make test

See Also

https://github.com/go-redis/redis

go-redis-pool's People

Contributors

git-hulk avatar nvorobev avatar yesphet avatar el-yurchito avatar lgtm-migrator avatar dependabot[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.