# Type Conversion of Environment Variables in Redis Cache Configuration

**URL:** <https://discuss.rubyonrails.org/t/type-conversion-of-environment-variables-in-redis-cache-configuration/82923>\
**Category:** rubyonrails-core\
**Created:** [May 29, 2023, 12:14pm UTC](https://discuss.rubyonrails.org/t/type-conversion-of-environment-variables-in-redis-cache-configuration/82923 "2023-05-29T12:14:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![okabe-yuya](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/okabe-yuya/32/9762_2.png) [@okabe-yuya](https://discuss.rubyonrails.org/u/okabe-yuya)\
**Post date:** [May 29, 2023, 12:14pm UTC](https://discuss.rubyonrails.org/t/type-conversion-of-environment-variables-in-redis-cache-configuration/82923/1 "2023-05-29T12:14:49Z")

</div>

Hi all,  
In my project, I’m using Redis as the `cache_store` .  
However, I noticed a behavior that concerns me, and I would like to hear your opinions.

I’m using environment variables to specify the configuration values such as `pool_size` for the `cache_store`. However, I encountered the following error unless the values are converted to numeric types

```ruby
# config/environments/development.rb
config.cache_store = :redis_cache_store, {
  url: ENV.fetch('CACHE_REDIS_URL'),
  pool_size: ENV.fetch('CACHE_REDIS_POOL_SIZE', 5),
  :
}

```

```ruby
[1] pry(main)> Rails.cache.write("/foo", "bar")
/usr/local/bundle/gems/connection_pool-2.3.0/lib/connection_pool/timed_stack.rb:67:in `+': String can't be coerced into Float (TypeError)

```

> [https://github.com/mperham/connection\_pool/blob/main/lib/connection\_pool/timed\_stack.rb:67](https://github.com/mperham/connection_pool/blob/main/lib/connection_pool/timed_stack.rb:67)

It seems that the values retrieved from the environment variables remain as strings, which causes the above error. Of course, the values retrieved from environment variables are of string type. This issue can be resolved by using `to_i` .

```ruby
# config/environments/development.rb
config.cache_store = :redis_cache_store, {
  url: ENV.fetch('CACHE_REDIS_URL'),
  pool_size: ENV.fetch('CACHE_REDIS_POOL_SIZE', 5).to_i,
  :
}

```

However, I don’t think it’s appropriate that the error occurs only when writing to the cache if a string value is specified. Perhaps it would be better if an error occurred when a string value is provided.

How do you feel about it? We look forward to hearing your thoughts.  
Thanks for everyone’s efforts on Rails!

---

<div class="post-metadata">

**Author:** ![byroot](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/byroot/32/780_2.png) [@byroot](https://discuss.rubyonrails.org/u/byroot)\
**Post date:** [May 30, 2023, 7:12am UTC](https://discuss.rubyonrails.org/t/type-conversion-of-environment-variables-in-redis-cache-configuration/82923/2 "2023-05-30T07:12:33Z")

</div>

> [@okabe-yuya](#):
>
> How do you feel about it

Yes, it’s not great.

There are two competing concerns here though. The cache store ideally shouldn’t try to connect to the cache server during boot so that the app can boot even if the cache is down.

But yes, we should boot if the parameters are clearly invalid.

But I’m sure we can improve things significantly. Feel free to file this as an issue on GitHub.

---

<div class="post-metadata">

**Author:** ![saiqulhaq](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/saiqulhaq/32/9744_2.png) [@saiqulhaq](https://discuss.rubyonrails.org/u/saiqulhaq)\
**Post date:** [May 30, 2023, 1:08pm UTC](https://discuss.rubyonrails.org/t/type-conversion-of-environment-variables-in-redis-cache-configuration/82923/3 "2023-05-30T13:08:00Z")

</div>

What if we make it raise an error if the Rails env is development?

---

<div class="post-metadata">

**Author:** ![okabe-yuya](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/okabe-yuya/32/9762_2.png) [@okabe-yuya](https://discuss.rubyonrails.org/u/okabe-yuya)\
**Post date:** [May 30, 2023, 1:26pm UTC](https://discuss.rubyonrails.org/t/type-conversion-of-environment-variables-in-redis-cache-configuration/82923/4 "2023-05-30T13:26:25Z")

</div>

Thank you for your response.

I see, I wasn’t aware of those concerns.  
In that case, the approach of causing an error that prevents Rails from booting may not be ideal.  
We need to consider minimizing the impact on Rails startup, perhaps by simply outputting a warning log.

Thank you. I will create an issue on GitHub.

---

<div class="post-metadata">

**Author:** ![okabe-yuya](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/okabe-yuya/32/9762_2.png) [@okabe-yuya](https://discuss.rubyonrails.org/u/okabe-yuya)\
**Post date:** [May 30, 2023, 1:41pm UTC](https://discuss.rubyonrails.org/t/type-conversion-of-environment-variables-in-redis-cache-configuration/82923/5 "2023-05-30T13:41:38Z")

</div>

Thank you for your response.  
I see, I didn’t come up with that idea.

As a concern, Rails provides configuration files for each environment under /config/environments.  
It is conceivable to have a scenario where the cache store is not used in the development environment for cost-saving purposes, but only used in the staging and production environments.

In that case, I feel that there is a possibility of not being able to detect this issue for behaviors specific to certain environments.

---

<div class="post-metadata">

**Author:** ![saiqulhaq](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/saiqulhaq/32/9744_2.png) [@saiqulhaq](https://discuss.rubyonrails.org/u/saiqulhaq)\
**Post date:** [May 30, 2023, 3:30pm UTC](https://discuss.rubyonrails.org/t/type-conversion-of-environment-variables-in-redis-cache-configuration/82923/6 "2023-05-30T15:30:22Z")

</div>

Yea agreed, I think converting to an integer and adding a Rails log warning is enough when the value is a string. Currently my app uses Figaro gem to read the ENV variables and I got this warning [How to disable 'WARNING: Use strings for Figaro configuration'? · Issue #248 · laserlemon/figaro · GitHub](https://github.com/laserlemon/figaro/issues/248)

```ruby
WARNING: Use strings for Figaro configuration. false was converted to "false".
WARNING: Use strings for Figaro configuration. 120 was converted to "120".

```

---

<div class="post-metadata">

**Author:** ![okabe-yuya](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/okabe-yuya/32/9762_2.png) [@okabe-yuya](https://discuss.rubyonrails.org/u/okabe-yuya)\
**Post date:** [May 30, 2023, 3:42pm UTC](https://discuss.rubyonrails.org/t/type-conversion-of-environment-variables-in-redis-cache-configuration/82923/7 "2023-05-30T15:42:07Z")

</div>

Oh, it’s very helpful to have an existing implemented application as a reference.  
I once again realize that having warning logs like the implementation in Figaro would be desirable.  
Thank you for the wonderful information 😄 .

---

<div class="post-metadata">

**Author:** ![okabe-yuya](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/okabe-yuya/32/9762_2.png) [@okabe-yuya](https://discuss.rubyonrails.org/u/okabe-yuya)\
**Post date:** [May 31, 2023, 2:24pm UTC](https://discuss.rubyonrails.org/t/type-conversion-of-environment-variables-in-redis-cache-configuration/82923/8 "2023-05-31T14:24:51Z")

</div>

Hi all. I just created an issue on GitHub.  
This is my first time creating an issue on the Rails GitHub repository, so I’m feeling a bit nervous.

> <https://github.com/rails/rails/issues/48352>
>
> In my project, I am using Redis as a cache store.   
> I have configured it to re…trieve the settings from environment variables.   
> However, I have encountered the following issue.
> 
> I have come here through \[Rails Discussion\](https://discuss.rubyonrails.org/t/type-conversion-of-environment-variables-in-redis-cache-configuration/82923).
> 
> 
> \### Steps to reproduce
> 
> \`\`\`ruby
> \# config/environments/development.rb
> config.cache\_store = :redis\_cache\_store, {
> url: ENV.fetch('CACHE\_REDIS\_URL'),
> pool\_size: ENV.fetch('CACHE\_REDIS\_POOL\_SIZE', 5),
> :
> }
> \`\`\`
> 
> IRB
> 
> \`\`\`ruby
> \[1\] pry(main)\> Rails.cache.write("/foo", "bar")
> \`\`\`
> 
> \### Expected behavior
> 
> The string value is specified as it is, but an error occurs during cache writing.  
> Therefore, when I discovered this issue, I initially thought that an error should occur during the startup of Rails.   
> However, after discussions in the Rails Discussion, I have come to the conclusion that it should not impact the launch of Rails.
> 
> In Figaro, there is an implementation to output warning logs, and it would be desirable to have similar warning logs outputted in this case as well.
> 
> \`\`\`ruby
> \# When Rails starts
> WARNING: The cache\_store(:pool\_size) is specified as a string.
> \`\`\`
> 
> \> reference: https://github.com/laserlemon/figaro/issues/248
> 
> \### Actual behavior
> 
> \`\`\`ruby
> /usr/local/bundle/gems/connection\_pool-2.3.0/lib/connection\_pool/timed\_stack.rb:67:in \`+': String can't be coerced into Float (TypeError)
> \`\`\`
> 
> \> https://github.com/mperham/connection\_pool/blob/main/lib/connection\_pool/timed\_stack.rb:67
> 
> It seems that the values retrieved from the environment variables remain as strings, which causes the above error.   
> Of course, the values retrieved from environment variables are of string type.   
> This issue can be resolved by using to\_i .
> 
> \### System configuration
> \*\*Rails version\*\*: 7.0.5
> 
> \*\*Ruby version\*\*: 3.2.2
> 
> redis: 5.0.6

Thank you for your feedback !.
