Slide 1

Slide 1 text

Writing faster Redis client Golang Warsaw #51 (spring) 2023 Oleg Kovalov 󰑒 olegk.dev

Slide 2

Slide 2 text

Me 2

Slide 3

Slide 3 text

- Open source addicted gopher - Fan of linters (co-author go-critic) Me 3

Slide 4

Slide 4 text

- Open source addicted gopher - Fan of linters (co-author go-critic) - Father of a labrador Me 4

Slide 5

Slide 5 text

- Open source addicted gopher - Fan of linters (co-author go-critic) - Father of a labrador - Also Twitter/Telegram @go_perf - ... Me 5

Slide 6

Slide 6 text

- Open source addicted gopher - Fan of linters (co-author go-critic) - Father of a labrador - Also Twitter/Telegram @go_perf - ... olegk.dev Me 6

Slide 7

Slide 7 text

Agenda 7

Slide 8

Slide 8 text

- Intro - Redis - Go clients - Benchmarks - Conclusions Agenda 8

Slide 9

Slide 9 text

9 Same old story

Slide 10

Slide 10 text

10 Same old story

Slide 11

Slide 11 text

11 Same old story

Slide 12

Slide 12 text

Redis 12

Slide 13

Slide 13 text

What is inside Redis? 13

Slide 14

Slide 14 text

- One of the best C code - not only code but also documentation What is inside Redis? 14

Slide 15

Slide 15 text

- One of the best C code - not only code but also documentation - RESP3 protocol - compare it with YAML spec What is inside Redis? 15

Slide 16

Slide 16 text

- One of the best C code - not only code but also documentation - RESP3 protocol - compare it with YAML spec - TCP - network is the bottleneck (or not?) What is inside Redis? 16

Slide 17

Slide 17 text

What is inside Redis? 17 - One of the best C code - not only code but also documentation - RESP3 protocol - compare it with YAML spec - TCP - network is the bottleneck (or not?) - It’s everywhere - see https://db-engines.com/

Slide 18

Slide 18 text

What is inside Redis? 18 - One of the best C code - not only code but also documentation - RESP3 protocol - compare it with YAML spec - TCP - network is the bottleneck (or not?) - It’s everywhere - see https://db-engines.com/ - PERFORMANCE!!11!!11

Slide 19

Slide 19 text

TLDR: RESP3 19

Slide 20

Slide 20 text

TLDR: RESP3 20

Slide 21

Slide 21 text

Redis architectures 21

Slide 22

Slide 22 text

Go clients 22

Slide 23

Slide 23 text

- The popularest (17k ⭐ / 26k lines) - https://github.com/redis/go-redis Go clients 23

Slide 24

Slide 24 text

- The popularest (17k ⭐ / 26k lines) - https://github.com/redis/go-redis - The 2n popular (9.5k ⭐ / 6k lines) - https://github.com/gomodule/redigo Go clients 24

Slide 25

Slide 25 text

- The popularest (17k ⭐ / 26k lines) - https://github.com/redis/go-redis - The 2n popular (9.5k ⭐ / 6k lines) - https://github.com/gomodule/redigo - The freshest (1k ⭐ / 108k lines) - https://github.com/rueian/rueidis Go clients 25

Slide 26

Slide 26 text

- The popularest (17k ⭐ / 26k lines) - https://github.com/redis/go-redis - The 2n popular (9.5k ⭐ / 6k lines) - https://github.com/gomodule/redigo - The freshest (1k ⭐ / 108k lines) - https://github.com/rueian/rueidis - The oldest (0.6k ⭐ / 10k lines) - https://github.com/mediocregopher/radix Go clients 26

Slide 27

Slide 27 text

- The popularest (17k ⭐ / 26k lines) - https://github.com/redis/go-redis - The 2n popular (9.5k ⭐ / 6k lines) - https://github.com/gomodule/redigo - The freshest (1k ⭐ / 108k lines) - https://github.com/rueian/rueidis - The oldest (0.6k ⭐ / 10k lines) - https://github.com/mediocregopher/radix - The pipelinest (238 ⭐ / 6k lines) - https://github.com/joomcode/redispipe Go clients 27

Slide 28

Slide 28 text

Go clients - The popularest (17k ⭐ / 26k lines) - https://github.com/redis/go-redis - The 2n popular (9.5k ⭐ / 6k lines) - https://github.com/gomodule/redigo - The freshest (1k ⭐ / 108k lines) - https://github.com/rueian/rueidis - The oldest (0.6k ⭐ / 10k lines) - https://github.com/mediocregopher/radix - The pipelinest (238 ⭐ / 6k lines) - https://github.com/joomcode/redispipe - Our try (15 💖 / 3.5k of poetry) - https://github.com/cristalhq/redis 28

Slide 29

Slide 29 text

API review 29

Slide 30

Slide 30 text

func ExampleClient() { rdb := redis.NewClient(&redis.Options{ Addr: "localhost:6379", }) err := rdb.Set(ctx, "key", "value", 0).Err() if err != nil { panic(err) } val2, err := rdb.Get(ctx, "key2").Result() if err == redis.Nil { fmt.Println("key2 does not exist") } else if err != nil { panic(err) } else { fmt.Println("key2", val2) } API review: redis/go-redis 30

Slide 31

Slide 31 text

func ExampleClient() { c, err := redis.Dial("tcp", ":6379") if err != nil { panic(err) } defer c.Close() _, err = c.Do("SET", "k1", 1) if err != nil { panic(err) } n, err := redis.Int(c.Do("GET", "k1")) if err != nil { panic(err) } fmt.Printf("%#v\n", n) API review: gomodule/redigo 31

Slide 32

Slide 32 text

func ExampleClient() { client, err := rueidis.NewClient(rueidis.ClientOption{ InitAddress: []string{"127.0.0.1:6379"}, }) if err != nil { panic(err) } defer client.Close() ctx := context.Background() // SET key val NX err = client.Do(ctx, client.B().Set().Key("key").Value("val").Nx().Build()).Error() API review: rueian/rueidis 32

Slide 33

Slide 33 text

func ExampleClient() { client, err := radix.NewPool("tcp", "127.0.0.1:6379", 10) if err != nil { panic(err) } cmd := radix.Cmd(nil, "SET", "foo", "bar") if err := client.Do(cmd); err != nil { panic(err) } API review: mediocregopher/radix 33

Slide 34

Slide 34 text

func ExampleClient() { sender, err := redis.SingleRedis(ctx) if err != nil { panic(err) } client := redis.SyncCtx{sender} res := client.Do(ctx, "SET", "key", "ho") if err := redis.AsError(res); err != nil { panic(err) } fmt.Printf("result: %q\n", res) API review: joomcode/redispipe 34

Slide 35

Slide 35 text

Observations 35

Slide 36

Slide 36 text

- ~all of the clients are based on Redis commands - create commands -> submit to client -> get response Observations 36

Slide 37

Slide 37 text

- ~all of the clients are based on Redis commands - create commands -> submit to client -> get response - Returning ‘interface{}’ (or a modern ‘any’) allocates - which you cannot omit Observations 37

Slide 38

Slide 38 text

- ~all of the clients are based on Redis commands - create commands -> submit to client -> get response - Returning ‘interface{}’ (or a modern ‘any’) allocates - which you cannot omit - Not all clients understand context package - this can be achieved via wrapper or timeouts Observations 38

Slide 39

Slide 39 text

- ~all of the clients are based on Redis commands - create commands -> submit to client -> get response - Returning ‘interface{}’ (or a modern ‘any’) allocates - which you cannot omit - Not all clients understand context package - this can be achieved via wrapper or timeouts - Reusing memory is completely impossible - Go GC is a cool thing but we can do better Observations 39

Slide 40

Slide 40 text

API review: cristalhq/redis 40

Slide 41

Slide 41 text

func Example() { ctx := context.Background() client, err := redis.NewClient(ctx, &redis.Config{ Address: "127.0.0.1:6379", }) if err != nil { panic(err) } API review: cristalhq/redis 41

Slide 42

Slide 42 text

API review: cristalhq/redis 42 func Example() { ctx := context.Background() client, err := redis.NewClient(ctx, &redis.Config{ Address: "127.0.0.1:6379", }) if err != nil { panic(err) } hashmap := redis.NewHashMap("my-hash-map", client) if err := hashmap.Set(ctx, "key", "value"); err != nil { panic(err) } }

Slide 43

Slide 43 text

HashMap example 43

Slide 44

Slide 44 text

func (hm HashMap) Name() string { return hm.name } func (hm HashMap) Delete(ctx context.Context, fields ...string) (int64, error) { func (hm HashMap) Exists(ctx context.Context, field string) (bool, error) { func (hm HashMap) Get(ctx context.Context, field string) (string, error) { func (hm HashMap) GetAll(ctx context.Context) (map[string]string, error) { func (hm HashMap) IncBy(ctx context.Context, field string, delta int64) (int64, error) { func (hm HashMap) IncByFloat(ctx context.Context, field string, delta float64) (float64, error) { func (hm HashMap) Keys(ctx context.Context) ([]string, error) { func (hm HashMap) Len(ctx context.Context) (int64, error) { func (hm HashMap) MultiGet(ctx context.Context, fields ...string) ([]Value, error) { ... HashMap example 44

Slide 45

Slide 45 text

BitMap Commander Function Geo HashMap HyperLogLog Keys List PubSub Script Scripting Set SortedSet Stream Strings More data structures 45

Slide 46

Slide 46 text

func (c Commander) BitCount(ctx context.Context, key string, start, end int64) (int64, error) { func (c Commander) BitCountAll(ctx context.Context, key string) (int64, error) { func (c Commander) BitField(ctx context.Context, key string) error { func (c Commander) BitFieldReadOnly(ctx context.Context, key string) error { func (c Commander) BitOp(ctx context.Context, op BitMapOp, destKey string, keys ...string) (int64, error) { func (c Commander) BitPos(ctx context.Context, key string, bit int64, pos ...int64) (int64, error) { func (c Commander) GetBit(ctx context.Context, key string, offset int64) (int64, error) { ... Command them all 46

Slide 47

Slide 47 text

Real life example 47

Slide 48

Slide 48 text

import "github.com/cristalhq/redis" type UserService struct { cache *redis.HashMap db *postgresDB } Real life example 48

Slide 49

Slide 49 text

Real life example import "github.com/cristalhq/redis" type UserService struct { cache *redis.HashMap db *postgresDB } func (s *UserService) GetUser(ctx context.Context, userID string) (any, error) { user, err := s.cache.Get(ctx, userID) if err == nil { return user, nil } s.db.GetUser(...) // ... 49

Slide 50

Slide 50 text

Memory wisdom 50

Slide 51

Slide 51 text

func (hm HashMap) Keys(ctx context.Context) ([]string, error) { Memory wisdom 51

Slide 52

Slide 52 text

func (hm HashMap) Keys(ctx context.Context) ([]string, error) { func (hm HashMap) Keys(ctx context.Context, keys []string) ([]string, error) { Memory wisdom 52

Slide 53

Slide 53 text

func (hm HashMap) Keys(ctx context.Context) ([]string, error) { func (hm HashMap) Keys(ctx context.Context, keys []string) ([]string, error) { // Now just do pooling and you’re perf engineer! Memory wisdom 53

Slide 54

Slide 54 text

⚠ Benchmark disclaimer 54

Slide 55

Slide 55 text

- There is no perfect benchmark ⚠ Benchmark disclaimer 55

Slide 56

Slide 56 text

- There is no perfect benchmark - Make them repeatable and stable ⚠ Benchmark disclaimer 56

Slide 57

Slide 57 text

- There is no perfect benchmark - Make them repeatable and stable - It’s always YMMV - your mileage may vary ⚠ Benchmark disclaimer 57

Slide 58

Slide 58 text

- There is no perfect benchmark - Make them repeatable and stable - It’s always YMMV - your mileage may vary BTW: this slide must be in every presentation where “benchmark” is mentioned ⚠ Benchmark disclaimer 58

Slide 59

Slide 59 text

Redis bench 59

Slide 60

Slide 60 text

> redis-benchmark -n 100000 set key value Summary: throughput summary: 19964.06 requests per second Redis bench 60

Slide 61

Slide 61 text

> redis-benchmark -n 100000 set key value Summary: throughput summary: 19964.06 requests per second > redis-benchmark -n 100000 -P 64 set key value Summary: throughput summary: 446571.41 requests per second -P = Pipeline requests. Default 1 (no pipeline). Redis bench 61

Slide 62

Slide 62 text

Redis bench but latencies 62

Slide 63

Slide 63 text

> redis-benchmark -n 100000 set key value latency summary (msec): avg min p50 p95 p99 max 2.280 0.560 2.103 3.759 5.527 17.791 Redis bench but latencies 63

Slide 64

Slide 64 text

> redis-benchmark -n 100000 set key value latency summary (msec): avg min p50 p95 p99 max 2.280 0.560 2.103 3.759 5.527 17.791 > redis-benchmark -n 100000 -P 64 set key value latency summary (msec): avg min p50 p95 p99 max 6.785 1.648 6.455 10.479 19.663 22.223 -P = Pipeline requests. Default 1 (no pipeline). Redis bench but latencies 64

Slide 65

Slide 65 text

│ sec/op │ Client_cristalhq/sequential-set-10 634.5µ ± 1% Client_cristalhq/sequential-get-10 631.2µ ± 1% Client_goredis/sequential-set-10 634.5µ ± 1% Client_goredis/sequential-get-10 633.9µ ± 1% Client_mediocregopher/sequential-set-10 638.6µ ± 1% Client_mediocregopher/sequential-get-10 640.1µ ± 1% Client_rueidis/sequential-set-10 630.4µ ± 1% Client_rueidis/sequential-get-10 731.2µ ± 13% Clients benchmark (sequential) 65

Slide 66

Slide 66 text

Clients benchmark (parallel) │ sec/op │ Client_cristalhq/parallel-set-10 112.7µ ± 2% Client_cristalhq/parallel-get-10 112.5µ ± 3% Client_goredis/parallel-set-10 118.2µ ± 2% Client_goredis/parallel-get-10 117.2µ ± 1% Client_mediocregopher/parallel-set-10 96.11µ ± 2% Client_mediocregopher/parallel-get-10 95.71µ ± 2% Client_rueidis/parallel-set-10 91.55µ ± 2% Client_rueidis/parallel-get-10 92.31µ ± 2% 66

Slide 67

Slide 67 text

│ allocs/op │ Client_cristalhq/sequential-set-10 0.000 ± 0% Client_cristalhq/parallel-set-10 0.000 ± 0% Client_cristalhq/sequential-get-10 1.000 ± 0% Client_cristalhq/parallel-get-10 1.000 ± 0% Client_goredis/sequential-set-10 9.000 ± 0% Client_goredis/parallel-set-10 9.000 ± 0% Client_goredis/sequential-get-10 8.000 ± 0% Client_goredis/parallel-get-10 8.000 ± 0% Clients benchmark (allocs) 67

Slide 68

Slide 68 text

Shipilev’s curve 68

Slide 69

Slide 69 text

Shipilev’s curve 69

Slide 70

Slide 70 text

Bonus slide 70

Slide 71

Slide 71 text

- memcached Bonus slide 71

Slide 72

Slide 72 text

- memcached - same TCP thing - similar protocol - and less code Bonus slide 72

Slide 73

Slide 73 text

- memcached - same TCP thing - similar protocol - and less code But slightly more in-progress than Redis client 😉 Bonus slide 73

Slide 74

Slide 74 text

Conclusions 74

Slide 75

Slide 75 text

- Think about API Conclusions 75

Slide 76

Slide 76 text

- Think about API - Hand-written protocol is OK Conclusions 76

Slide 77

Slide 77 text

- Think about API - Hand-written protocol is OK - Benchmark Conclusions 77

Slide 78

Slide 78 text

- Think about API - Hand-written protocol is OK - Benchmark - Benchmark - Benchmark Conclusions 78

Slide 79

Slide 79 text

- Think about API - Hand-written protocol is OK - Benchmark - Benchmark - Benchmark - Keep things easy Conclusions 79

Slide 80

Slide 80 text

References 80

Slide 81

Slide 81 text

- cristalhq/redis (⭐ and “go get”) - https://github.com/cristalhq/redis - go-perftuner - https://github.com/go-perf/go-perftuner References 81

Slide 82

Slide 82 text

- cristalhq/redis (⭐ and “go get”) - https://github.com/cristalhq/redis - go-perftuner - https://github.com/go-perf/go-perftuner - Redis author (antirez) about docs/comments - http://antirez.com/news/124 References 82

Slide 83

Slide 83 text

Golang Warsaw team 👌 GogoApps 💕 Thanks 83

Slide 84

Slide 84 text

Thank you Questions? Telegram: @olegkovalov Twitter: @oleg_kovalov That’s all folks