Redis®*: Using with Ruby
How to connect Redis with Ruby
👋 Welcome to the Stackhero documentation!
Stackhero provides a ready-to-use Redis cloud solution offering numerous advantages, including:
- Redis Commander web interface included.
- Unlimited message size and transfers.
- Updates made easy with just one click.
- 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 offering 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 various 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:
- Caching: Redis can be used as a caching system in Rails applications to speed up response times and reduce the load on the database.
- Background job processing: Redis serves as a backend for popular background job processing libraries such as Sidekiq and Resque, enhancing the performance and reliability of background tasks in Rails applications.
- Real-time features: Redis's Pub/Sub functionality enables Rails applications to offer real-time features such as notifications, chat systems, or real-time analytics by facilitating fast and efficient message exchange between different components of the application.
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.
- Benefit from 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.
Configuring Redis as a cache system for Ruby on Rails
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 details:
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.
Configuring Redis with Sidekiq
Sidekiq will automatically use the Redis server defined in the REDIS_URL environment variable.
You can set the REDIS_URL environment variable as follows, replacing <password> and <XXXXXX> with your details:
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 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) # Attempt 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 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 suit your specific needs.
For more information about Sidekiq and Redis, you can refer to the official documentation here.
Configuring Redis with Resque
Resque will automatically use the Redis server defined in the REDIS_URL environment variable.
You can set the REDIS_URL environment variable as follows, replacing <yourPassword> and <XXXXXX> with your details:
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.