Skip to content

Commit e07f55b

Browse files
authored
chore(buffers): Set the default read/write buffer size of Redis connection to 32KiB (#3483)
* update README.md Signed-off-by: Xiaolong Chen <[email protected]> * typo: 0.5MiB -> 256KiB Signed-off-by: Xiaolong Chen <[email protected]> * Set the default read/write buffer size of Redis connection to 32KiB Signed-off-by: Xiaolong Chen <[email protected]> --------- Signed-off-by: Xiaolong Chen <[email protected]>
1 parent b8682d0 commit e07f55b

File tree

9 files changed

+30
-30
lines changed

9 files changed

+30
-30
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ func main() {
301301
302302
### Buffer Size Configuration
303303
304-
go-redis uses 0.5MiB read and write buffers by default for optimal performance. For high-throughput applications or large pipelines, you can customize buffer sizes:
304+
go-redis uses 32KiB read and write buffers by default for optimal performance. For high-throughput applications or large pipelines, you can customize buffer sizes:
305305
306306
```go
307307
rdb := redis.NewClient(&redis.Options{
@@ -376,7 +376,7 @@ You can find further details in the [query dialect documentation](https://redis.
376376
377377
#### Custom buffer sizes
378378
Prior to v9.12, the buffer size was the default go value of 4096 bytes. Starting from v9.12,
379-
go-redis uses 256KiB read and write buffers by default for optimal performance.
379+
go-redis uses 32KiB read and write buffers by default for optimal performance.
380380
For high-throughput applications or large pipelines, you can customize buffer sizes:
381381
382382
```go

internal/pool/buffer_size_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ var _ = Describe("Buffer Size Configuration", func() {
3434
Expect(err).NotTo(HaveOccurred())
3535
defer connPool.CloseConn(cn)
3636

37-
// Check that default buffer sizes are used (256KiB)
37+
// Check that default buffer sizes are used (32KiB)
3838
writerBufSize := getWriterBufSizeUnsafe(cn)
3939
readerBufSize := getReaderBufSizeUnsafe(cn)
4040

41-
Expect(writerBufSize).To(Equal(proto.DefaultBufferSize)) // Default 256KiB buffer size
42-
Expect(readerBufSize).To(Equal(proto.DefaultBufferSize)) // Default 256KiB buffer size
41+
Expect(writerBufSize).To(Equal(proto.DefaultBufferSize)) // Default 32KiB buffer size
42+
Expect(readerBufSize).To(Equal(proto.DefaultBufferSize)) // Default 32KiB buffer size
4343
})
4444

4545
It("should use custom buffer sizes when specified", func() {
@@ -79,28 +79,28 @@ var _ = Describe("Buffer Size Configuration", func() {
7979
Expect(err).NotTo(HaveOccurred())
8080
defer connPool.CloseConn(cn)
8181

82-
// Check that default buffer sizes are used (256KiB)
82+
// Check that default buffer sizes are used (32KiB)
8383
writerBufSize := getWriterBufSizeUnsafe(cn)
8484
readerBufSize := getReaderBufSizeUnsafe(cn)
8585

86-
Expect(writerBufSize).To(Equal(proto.DefaultBufferSize)) // Default 256KiB buffer size
87-
Expect(readerBufSize).To(Equal(proto.DefaultBufferSize)) // Default 256KiB buffer size
86+
Expect(writerBufSize).To(Equal(proto.DefaultBufferSize)) // Default 32KiB buffer size
87+
Expect(readerBufSize).To(Equal(proto.DefaultBufferSize)) // Default 32KiB buffer size
8888
})
8989

90-
It("should use 256KiB default buffer sizes for standalone NewConn", func() {
91-
// Test that NewConn (without pool) also uses 256KiB buffers
90+
It("should use 32KiB default buffer sizes for standalone NewConn", func() {
91+
// Test that NewConn (without pool) also uses 32KiB buffers
9292
netConn := newDummyConn()
9393
cn := pool.NewConn(netConn)
9494
defer cn.Close()
9595

9696
writerBufSize := getWriterBufSizeUnsafe(cn)
9797
readerBufSize := getReaderBufSizeUnsafe(cn)
9898

99-
Expect(writerBufSize).To(Equal(proto.DefaultBufferSize)) // Default 256KiB buffer size
100-
Expect(readerBufSize).To(Equal(proto.DefaultBufferSize)) // Default 256KiB buffer size
99+
Expect(writerBufSize).To(Equal(proto.DefaultBufferSize)) // Default 32KiB buffer size
100+
Expect(readerBufSize).To(Equal(proto.DefaultBufferSize)) // Default 32KiB buffer size
101101
})
102102

103-
It("should use 256KiB defaults even when pool is created directly without buffer sizes", func() {
103+
It("should use 32KiB defaults even when pool is created directly without buffer sizes", func() {
104104
// Test the scenario where someone creates a pool directly (like in tests)
105105
// without setting ReadBufferSize and WriteBufferSize
106106
connPool = pool.NewConnPool(&pool.Options{
@@ -114,12 +114,12 @@ var _ = Describe("Buffer Size Configuration", func() {
114114
Expect(err).NotTo(HaveOccurred())
115115
defer connPool.CloseConn(cn)
116116

117-
// Should still get 256KiB defaults because NewConnPool sets them
117+
// Should still get 32KiB defaults because NewConnPool sets them
118118
writerBufSize := getWriterBufSizeUnsafe(cn)
119119
readerBufSize := getReaderBufSizeUnsafe(cn)
120120

121-
Expect(writerBufSize).To(Equal(proto.DefaultBufferSize)) // Default 256KiB buffer size
122-
Expect(readerBufSize).To(Equal(proto.DefaultBufferSize)) // Default 256KiB buffer size
121+
Expect(writerBufSize).To(Equal(proto.DefaultBufferSize)) // Default 32KiB buffer size
122+
Expect(readerBufSize).To(Equal(proto.DefaultBufferSize)) // Default 32KiB buffer size
123123
})
124124
})
125125

internal/pool/conn.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ func NewConnWithBufferSize(netConn net.Conn, readBufSize, writeBufSize int) *Con
3737
createdAt: time.Now(),
3838
}
3939

40-
// Use specified buffer sizes, or fall back to 0.5MiB defaults if 0
40+
// Use specified buffer sizes, or fall back to 32KiB defaults if 0
4141
if readBufSize > 0 {
4242
cn.rd = proto.NewReaderSize(netConn, readBufSize)
4343
} else {
44-
cn.rd = proto.NewReader(netConn) // Uses 0.5MiB default
44+
cn.rd = proto.NewReader(netConn) // Uses 32KiB default
4545
}
4646

4747
if writeBufSize > 0 {

internal/proto/reader.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import (
1212
"github.com/redis/go-redis/v9/internal/util"
1313
)
1414

15-
// DefaultBufferSize is the default size for read/write buffers (256 KiB).
16-
const DefaultBufferSize = 256 * 1024
15+
// DefaultBufferSize is the default size for read/write buffers (32 KiB).
16+
const DefaultBufferSize = 32 * 1024
1717

1818
// redis resp protocol data type.
1919
const (

options.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,14 @@ type Options struct {
135135
// Larger buffers can improve performance for commands that return large responses.
136136
// Smaller buffers can improve memory usage for larger pools.
137137
//
138-
// default: 256KiB (262144 bytes)
138+
// default: 32KiB (32768 bytes)
139139
ReadBufferSize int
140140

141141
// WriteBufferSize is the size of the bufio.Writer buffer for each connection.
142142
// Larger buffers can improve performance for large pipelines and commands with many arguments.
143143
// Smaller buffers can improve memory usage for larger pools.
144144
//
145-
// default: 256KiB (262144 bytes)
145+
// default: 32KiB (32768 bytes)
146146
WriteBufferSize int
147147

148148
// PoolFIFO type of connection pool.

osscluster.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,14 @@ type ClusterOptions struct {
9696
// Larger buffers can improve performance for commands that return large responses.
9797
// Smaller buffers can improve memory usage for larger pools.
9898
//
99-
// default: 256KiB (262144 bytes)
99+
// default: 32KiB (32768 bytes)
100100
ReadBufferSize int
101101

102102
// WriteBufferSize is the size of the bufio.Writer buffer for each connection.
103103
// Larger buffers can improve performance for large pipelines and commands with many arguments.
104104
// Smaller buffers can improve memory usage for larger pools.
105105
//
106-
// default: 256KiB (262144 bytes)
106+
// default: 32KiB (32768 bytes)
107107
WriteBufferSize int
108108

109109
TLSConfig *tls.Config

ring.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,14 @@ type RingOptions struct {
128128
// Larger buffers can improve performance for commands that return large responses.
129129
// Smaller buffers can improve memory usage for larger pools.
130130
//
131-
// default: 256KiB (262144 bytes)
131+
// default: 32KiB (32768 bytes)
132132
ReadBufferSize int
133133

134134
// WriteBufferSize is the size of the bufio.Writer buffer for each connection.
135135
// Larger buffers can improve performance for large pipelines and commands with many arguments.
136136
// Smaller buffers can improve memory usage for larger pools.
137137
//
138-
// default: 256KiB (262144 bytes)
138+
// default: 32KiB (32768 bytes)
139139
WriteBufferSize int
140140

141141
TLSConfig *tls.Config

sentinel.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,14 @@ type FailoverOptions struct {
9494
// Larger buffers can improve performance for commands that return large responses.
9595
// Smaller buffers can improve memory usage for larger pools.
9696
//
97-
// default: 256KiB (262144 bytes)
97+
// default: 32KiB (32768 bytes)
9898
ReadBufferSize int
9999

100100
// WriteBufferSize is the size of the bufio.Writer buffer for each connection.
101101
// Larger buffers can improve performance for large pipelines and commands with many arguments.
102102
// Smaller buffers can improve memory usage for larger pools.
103103
//
104-
// default: 256KiB (262144 bytes)
104+
// default: 32KiB (32768 bytes)
105105
WriteBufferSize int
106106

107107
PoolFIFO bool

universal.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ type UniversalOptions struct {
6565
// Larger buffers can improve performance for commands that return large responses.
6666
// Smaller buffers can improve memory usage for larger pools.
6767
//
68-
// default: 256KiB (262144 bytes)
68+
// default: 32KiB (32768 bytes)
6969
ReadBufferSize int
7070

7171
// WriteBufferSize is the size of the bufio.Writer buffer for each connection.
7272
// Larger buffers can improve performance for large pipelines and commands with many arguments.
7373
// Smaller buffers can improve memory usage for larger pools.
7474
//
75-
// default: 256KiB (262144 bytes)
75+
// default: 32KiB (32768 bytes)
7676
WriteBufferSize int
7777

7878
// PoolFIFO uses FIFO mode for each node connection pool GET/PUT (default LIFO).

0 commit comments

Comments
 (0)