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
770
マイ隙間家具OSSたちのご紹介
karupanerura
2
220
1つの言語を原点に色々な言語を学ぶということ
karupanerura
4
2.7k
Javaの実装をPerlでテストする
karupanerura
0
1k
Go Generics Overview
karupanerura
0
1.2k
Japan.pm 2021 開催まで至る道
karupanerura
0
2k
Perl MongersのためのAWS_CDK入門
karupanerura
0
90
Webシステムのパフォーマンス・チューニング
karupanerura
2
780
タイムテーブルを楽に組みたい
karupanerura
0
2.1k
Other Decks in Programming
See All in Programming
Arm移行タイムアタック
qnighy
0
330
CSC509 Lecture 11
javiergs
PRO
0
180
ActiveSupport::Notifications supporting instrumentation of Rails apps with OpenTelemetry
ymtdzzz
1
230
flutterkaigi_2024.pdf
kyoheig3
0
140
RubyLSPのマルチバイト文字対応
notfounds
0
120
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
1
100
CSC509 Lecture 09
javiergs
PRO
0
140
PHP でアセンブリ言語のように書く技術
memory1994
PRO
1
170
「今のプロジェクトいろいろ大変なんですよ、app/services とかもあって……」/After Kaigi on Rails 2024 LT Night
junk0612
5
2.2k
Kaigi on Rails 2024 〜運営の裏側〜
krpk1900
1
230
카카오페이는 어떻게 수천만 결제를 처리할까? 우아한 결제 분산락 노하우
kakao
PRO
0
110
What’s New in Compose Multiplatform - A Live Tour (droidcon London 2024)
zsmb
1
480
Featured
See All Featured
Six Lessons from altMBA
skipperchong
27
3.5k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
48k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
665
120k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.5k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
126
18k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.8k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
1.9k
Bootstrapping a Software Product
garrettdimon
PRO
305
110k
Making the Leap to Tech Lead
cromwellryan
133
8.9k
Fantastic passwords and where to find them - at NoRuKo
philnash
50
2.9k
Docker and Python
trallard
40
3.1k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
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ͳͲศརͳඪ४ύοέʔδΛ·ͣ׆༻͠Α͏
• ݶΓ͋ΔϦιʔεΛޮΑ͘׆༻͢Δϊϋͷๅݿ