Slide 1

Slide 1 text

@nateberkopec for Kaigi on Rails 2022 JA text on slides by @cafedomancer Queues in Rails Apps Sidekiq, Puma, and the GVL Rails ΞϓϦ έʔγϣϯʹ ͓͚ΔΩϡʔ

Slide 2

Slide 2 text

Hi Nate, Our application is really slow, but our dashboards look OK. We don't really understand what's going on. Can you help us? Nate ༷ ͍ͭ΋͓ੈ࿩ʹͳ͓ͬͯΓ·͢ɻ ฐࣾͰӡ༻͍ͯ͠ΔΞϓϦέʔ γϣϯʹύϑΥʔϚϯε্ͷ໰୊ ͕͋Γɺ͓٬༷͔Βෆຬͷ੠͕د ͤΒΕ͍ͯ·͢ɻμογϡϘʔυ ΛݟΔݶΓͰ͸ɺಛʹ໰୊͸ݟ౰ ͨΓ·ͤΜͰͨ͠ɻฐࣾͷํͰ ͸ɺͲͷΑ͏ͳ໰୊͕ى͖͍ͯΔ ͷ͔Λ೺ѲͰ͖͓ͯΓ·ͤΜɻ͓ ๩͍͠ͱ͜ΖେมڪॖͳͷͰ͢

Slide 3

Slide 3 text

Queues Ωϡʔ

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

Nate Berkopec

Slide 6

Slide 6 text

Agenda Three kinds of queues • Sidekiq • Puma/Unicorn • GVL 3 ͭͷछྨͷΩϡʔ ΞδΣϯμ

Slide 7

Slide 7 text

Queue Ωϡʔ “Servers” αʔόʔ Work ࡞ۀ

Slide 8

Slide 8 text

Queues increase availability Ωϡʔ͸Մ༻ੑΛ޲্ͤ͞Δ

Slide 9

Slide 9 text

Christoph Roser/allaboutlean.com

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Queues can be organized in a “queueing system”. Ωϡʔ͸ʮΩϡʔΠϯάγεςϜʯʹ Αͬͯߏ੒͞ΕΔ

Slide 12

Slide 12 text

Total Time = Queue Time + Service Time ߹ܭ࣌ؒ = Ωϡʔ࣌ؒ + αʔϏε࣌ؒ

Slide 13

Slide 13 text

Total Time = Queue Time + Service Time ߹ܭ࣌ؒ = Ωϡʔ࣌ؒ + αʔϏε࣌ؒ

Slide 14

Slide 14 text

Sidekiq

Slide 15

Slide 15 text

Queues (Redis) Process types, with threads ϓϩηεʮλΠϓʯͱεϨου Ωϡʔ

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

Total Time = Queue Time + Service Time ߹ܭ࣌ؒ = Ωϡʔ࣌ؒ + αʔϏε࣌ؒ

Slide 18

Slide 18 text

Important Point / ॏཁͳϙΠϯτ Add queue time instrumentation for Sidekiq Ωϡʔ࣌ؒͷܭଌπʔϧΛ Sidekiq ʹಋೖ͠Α͏

Slide 19

Slide 19 text

Queues (Redis) Process types, with threads ϓϩηεʮλΠϓʯͱεϨου Ωϡʔ

Slide 20

Slide 20 text

Domain Driven Queues •invoices •mailers •payments υϝΠϯۦಈΩϡʔ •੥ٻॻ •ϝʔϧ •ࢧ෷͍

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

Every Job Class has an “SLA” ͢΂ͯͷδϣϒΫϥεʹ͸ SLA ͕͋Δ

Slide 24

Slide 24 text

within_30_seconds within_5_minutes within_1_hour within_1_week

Slide 25

Slide 25 text

“Shards” w/ Consistent Hashing ίϯγεςϯτ ϋογϡ๏ʹΑΔ γϟʔυ

Slide 26

Slide 26 text

Predicted Latency = Current rate of job processing x # of Jobs in Queue ༧૝͞ΕΔϨΠςϯγʔ = ݱࡏͷδϣϒॲཧ཰ x Ωϡʔͷδϣϒ਺

Slide 27

Slide 27 text

https://tinyurl.com/kellysidekiq

Slide 28

Slide 28 text

TODO 1. Arrange queues based on SLAs 2. Create alerts based on queue SLAs 1. SLA ʹج͍ͮ ͯΩϡʔΛ 
 ഑ஔ͠Α͏ 2. SLA ʹج͍ͮ ͯΞϥʔτΛ 
 ࡞੒͠Α͏

Slide 29

Slide 29 text

Web

Slide 30

Slide 30 text

Total Time = Queue Time + Service Time ߹ܭ࣌ؒ = Ωϡʔ࣌ؒ + αʔϏε࣌ؒ

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

Header: X-Request-Start

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

More queue time? 1 worker 4 workers Ωϡʔ࣌ؒͳ͕௕ ͍ͷ͸ͲΕ͔ʁ

Slide 37

Slide 37 text

4 pods 1 worker 1 pod 4 workers More queue time? Ωϡʔ࣌ؒͳ͕௕ ͍ͷ͸ͲΕ͔ʁ

Slide 38

Slide 38 text

Queue time = 1/s

Slide 39

Slide 39 text

1 master process per container ίϯςφ͋ͨΓ 1 Ϛελʔϓϩηε

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

Always use at least 4 child processes/workers ࠷௿Ͱ΋ 4 ͭͷϓϩηε/ϫʔΧʔΛ࢖༻͠Α͏

Slide 42

Slide 42 text

TODO 1. Measure web request queueing 2. Set up autoscaling based on this 3. Use at least 4 child processes 1. Web ϦΫΤετ ͷΩϡʔΠϯά Λܭଌ͠Α͏ 2. ্هʹج͍ͮͯ ΦʔτεέʔϦ ϯάΛ 
 ઃఆ͠Α͏ 3. গͳ͘ͱ΋ 4 ͭ ͷϓϩηεΛ 
 ࢖༻͠Α͏

Slide 43

Slide 43 text

Global VM Lock (GVL)

Slide 44

Slide 44 text

Great Valuable Lock Ractor VM Lock

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

1.Exit 2.I/O 3.Interrupt (100ms)

Slide 47

Slide 47 text

No content

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

100 Threads?

Slide 50

Slide 50 text

# of Threads Service Time

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

tinyurl.com/gvlruby

Slide 53

Slide 53 text

wait_for_less_busy_worker tinyurl.com/pumasleep

Slide 54

Slide 54 text

Worker 1: 0/5 Threads Busy Worker 2: 1/5 Threads Busy Worker 3: 4/5 Threads Busy

Slide 55

Slide 55 text

TODO 1. Set thread count to 5 (web) or 10 (Sidekiq) 2. Monitor service time under load 3. Upgrade to Puma 6 1. εϨου਺Λ 5 (web) ·ͨ͸ 10 (Sidekiq) ʹ 
 ઃఆ͠Α͏ 2. ෛՙ࣌ͷαʔϏε࣌ ؒΛ 
 ϞχλϦϯά͠Α͏ 3. Puma 6 ʹόʔδϣϯ 
 Ξοϓ͠Α͏

Slide 56

Slide 56 text

TODO 1. Arrange queues based on SLAs 2. Create alerts based on queue SLAs 3. Measure web request queueing 4. Set up autoscaling based on this 5. Use at least 4 child processes 6. Set thread count to 5 (web) or 10 (Sidekiq) 7. Monitor service time under load 8. Upgrade to Puma 6 1. SLA ʹج͍ͮͯΩϡʔΛ 
 ഑ஔ͠Α͏ 2. SLA ʹج͍ͮͯΞϥʔτΛ 
 ࡞੒͠Α͏ 3. Web ϦΫΤετͷΩϡʔΠϯ άΛܭଌ͠Α͏ 4. ্هʹج͍ͮͯΦʔτεέʔ ϦϯάΛઃఆ͠Α͏ 5. গͳ͘ͱ΋ 4 ͭͷϓϩηεΛ ࢖༻͠Α͏ 6. εϨου਺Λ 5 (web) ·ͨ͸ 10 (Sidekiq) ʹઃఆ͠Α͏ 7. ෛՙ࣌ͷαʔϏε࣌ؒΛϞχ λϦϯά͠Α͏ 8. Puma 6 ʹόʔδϣϯΞοϓ͠ Α͏

Slide 57

Slide 57 text

@nateberkopec for Kaigi on Rails 2022 JA text on slides by @cafedomancer Queues in Rails Apps Sidekiq, Puma, and the GVL Rails ΞϓϦ έʔγϣϯʹ ͓͚ΔΩϡʔ