Learnitweb

Using Redisson Reactive Lists as Queue and Stack

Redisson provides reactive implementations of Redis data structures. While RListReactive behaves like a standard list, we can use additional methods to make it behave like a queue (FIFO) or stack (LIFO).

This tutorial explains:

  • Converting a reactive list into a queue.
  • Converting a reactive list into a stack.
  • Using repeat to perform multiple operations.
  • Observing the behavior with remaining items in the list.

1. Setting Up the List

Assume we already have a reactive list RListReactive<Long> with elements 1–10:

RListReactive<Long> reactiveList = redisson.getList("numberList");
  • The list contains numbers: 1, 2, 3, …, 10.
  • No changes are required for the initial list setup.

2. Making the List Behave Like a Queue (FIFO)

A queue removes elements from the beginning (first-in, first-out). In Redis/Redisson, we can simulate a queue using pollFirst():

Flux<Long> queueBehavior = Flux.range(1, 4)  // Repeat operation 4 times
    .flatMap(i -> reactiveList.remove(0));   // Remove the first element each time

queueBehavior.doOnNext(System.out::println)
    .then()
    .block();

Explanation:

  • remove(0) simulates poll() from a queue by removing the first element.
  • Flux.range(1, 4) repeats this operation 4 times, removing four elements.
  • After execution, six elements remain in the list: 5, 6, 7, 8, 9, 10.
  • Elements removed are printed sequentially: 1, 2, 3, 4.

Verification:

StepVerifier.create(reactiveList.size())
    .expectNext(6L)
    .verifyComplete();

3. Making the List Behave Like a Stack (LIFO)

A stack removes elements from the end (last-in, first-out). Instead of using Java’s old Stack class, we use a deque in Redisson:

RDequeReactive<Long> deque = redisson.getDeque("numberList");
  • pollLast() removes elements from the end, simulating a stack.

Example: Pop 4 elements from the end

Flux<Long> stackBehavior = Flux.range(1, 4)
    .flatMap(i -> deque.pollLast());  // Pop the last element each time

stackBehavior.doOnNext(System.out::println)
    .then()
    .block();

Explanation:

  • pollLast() retrieves and removes the last element.
  • Repeating 4 times will remove the last four elements.
  • For the original list 1–10, the removed elements are: 10, 9, 8, 7.
  • Remaining elements: 1, 2, 3, 4, 5, 6.

Verification:

StepVerifier.create(deque.size())
    .expectNext(6L)
    .verifyComplete();

4. Key Points

  1. Queue Behavior (FIFO):
    • Remove elements from the start using remove(0) or pollFirst().
    • Remaining elements stay in the list in their original order.
  2. Stack Behavior (LIFO):
    • Remove elements from the end using pollLast().
    • Elements are retrieved in reverse order (last-in, first-out).
  3. Repeating Operations:
    • Use Flux.range with flatMap to repeat the same removal multiple times.
    • Useful to simulate batch processing or repeated consumption.
  4. Efficiency Considerations:
    • Use RDequeReactive for stack/queue behavior instead of Java Stack class.
    • Deque supports both pollFirst() and pollLast() efficiently.
  5. Remaining Items:
    • After a series of queue or stack operations, verify the list/deque size using .size().
    • Remaining items reflect the unprocessed elements.

5. Summary

  • Redisson reactive lists can be adapted to act like queues or stacks.
  • pollFirst() or remove(0) → queue (FIFO).
  • pollLast() → stack (LIFO).
  • Use Flux and flatMap to repeat operations reactively.
  • For performance and clarity, use RDequeReactive instead of Java Stack.