skip to Main Content

As we all know, the pipeline mechanism provides a good performance of the interaction between the redis client and server. Admittedly, it has been applied in many projects. My question is whether the pipeline way supports redis server with cluster mode or not?

2

Answers


  1. Yes you can, but it’s limited to keys in the same slot. See more here Using jedis how to write to a specific slot/node in redis cluster

    Login or Signup to reply.
  2. My original answer https://stackoverflow.com/a/72971327/5807561

    Copy below:

    Yes you can use pipeline in cluster mode as long as key hashes to same keyslot (not node).

    To achieve keys hashing to same slot you can use hash tags. TLDR – if you have curly brackets in your key {} hash function will only be applied to that part https://redis.io/docs/reference/cluster-spec/#hash-tags

    In my particular case I needed to put countries and cities by id into the redis which totaled 150k records. This is how I did it (nodejs + ioredis):

    if (countries.length && cities.length) {
      const pipelines = []
      for (let i = 0; i < 21; i++) {
        pipelines.push(redis.pipeline())
      }
      await Promise.all([
        Promise.map(countries, (country) => {
          const randomPipeline = pipelines[country.id % 21]
          randomPipeline.set(`country_${country.id}_{${country.id % 21}}`, JSON.stringify(country))
        }),
        Promise.map(cities, (city) => {
          const randomPipeline = pipelines[city.id % 21]
          randomPipeline.set(`city_${city.id}_{${city.id % 21}}`, JSON.stringify(city))
        })
        
    ])
      for (let i = 0; i < 21; i++) {
        await pipelines[i].exec()
      }
      console.log('Countries and cities in cache')
    }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search