Redis®*: Getting Started
Quick guide to getting started with Redis
👋 Welcome to the Stackhero documentation!
Stackhero offers a ready-to-use Redis cloud solution that provides numerous benefits, including:
Redis Commanderweb UI included.- Unlimited message size and transfers.
- Simplified updates with just a click.
- Optimal performance and enhanced security powered by a private and dedicated VM.
Save time and simplify your life: it only takes 5 minutes to try Stackhero's Redis cloud hosting solution!
Redis is a powerful, incredibly fast in-memory database that serves multiple purposes. It can act as a caching system, a key-value store, facilitate real-time data sorting, and function as a publish-subscribe queue and event system.
Redis has changed its licence and is not open source anymore. The community, developers, and companies have created a Redis fork called Valkey which is a drop-in replacement for Redis. We recommend using Valkey instead of Redis.
To help you get started, we have shared some code examples demonstrating how to connect to a Redis instance. You can find these examples in the following GitHub repository: https://github.com/stackhero-io/redisGettingStarted.
Using Redis with Ruby and Ruby on Rails
Configure Redis as a cache system for Ruby on Rails
To begin, you might want to install the "redis" gem. You can do this by running:
bundle add redis
Next, open the config/environments/production.rb file and add the following line:
config.cache_store = :redis_cache_store, { url: ENV['REDIS_URL'] }
Finally, define the REDIS_URL environment variable. Here is a template you can use by replacing with your details:
REDIS_URL="rediss://default:<yourPassword>@<XXXXXX>.stackhero-network.com:<PORT_TLS>"
By default, caching is enabled only in the production environment. If you would like to test caching during development, you can edit the
config/environments/development.rbfile. Add the configuration above and includeconfig.action_controller.perform_caching = trueto enable caching. A simple way to verify that caching is working is to start a Rails console (withbin/rails console) and test by writingRails.cache.write("foo", "bar").
For more details about configuring Redis as a cache system for Ruby on Rails, check out the official Rails documentation.
Configure Redis for Sidekiq
Sidekiq will automatically utilise the Redis server specified in the REDIS_URL environment variable.
You can define the REDIS_URL environment variable with your specific information as follows:
REDIS_URL="rediss://default:<yourPassword>@<XXXXXX>.stackhero-network.com:<PORT_TLS>"
For more insights on using Sidekiq with Redis, refer to the official Sidekiq documentation.
Configure Redis for Resque
Resque, much like Sidekiq, will use the Redis server defined in the REDIS_URL environment variable.
Set the REDIS_URL with your details as follows:
REDIS_URL="rediss://default:<yourPassword>@<XXXXXX>.stackhero-network.com:<PORT_TLS>"
For further information on using Resque with Redis, please visit the official Resque documentation.
Handle PHP sessions with Redis
You can use the following code to store PHP sessions on Stackhero for Redis:
<?php
// Parse Redis URL
$redis_url = parse_url('rediss://default:<yourPassword>@<XXXXXX>.stackhero-network.com:<PORT_TLS>');
// Configure session handler
ini_set('session.save_handler', 'redis');
ini_set('session.save_path', "tls://{$redis_url['host']}:{$redis_url['port']}?auth={$redis_url['pass']}&timeout=5");
// Start the session
session_start();
?>
Enhancing Redis security
To ensure your Redis instance is secure, implementing certain security measures is crucial.
Encrypt communications with Redis (TLS)
By default, Redis does not encrypt communications. However, on Stackhero, we have incorporated TLS encryption by default.
To take advantage of this, configure your Redis client to use TLS encryption and connect to your instance through the <PORT_TLS> port. Avoid using <PORT_CLEAR>, as it does not encrypt data.
The best part is that there is nothing else you need to do on your end, we have taken care of the complex setup.
Protect Redis against brute force attacks
Your Redis instance is secured with a password, which Stackhero automatically sets to a highly secure default. If you choose to change it, ensure you select an exceedingly complex password.
Redis is highly efficient but lacks built-in brute force attack protections, allowing an attacker to potentially test up to 150,000 password combinations per second.
To counteract this risk, we enforce a minimum password length of 16 characters (yielding approximately 4.5231285e+74 combinations). By default, we use a 64-character password (offering about 9.61963e+111 combinations).
For added security and to significantly reduce the risk of brute force attacks, it is highly recommended to configure Stackhero's firewall settings (found under the "Firewall" tab) to limit connections strictly to your IP addresses. This step is essential for enhancing your security posture.