Skip to content

About

The Go implementation of the Couchbase to Redis with DCP.

Resources

Contributing

Stars

10 stars

Watchers

0 watching

Forks

Repository files navigation

Go Dcp Redis Go Reference Go Report Card OpenSSF Scorecard

Go Dcp Redis streams documents from Couchbase Database Change Protocol (DCP) and writes to Redis in near real-time.

Features

  • Custom Redis commands per DCP event.
  • Multiple Redis operations for a DCP event(see Example).
  • Handling different DCP events such as expiration, deletion and mutation(see Example).
  • Pipelined, batched writes: DCP events are buffered and sent to Redis as pipelines (one per Redis instance), so throughput is not bound to one Redis round trip per event. Order is preserved per key.
  • Retries transient Redis errors (OOM, BUSY, LOADING, TRYAGAIN, CLUSTERDOWN, MASTERDOWN, READONLY, network errors) with backoff instead of crashing the connector.
  • Managing batch configurations such as batch size, batch byte size and checkpoint commit interval.
  • Multiple storage types (string, hash, json) with TTL support.
  • Redis Sentinel support for high availability deployments.
  • Redis Cluster support for horizontal scaling and high availability.
  • Multiple Redis instances: route each collection to a different Redis (standalone, Sentinel or Cluster) from config.
  • Scale up and down by custom membership algorithms(Couchbase, KubernetesHa, Kubernetes StatefulSet or Static, see examples).
  • Easily manageable configurations.

Example

Note: If you prefer to use the default mapper by entering the configuration instead of creating a custom mapper, please refer to this topic. Otherwise, you can refer to the example provided below:

package main

import (
  "fmt"
  "time"
  dcpredis "github.com/Trendyol/go-dcp-redis"
  "github.com/Trendyol/go-dcp-redis/couchbase"
  "github.com/Trendyol/go-dcp-redis/redis"
)

func mapper(event couchbase.Context) []redis.Model {
  var set = redis.Set{
    Key:   fmt.Sprintf("doc:%s", string(event.Event.Key)),
    Value: string(event.Event.Value),
    TTL:   time.Hour * 24, // 24 hours TTL
  }

  return []redis.Model{&set}
}

func main() {
  connector, err := dcpredis.NewConnectorBuilder("config.yml").
    SetMapper(mapper). // NOT NEEDED IF YOU'RE USING DEFAULT MAPPER. JUST CALL Build() FUNCTION
    Build()
  if err != nil {
    panic(err)
  }

  defer connector.Close()
  connector.Start()
}

Advanced Redis Usage Examples

Hash Storage Example:

func hashMapper(event couchbase.Context) []redis.Model {
  return []redis.Model{
    &redis.HSet{
      Key:   fmt.Sprintf("user:%s", string(event.Event.Key)),
      Field: "profile",
      Value: string(event.Event.Value),
      TTL:   time.Hour * 48,
    },
  }
}

Multiple Operations Example:

func multiMapper(event couchbase.Context) []redis.Model {
  key := string(event.Event.Key)
  value := string(event.Event.Value)

  return []redis.Model{
    // Store as string
    &redis.Set{
      Key:   fmt.Sprintf("doc:%s", key),
      Value: value,
      TTL:   time.Hour * 24,
    },
    // Store in hash for indexing
    &redis.HSet{
      Key:   "docs:index",
      Field: key,
      Value: time.Now().Unix(),
    },
  }
}

Raw Command Example:

func rawMapper(event couchbase.Context) []redis.Model {
  return []redis.Model{
    &redis.Raw{
      Operation: "ZADD",
      Key:       "docs:timeline",
      Args:      []interface{}{time.Now().Unix(), string(event.Event.Key)},
    },
  }
}

redis.Raw can send any command, including module commands such as HYRAT.SET or JSON.SET, and it is pipelined like every other model. A Raw command is sent as <Operation> <Key> <Args...>; Value and TTL are only used when Operation is SET (SET Key Value with TTL as expiry) or HSET (HSET Key Args[0] Args[1] followed by EXPIRE Key TTL). DEL and HDEL use Key and Args[0]. A nil reply (for example from a command with NX/XX) is not an error.

func hyratMapper(event couchbase.Context) []redis.Model {
  return []redis.Model{
    &redis.Raw{
      Operation: "HYRAT.SET",
      Key:       string(event.Event.Key),
      Args:      []interface{}{string(event.Event.Value)},
    },
  }
}

How Writes Are Batched

The DCP listener only appends the commands returned by the mapper to a pending batch and acknowledges the event. A background flusher sends everything that is pending as one pipeline per Redis instance while new events keep accumulating, so batches grow with the load (up to batchSizeLimit commands or batchByteSizeLimit bytes) and an event is written as soon as the previous pipeline completes. When the pending batch is full the listener waits, which applies back-pressure to the DCP stream.

The checkpoint is committed every batchTickerDuration, and also before the streams stop for a rebalance or shutdown, but only after every acknowledged event has been written to Redis. After a crash the connector replays events from the last checkpoint (at-least-once), so mapper commands should be idempotent, as before.

Configuration Examples

Standalone Redis Configuration

redis:
  host: localhost
  port: 6379
  password: ""
  db: 0

Redis Sentinel Configuration

redis:
  db: 0
  sentinel:
    masterName: mymaster
    sentinelAddrs:
      - localhost:26379
      - localhost:26380
      - localhost:26381
    password: ""

Redis Cluster Configuration

redis:
  cluster:
    addrs:
      - localhost:7000
      - localhost:7001
      - localhost:7002
      - localhost:7003
      - localhost:7004
      - localhost:7005
    routeByLatency: false
    routeRandomly: false
    readOnly: false

Multiple Redis Instances Configuration

The top-level redis block is the default instance. Additional named instances go under redis.instances, and a collection is routed to one of them with collectionKeyMapping[].target. Collections without a target keep writing to the default instance.

redis:
  host: localhost            # default instance
  port: 6379
  instances:
    cache-eu:
      host: redis-eu
      port: 6379
      username: eu-user
      password: eu-pass
    cache-cluster:
      cluster:
        addrs:
          - redis-c-1:7000
          - redis-c-2:7001
        password: cluster-pass
  collectionKeyMapping:
    - collection: products
      keyPrefix: "product:"
      target: cache-eu       # -> cache-eu
    - collection: orders
      keyPrefix: "order:"
      target: cache-cluster  # -> cache-cluster
    - collection: users
      keyPrefix: "user:"     # no target -> default instance

Each instance uses only its own credentials; nothing is inherited from the default instance. An unknown target, or an instance without host, sentinel or cluster, fails at startup, before any connection is opened.

Routing also applies to custom mappers: add a mapping entry with just collection and target, and event.RedisClient inside the mapper is the client of that instance.

To keep passwords out of the YAML file, load the config yourself and fill them in before building the connector:

file, _ := os.ReadFile("config.yml")
var cfg config.Connector
_ = yaml.Unmarshal(file, &cfg)

instance := cfg.Redis.Instances["cache-eu"]
instance.Password = os.Getenv("CACHE_EU_PASSWORD")
cfg.Redis.Instances["cache-eu"] = instance

connector, err := dcpredis.NewConnectorBuilder(&cfg).Build()

Dcp Configuration

Check out on go-dcp

Redis Specific Configuration

Variable Type Required Default Description
redis.host string yes* Redis host (*required if not using Sentinel)
redis.port int no 6379 Redis port
redis.username string no Redis username (Redis 6.0+)
redis.password string no Redis password
redis.db int no 0 Redis database number
redis.defaultTTL time.Duration no Default TTL for keys
redis.batchTickerDuration time.Duration no 10s Checkpoint commit interval. The commit waits until every acknowledged event is written to Redis
redis.batchSizeLimit int no 1000 Maximum number of commands in one pipeline; the DCP listener waits while the pending batch is full
redis.batchByteSizeLimit int, string no 10mb Maximum estimated payload of one pipeline, as bytes (10485760) or a size string (10mb)
redis.maxRetries int no 30 Retries of a command that failed with a transient error; -1 disables retries. Backoff starts at 5ms and doubles, capped at 1s for back-pressure errors (OOM, BUSY, TRYAGAIN, ~23s total) and 3s for failover / topology / network errors (~65s total, cluster slot map is reloaded before each retry)
redis.collectionKeyMapping []CollectionKeyMapping no Will be used for default mapper. Please read the next topic.
redis.sentinel RedisSentinel no Redis Sentinel configuration for high availability
redis.cluster RedisCluster no Redis Cluster configuration for horizontal scaling
redis.instances map[string]RedisInstance no Additional named Redis instances. Please read the Multiple Redis Instances topic.

Redis Instance Configuration

Every entry under redis.instances.<name> accepts the same connection fields as the top-level redis block.

Variable Type Required Default Description
redis.instances.<name>.host string yes* Redis host (*required if not using Sentinel/Cluster)
redis.instances.<name>.port int no 6379 Redis port
redis.instances.<name>.username string no Redis username (Redis 6.0+)
redis.instances.<name>.password string no Redis password
redis.instances.<name>.db int no 0 Redis database number
redis.instances.<name>.sentinel RedisSentinel no Sentinel configuration, same fields as redis.sentinel
redis.instances.<name>.cluster RedisCluster no Cluster configuration, same fields as redis.cluster

Redis Sentinel Configuration

Variable Type Required Default Description
redis.sentinel.masterName string yes Name of the Redis master in Sentinel configuration
redis.sentinel.sentinelAddrs []string yes List of Sentinel addresses (host:port)
redis.sentinel.username string no Sentinel-specific username (overrides redis.username if provided)
redis.sentinel.password string no Sentinel-specific password (overrides redis.password if provided)

Redis Cluster Configuration

Variable Type Required Default Description
redis.cluster.addrs []string yes List of Redis Cluster node addresses (host:port)
redis.cluster.username string no Cluster-specific username (overrides redis.username if provided)
redis.cluster.password string no Cluster-specific password (overrides redis.password if provided)
redis.cluster.routeByLatency bool no false Route commands to the node with lowest latency
redis.cluster.routeRandomly bool no false Route commands randomly to distribute load
redis.cluster.readOnly bool no false Allow read operations on replica nodes

Collection Key Mapping Configuration

Collection key mapping configuration is optional. The default mapper needs an entry for every collection it handles. With a custom mapper function only target is used, so you need entries only for collections you want routed to another Redis instance; the remaining fields are ignored.

Variable Type Required Default Description
redis.collectionKeyMapping[].collection string yes Couchbase collection name
redis.collectionKeyMapping[].keyPrefix string no Prefix to add to Redis keys
redis.collectionKeyMapping[].keySuffix string no Suffix to add to Redis keys
redis.collectionKeyMapping[].storageType string no string Storage type: "string", "hash", "json"
redis.collectionKeyMapping[].ttl time.Duration no TTL for this collection's keys
redis.collectionKeyMapping[].hashField string no value Field name when using hash storage type
redis.collectionKeyMapping[].target string no Name of a redis.instances entry; empty writes to the default Redis

Exposed metrics

Metric Name Description Labels Value Type
redis_connector_latency_ms Time to adding to the batch. N/A Gauge
redis_connector_bulk_request_process_latency_ms Time to process bulk request. N/A Gauge
redis_connector_bulk_request_size Number of commands in the last pipeline sent to Redis. N/A Gauge
redis_connector_retried_commands_total Total number of Redis commands re-sent after a transient error. N/A Counter

You can also use all DCP-related metrics explained here. All DCP-related metrics are automatically injected. It means you don't need to do anything.

Contributing

Go Dcp Redis is always open for direct contributions. For more information please check our Contribution Guideline document.

License

Released under the MIT License.

About

The Go implementation of the Couchbase to Redis with DCP.

Resources

Contributing

Stars

10 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages