Auto pipelining allows you to use the Redis client as usual
while in the background it tries to send requests in batches
whenever possible.In a nutshell, the client will accumulate commands in a pipeline
and wait for a short amount of time for more commands to arrive.
When there are no more commands, it will execute them as a batch.To enable the feature, simply pass enableAutoPipelining: true
when creating the Redis client:
This is especially useful in cases when we want to make async
requests or when we want to make requests in batches.
Copy
Ask AI
import { Redis } from "@upstash/redis";const redis = Redis.fromEnv({ latencyLogging: false, enableAutoPipelining: true});// async call to redis. Not executed right away, instead// added to the pipelineredis.hincrby("Brooklyn", "visited", 1);// making requests in batchesconst brooklynInfo = Promise.all([ redis.hget("Brooklyn", "coordinates"), redis.hget("Brooklyn", "population")]);// when we call await, the three commands are executed// as a pipeline automatically. A single PIPELINE command// is executed instead of three requests and the results// are returned:const [ coordinates, population ] = await brooklynInfo;
For auto pipeline to work, the client keeps an active pipeline
and adds incoming commands to this pipeline. After the command
is added to the pipeline, execution of the pipeline is delayed
by releasing the control of the Node thread.The pipeline executes when one of these two conditions are met:
No more commands are being added or at least one of the commands
added is being ‘awaited’.This means that if you are awaiting every time you run a command,
you won’t benefit much from auto pipelining since each await will
trigger a pipeline:
Copy
Ask AI
const foo = await redis.get("foo") // makes a PIPELINE callconst bar = await redis.get("bar") // makes another PIPELINE call
In these cases, we suggest using Promise.all:
Copy
Ask AI
// makes a single PIPELINE call:const [ foo, bar ] = await Promise.all([ redis.get("foo"), redis.get("bar")])
In addition to resulting in a single PIPELINE call, the commands
in Promise.all are executed in the order they are written!