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

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Boosting Application Performance With MicroStream and Redis Integration
  • Redis-Based Tomcat Session Management
  • Multi-Tenancy Implementation Using Spring Boot, MongoDB, and Redis
  • Learning Redis Basic Data Structure and Mapping With Java Data Structure

Trending

  • Is Agile Right for Every Project? When To Use It and When To Avoid It
  • The 4 R’s of Pipeline Reliability: Designing Data Systems That Last
  • AI's Dilemma: When to Retrain and When to Unlearn?
  • Comprehensive Guide to Property-Based Testing in Go: Principles and Implementation
  1. DZone
  2. Coding
  3. Java
  4. How to Connect to Redis on Java Over SSL

How to Connect to Redis on Java Over SSL

This article explains how to connect to redis on Java over SSL.

By 
Nikita Koksharov user avatar
Nikita Koksharov
·
Updated Mar. 04, 19 · Tutorial
Likes (7)
Comment
Save
Tweet
Share
33.0K Views

Join the DZone community and get the full member experience.

Join For Free

Redis, an open-source, in-memory data structure store, is one of the most popular choices for building NoSQL databases. However, one major stumbling block for using Redis is that it does not come with any of its own encryption features.

Of course, any enterprise-class database must be able to guarantee the security of the information stored within it. If your database may be accessible by untrusted parties, you will need to develop your own encryption capabilities, enveloping data within an encryption protocol.

SSL (Secure Sockets Layer) is the protocol of choice for transmitting data between different servers or computers. Being able to securely connect to a server using SSL/TLS is an essential skill for any database developer.

Although SSL support for Redis may come in a future update, the feature is not currently on the table. According to the developers of Redis: "The idea of adding SSL support to Redis was proposed many times. However, currently, we believe that given the small percentage of users requiring SSL support, and the fact that each scenario tends to be different, using a different "tunneling" strategy can be better."

This means that developers will need to figure out their own way to get SSL to work with Redis. However, navigating the ins and outs of the SSL/TLS protocol can be challenging for developers, especially those without significant experience in security. You might have to follow a series of long, frustrating instructions and try to debug mysterious errors, wasting valuable development time.

Fortunately, there's a better solution, especially for those who are already programming in Java. Redisson is a Redis client for the Java programming language. It includes implementations of dozens of the most important classes and interfaces in Java for a distributed computing paradigm so that you can continue to use this functionality with Redis.

Thanks to Redisson, it's significantly easier to design, develop, and deploy large distributed systems using Redis.Image title

Let's look at just how easy it is to connect to Redis over SSL with Redisson. An example for using Redisson with SSL and Java is below:

import java.util.concurrent.ConcurrentMap;
import org.redisson.Redisson;
import org.redisson.api.RMap;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;

public class MapExample {

    public static void main(String[] args) {
        Config config = new Config();
        // rediss - defines to use SSL for Redis connection
        config.useSingleServer().setAddress("rediss://127.0.0.1:6379");
        RedissonClient redisson = Redisson.create(config);

        RMap<String, String> map = redisson.getMap("test");
        map.put("mykey", "myvalue");
        String value =  map.get("mykey");

        // ...

        redisson.shutdown();
    }

}


As you can see, Redisson makes it simple and straightforward to connect to Redis on Java over SSL. We use the "rediss" provisional protocol to designate a connection to Redis via SSL/TLS. We then instantiate a RedissonClient object, which represents a Redisson object with a synchronous/asynchronous interface.

Once created, the RedissonClient object can begin working with data immediately. In the example above, we can see the use of RMap object, which is a distributed implementation of the ConcurrentMap interface in Java. The RedissonClient object is able to securely store the RMap's data within Redis using SSL, avoiding the need for lengthy, time-consuming configurations.

Configuring Redis to Accept SSL Connections With Stunnel

Stunnel is an open-source proxy application for wrapping client-server connections with SSL/TSL encryption. Because Redis does not include native support for SSL encryption, many developers use stunnel for secure client-server communications.

The good news is that Redisson already supports SSL on the client side, which means that there's no need for you to install stunnel on the client.

However, you'll still need to install stunnel on the Redis server in order to have encryption on both ends. To do so, follow the steps below on a system running Ubuntu.

First, use the apt-get tool to install stunnel by running the commands:

sudo apt-get update
sudo apt-get install stunnel4


In order to enable stunnel to launch at startup, you must edit the /etc/default/stunnel4 file:

sudo nano /etc/default/stunnel4


Modify the ENABLED parameter so that it reads, and then save and close the file.

Create SSL Certificate in the /etc/stunnel directory by follow command:

sudo openssl -nodes -days 3650 req -x509 -newkey rsa:2048 -keyout /etc/stunnel/redis.key -out /etc/stunnel/redis.crt


Once you get certificate and key, you can create a stunnel config file for Redis located at /etc/stunnel/redis.conf. Inside, you need to define the location to PID file and configuration for Redis server:

pid = /run/stunnel-redis.pid
[redis-server]
accept = REDIS_SERVER_EXTERNAL_IP:6379
connect = 127.0.0.1:6379
cert = /etc/stunnel/redis.crt
key = /etc/stunnel/redis.key


About Redisson

When you use Redisson, getting Redis to work with Java and SSL is just a matter of a few lines of code.

Enabling SSL connections is just one way that Redisson makes it easier for Java developers to work with Redis. From data structures and collections to locks and synchronizers, Redisson comes equipped with a variety of distributed objects that enable developers to use Java on top of Redis.

To learn more about using Redisson, visit the project page on GitHub.

Redis (company) Java (programming language)

Opinions expressed by DZone contributors are their own.

Related

  • Boosting Application Performance With MicroStream and Redis Integration
  • Redis-Based Tomcat Session Management
  • Multi-Tenancy Implementation Using Spring Boot, MongoDB, and Redis
  • Learning Redis Basic Data Structure and Mapping With Java Data Structure

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and 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: