Skip to content

Zeroplex 生活隨筆

軟體開發、伺服器和生活瑣事

小 縮小字型大小。 中 重設字型大小。 大 放大字型大小。

標籤: Redis

清除 redis 中所有含有 prefix 的 keys

Posted on 2024 年 2 月 4 日2024 年 2 月 4 日 By 日落 在〈清除 redis 中所有含有 prefix 的 keys〉中尚無留言

這個需要透過 redis-cli 工具來協助。

列出所有的 keys:

redis-cli KEYS '*'

列出含有特定 prefix 的 keys

redis-cli KEYS 'zeroplex_*'

透過 xargs 將 key name 轉送給 redis-cli DEL

redis-cli KEYS 'zeroplex_*' | xargs redis-cli DEL
Tags:Redis

Laravel Horizon 僅支援 queue in Redis

Posted on 2023 年 4 月 2 日2023 年 4 月 2 日 By 日落 在〈Laravel Horizon 僅支援 queue in Redis〉中尚無留言

透過 Laravel Horizon 可以快速監看 queued jobs 的運作狀況,不過要注意的是僅支援資料放在 Redis 中的 queue:

Laravel Horizon requires that you use Redis to power your queue. Therefore, you should ensure that your queue connection is set to redis in your application’s config/queue.php configuration file.

所以要先檢查幾個項目:

  • 記得 composer reuqire predis/predis
  • 在 .env 中的 QUEUE_CONNECTION 數值必須是 redis
  • config/queue.php 中的 default 建議直接改成 redis
Tags:Laravel, PHP, Redis

將整台機器的記憶體給 Redis 使用時該調整的設定

Posted on 2022 年 2 月 16 日2022 年 8 月 30 日 By 日落 在〈將整台機器的記憶體給 Redis 使用時該調整的設定〉中有 4 則留言

塞個資料,沒想到 Redis 一直炸,看 log 才發現有設定要調整:

356416:C 15 Feb 2022 14:53:43.009 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
356416:C 15 Feb 2022 14:53:43.009 # Redis version=6.2.6, bits=64, commit=00000000, modified=0, pid=356416, just started
356416:C 15 Feb 2022 14:53:43.009 # Configuration loaded
356416:M 15 Feb 2022 14:53:43.010 * monotonic clock: POSIX clock_gettime
356416:M 15 Feb 2022 14:53:43.012 * Running mode=standalone, port=6379.
356416:M 15 Feb 2022 14:53:43.012 # Server initialized
356416:M 15 Feb 2022 14:53:43.012 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
356416:M 15 Feb 2022 14:53:43.017 * Loading RDB produced by version 6.2.6
356416:M 15 Feb 2022 14:53:43.017 * RDB age 1475 seconds
356416:M 15 Feb 2022 14:53:43.017 * RDB memory usage when created 5243.48 Mb
356416:signal-handler (1644908113) Received shutdown signal during loading, exiting now.
356427:C 15 Feb 2022 14:55:14.014 * Supervised by systemd. Please make sure you set appropriate values for TimeoutStartSec and TimeoutStopSec in your service unit.

注意到錯誤訊息中間有個關鍵字 vm.overcommit_memory,如果遇到 sysctl 或 vm 相關的,大多是與 Linux 系統參數有關係,這邊有 overcommit_memory 的設定說明,該設定會保護系統不會接受大型記憶體 allocate 的請求。

但 Redis 本來就是使用記憶體做服務,因此需要去 /etc/sysctl.conf 新增下面這段設定:

vm.overcommit_memory = 1

重新開機後即可套用設定。


另外 Redis 會在機器啟動時,從硬碟將 persistent data 載入記憶體中,若資料不少,載入會花掉相當的時間,因此 systemd 預設的設定,可能會誤以為 Redis 啟動失敗:

redis-server.service: start operation timed out. Terminating.

依據資料多寡,會影響 Redis 啟動的時間,所以需要調整 systemd 的 timeout。

開啟 /etc/systemd/system/redis.service 注意下面幾個 timeout 相關的參數:

TimeoutStartSec=300
TimeoutStopSec=300

參考 systemd 的文件,TimeoutStartSec=300 也可以改成可讀格式 TimeoutStartSec="5min"。


2022-02-18 更新:

感謝 JoeHorn 提醒,直接修改 /etc/systemd/ 底下的設定檔是有風險的,建議透過 systemctl edit xxx.service 的方式 override 預設參數值。

Tags:Linux, Redis

其他

關於我  (About me)

小額贊助

  文章 RSS Feed

  留言 RSS Feed

Apache AWS Bash C/C++ Docker FreeBSD GCP Git Google Java JavaScript Laravel Linux Microsoft MSSQL MySQL Nginx PHP PHPUnit PostgreSQL Python Qt Ubuntu Unix Vim Web Windows WordPress XD 作業系統 分享 好站推薦 專題 攝影 新奇搞笑 新聞 旅遊 生活雜記 程式設計 網路架站 網頁設計 資訊學習 資訊安全 遊戲 音樂


創用 CC 授權條款
本著作係採用創用 CC 姓名標示-相同方式分享 4.0 國際 授權條款授權.