SQS を使った非同期処理の導入
by
Keigo Ibaraki
Link
Embed
Share
Beginning
This slide
Copy link URL
Copy link URL
Copy iframe embed code
Copy iframe embed code
Copy javascript embed code
Copy javascript embed code
Share
Tweet
Share
Tweet
Slide 1
Slide 1 text
SQS Λͬͨඇಉظॲཧͷಋೖ AWS cafeteria #1 גࣜձࣾαΠόʔΤʔδΣϯτ AI ࣄۀຊ෦ Ἒ ܒޞ
Slide 2
Slide 2 text
Ἒܒޞ ɹ @oyasumi_pants 2022৽ଔೖࣾ CyberAgentʛAIࣄۀຊ෦ Go, GoogleCloud, AWS, Terraform ٸ͕ճΕ 🏢 💻 🗣 2
Slide 3
Slide 3 text
ຊͷ༰ 1. ීஈͲΜͳ͜ͱΛ͍ͯ͠Δ͔ʁ 2. ඇಉظॲཧͷಋೖͱʁ 3. SQS ͱʁ 4. ࣮લޙͷΞʔΩςΫνϟ 5. SQS બఆ࣌ʹߟྀͨ͠ 6. SQS બఆޙʹߟྀͨ͠ 7. ෛՙࢼݧͷ 8. ࠓޙͷల 3
Slide 4
Slide 4 text
1. ීஈͲΜͳ͜ͱΛ͍ͯ͠Δͷ͔ 4
Slide 5
Slide 5 text
ීஈͲΜͳ͜ͱΛ͍ͯ͠Δͷ͔ 5 ● ࠂ৴ϓϩμΫτΛ࡞͍ͬͯ·͢ʂ
Slide 6
Slide 6 text
ࠂ৴ϓϩμΫτͷ֓ཁ
Slide 7
Slide 7 text
ࠂ৴ϓϩμΫτͷ֓ཁ ࠂओʹͱͬͯ࠷େͷ ίετύϑΥʔϚϯεΛ ࣮ݱ͢ΔΑ͏੍ޚ
Slide 8
Slide 8 text
ࠂ৴ϓϩμΫτͷ֓ཁ ϝσΟΞଆͷऩӹ͕ ࠷େͱͳΔΑ͏ʹ੍ޚ
Slide 9
Slide 9 text
ࣗୡ͕ߏங͍ͯ͠Δͷ ࠂ৴ϓϩμΫτͷ֓ཁ
Slide 10
Slide 10 text
2. ඇಉظॲཧͷಋೖͱʁ 10
Slide 11
Slide 11 text
● ࠂओ͔Β͍͍ͨͩͨ༧ࢉΛదʹ͏ͨΊʹɺࠓ͍͘Β༧ࢉΛফԽ͔ͨ͠ ͱ͍͏ ใΛ DynamoDB ʹॻ͖ࠐΜͰ͍Δ ● ༧ࢉ͓ۚʹؔΘΔ͜ͱͳͷͰ ࣮֬ʹɾਖ਼֬ʹ ॻ͖ࠐ·ΕΔඞཁ͕͋Δ ● ॲཧ͕ࣦഊ͢Δͱɺ༧ࢉരൃͷݒ೦͕͋ΔͷͰɺ҆શଆʹͤΔΑ͏ʹ͍ͨ͠ ༧ࢉফԽػೳͷઆ໌ 11
Slide 12
Slide 12 text
● ༧ࢉܭ্Λߦ͍ͬͯΔαʔόʔʹෛՙ͕ूதͨ͠ࡍʹ༧ࢉͷॻ͖ࠐΈ͕ࣦഊͯ͠͠ ·ͬͨ ͋ΔεϩοτϦϯά͕ى͖ͨ 12
Slide 13
Slide 13 text
● ༧ࢉॻ͖ࠐΈʹؔͯ͠ଈ࣌ੑΑΓ࣮֬ੑΛॏࢹ͔ͨͬͨͨ͠ΊɺSQS Λͬͯ͜ ͷػೳͷΈ͔ͨͬͨ͠ ● ·ͨɺDLQ ͷΈΛͬͯػೳͷ࠶ࢼߦɾఀࢭΛࢼΈ͔ͨͬͨ ● SQS ΛͬͯҰ෦ͷॲཧΛ͢Δ͜ͱΛʮඇಉظॲཧͷಋೖʯͱݺΜͰ͍Δ εϩοτϦϯάରࡦΛ͍ͨ͠ 13
Slide 14
Slide 14 text
3. SQS ͱʁ 14
Slide 15
Slide 15 text
● Amazon Simple Queue ServiceʢSQSʣͷ͜ͱ ● ιϑτΣΞίϯϙʔωϯτؒͰҙͷྔͷϝοηʔδΛૹ৴ɺอଘɺड৴͢Δ͜ͱ ͕Ͱ͖ΔαʔϏε ● ϝοηʔδΛࣦͬͨΓɺଞͷαʔϏεΛར༻͢Δඞཁͳ͍ SQS ͱʁ 15 ग़య : https://aws.amazon.com/jp/sqs/
Slide 16
Slide 16 text
SQS ͱʁ 16 ࢀߟ : https://aws.amazon.com/jp/sqs/ https://d1.awsstatic.com/webinars/jp/pdf/services/20190717_AWS-BlackBelt_AmazonSQS.pdf ʮΩϡʔʯͷʮϝοηʔδʯΛ औಘ͢ΔΞϓϦέʔγϣϯ ίϯγϡʔϚ ϓϩσϡʔα͕ੜ͢Δσʔλ ϝοηʔδ ʮΩϡʔʯʹʮϝοηʔδʯΛ ૹ৴͢ΔΞϓϦέʔγϣϯ ϓϩσϡʔα ϝοηʔδΛΩϡʔΠϯά͢Δ ࢄΩϡʔ Ωϡʔ
Slide 17
Slide 17 text
4. ࣮લޙͷΞʔΩςΫνϟ 17
Slide 18
Slide 18 text
● ECS ͕ DynamoDB ͱ Aurora ʹ ʮಡΈऔΓʯʮॻ͖ࠐΈʯΛߦ͍ͬͯͨ લޙͷΞʔΩςΫνϟ ● ؒʹ SQS ͱ Lambda ΛڬΉ͜ͱͰɺ ʮ༧ࢉফԽʯ෦ͷΈΛΓͨ͠ ࣮લ ࣮ޙ 18
Slide 19
Slide 19 text
5. SQS બఆ࣌ʹߟྀͨ͜͠ͱ 19
Slide 20
Slide 20 text
SQS બఆ࣌ʹߟྀͨ͜͠ͱ 20 ● εϧʔϓοτߴ͍ͨ͘͠...ʂ ● ॻ͖ࠐΈ 1͚ͩʹ͍ͨ͠...ʂ
Slide 21
Slide 21 text
ඪ४Ωϡʔ vs FIFO Ωϡʔͷ ͋ ● API ΞΫγϣϯ͝ͱʹ 1ඵ͋ͨΓ 300τϥϯβΫγϣϯ ͷΫΥʔλΛα ϙʔτ ● ϝοηʔδ ඞͣ1ճ ͚ͩ৴ ඪ४Ωϡʔ FIFO Ωϡʔ ● API ΞΫγϣϯ͝ͱʹ 1ඵ͋ͨΓ ΄΅ແ੍ݶ ͷ API ίʔϧΛαϙʔτ ● ϝοηʔδ গͳ͘ͱ1ճ ৴ 21 ࢀߟ : https://docs.aws.amazon.com/ja_jp/AWSSimpleQueueService/latest/SQSDeveloperGuide/quotas-messages.html https://docs.aws.amazon.com/ja_jp/AWSSimpleQueueService/latest/SQSDeveloperGuide/FIFO-queues-exactly-once-processing.html
Slide 22
Slide 22 text
● FIFO ΩϡʔͷߴεϧʔϓοτϞʔυΛద༻͢Δͱɺ1ඵʹ͖ͭ 3000݅ ͷϦΫΤετʹ ରԠͰ͖Δ ○ 2023.10.04 ࣌Ͱ౦ژϦʔδϣϯͰඵؒ 9000τϥϯβΫγϣϯ ● ϝοηʔδάϧʔϓΛࢄͤ͞Δ͜ͱͰɺͦΕͧΕͷ ϝοηʔδάϧʔϓ͕1ඵ͋ͨΓ 300݅ͷϦΫΤετΛαϙʔτ ͢Δ High Throughput Queue ʹ͍ͭͯ 22 ࢀߟ : https://docs.aws.amazon.com/ja_jp/AWSSimpleQueueService/latest/SQSDeveloperGuide/high-throughput-fifo.html https://aws.amazon.com/jp/about-aws/whats-new/2023/10/amazon-sqs-increased-throughput-quota-fifo-high-throughput-mode/
Slide 23
Slide 23 text
SQS બఆ࣌ʹߟྀͨ͜͠ͱ 23 ● ॲཧ͕ࣦഊͨ࣌͠ʹݕͰ͖ΔΑ͏ʹ͍ͨ͠...ʂ
Slide 24
Slide 24 text
Dead Letter Queue ʹ͍ͭͯ 24 ϓϩσϡʔα͕ϝοηʔδΛ ૹ৴͢ΔͨΊͷओཁͳΩϡʔ ιʔεΩϡʔ ॲཧ͕ࣦഊͨ͠ࡍͷ ૹ৴ઌͱͳΔΩϡʔ σουϨλʔΩϡʔ ίϯγϡʔϚ͕ϝοηʔδΛ ड৴͠Α͏ͱ͢Δճ maxReceiveCount ࢀߟ : https://docs.aws.amazon.com/ja_jp/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-dead-letter-queues.html
Slide 25
Slide 25 text
Dead Letter Queue ʹ͍ͭͯ 25 ● DynamoDB ͷॻ͖ࠐΈʹࣦഊͨ࣌͠ʹ DeadLetterQueue ʹϝοηʔδΛૹ৴͠ɺ Lambda Ͱ ৴ΛҰ࣌ఀࢭ͢Δ ϑϥάΛཱͨͤΔΑ͏ʹͨ͠
Slide 26
Slide 26 text
6. SQS બఆޙʹߟྀͨ͜͠ͱ 26
Slide 27
Slide 27 text
SQS બఆޙʹߟྀͨ͜͠ͱ 27 ● SQS ͷϝοηʔδΛ Lambda ʹॲཧͯ͠ཉ͍͠...ʂ
Slide 28
Slide 28 text
ΠϕϯτιʔεϚοϐϯά 28 ● ΠϕϯτιʔεʢSQS ͷΩϡʔʣ͔ΒΠϕϯτΛಡΈऔΓɺLambda ؔΛݺͼग़ ͢ Lambda Ϧιʔεͷ͜ͱ ● Πϕϯτιʔεͷ SQS ʹϝοηʔδ͕ૹ৴͞Εͨ͜ͱΛݕͯ͠ɺLambda ͕࣮ؔ ߦ͞ΕΔ ࢀߟ : https://docs.aws.amazon.com/ja_jp/lambda/latest/dg/invocation-eventsourcemapping.html
Slide 29
Slide 29 text
ΠϕϯτιʔεϚοϐϯάͷ batch sizeʢಉ࣌ݺͼग़্͠ݶʣ ● ΠϕϯτιʔεʢSQS ͷΩϡʔʣ͔Β Lambda ͕ಉ࣌ʹಡΈऔΕΔΠϕϯτ ● FIFO Ωϡʔͷ߹࠷େ 10 ● Lambda ͷಉ࣮࣌ߦ੍ݶ͕͋ΔͷͰɺ1ճͷ Lambda ىಈͰΑΓଟ͘ͷϝοηʔδ ΛॲཧͰ͖ΔΑ͏ʹͨ͠ 29 ࢀߟ : https://docs.aws.amazon.com/ja_jp/lambda/latest/dg/with-sqs.html
Slide 30
Slide 30 text
ΠϕϯτιʔεϚοϐϯάͷ Lambda ࠷େಉ࣮࣌ߦ 30 ● ΠϕϯτιʔεʢSQS ͷΩϡʔʣ͕ಉ࣌ʹݺͼग़ͤΔ Lambda ؔͷ࠷େ ● FIFO Ωϡʔͷ߹ɺಉ࣌ݺͼग़͠ͷ্ݶɺʮϝοηʔδάϧʔϓ ID ͷʯ·ͨ ʮ࠷େಉ࣮࣌ߦͷઃఆʯͷͲͪΒ͔ খ͍͞ํ ࢀߟ : https://docs.aws.amazon.com/ja_jp/lambda/latest/dg/with-sqs.html
Slide 31
Slide 31 text
ΠϕϯτιʔεϚοϐϯάͷ࠷େಉ࣮࣌ߦ 31 ࢀߟ : https://docs.aws.amazon.com/ja_jp/lambda/latest/dg/with-sqs.html
Slide 32
Slide 32 text
ΠϕϯτιʔεϚοϐϯάͷ࠷େಉ࣮࣌ߦ 32 ࢀߟ : https://docs.aws.amazon.com/ja_jp/lambda/latest/dg/with-sqs.html
Slide 33
Slide 33 text
SQS બఆޙʹߟྀͨ͜͠ͱ 33 ● ϝοηʔδ͕దʹॲཧ͞Εͯཉ͍͠...ʂ
Slide 34
Slide 34 text
SQS ͷՄࢹੑλΠϜΞτʹ͍ͭͯ 34 ● ϝοηʔδ͕ड৴͞ΕͨޙʹɺଞͷίϯγϡʔϚ͕ಉ͡ϝοηʔδͷड৴ॲཧΛ Ͱ͖ͳ͘ͳΔظؒͷ͜ͱ ● ՄࢹੑλΠϜΞτ Lambda ͷ࣮ߦ࣌ؒͷগͳ͘ͱ 6ഒ ʹઃఆ͞ΕΔ͜ͱ͕ਪ ͞Ε͍ͯΔ ࢀߟ : https://docs.aws.amazon.com/ja_jp/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-visibility-timeout.html
Slide 35
Slide 35 text
SQS ͷ maxReceiveCount ʹ͍ͭͯ 35 ● ίϯγϡʔϚʔ͕ϝοηʔδΛड৴͠Α͏ͱͨ͠ࡍʹɺmaxReceiveCount ΛॳΊͯ ͑ͨ࣌ʹ DLQ ʹߦ༷͘ʹͳ͍ͬͯΔ ● ϝοηʔδ͕ॲཧ͞ΕΔ֬ΛߴΊΔʹ maxReceiveCount Λ 5 Ҏ্ʹઃఆ͢Δ͜ ͱ͕ਪ͞Ε͍ͯΔ ࢀߟ : https://docs.aws.amazon.com/ja_jp/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-dead-letter-queues.html
Slide 36
Slide 36 text
7. ෛՙࢼݧͷ 36
Slide 37
Slide 37 text
● k6 ΛͬͨෛՙࢼݧΛߦͬͨ ● ࣌ͷฏۉ qps ͷ 100 ഒ·Ͱ͑ΒΕΔ͔ͷෛՙࢼݧΛߦ͑ͨ ෛՙࢼݧͷ 37 ࢀߟ : https://k6.io/
Slide 38
Slide 38 text
● ࠷ॳϝοηʔδάϧʔϓ ID Λ୯Ұʹ͍ͯͨ͠ͷͰύϑΥʔϚϯε͕Ͱͳ͔ͬͨ ● SQS FIFO ΩϡʔͷߴεϧʔϓοτϞʔυͰɺϝοηʔδάϧʔϓ ʹ͖ͭ 1ඵؒ 300 ݅ͷϦΫΤετΛαϙʔτ͢ΔͷͰɺͦ͜Λదʹࢄͤͨ͞ Queue ͷ API Limit ͷ্ݶʹର͢Δݟམͱ͠ 38 ࢀߟ : https://docs.aws.amazon.com/ja_jp/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-dead-letter-queues.html
Slide 39
Slide 39 text
8. ࠓޙͷల 39
Slide 40
Slide 40 text
● LT ͷࢿྉ࡞Λ͢Δ·ͰΠϕϯτιʔεϚοϐϯάͷ Lambda ಉ࣮࣌ߦ੍ޚΛΒ ͳ͔ͬͨ... ࠓޙͷల 40
Slide 41
Slide 41 text
● SQS ͷ API Limit Λ͑ͯ͠·ͬͨ߹ʹෳͷ Queue ʹࢄͤ͞Δඞཁ͕͋Δ ࠓޙͷల 41
Slide 42
Slide 42 text
͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠ɻ 42