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
5
800
マイ隙間家具OSSたちのご紹介
karupanerura
2
230
Mustache Template 話 2024
karupanerura
0
3
1つの言語を原点に色々な言語を学ぶということ
karupanerura
4
2.8k
Javaの実装をPerlでテストする
karupanerura
0
1k
Go Generics Overview
karupanerura
0
1.2k
Japan.pm 2021 開催まで至る道
karupanerura
0
2k
Perl MongersのためのAWS_CDK入門
karupanerura
0
96
Webシステムのパフォーマンス・チューニング
karupanerura
2
780
Other Decks in Programming
See All in Programming
Full stack testing :: basic to basic
up1
1
930
生成AIでGitHubソースコード取得して仕様書を作成
shukob
0
270
LLM Supervised Fine-tuningの理論と実践
datanalyticslabo
3
1k
CSC305 Lecture 26
javiergs
PRO
0
140
StarlingMonkeyを触ってみた話 - 2024冬
syumai
3
270
Security_for_introducing_eBPF
kentatada
0
110
talk-with-local-llm-with-web-streams-api
kbaba1001
0
170
Symfony Mapper Component
soyuka
2
730
アクターシステムに頼らずEvent Sourcingする方法について
j5ik2o
4
200
わたしの星のままで一番星になる ~ 出産を機にSIerからEC事業会社に転職した話 ~
kimura_m_29
0
180
創造的活動から切り拓く新たなキャリア 好きから始めてみる夜勤オペレーターからSREへの転身
yjszk
1
130
42 best practices for Symfony, a decade later
tucksaun
1
180
Featured
See All Featured
Automating Front-end Workflow
addyosmani
1366
200k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
48k
4 Signs Your Business is Dying
shpigford
181
21k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
330
21k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
2
170
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
32
2.7k
Rebuilding a faster, lazier Slack
samanthasiow
79
8.7k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Building Flexible Design Systems
yeseniaperezcruz
327
38k
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.4k
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ͳͲศརͳඪ४ύοέʔδΛ·ͣ׆༻͠Α͏
• ݶΓ͋ΔϦιʔεΛޮΑ͘׆༻͢Δϊϋͷๅݿ