Skip to content

Set the default read/write buffer size of Redis connection to 32KiB #3483

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

Merged
merged 3 commits into from
Aug 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ func main() {

### Buffer Size Configuration

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:
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:

```go
rdb := redis.NewClient(&redis.Options{
Expand Down Expand Up @@ -376,7 +376,7 @@ You can find further details in the [query dialect documentation](https://redis.

#### Custom buffer sizes
Prior to v9.12, the buffer size was the default go value of 4096 bytes. Starting from v9.12,
go-redis uses 256KiB read and write buffers by default for optimal performance.
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:

```go
Expand Down
28 changes: 14 additions & 14 deletions internal/pool/buffer_size_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ var _ = Describe("Buffer Size Configuration", func() {
Expect(err).NotTo(HaveOccurred())
defer connPool.CloseConn(cn)

// Check that default buffer sizes are used (256KiB)
// Check that default buffer sizes are used (32KiB)
writerBufSize := getWriterBufSizeUnsafe(cn)
readerBufSize := getReaderBufSizeUnsafe(cn)

Expect(writerBufSize).To(Equal(proto.DefaultBufferSize)) // Default 256KiB buffer size
Expect(readerBufSize).To(Equal(proto.DefaultBufferSize)) // Default 256KiB buffer size
Expect(writerBufSize).To(Equal(proto.DefaultBufferSize)) // Default 32KiB buffer size
Expect(readerBufSize).To(Equal(proto.DefaultBufferSize)) // Default 32KiB buffer size
})

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

// Check that default buffer sizes are used (256KiB)
// Check that default buffer sizes are used (32KiB)
writerBufSize := getWriterBufSizeUnsafe(cn)
readerBufSize := getReaderBufSizeUnsafe(cn)

Expect(writerBufSize).To(Equal(proto.DefaultBufferSize)) // Default 256KiB buffer size
Expect(readerBufSize).To(Equal(proto.DefaultBufferSize)) // Default 256KiB buffer size
Expect(writerBufSize).To(Equal(proto.DefaultBufferSize)) // Default 32KiB buffer size
Expect(readerBufSize).To(Equal(proto.DefaultBufferSize)) // Default 32KiB buffer size
})

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

writerBufSize := getWriterBufSizeUnsafe(cn)
readerBufSize := getReaderBufSizeUnsafe(cn)

Expect(writerBufSize).To(Equal(proto.DefaultBufferSize)) // Default 256KiB buffer size
Expect(readerBufSize).To(Equal(proto.DefaultBufferSize)) // Default 256KiB buffer size
Expect(writerBufSize).To(Equal(proto.DefaultBufferSize)) // Default 32KiB buffer size
Expect(readerBufSize).To(Equal(proto.DefaultBufferSize)) // Default 32KiB buffer size
})

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

// Should still get 256KiB defaults because NewConnPool sets them
// Should still get 32KiB defaults because NewConnPool sets them
writerBufSize := getWriterBufSizeUnsafe(cn)
readerBufSize := getReaderBufSizeUnsafe(cn)

Expect(writerBufSize).To(Equal(proto.DefaultBufferSize)) // Default 256KiB buffer size
Expect(readerBufSize).To(Equal(proto.DefaultBufferSize)) // Default 256KiB buffer size
Expect(writerBufSize).To(Equal(proto.DefaultBufferSize)) // Default 32KiB buffer size
Expect(readerBufSize).To(Equal(proto.DefaultBufferSize)) // Default 32KiB buffer size
})
})

Expand Down
4 changes: 2 additions & 2 deletions internal/pool/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ func NewConnWithBufferSize(netConn net.Conn, readBufSize, writeBufSize int) *Con
createdAt: time.Now(),
}

// Use specified buffer sizes, or fall back to 0.5MiB defaults if 0
// Use specified buffer sizes, or fall back to 32KiB defaults if 0
if readBufSize > 0 {
cn.rd = proto.NewReaderSize(netConn, readBufSize)
} else {
cn.rd = proto.NewReader(netConn) // Uses 0.5MiB default
cn.rd = proto.NewReader(netConn) // Uses 32KiB default
}

if writeBufSize > 0 {
Expand Down
4 changes: 2 additions & 2 deletions internal/proto/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import (
"github.com/redis/go-redis/v9/internal/util"
)

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

// redis resp protocol data type.
const (
Expand Down
4 changes: 2 additions & 2 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,14 @@ type Options struct {
// Larger buffers can improve performance for commands that return large responses.
// Smaller buffers can improve memory usage for larger pools.
//
// default: 256KiB (262144 bytes)
// default: 32KiB (32768 bytes)
ReadBufferSize int

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

// PoolFIFO type of connection pool.
Expand Down
4 changes: 2 additions & 2 deletions osscluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,14 @@ type ClusterOptions struct {
// Larger buffers can improve performance for commands that return large responses.
// Smaller buffers can improve memory usage for larger pools.
//
// default: 256KiB (262144 bytes)
// default: 32KiB (32768 bytes)
ReadBufferSize int

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

TLSConfig *tls.Config
Expand Down
4 changes: 2 additions & 2 deletions ring.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,14 @@ type RingOptions struct {
// Larger buffers can improve performance for commands that return large responses.
// Smaller buffers can improve memory usage for larger pools.
//
// default: 256KiB (262144 bytes)
// default: 32KiB (32768 bytes)
ReadBufferSize int

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

TLSConfig *tls.Config
Expand Down
4 changes: 2 additions & 2 deletions sentinel.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,14 @@ type FailoverOptions struct {
// Larger buffers can improve performance for commands that return large responses.
// Smaller buffers can improve memory usage for larger pools.
//
// default: 256KiB (262144 bytes)
// default: 32KiB (32768 bytes)
ReadBufferSize int

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

PoolFIFO bool
Expand Down
4 changes: 2 additions & 2 deletions universal.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ type UniversalOptions struct {
// Larger buffers can improve performance for commands that return large responses.
// Smaller buffers can improve memory usage for larger pools.
//
// default: 256KiB (262144 bytes)
// default: 32KiB (32768 bytes)
ReadBufferSize int

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

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