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
Sponsored
·
Ship Features Fearlessly
Turn features on and off without deploys. Used by thousands of Ruby developers.
→
Mike Perham
November 02, 2012
Technology
11
890
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
340
A Tribute to Ezra Zygmuntowicz
mperham
1
1.1k
Other Decks in Technology
See All in Technology
Claude Codeの進化と各機能の活かし方
oikon48
8
2.7k
Introduction to Sansan, inc / Sansan Global Development Center, Inc.
sansan33
PRO
0
3k
Oracle Database@AWS:サービス概要のご紹介
oracle4engineer
PRO
4
1.6k
プロジェクトマネジメントをチームに宿す -ゼロからはじめるチームプロジェクトマネジメントは活動1年未満のチームの教科書です- / 20260304 Shigeki Morizane
shift_evolve
PRO
1
110
生成AIの利用とセキュリティ /gen-ai-and-security
mizutani
1
1.2k
Contract One Engineering Unit 紹介資料
sansan33
PRO
0
14k
Introduction to Sansan for Engineers / エンジニア向け会社紹介
sansan33
PRO
6
72k
どこで打鍵するのが良い? IaCの実行基盤選定について
nrinetcom
PRO
2
170
問い合わせ自動化の技術的挑戦
recruitengineers
PRO
2
150
Introduction to Bill One Development Engineer
sansan33
PRO
0
380
オンプレとGoogle Cloudを安全に繋ぐための、セキュア通信の勘所
waiwai2111
3
1.1k
ヘルシーSRE
tk3fftk
2
240
Featured
See All Featured
Rails Girls Zürich Keynote
gr2m
96
14k
Agile Actions for Facilitating Distributed Teams - ADO2019
mkilby
0
140
Paper Plane (Part 1)
katiecoart
PRO
0
5.1k
Amusing Abliteration
ianozsvald
0
120
世界の人気アプリ100個を分析して見えたペイウォール設計の心得
akihiro_kokubo
PRO
67
37k
Bioeconomy Workshop: Dr. Julius Ecuru, Opportunities for a Bioeconomy in West Africa
akademiya2063
PRO
1
68
Applied NLP in the Age of Generative AI
inesmontani
PRO
4
2.1k
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.3k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
49
9.9k
More Than Pixels: Becoming A User Experience Designer
marktimemedia
3
340
Gemini Prompt Engineering: Practical Techniques for Tangible AI Outcomes
mfonobong
2
300
Agile that works and the tools we love
rasmusluckow
331
21k
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]