Valkey: Getting started

Quick guide to getting started with Valkey

👋 Welcome to the Stackhero documentation!

Stackhero offers a ready-to-use Valkey cloud solution that provides a host of benefits, including:

  • Redis Commander web UI included.
  • Unlimited message size and transfers.
  • Effortless updates with just a click.
  • Optimal performance and robust security powered by a private and dedicated VM.

Save time and simplify your life: it only takes 5 minutes to try Stackhero's Valkey cloud hosting solution!

Valkey is a powerful and incredibly fast in-memory database, based on Redis. As Redis transitioned away from the open-source model, Valkey emerged from the community and several companies as a robust open-source alternative. It seamlessly integrates with Redis libraries, making it an ideal drop-in replacement.

Valkey is versatile, serving multiple purposes: it can function as a caching system, a key/value database, facilitate real-time data sorting, or act as a publish/subscribe queue like an event system.

To assist you in connecting to a Valkey instance, we have prepared some code examples. Feel free to explore them in the following Git repository: https://github.com/stackhero-io/valkeyGettingStarted.

To get started, you can install the redis gem with this command:

bundle add redis

Next, open the config/environments/production.rb file and include this line:

config.cache_store = :redis_cache_store, { url: ENV["VALKEY_URL"] }

You will then need to set the VALKEY_URL environment variable. Here is an example of how you can format the URL with your information:

VALKEY_URL="rediss://default:<yourPassword>@<XXXXXX>.stackhero-network.com:<PORT_TLS>"

By default, caching is only enabled in the production environment. If you wish to test caching in development, modify the config/environments/development.rb file by adding the above configuration, and set config.action_controller.perform_caching = true to enable caching. A good way to verify that caching works is to start a Rails console (using bin/rails console) and test writing with Rails.cache.write("foo", "bar").

For more detailed information about configuring Valkey as a cache system for Ruby on Rails, you can consult the official Rails documentation here.

Sidekiq will automatically use the Valkey server configured in the REDIS_URL environment variable.

You can set the REDIS_URL environment variable with your information as follows:

REDIS_URL="rediss://default:<yourPassword>@<XXXXXX>.stackhero-network.com:<PORT_TLS>"

For more insights into using Sidekiq with Valkey, you can consult the official documentation here.

Resque will automatically use the Valkey server defined in the REDIS_URL environment variable.

You can set the REDIS_URL environment variable with your information as follows:

REDIS_URL="rediss://default:<yourPassword>@<XXXXXX>.stackhero-network.com:<PORT_TLS>"

For additional information on using Resque with Valkey, you can consult the official documentation here.

You can use the following code to store PHP sessions on Stackhero for Valkey:

<?php

// Parse Valkey URL
$valkey_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://{$valkey_url['host']}:{$valkey_url['port']}?auth={$valkey_url['pass']}&timeout=5");

// Start the session
session_start();

?>

Enhancing the security of your Valkey setup is crucial. Here are two important measures you can implement to maximize security.

By default, Valkey does not encrypt communications. However, on Stackhero, TLS encryption is provided out of the box.

To leverage this feature, simply configure your Valkey client to use TLS encryption and connect to your instance using the port <PORT_TLS>, instead of the non-encrypted <PORT_CLEAR>.

The great news is that you do not need to worry about additional setup, we have prepared everything for you.

Your Valkey instance is safeguarded with a strong, automatically predefined password by Stackhero. If you decide to change it, ensure the password is extremely complex.

Valkey is swift and, unfortunately, lacks built-in brute force attack protection. An attacker could potentially attempt up to 150,000 password combinations per second.

To mitigate this, passwords must be at least 16 characters long (4.5231285e+74 possibilities), and by default, we set a 64-character password (9.61963e+111 possibilities!).

For added security and to effectively thwart brute force attempts, it is advisable to configure Stackhero's firewall (accessible in the "Firewall" tab) to restrict connections to only your IP addresses. This is a critical step that will significantly bolster your security!