Valkey: Using with Ruby

How to connect Valkey with Ruby

👋 Welcome to the Stackhero documentation!

Stackhero provides a ready-to-use Valkey cloud solution that delivers numerous advantages, including:

  • Redis Commander web interface included.
  • Unlimited message size and transfers.
  • One-click simplified updates.
  • Optimal performance and enhanced security thanks to a private, dedicated infrastructure.

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

Valkey is an open-source, in-memory database designed for outstanding performance and flexibility. Forked from Redis after it moved to a more restrictive license, Valkey provides a fully open-source alternative. It ensures full compatibility with Redis libraries, making it an excellent drop-in replacement for projects that previously used Redis.

Often described as a NoSQL database, Valkey supports a wide variety of data structures such as strings, hashes, lists, sets, and sorted sets, among others. Thanks to its ultra-fast operations and its ability to handle large volumes of data, Valkey is highly valued for use cases like caching, real-time analytics, messaging, and much more.

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

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

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

  2. Background job processing: Valkey can be used 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 allows Rails applications to offer real-time features such as notifications, chat systems, or real-time analytics by enabling fast and efficient message passing between application components.

Stackhero offers a ready-to-use Ruby cloud solution that provides numerous advantages, 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 simple, transparent, and predictable pricing.
  • Benefit from optimal performance and enhanced security thanks to a private, dedicated VM.

Save time and make your life easier: 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. Set 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 can refer to the official Rails documentation here.

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

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

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

By default, Sidekiq is configured without any tolerance for network disruptions. To improve this, we recommend updating the Sidekiq client configuration in config/initializers/sidekiq.rb to increase 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 raising an error
  }
end

This configuration ensures that if your Valkey server becomes unavailable, the client will attempt to reconnect for 2 minutes before raising an error. This gives the Valkey server time to restart, for example. Depending on how you use Sidekiq in your application, you can adjust this setting to better fit your specific needs.

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

To use Valkey with Resque, set the REDIS_URL environment variable 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.