Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Asynchronous Processing for Fun and Profit
Search
Mike Perham
November 02, 2012
Technology
11
870
Asynchronous Processing for Fun and Profit
Some pro tips and an overview of Sidekiq for great async victory!
Mike Perham
November 02, 2012
Tweet
Share
More Decks by Mike Perham
See All by Mike Perham
Computer Science vs Software Engineering
mperham
0
330
A Tribute to Ezra Zygmuntowicz
mperham
1
990
Other Decks in Technology
See All in Technology
カップ麺の待ち時間(3分)でわかるPartyRockアップデート
ryutakondo
0
130
20250116_JAWS_Osaka
takuyay0ne
2
200
AWS re:Invent 2024 recap in 20min / JAWSUG 千葉 2025.1.14
shimy
1
100
SpiderPlus & Co. エンジニア向け会社紹介資料
spiderplus_cb
0
870
dbtを中心にして組織のアジリティとガバナンスのトレードオンを考えてみた
gappy50
0
130
三菱電機で社内コミュニティを立ち上げた話
kurebayashi
1
350
re:Invent 2024のふりかえり
beli68
0
110
Alignment and Autonomy in Cybozu - 300人の開発組織でアラインメントと自律性を両立させるアジャイルな組織運営 / RSGT2025
ama_ch
1
2.3k
Copilotの力を実感!3ヶ月間の生成AI研修の試行錯誤&成功事例をご紹介。果たして得たものとは・・?
ktc_shiori
0
340
CDKのコードレビューを楽にするパッケージcdk-mentorを作ってみた/cdk-mentor
tomoki10
0
200
Oracle Base Database Service 技術詳細
oracle4engineer
PRO
6
54k
AWS re:Invent 2024 re:Cap Taipei (for Developer): New Launches that facilitate Developer Workflow and Continuous Innovation
dwchiang
0
160
Featured
See All Featured
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
19
2.3k
The World Runs on Bad Software
bkeepers
PRO
66
11k
Building Your Own Lightsaber
phodgson
104
6.2k
The Cost Of JavaScript in 2023
addyosmani
46
7.2k
It's Worth the Effort
3n
183
28k
A designer walks into a library…
pauljervisheath
205
24k
How STYLIGHT went responsive
nonsquared
96
5.3k
Fireside Chat
paigeccino
34
3.1k
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
45
2.3k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Designing on Purpose - Digital PM Summit 2013
jponch
116
7.1k
Producing Creativity
orderedlist
PRO
343
39k
Transcript
Async Processing for Fun and Profit Mike Perham @mperham
Me Director of Engineering, TheClymb.com
Agenda • Basics • Protips • Sidekiq
Why? • User-Perceived Performance • I/O is SLOOOOOOOOOOOW • I/O
is unreliable
What? • Optional work • Anything not required to build
the HTTP response
How? • Client puts message on a queue • Server
pulls message off queue • Worker executes code based on message
How? async do # perform some work end
How? • Marshalling a Proc • Need to serialize closure
How? async(instance, :method, args)
How? • Marshal instance • Marshal args
How? async(Class, :method, args)
How? • Serialize just the class name • Marshal the
arguments
Congratulations! This is exactly how Resque and Sidekiq work.
How?
How?
How?
Tip #1 Small, Stateless Messages
Stateless • Database holds objects (nouns) • Queue holds actions
(verbs) • “Perform X on Object 123”
Avoid State • Bad • @user.delay.sync_images • Good • User.delay.sync_images(@user.id)
Simple Types • Small & Easy to read • Cross-platform
• Sidekiq / Resque use JSON
Debugging
Tip #2 Idempotent, Transactional Units of Work
Idempotent • Fancy computer science term • “Work can be
applied multiple times without changing the result beyond the initial application”
Idempotent • Canceling an order • Updating user’s email address
Not Idempotent! • Charging credit card • Sending email
Idempotent • Your code has bugs! • Sidekiq will retry
jobs that raise • Design jobs to be retried • e.g. verify you need to perform action before performing it
Transactional • Infinite credit?
Tip #3 Embrace Concurrency
Concurrency • Resque/DJ = 4 workers • Sidekiq = 100
workers • Mike, what is best in life?
“To crush their servers, see them smoking before you and
hear the lamentations of their admins.”
Concurrency • Use connection_pool gem to limit client connections •
Split work into small batches • 100 items => 10 jobs of 10 items
Concurrency • Thread safety rarely an issue • Most gem
maintainers very responsive • Recently fixed: • cocaine, typheuos
Theory, meet Practice
Sidekiq • Simple, efficient message processing • Like Resque, but
10x faster
MODERN RUBY IS NOT SLOW SINGLE THREADING IS SLOW
Concurrency • To scale single-threaded, create lots of processes. •
HORRIBLY RAM INEFFICIENT
Threads vs Processes • Example: 400MB single-threaded process • 25
processes = 10GB RAM = EC2 xlarge • 25 threads = 1GB RAM = EC2 small • $60/month vs $480/month • 160 dynos => 10 dynos • 150 dynos * $35/month =~ $5000/month
Quick Rant • GIL + poor GC • C extension
API must DIE • What’s larger: 50% or 800%?
Client Your App Client Middleware Redis Sidekiq Client API Rails
Process
Server Processor Server Middleware Redis Worker Sidekiq Process Processor Server
Middleware Worker Processor Server Middleware Worker Processor Server Middleware Worker Fetcher Manager
Versions • Sidekiq - Free, LGPLv3 • Sidekiq Pro -
more features, support, $ • motivation!
Features Sidekiq Resque DJ Concurrency Store Hooks Web UI Scheduler
Retry Delay Batches Threads Processes Processes Redis Redis DB middleware callbacks callbacks ✓ ✓ ? ✓ ? ✓ ✓ ? ✓ ✓ ? ✓ Pro ? - ? - optional
Future • Nicer, more functional Web UI • APIs for
managing queues / retries • Rails 4 Queue API
Pro Future • Enterprise-y features • Workflow • Notifications
Conclusion • small, stateless messages • idempotent / transactional •
concurrency • sidekiq
Questions? @mperham
[email protected]