Skip to content

feat: Redis pipeline support #134

@FumingPower3925

Description

@FumingPower3925

Summary

Implement Redis pipelining — batching multiple commands into a single write and collecting responses in order. This dramatically reduces round-trip overhead for bulk operations.

Design

Package: driver/redis/pipeline.go

API

type Pipeline struct {
    client   *Client
    commands []pipelineCmd
}

type pipelineCmd struct {
    args   []string
    doneCh chan Value
}

func (c *Client) Pipeline() *Pipeline

func (p *Pipeline) Get(key string) *StringCmd
func (p *Pipeline) Set(key string, value any, exp time.Duration) *StatusCmd
func (p *Pipeline) Del(keys ...string) *IntCmd
func (p *Pipeline) Incr(key string) *IntCmd
// ... all command methods mirror Client but return deferred result types

func (p *Pipeline) Exec(ctx context.Context) ([]Cmd, error)

Deferred Result Types

type StringCmd struct {
    val string
    err error
}
func (c *StringCmd) Result() (string, error)

type IntCmd struct {
    val int64
    err error
}
func (c *IntCmd) Result() (int64, error)

type StatusCmd struct {
    val string
    err error
}
func (c *StatusCmd) Result() (string, error)

Execution Flow

  1. User calls pipeline methods — commands are buffered, not sent
  2. Exec(ctx) serializes ALL commands into a single byte buffer
  3. Writes entire buffer in one event loop write
  4. Creates N doneCh channels (one per command)
  5. Event loop receives responses — FIFO order matches command order
  6. Each response dispatched to corresponding doneCh
  7. Exec collects all results, returns

Automatic Pipelining (optional)

For advanced use, automatic pipelining collects commands from multiple goroutines within a time window:

func WithAutoPipeline(window time.Duration) Option

Commands arriving within the window are batched automatically. This is an optimization for high-concurrency scenarios.

Acceptance Criteria

  • Pipeline API with all major command methods
  • Exec batches commands into single write
  • Responses correctly matched to commands (FIFO)
  • Deferred result types with type-safe Result() methods
  • Error in one command doesn't affect others
  • Context cancellation during Exec
  • Benchmark: pipelined vs non-pipelined throughput
  • Unit tests with 100+ commands in single pipeline

Dependencies

  • Depends on 133 (Redis client API + RedisState)

Metadata

Metadata

Labels

area/driverDatabase/cache driver infrastructuredriver/redisRedis driver

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions