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
WebシステムのパフォーマンスとGo
Search
karupanerura
November 01, 2019
Programming
3
1.6k
WebシステムのパフォーマンスとGo
DeNA.go #3
karupanerura
November 01, 2019
Tweet
Share
More Decks by karupanerura
See All by karupanerura
GoでParserを書く
karupanerura
4
870
マイ隙間家具OSSたちのご紹介
karupanerura
2
250
Mustache Template 話 2024
karupanerura
0
14
1つの言語を原点に色々な言語を学ぶということ
karupanerura
4
2.8k
Javaの実装をPerlでテストする
karupanerura
0
1.1k
Go Generics Overview
karupanerura
0
1.3k
Japan.pm 2021 開催まで至る道
karupanerura
0
2.1k
Perl MongersのためのAWS_CDK入門
karupanerura
0
120
Webシステムのパフォーマンス・チューニング
karupanerura
2
810
Other Decks in Programming
See All in Programming
私の愛したLaravel 〜レールを超えたその先へ〜
kentaroutakeda
12
3.5k
eBPF Updates (March 2025)
kentatada
0
130
Devinのメモリ活用の学びを自社サービスにどう組み込むか?
itarutomy
0
1.7k
보일러플레이트 코드가 진짜 나쁜 건가요?
gaeun5744
0
370
Let's Take a Peek at PHP Parser 5.x!
inouehi
0
100
Return of the Full-Stack Developer
simas
PRO
1
310
生成AIの使いどころ
kanayannet
0
100
Develop Faster With FrankenPHP
dunglas
2
2.5k
Devin入門と最近のアップデートから見るDevinの進化 / Introduction to Devin and the Evolution of Devin as Seen in Recent Update
rkaga
7
3.8k
英語文法から学ぶ、クリーンな設計の秘訣
newnomad
1
270
PHPer's Guide to Daemon Crafting Taming and Summoning
uzulla
2
1.1k
CQRS+ES勉強会#1
rechellatek
0
400
Featured
See All Featured
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
40
2k
How to Think Like a Performance Engineer
csswizardry
22
1.5k
Git: the NoSQL Database
bkeepers
PRO
429
65k
Thoughts on Productivity
jonyablonski
69
4.5k
Designing for Performance
lara
606
69k
Code Reviewing Like a Champion
maltzj
522
39k
Imperfection Machines: The Place of Print at Facebook
scottboms
267
13k
Fantastic passwords and where to find them - at NoRuKo
philnash
51
3.1k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
Typedesign – Prime Four
hannesfritz
41
2.6k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
120k
Reflections from 52 weeks, 52 projects
jeffersonlam
349
20k
Transcript
WebγεςϜͷ ύϑΥʔϚϯεͱGo DeNA.go #3 id:karupaneura
id:karupanerura • ιϑτΣΞΤϯδχΞ @ DeNA, Co,. LTD. • ͓ࣄ: ϓϥοτϑΥʔϜ։ൃ
(Perl/Go/Java/etc..) • ISUCON8: ༧બग़νʔϜ (࡞ɺϕϯνϚʔΧʔ) • αϚʔΠϯλʔϯγοϓ2019: ΤϯδχΞ͚ίʔε։ൃ • දཧࣄ @ Japan Perl Association
Goal
WebγεςϜʹ͓͚ΔຊͷGoͷڧΈ Λߟ͑ͯ͠Β͏
ҙࣝ • ʮͱΓ͋͑ͣGoͰॻ͍͓͚͍ͯΜͰ͠ΐʁʯ • ͍͘ΒLLͱൺͯϕϯνϚʔΫ͕༏ल͔ͩΒͱ͍ͬͯͦΓΌ… • ʮͰISUCONͰ༧બ௨աͯ͠ΔͷGoͷνʔϜ͔Γ͡ΌΜʯ • ༗རͳଆ໘ͨ͘͞Μ͋Δ͚Ͳ…είΞ͕͍νʔϜ…
ݱ࣮ΛݟΑ͏
ͱ͔ͬ͠Γ͖߹͓͏
ࠓճ͢͜ͱ • WebγεςϜʹ͓͚ΔύϑΥʔϚϯεͱͳʹ͔ • Ͳ͏͍ͬͨͷ͕ϘτϧωοΫʹͳΔͷ͔ • ͦͷͨΊʹGoͷػೳΛͲͷΑ͏ʹ׆༻͢Δͱྑ͍ͷ͔
ࠓճ͞ͳ͍͜ͱ • DeNAαϚʔΠϯλʔϯγοϓͷৄࡉ • ͲͷΑ͏ʹܭଌͯ͠ϘτϧωοΫΛൃݟ͢ΔͱΑ͍͔
ͦͦύϑΥʔϚϯεͬͯͳΜ͚ͩͬʁ
WebγεςϜʹ͓͚ΔཧతύϑΥʔϚϯε • γεςϜશମͰߟ͑ͨͱ͖... • ୯Ґ࣌ؒͨΓʹͨ͘͞ΜͷϦΫΤετΛॲཧͰ͖Δ • ಉ࣌ʹෳͷϦΫΤετΛޮΑ͘ॲཧͰ͖Δ • ϦΫΤετ୯ҐͰඞཁͱ͢ΔϦιʔε͕গͳ͘ࡁΉ •
͋·ΓແବʹϦιʔεΛফඅ͠ͳ͍
ҰํͰݱ࣮ͷγεςϜͷଟ͘ ཧతͳύϑΥʔϚϯεͰͳ͍
ͳͥύϑΥʔϚϯεԼ͢Δ͔ • γεςϜશମͰߟ͑ͨͱ͖ʹ... • ୯Ґ࣌ؒͨΓʹͨ͘͞ΜͷϦΫΤετΛॲཧͰ͖ͳ͍ • ಉ࣌ʹෳͷϦΫΤετΛॲཧ͢Δޮ͕ѱ͍ • ϦΫΤετ୯ҐͰඞཁͱ͢ΔϦιʔε͕ଟ͍ •
ແବʹϦιʔεΛফඅͯ͠͠·͏
ύϑΥʔϚϯεͷԼཁҼΛഉআ͢Ε ύϑΥʔϚϯε্Ͱ͖Δ
ύϑΥʔϚϯεͷԼཁҼΛഉআ͢Ε ύϑΥʔϚϯε্Ͱ͖Δ γεςϜશମͷϘτϧωοΫʹ͢Δ͖
۩ମྫΛҎͯߟ͑ͯΈΔ
DeNA αϚʔΠϯλʔϯγοϓ2019 ΤϯδχΞίʔε
՝: ΞϓϦέʔγϣϯΛ͍͍ײ͡ʹͤΑ (͍͍ײ͡ʹύϑΥʔϚϯεؚ·ΕΔ)
ΞϓϦέʔγϣϯհ (demo)
ύϑΥʔϚϯεཁ݅ ʮ֨҆εϚϗͷൃചΛه೦͠CMʹग़ԋதͷਓؾܳਓ͕ϦΞϧλ ΠϜͰ࣭Λड͚͚ΔઐνϟϯωϧΛ։ઃ͠·͢ɻʯ ͜͜·Ͱ͑ΒΕΕޚͷࣈɺഒͰେৎͳΒ҆Մೳɿ • 5000ൃݴ/ඵ (x2: 10000ൃݴ/ඵ) • 5ສਓͷಉ࣌ଓ
(10ສಉ࣌ଓ)
γεςϜߏਤ • EC2 1 • AppͱDBಉډ • ALBͰTLSΛॲཧ • S3/Cloud
Frontະ༻
ύϑΥʔϚϯεվળͷྲྀΕ for { • ܭଌ • ϘτϧωοΫΛಛఆ • ϘτϧωοΫΛղফ }
ύϑΥʔϚϯεվળͷྲྀΕ for { • ܭଌ • ϘτϧωοΫΛಛఆ • ϘτϧωοΫΛղফɹˡɹࠓճ͜͜ʹ }
࣮ࡍͷύϑΥʔϚϯεվળ
վળޙͷγεςϜߏਤ • App/DB • S3/Cloud Front׆༻ • ΄͔MySQLνϡʔχϯά
ൃݴ͕ଟ͘ͳΔͷରࡦ
ൃݴ͕ଟ͘ͳΔͷରࡦ • νϟοταʔϏεͷൃݴ͕ଟ͍ͱͲ͏ͳΔ͔ • DBͷinsert͕٧·Δ (MySQLͷνϡʔχϯάͰҰఆվળ͢Δ) • insert͕ͳͥ٧·Δ͔ • DBͷϩοΫΛಉ࣌ଟൃతʹऔΔ͜ͱʹͳΔ͔Β
insertͷϩοΫڝ߹ʹͲ͏ରॲ͢Δ͔ • insertDBͷϩοΫΛऔΔ͜ͱʹͳΔ • ϩοΫΛऔΔػձΛΒ͢͜ͱʹΑͬͯϩοΫڝ߹ΛݮΒͤΔ • ෳͷൃݴΛ·ͱΊͯinsert͢Δͱ... • 1ճͷϩοΫͰෳͷൃݴ͕insertͰ͖͓ͯಘ
insertΛbufferingͯ͠·ͱΊͯΔ • ൃݴΛόοϑΝϦϯάͯ͠insert͢Δworker goroutineΛ࡞ • ϦΫΤετΛॲཧ͢Δgoroutine͔Βchannelܦ༝Ͱ͛Δ • channelͰinsertͷ݁ՌΛड͚औΔ • workerbuffer͕Ұఆ݅ཷ·ͬͨͱ͖͔ҰఆͷintervalͰॲཧ
࣮ࡍͷίʔυ • ʢDBνϡʔχϯά͚ͩͰΛղফͯ͠͠·ͬͨͷͰະ࣮ʣ • DeNA.goͷͨΊʹॻ͖ԼΖͦ͏ͱࢥ͚ͬͨͲؒʹ߹Θͳ͔ͬͨ
ಉ࣌ଓ͕ଟ͘ͳΔͷରࡦ
ಉ࣌ଓ͕ଟ͘ͳΔͱ • ൃݴͷऔಘ͕ଟ͘ͳΔ • ͚ࠩͩऔಘ͍ͨ͠ • ͔͠͠৽ணΛpolling͢Δͱൃݴͷগͳ͍νϟϯωϧ͕ਏ͍ • ʢॳظ࣮શൃݴΛऔΔAPIΛpollingͯ͠Δʣ
Server Sent Events • ҰੲલͷComet͕༷Λཧͯ͠ਖ਼نԽ͞Ε·ͨ͠తͳͭ • WebSocketΑΓѻ͍͕؆୯ • αʔόʔͱΫϥΠΞϯτͰίωΫγϣϯΛషΓଓ͚Δ •
αʔόʔ͔ΒΫϥΠΞϯτʹσʔλΛpushͰ͖Δ (ٯෆՄ)
DBʹϦΫΤετΛඈ͞ͳ͍ • ͖ͬ͞ͷͭͱಉ͡ཁྖͰworker͕࠷৽ͷൃݴΛbuffering͢Δ • DBworker͚͕ͩpollingͯ͠bufferΛߋ৽͍ͯ͘͠ • client͝ͱʹ࠷ޙͷൃݴΛอ͓͍࣋ͯͯͦ͠ΕҎ߱ͷΛ௨ • ௨͞ΕͨͷΛServer Sent
EventsͰclientʹૹΔ
࣮ࡍͷίʔυ (worker goroutine)
࣮ࡍͷίʔυ (worker goroutine)
࣮ࡍͷίʔυ (worker goroutine)
࣮ࡍͷίʔυ (http handler)
ͦͷଞ
sync.Pool • ϦΫΤετͷbytes.BufferͳͲΛৗʹ࡞ΔͱGC͕ਏ͍ • sync.PoolΛ͏ͱgoroutine͝ͱʹڝ߹͠ͳ͍Pool͕࡞ΕΔ • ϝϞϦޮྑ͍͕ϝϞϦফඅ͍ͯ͘͠ͷͰѻ͍ʹҙ
encoding/json • ϨεϙϯεΛ࡞Δͷʹඞͣͦ͠ΕͱಉαΠζͷόοϑΝΛ࡞ Δඞཁͳ͍ • ࡞ͬͨͿΜ͔Βૹ͍͚ͬͯΑ͍ • ͨͩ͠ (*json.Encoder).Write ్தͰerrorΛฦ͠ࢭ·Δ͜ͱ
͕͋Δ • ਖ਼͘͠ϨεϙϯεΛฦͨ͢Ίʹbufferඞཁ
io • sync.PoolͰอ࣋ͨ͠bytes.BufferΛhttp.ResponseWriter • bytes.Bufferio.WriterToΛ࣮͍ͯ͠ΔͷͰWriteToͰ͖Δ • bytes.BufferͷWriteTobytes.Bufferͷposition͚ͩม͍͑ͯ ͖ޮΑ͘WriteͰ͖Δ • io.CopyBufferͳͲศར
database/sql • σϑΥϧτͰDBͱͷଓΛόοϑΝϦϯάͯ͘͠ΕΔ • SetMaxIdleConns/SetMaxOpenConns/SetConnMaxLifetime • ࣮ࡍͷنʹ͋Θͤͯௐ͢Δͱଓ͕࠷దԽͰ͖Δ • SetMaxIdleConnsͷσϑΥϧτ2
·ͱΊ
·ͱΊ • Goʹ͚ͨͩ͠Ͱ͘ͳΒͳ͍ͷͰϦιʔεΛޮΑ͓͘͏ • ಛʹϘτϧωοΫʹͳΓ͍͢Ϧιʔεʹҙ • ༨༟͕͋ΔϦιʔεΛ׆༻ͯ͠͠Α͏ • جຊػೳio, syncͳͲศརͳඪ४ύοέʔδΛ·ͣ׆༻͠Α͏
• ݶΓ͋ΔϦιʔεΛޮΑ͘׆༻͢Δϊϋͷๅݿ