blog.kotamiyake.me

為せば成る、為さねば成らぬ何事も

個人的に動かしているアプリでsidekiqを利用しているのですが、先日以下のようなエラーが発生しました。

Redis::CommandError: ERR Error running script (call to f_7b91ed9f4cba40689cea7172d1fd3e08b2efd8c9): @user_script:7: @user_script: 7: -MISCONF Redis is configured to save RDB snapshots, but is currently not able to persist on disk. Commands that may modify the data set are disabled. Please check Redis logs for details about the error.
  File "/home/deploy/apps/commerce/shared/bundle/ruby/2.5.0/gems/redis-4.0.1/lib/redis/client.rb", line 119, in call
# snip...

Googleで検索してみると、結論としてはどうやらメモリ不足だった模様。

Redisで発生したメモリ不足エラーの調査メモ | ソシャゲの作り方
http://www.24w.jp/blog/

RailsではSQLを発行する際に同じクエリがリクエストされるとSQLキャッシュが利用されます。

Rails のキャッシュ: 概要 | Rails ガイド
https://railsguides.jp/caching_with_rails.html#sql-%E3%82%AD%E3%83%A3%E3%83%83%E3%82%B7%E3%83%A5

キャッシュを利用すると高速化されますが、その分メモリを多く使用します。

今回はそれほどスピードを優先していないため、省メモリを優先してSQLキャッシュをオフにすることにしました。

def perform(shop, url)
  ActiveRecord::Base.connection.uncached do
    # code...
  end
end

ActiveJob Memory bloat · Issue #27002 · rails/rails
https://github.com/rails/rails/issues/27002#issuecomment-260086170

他にもやることはあるとは思いますが、一旦はこれで様子を見ることにします。

 

 

プロジェクトでredisを使う機会があったのですが、macでredis-serverを起動すると、そのディレクトリにdump.rdbが生成されて困っていました。

とりあえずディレクトリ設定はどうすればいいのか調べてみると、redis.confのdirパラメータに設定するようでした。

http://stackoverflow.com/questions/11737440/location-of-redis-temp-file-for-replication

私はhomebrewでredisをインストールしたので/usr/local/etc/redis.confに設定ファイルが設置されていました。

中身を確認してみると、ちゃんと別の場所に保存するように設定されていました。

# The working directory.
#
# The DB will be written inside this directory, with the filename specified
# above using the 'dbfilename' configuration directive.
#
# The Append Only File will also be created inside this directory.
#
# Note that you must specify a directory here, not a file name.
dir /usr/local/var/db/redis/

じゃあ実際の設定はどうなっているのか調べてみました。

$ redis-server
$ redis-cli
127.0.0.1:6379> config get dir
1) "dir"
2) "/Users/kotamiyake"

カレントディレクトリになっている。。。

さらに調べてみると、どうやらそもそもredis-serverの起動時に設定ファイルのパスを指定してあげる必要があったようです。

$ redis-server /usr/local/etc/redis.conf
$ ls /usr/local/var/db/redis/
dump.rdb

ちゃんと設定通りの場所にdump.rdbが生成されました。めでたしめでたし。