Redis®*: Using with Ruby

How to connect Redis with Ruby

👋 Welcome to the Stackhero documentation!

Stackhero provides a ready-to-use Redis 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 Redis cloud hosting solution!

Redis, which stands for Remote Dictionary Server, is an open-source, in-memory database that delivers outstanding performance and flexibility.

Often described as a NoSQL database, Redis 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, Redis has become a preferred choice for caching, real-time analytics, messaging, and many other use cases.

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

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

  1. Caching: Redis can be used as a cache store in Rails applications to speed up response times and reduce database load.
  2. Background job processing: Redis serves as a backend for popular background job processing libraries like Sidekiq and Resque, which enhances the performance and reliability of background tasks in Rails applications.
  3. Real-time features: Redis's Pub/Sub functionality allows Rails applications to provide real-time features such as notifications, chat systems, or real-time analytics by enabling fast and efficient message exchange between different application components.

Stackhero offers a ready-to-use Ruby cloud solution that provides a range 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 simple, transparent, and predictable pricing.
  • Get 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 Ruby cloud hosting solution.

To get started, you can install the "redis" gem:

bundle add redis

Next, edit the config/environments/production.rb file and add this line:

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

Finally, set the REDIS_URL environment variable. Use this URL, simply replacing <yourPassword> and <XXXXXX> with your information:

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

For more information about configuring Redis as a cache system for Ruby on Rails, you can refer to the official Rails documentation here.

Sidekiq will automatically use the Redis server defined in the REDIS_URL environment variable.

You can set the REDIS_URL environment variable like this, 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 way that does not provide 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 triggering an error
  }
end

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

For more information about Sidekiq and Redis, you can refer to the official documentation here.

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

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

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

For more information about Resque and Redis, you can refer to the official documentation here.