DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Enterprise AI Trend Report: Gain insights on ethical AI, MLOps, generative AI, large language models, and much more.

2024 Cloud survey: Share your insights on microservices, containers, K8s, CI/CD, and DevOps (+ enter a $750 raffle!) for our Trend Reports.

PostgreSQL: Learn about the open-source RDBMS' advanced capabilities, core components, common commands and functions, and general DBA tasks.

AI Automation Essentials. Check out the latest Refcard on all things AI automation, including model training, data security, and more.

Related

  • Overview of Redisson: The Redis Java Client
  • Scaling Java Microservices to Extreme Performance Using NCache
  • Distributed Computing Simplified
  • Boosting Application Performance With MicroStream and Redis Integration

Trending

  • Organizing Knowledge With Knowledge Graphs: Industry Trends
  • Getting Started With NCache Java Edition (Using Docker)
  • Being a Backend Developer Today Feels Harder Than 20 Years Ago
  • Modern Digital Authentication Protocols
  1. DZone
  2. Software Design and Architecture
  3. Microservices
  4. Redis Cluster on Java for Scaling and High Availability

Redis Cluster on Java for Scaling and High Availability

Learn more about building a Redis cluster on Java — for scaling and high availability.

By 
Nikita Koksharov user avatar
Nikita Koksharov
·
Sep. 20, 19 · Tutorial
Like (8)
Save
Tweet
Share
42.5K Views

Join the DZone community and get the full member experience.

Join For Free

Redis cluster

What Is a Redis Cluster?

Scalability and availability are two of the most important qualities of any enterprise-class database.

It’s very unusual that you can exactly predict the maximum amount of resources your database will consume, so scalability is a must in order to deal with periods of unexpectedly high demand. Yet, scalability is useless without availability, which ensures that users will always be able to access the information within the database when they need it.

Redis is an in-memory data structure store that can be used to implement a non-relational key-value database. However, the bare-bones installation of Redis does not come equipped for maximum performance right off the bat.

In order to improve the scalability and availability of a Redis deployment, you can use Redis Cluster, a method of automatically sharding data across different Redis nodes. Redis Cluster breaks up a very large Redis database into smaller horizontal partitions known as shards that are stored on separate servers.

This makes the Redis database able to accommodate a larger number of requests, and therefore more scalable. What’s more, availability improves because the database can continue operations even when some of the nodes in the cluster have failed.

Before version 3.0, Redis Cluster used asynchronous replication. This meant that, in practice, if a master in Redis Cluster crashes before sending a write to all of its slaves, one of the slaves that did not receive the write can be promoted to master, which will cause the write to be lost.

Since version 3.0, Redis Cluster also has a synchronous replication option in the form of the WAIT command, which blocks the current client until all write commands are successfully completed. While this is not enough to guarantee strong consistency, it does make the data transfer process significantly more secure.

How to Run a Redis Cluster

There are two ways to get Redis Cluster up and running: the easy way and the hard way.

The easy way involves using the  create-cluster bash script, which you can find in the  utils/create-cluster directory in your Redis installation. The following two commands will create a default cluster with 6 nodes, 3 masters, and 3 slaves:

create-cluster start
create-cluster create


Once the cluster is created, you can interact with it. By default, the first node in the cluster starts at port 30001. Stop the cluster with the command:

create-cluster stop


The hard way of running Redis Cluster involves setting up your own configuration files for the cluster. All instances of Redis Cluster must contain at least three master nodes.

Below is an example configuration file for a single node:

port 7000
cluster-enabled yes
cluster-config-file nodes.conf
cluster-node-timeout 5000
appendonly yes


As its name suggests, the  cluster-enabled option enables the cluster mode. The  cluster-config-file option contains a path to the configuration file for the given node.

To create a test Redis Cluster instance with three master nodes and three slave nodes, execute the following commands in your terminal:

mkdir cluster-test
cd cluster-test
mkdir 7000 7001 7002 7003 7004 7005


Within each of these six directories, create a redis.conf configuration file using the example configuration file given above. Then, copy your redis-server executable into the cluster-test directory and use it to launch six different nodes in six different tabs of your terminal.

Connecting to Redis Cluster on Java

Like the base Redis installation, Redis Cluster is not able to work with the Java programming language out of the box. The good news is that there are frameworks that make it easy for you to use Redis Cluster and Java together.

Redisson is a Java client for Redis that includes many common constructs in Java, including a variety of objects, collections, locks, and services. Because Redisson reimplements these constructs in a distributed fashion, they can be shared across multiple applications and servers, allowing them to work with tools like Redis Cluster.

The following code demonstrates the usage of Redisson with Redis Cluster:

package redis.demo;
import org.redisson.Redisson;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;

/**
 * Redis Sentinel Java example
 *
 */
public class Application 
{
    public static void main( String[] args )
    {
        Config config = new Config();
        config.useClusterServers()
              .addNodeAddress("redis://127.0.0.1:6379", "redis://127.0.0.1:6380");

        RedissonClient redisson = Redisson.create(config);

        // operations with Redis based Lock

        // implements java.util.concurrent.locks.Lock
        RLock lock = redisson.getLock("simpleLock");
        lock.lock();

        try {
           // do some actions
        } finally {
           lock.unlock();
        }

        // operations with Redis based Map

        // implements java.util.concurrent.ConcurrentMap
        RMap<String, String> map = redisson.getMap("simpleMap");
        map.put("mapKey", "This is a map value");

        String mapValue = map.get("mapKey");
        System.out.println("stored map value: " + mapValue);

        redisson.shutdown();
    }
}


Redisson is an open-source client that enables Java programmers to work with Redis with minimal stress and complication, making the development process drastically easier and more familiar.

Further Reading

Quickstart: How to Use Redis on Java

Java Distributed Caching in Redis

Java (programming language) cluster Redis (company) Scaling (geometry)

Opinions expressed by DZone contributors are their own.

Related

  • Overview of Redisson: The Redis Java Client
  • Scaling Java Microservices to Extreme Performance Using NCache
  • Distributed Computing Simplified
  • Boosting Application Performance With MicroStream and Redis Integration

Partner Resources


Comments

ABOUT US

  • About DZone
  • Send feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: