Valkey: Using with Ruby

How to connect Valkey with Ruby

👋 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 an open-source, in-memory database designed for exceptional performance and flexibility. Forked from Redis after it adopted a more restrictive licence, Valkey offers a fully open-source alternative. It provides seamless compatibility with Redis libraries, making it an excellent drop-in replacement for projects that previously relied on Redis.

Often referred to as a NoSQL database, Valkey supports a wide range of data structures such as strings, hashes, lists, sets, and sorted sets, among others. With its high-speed operations and ability to handle large volumes of data, Valkey has become a popular choice for use cases like caching, real-time analytics, messaging, and more.

Valkey integrates effortlessly with Ruby and Ruby on Rails, enhancing performance, scalability, and flexibility. Ruby developers can leverage Valkey to implement features like caching, session management, real-time data processing, and background job handling, among others.

Here are some common ways Valkey is utilised with Ruby and Ruby on Rails:

  1. Caching: Valkey can serve as a caching layer in Rails applications, accelerating response times and reducing database load.

  2. Background job processing: Valkey can operate as a backend for popular background job processing libraries like Sidekiq and Resque, improving the performance and reliability of background tasks in Rails applications.

  3. Real-time features: Valkey's Pub/Sub functionality enables Rails applications to provide real-time features such as notifications, chat systems, and real-time analytics by facilitating fast and efficient message passing between application components.

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

  • Deploy your application in seconds with a simple git push.
  • Use your own domain name and benefit from the automatic configuration of HTTPS certificates for enhanced security.
  • Enjoy peace of mind with automatic backups, one-click updates, and straightforward, transparent, and predictable pricing.
  • Get optimal performance and robust security thanks to a private and dedicated VM.

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

To configure Valkey as a cache system in Ruby on Rails, follow these steps:

  1. Install the redis gem:
bundle add redis
  1. Edit the config/environments/production.rb file and add the following configuration:
config.cache_store = :redis_cache_store, { url: ENV['VALKEY_URL'] }
  1. Define the VALKEY_URL environment variable. Use a URL in the following format, replacing <yourPassword> and <XXXXXX> with your credentials:
VALKEY_URL="rediss://default:<yourPassword>@<XXXXXX>.stackhero-network.com:<PORT_TLS>"

For more details about configuring Valkey as a cache system for Ruby on Rails, you might want to consult the official Rails documentation here.

Sidekiq will automatically use the Valkey server defined by the environment variable REDIS_URL.

You can set the environment variable REDIS_URL like this, just replacing <password> and <XXXXXX> with your information:

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

By default, Sidekiq is configured in a manner that does not accommodate any tolerance during network disturbances. To improve this, we suggest modifying the Sidekiq client configuration in config/initializers/sidekiq.rb to enhance stability:

# File config/initializers/sidekiq.rb
Sidekiq.configure_client do |config|
  config.redis = {
    network_timeout: 5, # Set the timeout to 5 seconds
    pool_timeout: 5, # Set the timeout to 5 seconds
    reconnect_attempts: Array.new(240, 0.5) # Try to reconnect 240 times, every 0.5 second (120 seconds/2 minutes in total) before triggering an error
  }
end

This configuration ensures that if your Valkey server becomes unavailable, the client will attempt retries for a duration of 2 minutes before reporting an error. This gives the Valkey server time to restart, for example. Depending on your Sidekiq's usage, you may adjust this setting to better suit your specific requirements.

For more information about Sidekiq and Valkey, you can consult the official documentation here.

To use Valkey with Resque, set the environment variable REDIS_URL as follows, replacing <yourPassword> and <XXXXXX> with your credentials:

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

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

For more information about using Resque with Valkey, you can refer to the Resque documentation here.