4

I am looking at the possibility of triggering the redis command from the client as a normal api and the library can pipeline the commands into it and possibly reply asynchronously back. Any library for the Java would be highly appreciated.

Any pointers to opensource work on the same lines would also be of the great help.

titan
  • 435
  • 1
  • 4
  • 7
  • Doesn't sound like a good idea, your clinets threads will hang while waiting for your wrapper to send and process the pipeline. What is your usecase? – Ofir Luzon Jan 20 '15 at 12:23
  • So the wrapper should push out the commands if a certain threshold of commands reached the wrapper, or a max timeout may be order of 3ms. Idea is to optimize the number of commands sent to the data store, which is kind of vertical scaling – titan Jan 29 '15 at 08:55

2 Answers2

2

Both popular Java Redis clients, Jedis and lettuce provide async/pipelining. See here for an example. You get a Future after issuing commands so you can sync on your own or work with future callbacks.

mp911de
  • 17,546
  • 2
  • 55
  • 95
2

Redisson provides asynchronous pipeline in more convenient way. Here is code example:

RBatch batch = redisson.createBatch();
RFuture<String> mapFuture = batch.getMap("test").putAsync("2", "5");
RFuture<Long> longFuture = batch.getAtomicLongAsync("counter").incrementAndGetAsync();

List<?> res = batch.execute();
// or 
RFuture<List<?>> asyncRes = batch.executeAsync();

Asynchronous methods returns RFuture object which implements both CompletionStage and Future interfaces.

Further, you can get result

mapFuture.get() result or asyncRes.get()[0]

Binding listeners:

future.whenComplete((res, exception) -> {

  // handle both result and exception

});

// or

future.thenAccept(res -> {

  // handle result

}).exceptionally(exception -> {

  // handle exception

});
Nikita Koksharov
  • 10,283
  • 1
  • 62
  • 71