Introducing Dragonfly Cloud! Learn More

Redis XTRIM in Java (Detailed Guide w/ Code Examples)

Use Case(s)

XTRIM is a command used in Redis for stream data structures to limit the number of elements or to restrict the size of the stream to a maximum number of elements. In Java, when interfacing with Redis using libraries like Jedis or Lettuce, you may use XTRIM to manage the memory footprint of your streams, especially in systems where streams are continuously written to and could grow indefinitely.

Code Examples

Example 1: Using Jedis to Trim a Stream by Maximum Length

Here's how you might use XTRIM in Java with the Jedis client:

import redis.clients.jedis.Jedis; public class RedisXTrimExample { public static void main(String[] args) { // Connecting to Redis server on localhost try (Jedis jedis = new Jedis("localhost", 6379)) { // Trimming the stream to retain the last 1000 entries long entriesRetained = jedis.xtrim("mystream", 1000, false); System.out.println("The number of entries deleted from the stream: " + entriesRetained); } } }

In this example, we connect to Redis and trim the stream named mystream to only keep the last 1000 entries. The third argument false means that we do not want to use the ~ approximation flag; we are requesting an exact trimming.

Example 2: Using Lettuce to Trim a Stream with Approximation

Lettuce is another popular Redis client for Java. Here’s an example using Lettuce:

import io.lettuce.core.RedisClient; import io.lettuce.core.api.StatefulRedisConnection; import io.lettuce.core.api.sync.RedisCommands; public class RedisXTrimExample { public static void main(String[] args) { // Connect to Redis server RedisClient redisClient = RedisClient.create("redis://localhost:6379"); try (StatefulRedisConnection<String, String> connection = redisClient.connect()) { RedisCommands<String, String> syncCommands = connection.sync(); // Using approximate trimming to delete older messages Long trimmed = syncCommands.xtrim("mystream", true, 1000); System.out.println("Number of entries deleted (approximately): " + trimmed); } redisClient.shutdown(); } }

With Lettuce, we create a RedisClient, obtain a synchronous command interface, and call xtrim with the true flag to use approximation. This can be more performance-efficient but less accurate.

Best Practices

  • Memory Management: Stream trimming should be part of your memory management strategy, especially if your streams are expected to grow without bounds.
  • Use Approximate Trimming: For large streams where performance is more critical than precision, consider using approximate trimming (true as the second parameter in Lettuce or passing ~ in the command with Jedis).

Common Mistakes

  • Ignoring Error Handling: Always ensure proper error handling around your Redis commands to gracefully handle connection issues or unexpected behavior from Redis.
  • Not Considering Stream Growth: Failing to implement a stream capping strategy can lead to unbounded growth of the Redis dataset, which may result in memory issues.


Q: What does the ~ flag mean in XTRIM? A: The ~ flag allows for approximate trimming. It speeds up the operation at the cost of precision and can delete slightly more or fewer elements than specified.

Was this content helpful?

Start building today 

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.