Examples of Redis Pipelining

Let’s consider a few practical examples where Redis Pipelining can be beneficial:

1. E-commerce Inventory Management

In an e-commerce system, managing product inventory efficiently is crucial. Redis Pipelining can be used to update product quantities, retrieve stock status, and log inventory changes:

Python3




# Initialize the pipeline
pipe = r.pipeline()
 
# Update product quantity
pipe.decrby('product:1:quantity', 3)
pipe.get('product:1:quantity')
 
# Log the inventory change
pipe.rpush('inventory:log', 'Product 1 quantity reduced by 3')
 
# Execute the pipeline
responses = pipe.execute()
 
new_quantity = int(responses[1])
 
print(f"New Quantity: {new_quantity}")


Javascript




const Redis = require('ioredis');
const redis = new Redis();
 
(async () => {
  // Initialize the pipeline
  const pipeline = redis.pipeline();
 
  // Update product quantity
  pipeline.decrby('product:1:quantity', 3);
  pipeline.get('product:1:quantity');
 
  // Log the inventory change
  pipeline.rpush('inventory:log', 'Product 1 quantity reduced by 3');
 
  // Execute the pipeline
  const responses = await pipeline.exec();
 
  // Retrieve responses
  const newQuantityResponse = responses[1][1];
 
  // Parse the response as an integer
  const newQuantity = parseInt(newQuantityResponse, 10);
 
  // Print the result
  console.log(`New Quantity: ${newQuantity}`);
 
  // Close the Redis connection
  redis.quit();
})();


Output:

New Quantity: <updated quantity>
The output will be the updated quantity of product 1 after decrementing it by 3.

2. Real-time Leaderboards:

Leaderboards are common in gaming and competitive applications. Redis is often used for leaderboard implementations. Pipelining can help fetch and update leaderboard scores efficiently:

Python3




# Initialize the pipeline
pipe = r.pipeline()
 
# Add or update player scores
pipe.zadd('leaderboard', {'player1': 1500, 'player2': 1800, 'player3': 1600})
 
# Get the top 10 players
pipe.zrevrange('leaderboard', 0, 9, withscores=True)
 
# Execute the pipeline
responses = pipe.execute()
 
top_players = [(player.decode('utf-8'), score)
               for player, score in responses[1]]
 
print("Top Players:")
for player, score in top_players:
    print(f"{player}: {score}")


Javascript




const Redis = require('ioredis');
const redis = new Redis();
 
(async () => {
  // Initialize the pipeline
  const pipeline = redis.pipeline();
 
  // Add or update player scores
  pipeline.zadd('leaderboard', { 'player1': 1500, 'player2': 1800, 'player3': 1600 });
 
  // Get the top 10 players with scores
  pipeline.zrevrange('leaderboard', 0, 9, 'WITHSCORES');
 
  // Execute the pipeline
  const responses = await pipeline.exec();
 
  // Retrieve the response for zrevrange
  const topPlayersResponse = responses[1][1];
 
  // Parse the response and format it as an array of player-score pairs
  const topPlayers = [];
  for (let i = 0; i < topPlayersResponse.length; i += 2) {
    const player = topPlayersResponse[i];
    const score = parseInt(topPlayersResponse[i + 1], 10);
    topPlayers.push({ player, score });
  }
 
  // Print the top players
  console.log('Top Players:');
  topPlayers.forEach(({ player, score }) => {
    console.log(`${player}: ${score}`);
  });
 
  // Close the Redis connection
  redis.quit();
})();


Output:

Top Players:
player2: 1800.0
player3: 1600.0
player1: 1500.0
The output will be the top 10 players and their scores in descending order.

3. Rate Limiting for APIs

Rate limiting is essential for protecting APIs from abuse. Redis Pipelining can be used to enforce rate limits efficiently:

Python3




# Initialize the pipeline
pipe = r.pipeline()
 
# Check if the user has exceeded the rate limit
pipe.zscore('ratelimit:user123', 'timestamp')
 
# Increment the rate limit counter and set an expiration
pipe.multi()
pipe.zadd('ratelimit:user123', {'timestamp': time.time()})
pipe.expire('ratelimit:user123', 60)
pipe.execute()
 
# Execute the pipeline
responses = pipe.execute()
 
timestamp = responses[0]
 
if timestamp:
    remaining_time = 60 - (time.time() - float(timestamp))
    print(f'Rate limit exceeded. Try again in {remaining_time:.2f} seconds.')
else:
    print('Request allowed.')


Javascript




const Redis = require('ioredis');
const redis = new Redis();
 
(async () => {
  // Initialize the pipeline
  const pipeline = redis.pipeline();
 
  // Check if the user has exceeded the rate limit
  pipeline.zscore('ratelimit:user123', 'timestamp');
 
  // Increment the rate limit counter and set an expiration
  pipeline.multi();
  pipeline.zadd('ratelimit:user123', { 'timestamp': Math.floor(Date.now() / 1000) });
  pipeline.expire('ratelimit:user123', 60);
 
  // Execute the pipeline
  await pipeline.exec();
 
  // Execute another pipeline to get the timestamp
  const timestampResponse = await redis.zscore('ratelimit:user123', 'timestamp');
 
  // Parse the timestamp response
  const timestamp = parseFloat(timestampResponse);
 
  if (timestamp) {
    const currentTime = Math.floor(Date.now() / 1000);
    const remainingTime = 60 - (currentTime - timestamp);
    console.log(`Rate limit exceeded. Try again in ${remainingTime.toFixed(2)} seconds.`);
  } else {
    console.log('Request allowed.');
  }
 
  // Close the Redis connection
  redis.quit();
})();


Output:

If the user has exceeded the rate limit: Rate limit exceeded. Try again in <remaining_time> seconds.
If the user is within the rate limit: Request allowed.

Note: The output will depend on whether the user has exceeded the rate limit or not, and if exceeded, it will display the remaining time until the rate limit resets.

Complete Guide to Redis Pipelining

Redis is unbeatable when it comes to storing and retrieving data. It is open source and functions as an in-memory data structure store that provides performance, low latency data to different applications. Among others, one feature to effectuate this is Redis Pipelining – so stated by Redis. Here we analyze the concept of Redis pipelining

Important Topics for Redis Pipelining

  • What is Redis Pipelining?
  • Request/Response Protocols and Round Trip Time (RTT)
  • Commands and Syntax of Redis Pipelining
  • Examples of Redis Pipelining
  • Pros of Redis Pipelining
  • Cons of Redis Pipelining
  • Conclusion

Similar Reads

What is Redis Pipelining?

...

Request/Response Protocols and Round Trip Time (RTT)

Redis Pipelining is an optimization that helps execute multiple commands in Redis sequentially. Typically for a Redis interaction, a client would first issue a command to the server and then send another when processing of its command is done. The synchronous nature of this approach incurs a considerable delay, especially involving numerous commands....

Commands and Syntax of Redis Pipelining

Before diving deeper into Redis Pipelining, it’s essential to understand the concept of Request/Response protocols and Round Trip Time (RTT). In networking, when a client sends a request to a server, it must wait for the server’s response....

Examples of Redis Pipelining

With Redis pipelining, it is as easy as firing off a sequence of commands in succession without waiting for responses. Here’s a basic outline of how it works:...

Pros of Redis Pipelining

...

Cons of Redis Pipelining

...

Conclusion

...

Contact Us