Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add DisableAutoPipelining to serve requests from the conn pool #646

Merged
merged 1 commit into from
Oct 13, 2024

Conversation

rueian
Copy link
Collaborator

@rueian rueian commented Oct 10, 2024

Auto pipelining can cause high p99 latencies (ref) and we are still trying to find a solution. This PR allows users to always use the pooled connections instead of pipelining commands into the same connections, except for client-side caching and pubsub requests.

The benchmark on 2 GCP n2-highcpu-2 machines, one runs redis-server, and the other one runs the following program, shows slightly better latencies and throughput than go-redis across concurrency 2, 4, 8, 16, and 32.

image
package main

import (
	"context"
	"fmt"
	"math/rand"
	"net/http"
	_ "net/http/pprof"
	"os"
	"strconv"
	"time"

	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promauto"
	"github.com/prometheus/client_golang/prometheus/promhttp"
	"github.com/redis/go-redis/v9"
	"github.com/redis/rueidis"
)

func prepData(n int) []string {
	data := make([]string, n)
	for i := range data {
		data[i] = strconv.Itoa(i)
	}
	rand.Shuffle(len(data), func(i, j int) { data[i], data[j] = data[j], data[i] })
	return data
}

const (
	keyCount = 1000000
)

func main() {
	useGoRedis := os.Args[1] == "goredis"
	concurrency, err := strconv.Atoi(os.Args[2])
	if err != nil {
		panic(err)
	}

	rand.Seed(time.Now().UnixNano())
	bucket := []float64{50, 100, 150, 200, 250, 300, 350, 400, 450, 500, 750, 1000, 1500, 2000, 3000, 4000}

	rl := promauto.NewHistogram(prometheus.HistogramOpts{
		Name: "micro_read_latency", Buckets: bucket,
		ConstLabels: map[string]string{"client": os.Args[1]},
	})

	go func() {
		http.Handle("/metrics", promhttp.Handler())
		http.ListenAndServe(":2112", nil)
	}()

	rc, err := rueidis.NewClient(rueidis.ClientOption{
		InitAddress:           []string{os.Args[3]},
		DisableAutoPipelining: true,
	})
	if err != nil {
		panic(err)
	}

	gc := redis.NewUniversalClient(&redis.UniversalOptions{
		Addrs: []string{os.Args[3]},
	})

	ctx := context.Background()

	goredisRead := func(key string) error {
		return gc.Get(ctx, key).Err()
	}
	rueidisRead := func(key string) error {
		return rc.Do(ctx, rc.B().Get().Key(key).Build()).Error()
	}

	var rfn func(key string) error

	if useGoRedis {
		rfn = goredisRead
	} else {
		rfn = rueidisRead
	}

	read1Fn := func(keys []string) {
		for _, k := range keys {
			ts := time.Now()
			err := rfn(k)
			rl.Observe(float64(time.Since(ts).Microseconds()))
			if err != nil {
				panic(err)
			}
		}
	}

	{
		keys := prepData(keyCount)
		data := prepData(keyCount)
		commands := make(rueidis.Commands, len(keys))
		for i := range commands {
			commands[i] = rc.B().Set().Key(keys[i]).Value(data[i]).Build()
		}
		ts := time.Now()
		for _, resp := range rc.DoMulti(ctx, commands...) {
			if err := resp.Error(); err != nil {
				panic(err)
			}
		}
		fmt.Println("ready", time.Since(ts))
	}

	if useGoRedis {
		rc.Close()
	} else {
		gc.Close()
	}

	for i := 0; i < concurrency; i++ {
		go func() {
			keys := prepData(keyCount)
			for {
				read1Fn(keys)
			}
		}()
	}
	time.Sleep(time.Minute * 2)
}

@rueian rueian force-pushed the feat-use-pool-env branch from 5ce9446 to a6f104d Compare October 10, 2024 01:43
@rueian rueian changed the title feat: add RUEIDIS_USE_CONN_POOLING env to serve requests from the conn pool feat: add DISABLE_AUTO_PIPELINING env to serve requests from the conn pool Oct 10, 2024
@rueian rueian force-pushed the feat-use-pool-env branch 4 times, most recently from b374d63 to 57f44fe Compare October 10, 2024 02:24
@rueian rueian changed the title feat: add DISABLE_AUTO_PIPELINING env to serve requests from the conn pool feat: add DisableAutoPipelining to serve requests from the conn pool Oct 10, 2024
@rueian rueian force-pushed the feat-use-pool-env branch 4 times, most recently from 4cf6b70 to 8871aab Compare October 12, 2024 18:20
@rueian rueian force-pushed the feat-use-pool-env branch from 8871aab to 679f295 Compare October 12, 2024 19:44
@rueian rueian marked this pull request as ready for review October 13, 2024 20:01
@rueian rueian merged commit b93778c into main Oct 13, 2024
54 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant