Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Rails Async Process with Sidekiq
Search
wtnabe
August 29, 2015
Programming
660
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Rails Async Process with Sidekiq
『Railsで非同期処理 - Sidekiqを使うための準備と使ってみて気づいたこと』 via Kanazawa.rb meetup #36
wtnabe
August 29, 2015
More Decks by wtnabe
See All by wtnabe
新しいフロントプロセスとドキュメントフォーマットを考えてみた
wtnabe
0
55
Rubyでもモノリポしたい - 調査、おさわり編 -
wtnabe
0
89
Ruby de Railway Oriented Programming
wtnabe
0
140
Bindanのススメ
wtnabe
0
83
そのオブジェクト、何を保証してくれますか? - GuideRailのススメ -
wtnabe
0
88
Effective Jekyll
wtnabe
0
110
5 min Jekyll/Liquid Plugin cooking
wtnabe
0
78
Ruby de Wasm
wtnabe
0
110
Cloud Native Buildpacksって結局どうなの?
wtnabe
0
96
Other Decks in Programming
See All in Programming
S3 TablesのIcebergデータに、AIエージェントからアクセスしてみた / 20261016developersio2026joetsu-accessing-iceberg-in-s3tables-via-agent
kasacchiful
PRO
0
290
SODA - FACT BOOK(EN)
sodainc
1
220
Crash Auto Pipelines
veronikapj
1
400
クラウドゲーミングのパフォーマンス最適化に挑む / Cloud Gaming Performance Tuning
vivion
0
330
SFCで実現する病院・医療システムの動的組み立て式UI
koukimiura
1
170
動画プレイヤーはなぜカクつくのか 〜Flutterのレンダリングとネイティブの境界を解剖する〜
naruogram
0
330
3回つくってわかった、 定期実行ジョブ基盤の本質情報
takai
4
3.8k
Engineering Biometric UX Across Android Versions | DroidKnights 2026
ryunen344
1
190
[JA] Domain Modeling with Vanilla Rails: 「操作」から再考するRailsの設計論
yasaichi
4
1.8k
全ての永続トークンを、生まれる前に消し去りたい!
sylph01
3
1.7k
React Native経験者が Flutterを30⽇間触って驚いたこと
3saito
0
160
Distance field of Julia set
kinakomoti321
0
310
Featured
See All Featured
Java REST API Framework Comparison - PWX 2021
mraible
34
9.8k
Designing for Timeless Needs
cassininazir
1
550
Conquering PDFs: document understanding beyond plain text
inesmontani
PRO
4
3.3k
A better future with KSS
kneath
240
18k
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
330
Context Engineering - Making Every Token Count
addyosmani
9
1.3k
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
2
390
More Than Pixels: Becoming A User Experience Designer
marktimemedia
3
620
Evolving SEO for Evolving Search Engines
ryanjones
0
360
BBQ
matthewcrist
90
10k
From π to Pie charts
rasagy
1
440
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.6k
Transcript
Railsで⾮同期処理 Sidekiqを使うための準備と 使ってみて気づいたこと @wtnabe Kanazawa.rb meetup 36 2015-08-29 (Sat) at
IT-Plaza MUSASHI
お品書き ⾮同期処理とは Sidekiq ⾮同期Worker作成時の注意 課題はテスト
⾮同期処理とは
Railsで⾮同期処理とは HTTP requestと同期しないもの ジョブの実⾏指定と実際の実⾏時間が同 期しないもの 伝統的なバッチジョブは違う? クライアントサイドの⾮同期処理とは意味が違う
例えば POST時の処理を後回しに メールの送信 画像のサムネイル⽣成 response time短縮向け ⼤量の作業をスケジュール実⾏
⾮同期⽤のGem Rails 4.2+ ActiveJob ( adaptor ) Rails 4.1- (various
methods) DelayedJob BackgrounDrb Rescue Sidekiq
Sidekiq
Sidekiqの特徴 http://sidekiq.org/ Queue ⽤のストレージは Redis Celluloid を使ったスレッドモデル Sinatra 製の Dashboard
付き Sidekiq Pro アリ
今回選んだ理由 もともとよく名前を⽬にしていた redis 環境の⽤意が楽になっていた Sidekiq Pro があるってことはビジネスを 起こせるレベルのツールと看做せる Dashboard がレポートとしても使えそう
セットアップ bundle install Procfile initializers/sidekiq.rb config/sidekiq.yml config/routes.rb ( if you
wish Dashboard )
Redisのインストールは省略
bundle install in Gemfile gem 'sidekiq'
Foremanの話はこちら 前回 meetup #35 で紹介済み Lightweight PHP development environment with
Foreman and built-in server // Speaker Deck
Procfile Sidekiq⽤に別なRailsを⽴ち上げるイメージ web: bundle exec rails s redis: redis-server worker:
mkdir -p tmp/pids && \ bundle exec sidekiq \ -C config/sidekiq.yml Unicornと同じように全部bundle execで済んじゃう
Herokuでは初期化に注意 RailsのバージョンによってDATABASE接 続の設定反映の挙動が違う Herokuは環境変数設定を推奨している が、特にその辺りが違う see Advanced Options · mperham/sidekiq
Wiki
Concurrencyに注意 RailsのDB connection poolはdefault 5 Sidekiqのconcurrencyはdefault 25 溢れるに決まってる><
sidekiq.ymlの例 :concurrency: 3 :staging: :concurrency: 9 :production: :concurrency: 9 development
は SQLite, production と staging で web と worker 2つの サーバから max 20 conn までの DB サーバに繋ぐ想定
Dashboard in Gemfile gem 'sinatra' in routes.rb require 'sidekiq/web' mount
Sidekiq::Web => '/sidekiq' もちろんアクセス制限は忘れないで><
Sidekiq Dashboard Demo
Workerの作り⽅
in Gemfile gem 'sidekiq'
in application.rb app/workers ディレクトリを 作る autoload 対象に eager_load 対象に eager_load
については ggl `rails thread'
in foo_worker.rb class FooWorker include Sidekiq::Worker def perform(*args) end end
How to use FooWorker.perform_async(arg1, ...) FooWorker.perform_in(time, arg1, ...) etc
worker⽤のRails上で perform()が呼ばれる request を受けた Rails とは別な⼈であることに注意 perform_in() を使うと遅延時間が指定できる
Worker作成時の注意点
基本⽤語 ここでは以下のように⾔葉を⽤いる Worker 実際に⾮同期の動作を担当するclass ジョブ Workerが実⾏する⼀つ⼀つの処理
注意点 perform() にはプリミティブを与える できるだけ細かくジョブを分ける ※ Rubyにはプリミティブなんて存在しないけど
serializeしたオブジェクト じゃダメなの?
オブジェクト渡し 簡単だしDBに負担掛けない Queueの容量膨らむ 安定してdeserializeできるとは限らない 処理の記録の意味が分からない
処理の記録重要 ⾮同期処理はいつ終わるか分からない 「ポチッとやって結果を⽬視」みたいな やり⽅は通⽤しない
Sidekiqの記録 Workerの名前と引数がそのまま残る readableな名前と引数を使う
class BulkThumbnailGeneratorWorker include Sidekiq::Worker def perform(k, id, attr, size) r
= Object.const_get(k).find(id) ... end end 例えば⼀括サムネイル⽣成
こうすると どのModelの どのIDの どのカラムの画像を どのサイズにするのか これらが記録に残る。readableで丸分かり。
しかも ⼩分けにできる
ジョブの⼩分け重要 × 1万回のeachのある1つのジョブ ◦ 1万のジョブ ※ ⼩分けなら記録も明確で途中からretryできる
ジョブの分け⽅ ⼤量の作業が必要な場合 peform の中で each しない each の中で perform_in を呼ぶ
処理に掛かる時間を予測して遅延時間 を指定しておく もちろん必要な遅延時間はあらかじめ予測して検証する
課題はテスト
今のところ⾃動テストなし そんなに変更しない いつも⾛らせる必要はない
⼤きな課題は peform の呼び⽅呼ばれ⽅のコードというよりは 遅延時間やジョブの衝突などの⽅で、こ れは⾮常にテストしにくい
(機会があれば) 頑張ります><