Skip to content

Pipelining

opuneet edited this page Oct 22, 2014 · 3 revisions

USE WITH CAUTION

  • Since Dyno is token aware and sends all your commands to just one Dynomite server depending on the token hash - please ensure that all commands used within one pipeline have the same key
  • Also every thread must use it's own pipeline. Multiple threads can use the same Dyno client in a thread safe manner, but must NOT share pipelines among each other.

Pipelining example using multiple threads

int numThreads = 5; 
		
final ExecutorService threadPool = Executors.newFixedThreadPool(numThreads);
final AtomicBoolean stop = new AtomicBoolean(false);
final CountDownLatch latch = new CountDownLatch(numThreads);
		
for (int i=0; i<numThreads; i++) {
	threadPool.submit(new Callable<Void>() {

		final Random rand = new Random();
		@Override
		public Void call() throws Exception {
					
			final AtomicInteger iter = new AtomicInteger(0);
					
			while (!stop.get()) {
						
			  int index = rand.nextInt(5);
			  int i = iter.incrementAndGet();

                          // NOTE THAT EACH THREADS CREATES IT'S OWN PIPELINE
			  DynoJedisPipeline pipeline = client.pipelined();

			  Response<Long> resultA1 = pipeline.hset("Puneet_pipeline" + index, "a1", "v" + i);
			  Response<Long> resultA2 = pipeline.hset("Puneet_pipeline" + index, "a2", "v" + i);

			  pipeline.sync();
			  System.out.println(resultA1.get() + " "  + resultA2.get());
		}
		latch.countDown();
		return null;
	}
});
		
Thread.sleep(5000);
stop.set(true);
latch.await();
		
threadPool.shutdownNow();