Advanced Programming Guide in Redis using Jedis

  •        0
  

We aggregate and tag open source projects. We have collections of more than one million projects. Check out the projects section.



Redis is an in-memory data structure store, used as a database, cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes with radius queries and streams. This blog covers the advanced concepts like cluster, publish and subscribe, pipeling concepts of Redis using Jedis Java library. If you are beginner then refer to How to install and setup Redis and Basic programming in Redis using Jedis.

 

Pipeling

Redis is a TCP server which works in request and respone model. When a continuous set of commands need to be executed, more time will be wasted in round trip of sending request and receiving the response. Scenarios like huge items to list, if it is sent through request and response model, round trip time loss will be more. This can avoided using pipelining, where we can batch the huge set of commands to the server, so server process the commands in batch and queue the responses and send it.

Jedis client creates pipeline where all the batch set of commands queued into it. Then sync will send the batch request of list of jobs from 1 to 10 with each have 5 members of taskid to be added to server. Sync will block the client till the server process the requests. Then the jedis client can be checked for the lists having the jobs with taskids.

  Pipeline pipeline = jedis.pipelined();
  for (int i=1; i<10; i++) {
     for (int j=1; j<5; j++) {
        pipeline.lpush("job" + i, "taskid" + j);
    }
  }
  pipeline.sync();
  for (int i=1; i<10; i++) {
       System.out.println(jedis.lrange("job"+i, 0, -1));
  }

Messaging Broker

Redis is a fast and stable Publish/Subscribe messaging system. Senders send the messages to a channel and subscribers who subscribed to the channel can receive the messages. It is non blocking way of sharing the messages between two parties.

Jedis client can be subscribed to the channel. Message handler need to be provided along with subscribe method. It passively listen the channel for message.

 final Jedis subJedis = jedisPool.getResource();
 subJedis.subscribe(new JedisPubSub() {
    @Override
    public void onMessage(String channel, String message) {
          System.out.printf("Received msg: %s from channel %s", message, channel);
    }
 }, "channel1");

New connection can be created again with jedis client to publish the message. It provides publish method with channel and message to publish in the channel.

 Jedis pubJedis = jedisPool.getResource();

 pubJedis.publish("channel1", "hello pubsub model");


Clustering Of Redis Nodes

Redis cluster is a distributed implementation of redis with linear scalability upto 1000 nodes and maximum availability of each master with atleast one reachable slave node. Cluster nodes are connected with TCP bus and a binary protocol called Redis Cluster Bus. All the data types supported in stand alone redis server is available in cluster also. It also hashtags where keys tagged to particular node. Learn more about how to install and setup Redis cluster

Redis server supports master-slave replication where master is for write and slave is for read. Master can have many slaves and it is non-blocking asynchronous replication. To scale up the writes, multiple masters are required with respective slaves for it. Peer-to-Peer kind of setup is not available in redis.
To know more about the replication.

Using Redis Cluster

To connect to redis cluster, either set of nodes in the cluster has to be created as Set<HostAndPort> or one master node. JedisCluster takes the cluster nodes set as constructor parameter and creates cluster client.

 Set<HostAndPort> jedisClusterNodes = new HashSet<HostAndPort>();
 jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7000));
 jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7001));
 jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7002));
 jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7003));
 jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7004));
 jedisClusterNodes.add(new HostAndPort("127.0.0.1", 7005));

 JedisCluster jedisCluster = new JedisCluster(jedisClusterNodes);
 /* will print all the cluster nodes */
System.out.println(jedisCluster.getClusterNodes());


Once cluster client created, it can directly used to store and retrieve data types as it manages all the redis nodes resources.

jedisCluster.set("blog-summary/advanced-redis", "Advanced programming in Redis using Jedis");

System.out.println(jedisCluster.get("blog-summary/advanced-redis"));

Likewise all the data types supported in redis is also available in redis cluster. Data types can be stored and retrieved with the same kind of methods which is available in JedisCluster.

Reference:

https://redis.io/

How to install and setup Redis

Basic programming in Redis using Jedis


Sponsored:
To find embedded technology information about MCU, IoT, AI etc Check out embedkari.com.


   

Nagappan is a techie-geek and a full-stack senior developer having 10+ years of experience in both front-end and back-end. He has experience on front-end web technologies like HTML, CSS, JAVASCRIPT, Angular and expert in Java and related frameworks like Spring, Struts, EJB and RESTEasy framework. He hold bachelors degree in computer science and he is very passionate in learning new technologies.

Subscribe to our newsletter.

We will send mail once in a week about latest updates on open source tools and technologies. subscribe our newsletter



Related Articles

Quick Start Programming Guide for redis using java client Jedis

  • redis jedis redis-client programming database java

Redis is an open source (BSD licensed), in-memory data structure store, used also as a database cache and message broker. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes with radius queries and streams. This article explains about how to communicate with Redis using Java client Jedis.

Read More


How to install and setup Redis

  • redis install setup redis-cluster

Redis is an open source (BSD licensed), in-memory data structure store, used also as a database cache and message broker. It is written in ANSI C and works in all the operating systems. It supports data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes with radius queries and streams. This article explains about how to install Redis.

Read More


RESTEasy Advanced guide - File Upload

  • resteasy rest-api file-upload java

RESTEasy is a JBoss project that provides various frameworks to help you build RESTful Web Services and RESTful Java applications. It is a fully certified and portable implementation of the JAX-RS 2.1 specification, a JCP specification that provides a Java API for RESTful Web Services over the HTTP protocol. It is licensed under the ASL 2.0.

Read More


ETag Easy With RESTEasy

  • resteasy etag http-header rest-api

RESTEasy is a JBoss project that provides various frameworks to help you build RESTful Web Services and RESTful Java applications. It comprises of frameworks for mock, embeddable server, rest client, proxy servers, logging and so on.In this article, we will walk-through ETag implementation and show the behaviour related to ETag done by rest easy framework. Example is developed using RESTEasy 3.7 and deployed in tomcat as RESTEasy framework is portable.

Read More


Introduction to Apache Cassandra

  • cassandra database nosql

Apache Cassandra was designed by Facebook and was open-sourced in July 2008. It is regarded as perfect choice when the users demand scalability and high availability without any impact towards performance. Apache Cassandra is highly scalable, high-performance distributed database designed to handle large voluminous amounts of data across many commodity servers with no failure.

Read More



RESTEasy Advanced Guide - Filters and Interceptors

  • resteasy rest-api filters interceptors java

RESTEasy is JAX-RS 2.1 compliant framework for developing rest applications. It is a JBoss project that provides various frameworks to help you build RESTful Web Services and RESTful Java applications. It is a fully certified and portable implementation of the JAX-RS 2.1 specification, a JCP specification that provides a Java API for RESTful Web Services over the HTTP protocol.

Read More


Best open source Text Editors

  • text-editor editor tools dev-tools

Text editors are mainly used by programmers and developers for manipulating plain text source code, editing configuration files or preparing documentation and even viewing error logs. Text editors is a piece of software which enables to create, modify and delete files that a programmer is using while creating website or mobile app.In this article, we will discuss about top 7 all-round performing text editors which is highly supportive for programmers.

Read More


Holistic usage guide for OpenSSL

  • openssl security certificate tools

OpenSSL is a general purpose cryptographty toolkit that provides an open source implementation of Transport Layer Security(TLS) and Secure Socket Layer(SSL) protocols. It is written in C,assembly and Perl language but wrappers are available in all languages. This article explains about OpenSSL commands.

Read More


Angular Service Workers Usage Guide

  • angular service-worker offline-app

Web developers come across scenarios like web application completely breaks when workstation goes offline. Likewise to get into our application, every time we need to open a browser and then access it. Instead if it is in app, it will be easy to access for end-user. Push notifications similar to email client need to be done through web application. All these are addressed by a magic called service worker.

Read More


Cache using Hazelcast InMemory Data Grid

  • hazelcast cache key-value

Hazelcast is an open source In-Memory Data Grid (IMDG). It provides elastically scalable distributed In-Memory computing, widely recognized as the fastest and most scalable approach to application performance.&nbsp;Hazelcast makes distributed computing simple by offering distributed implementations of many developer-friendly interfaces from Java such as Map, Queue, ExecutorService, Lock and JCache.

Read More


Exonum Blockchain Framework by the Bitfury Group

  • blockchain bitcoin hyperledger blockchain-framework

Exonum is an extensible open source blockchain framework for building private blockchains which offers outstanding performance, data security, as well as fault tolerance. The framework does not include any business logic, instead, you can develop and add the services that meet your specific needs. Exonum can be used to build various solutions from a document registry to a DevOps facilitation system.

Read More


AbanteCart - Easy to use open source e-commerce platform, helps selling online

  • e-commerce ecommerce cart

AbanteCart is a free, open source shopping cart that was built by developers with a passion for free and accessible software. Founded in 2010 (launched in 2011), the platform is coded in PHP and supports MySQL. AbanteCart’s easy to use admin and basic layout management tool make this open source solution both easy to use and customizable, depending on the skills of the user. AbanteCart is very user-friendly, it is entirely possible for a user with little to no coding experience to set up and use this cart. If the user would be limited to the themes and features available in base AbanteCart, there is a marketplace where third-party extensions or plugins come to the rescue.

Read More


LogicalDOC - Open Source DMS

  • dms document-management-system

LogicalDOC is both a document management and a collaboration system. The software is loaded with many functions and allows organizing, indexing, retrieving, controlling and distributing important business documents securely and safely for any organization and individual.

Read More


Free Open Source Code Search Engines

  • code-search code-search-engine search-engine

There are couple of sites which indexes the open source code and provides support to search code. Recently Google announced that they removed code search support from Google code. This article provides pointer for code search engine sites.

Read More


8 Best Open Source Searchengines built on top of Lucene

  • lucene solr searchengine elasticsearch

Lucene is most powerful and widely used Search engine. Here is the list of 7 search engines which is built on top of Lucene. You could imagine how powerful they are.

Read More


Whats new in Lucene / Solr 4.0

  • lucene solr new-release

The release 4.0 is one of the important milestone for Lucene and Solr. It has lot of new features and performance important. Few important ones are highliggted in this article.

Read More


Marketing stratigies required to sell open source product

  • opensource selling promote

Many new products are coming in the open source world. Few are forking existing project, adding new features to it and selling it as open source product. Few strategies required to follow to sell the product better.

Read More


Getting Started on Angular 7

  • angular ui-ux front-end-framework

Angular is a platform for building responsive web, native desktop and native mobile applications. Angular client applications are built using HTML, CSS and Typescript. Typescript is a typed superset of Javascript that compiles to plain Javascript. Angular core and optional modules are built using Typescript. Code has been licensed as MIT License.

Read More


PySpark: Installation, RDDs, MLib, Broadcast and Accumulator

  • pyspark spark python rdd big-data

We knew that Apace Spark- the most famous parallel computing model or processing the massive data set is written in Scala programming language. The Apace foundation offered a tool to support the Python in Spark which was named PySpark. The PySpark allows us to use RDDs in Python programming language through a library called Py4j. This article provides basic introduction about PySpark, RDD, MLib, Broadcase and Accumulator.

Read More


Activiti - Open Source Business Automation

  • business-automation business bpm

Activiti Cloud is the first Cloud Native BPM framework built to provide a scalable and transparent solution for BPM implementations in cloud environments. The BPM discipline was created to provide a better understanding of how organisations do their work and how this work can be improved in an iterative fashion.

Read More