Slide 1

Slide 1 text

Persisting Massive Data with Rails Designing for 60 billion rows, and fighting the bottlenecks Shoichi Ochi (@occhi) Kaigi on Rails 2026 2026-10-17 Railsで挑む大量データ永続化 〜600億件を永続化するための設計とボトルネックとの戦い〜

Slide 2

Slide 2 text

60 Billion Rows of History 扱うのは、全部の月を素直に持つと 600億件になる履歴データ 2

Slide 3

Slide 3 text

How Do We Handle It to Fit the Requirements? これを、要件に合わせてどう扱うか? 3

Slide 4

Slide 4 text

Once That Is Settled, How Do We Think About the Design? 扱い方が決まったら、設計はどう考えればいいか? 4

Slide 5

Slide 5 text

That's What This Talk Is About 今日はそんな話をします 5

Slide 6

Slide 6 text

About Me Shoichi Ochi SmartBank, Inc. Software Engineer Career: ITANDI, Inc. → GMO Beauty, Inc. → SmartBank, Inc. Hobby: Weight training @ochi11181101 @sho-work 6

Slide 7

Slide 7 text

77

Slide 8

Slide 8 text

Even Complex Technical Problems Can Be Solved by Steadily Sorting Out Requirements and Constraints and Identifying and Analyzing Bottlenecks 複雑な課題でも、特別な解決策が要るとは限らない。要件・制約を整理し、ボトルネックの特定・分析を積み重 ねれば解決できる 8

Slide 9

Slide 9 text

Today's Theme 今日の軸:要件・制約・ボトルネックを判断基準にして、技術選定・設計・実装の意思決定を積み重ねる 9

Slide 10

Slide 10 text

Agenda 流れは3つ。600億件は現実的か → 現実的な設計と実装 → 運用で出たボトルネックとの戦い 10

Slide 11

Slide 11 text

Project Overview This project Goal Use phone numbers' contract status for things like fraud prevention Requirements Real-time reads / monthly import within a day Constraints Limited extra budget and engineering effort Bottlenecks ? (we check with a thought experiment next) 目的は、電話番号の契約状況を不正利用対策などに使うこと。要件は「リアルタイムに読む」「毎月 1日以内に 取り込む」。予算も工数も限られる 11

Slide 12

Slide 12 text

The Phone Number Data ● ● Preconditions ○ Every month, a vendor uploads compressed CSV files to S3 Delivered CSV characteristics ○ More than 250 million phone numbers ○ Holds up to 20+ years of contract status ○ Contains the date and status at each contract-status change ベンダーから毎月、圧縮 CSVが S3 に届く。電話番号は 2.5億件超で、最大 20年以上の契約状況が入っている 12

Slide 13

Slide 13 text

The Delivered CSV Has One Row per Phone Number (~250M Rows) 納品CSVは1電話番号 = 1行。最新の状態と、状態が変わった時点の履歴を横に持っていて、全部で約 2.5億 行 13

Slide 14

Slide 14 text

Storing Every Month's State Means 60 Billion Rows in Theory 毎月の状態を 1行ずつ持つと、変化のない月も 1行ずつ。2.5億番号 × 240か月 = 理論上600億行 14

Slide 15

Slide 15 text

Do We Need to Store 60 Billion Rows? Consider cases that need data for months where the contract status did not change 本当に600億件も保存する必要がある?契約状況が変わっていない月のデータが要るケースを考えてみる 15

Slide 16

Slide 16 text

Cases Where We Also Want to Search Months with No Change in Contract Status 納品データにあるのは、状態が変わった時点だけ。例えば、 2025年8月に契約中だった電話番号をすべて取 りたいとする 16

Slide 17

Slide 17 text

Cases Where We Also Want to Search Months with No Change in Contract Status 2025年8月のレコードはどれにもない。でも前後の変化点を見れば、その時点の状態は分かる 17

Slide 18

Slide 18 text

Cases Where We Also Want to Search Months with No Change in Contract Status 18

Slide 19

Slide 19 text

Cases Where We Also Want to Search Months with No Change in Contract Status 2つ目と3つ目が契約中。こうした変化点の間の月も検索するなら、月ごとの状態も保存しておくべき? 19

Slide 20

Slide 20 text

Agenda まずは1つ目、600億件を永続化するのは現実的か 20

Slide 21

Slide 21 text

1. Is Persisting 60 Billion Rows Realistic? 🤔 600億件を永続化するのは現実的? 21

Slide 22

Slide 22 text

Steps of the Thought Experiment Back-of-the-envelope estimation 1. Estimate the rows written per second 2. Measure a reference value on our in-house DB 3. From [1.] and [2.], work out what system setup is needed 作り込む前に概算で桁をつかむ( Back-of-the-envelope estimation)。3つの手順で考える https://highscalability.com/google-pro-tip-use-back-of-the-envelope-calculations-to-choo/ 22

Slide 23

Slide 23 text

1. Estimate the Rows per Second Needed to Write 60 Billion Rows in a Day 600億件を1日(86,400秒)で書くには、約 69.4万行/秒が必要 23

Slide 24

Slide 24 text

2. Measure How Fast Our Existing In-House DB Can Write ● ● ● DB: Amazon Aurora MySQL Measured with parallel Bulk Upsert Sustained throughput (saturated with more parallelism): about 10,000 – 50,000 rows/sec 既存の Aurora MySQL に、並列 Bulk Upsert で限界まで書き込んで計測。持続できるのは約 1万〜5万行/ 秒 24

Slide 25

Slide 25 text

With One Writer, a Full Build Does Not Fit in a Day 1 Writer の限界は約1万〜5万行/秒。必要な約69.4万行/秒には全然届かない 25

Slide 26

Slide 26 text

Converted at the Aurora MySQL Performance We Just Measured, How Much Do We Need? いまの Aurora の性能で換算すると、十数〜数十 Writer 相当の構成が必要になる 26

Slide 27

Slide 27 text

3. Work Out What System Setup Reaches the Required Rows per Second 方向は2つ。A:分散DBなど別のデータストアに変える。 B:Aurora を分割する 27

Slide 28

Slide 28 text

But Is That Setup Really Realistic? ● ● The additional infrastructure budget becomes huge ○ A dozen to several dozen writers' worth (in Aurora terms), and 6–12 TB of storage Implementation effort and operations would be painful ○ A. Change the data store: migrating the DB and new operational know-how ○ B. Split Aurora: shard routing, re-runs, failure recovery, and operations でもどちらも、予算・容量・実装・運用がかなり大変。本当に現実的? 28

Slide 29

Slide 29 text

Hmm, This Isn't Realistic... 🤯 うーん、現実的じゃない … 29

Slide 30

Slide 30 text

Let's Calm Down and Revisit the Requirements 🧘🧘 一度冷静になって、要件から見直そう 30

Slide 31

Slide 31 text

Our Requirements, Constraints, and Bottlenecks This project Requirements Real-time reads / monthly import within a day Constraints Limited extra budget and engineering effort Bottlenecks Writing 60B rows a day is unrealistic ここまでを整理すると、ボトルネックは「 600億件を1日で書き込むのは非現実的」 31

Slide 32

Slide 32 text

2. Turning It into a Realistic Design and Implementation ここからは、現実的な設計と実装に落とし込む 32

Slide 33

Slide 33 text

Agenda 2つ目、現実的な設計と実装へ。まずはデータモデルから 33

Slide 34

Slide 34 text

2.1 Data Model Design まずはデータモデルの設計 34

Slide 35

Slide 35 text

Project Overview, Again This project Goal Use phone number usage for things like fraud prevention Requirements Real-time reads / monthly import within a day Constraints Limited extra budget and engineering effort Bottlenecks Writing 60B rows a day is unrealistic プロジェクトの要件・制約・ボトルネックをおさらい 35

Slide 36

Slide 36 text

Project Overview, Again This project Goal Use phone number usage for things like fraud prevention Requirements Real-time reads / monthly import within a day Constraints Limited extra budget and engineering effort Bottlenecks Writing 60B rows a day is unrealistic ここからは、要件のうち「履歴をリアルタイムに読み取る」に注目する 36

Slide 37

Slide 37 text

Sharpening the Read Requirements 知りたいのは「直近 24か月で契約・解約を何回繰り返したか」「契約から何か月か」。これは変化した時点さえ 分かれば答えられる 37

Slide 38

Slide 38 text

(Recap) Cases Where We Also Want to Search Months with No Change in Contract Status (再掲)最初に考えたユースケースをもう一度。 2025年8月に契約中だった電話番号を、全部取りたい 38

Slide 39

Slide 39 text

We Did Not Need to Search Across Phone Numbers by a Month with No Change in Contract Status でも読み取り要件を確かめると、契約状況が変わっていない期間のデータが要るユースケースはなかった 39

Slide 40

Slide 40 text

Store Only the Changes ● ● Store the points where the state changed, not the state in every month This brings the number of rows down to a few billion 毎月の状態ではなく、状態が変わった時点だけを保存する。これで数十億件くらいまで減る 40

Slide 41

Slide 41 text

Still Billions of Rows, So See If We Can Cut Further Does each history entry need to be its own record? まだ数十億件あるので、さらに削れないか。履歴の 1件1件を、別々のレコードにする必要はある? 41

Slide 42

Slide 42 text

Think from the Unit We Want to Read ● There is no requirement to search or aggregate across numbers by the history's month or status → We do not need to keep each history entry as its own record 履歴の年月やステータスで電話番号を横断して検索・集計する要件はない。だから履歴を 1件ずつ別レコード にしなくていい 42

Slide 43

Slide 43 text

The Final Data Model: Keep the History as a List ● ● ● One phone number = one document The history is held as a list The number of stored items stays at the number of phone numbers 1電話番号 = 1ドキュメントにして、履歴はリストで持つ。保存件数は電話番号の数まで減らせる Works well if you don't need indexes on month or status! There are trade-offs, though... 43

Slide 44

Slide 44 text

What We Actually Have to Store 600億件 → 2.5億件(約1/240)。ここまでがデータモデリングの成果 44

Slide 45

Slide 45 text

Is Persisting 250 Million Records Realistic? 2.5億件の永続化なら、現実的? 45

Slide 46

Slide 46 text

At ~2,900 Records/sec, the Thought Experiment Says It Looks Realistic ● Needed: ○ 250M ÷ 86,400 sec ≈ ~2,900 records/sec ● Thought experiment measurement: ○ ~10K–50K records/sec (single writer) ● Note: ○ Each record is ~4× larger than in the thought experiment, so expect slightly lower write throughput. → It looks feasible within one writer 必要なのは約 2,900件/秒。思考実験の計測( 1万〜5万件/秒)から見て、 1 Writer でいけそう 46

Slide 47

Slide 47 text

What We Know So Far: Down to 250M Records, and Now Realistic Bottlenecks This project After the data model Writing 60B records a day (~694K/sec) Writing 250M records a day (~2,900/sec) → Revising the data model made the bottleneck manageable データモデルを見直したことで、ボトルネックが現実的な範囲に収まった 47

Slide 48

Slide 48 text

Agenda 次は2.2、データストア選定 48

Slide 49

Slide 49 text

2.2 Data Store Selection データストアの選定 49

Slide 50

Slide 50 text

The Architecture We Are Aiming At (Deciding the Data Store) 決めたいのは、この「 ?」の部分 50

Slide 51

Slide 51 text

Three Data Store Candidates Fit the Data Model このデータモデルをそのまま載せられる候補は 3つ。Aurora MySQL の JSON 型、DynamoDB、 Elasticsearch 51

Slide 52

Slide 52 text

We Compare Data Stores Against These Requirements and Constraints Item Criterion Reads Fetch within 500 ms Writes Import 250M rows within a day Infra budget ~$1,000/month (extra) Build effort ~1 person-month Ops burden Keep monthly ops simple Requirement Constraint 比べる基準は要件と制約。要件は読み取り・書き込み、制約は予算・工数・運用 52

Slide 53

Slide 53 text

Compare on the Requirements まずは要件で比べる 53

Slide 54

Slide 54 text

Thinking About the Requirements ● Reads: fetch within 500 ms ● Writes: import 250M rows within a day (~2,900 rows/sec needed) 要件は2つ。500ms以内に読めること、 2.5億件を1日以内(約2,900件/秒)で取り込めること 54

Slide 55

Slide 55 text

Checking Whether Each Candidate Meets the Read and Write Requirements * Details are in the appendix 3つの候補が、読み取り・書き込みの要件を満たせるかを調べた。詳しい根拠は Appendix に載せている 55

Slide 56

Slide 56 text

Every Candidate Is Expected to Meet the Requirements Aurora MySQL DynamoDB Elasticsearch Reads ◯ ◯ ◯ Writes ◯ ◯ ◯ → All three are expected to meet them 要件は、3候補とも満たせる 56

Slide 57

Slide 57 text

No Difference on the Requirements 要件の観点では、差がつかない 57

Slide 58

Slide 58 text

Next, Compare on the Constraints 次は制約で比べる 58

Slide 59

Slide 59 text

Thinking About the Constraints ● Extra infrastructure budget: ~$1,000/month ● Development effort: ~1 person-month ● Operational overhead: keep monthly operations simple 制約は3つ。追加予算は月約 1,000ドル、開発工数は約 1人月、毎月の運用を複雑にしない 59

Slide 60

Slide 60 text

Constraint (Cost): Every Candidate Fits the Budget Billed for Within budget? Aurora MySQL DynamoDB Elasticsearch Added storage and I/O Import or writes + storage VCUs + storage ○ ○ ○ → Every candidate can meet the cost constraint コストは課金の仕方こそ違うけど、どれも予算内に収まりそう。ここでも決まらない 60

Slide 61

Slide 61 text

Comparing on Development Effort and Operational Overhead 残るのは開発工数と運用負担。既存の資産を活かせるか、運用が複雑にならないか 61

Slide 62

Slide 62 text

Aurora MySQL: Built on Existing Assets, but Writer Load Needs Managing MySQL は既存の Rails・ActiveRecord 資産で作れる。でも既存サービスと同じ Writer に大量に書くので、 負荷の管理が要る 62

Slide 63

Slide 63 text

DynamoDB: Both Build and Operations Need New Mechanisms DynamoDB は取り込みの仕組みを一から作る必要がある。毎月テーブルを切り替える運用も要るし、運用 実績もない https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/transaction-apis.html 63

Slide 64

Slide 64 text

Elasticsearch: Import Needs Design, and Ops Must Handle Bulk API Partial Failures Elasticsearch は運用実績あり。でも Bulk API はアトミックじゃないので、部分失敗を拾って再試行する仕組 みが要る https://www.rkenmi.com/posts/atomic-operations-with-elasticsearch 64

Slide 65

Slide 65 text

Compared, Only Development Effort Differs Aurora MySQL DynamoDB Elasticsearch Development effort ◯ ✕ △ Operational overhead △ ✕ △ 並べてみると、運用はどれも手間がかかる。差がついたのは開発工数 65

Slide 66

Slide 66 text

We Adopted Aurora MySQL, Prioritizing Development Effort ● The three did not differ on requirements or cost ● We chose MySQL in terms of development effort and operational overhead → The downside, higher write load on the DB, we decided to handle by running in low-traffic hours 3候補とも、要件・コストでは差がつかなかった。開発工数と運用負担から MySQL を選び、書き込み負荷はト ラフィックの少ない時間帯に逃がす 66

Slide 67

Slide 67 text

The Architecture So Far これでデータストアが決まった。次は、負荷を管理しながら 1日以内に取り込むバッチの設計 67

Slide 68

Slide 68 text

Agenda 次は2.3、バッチアーキテクチャの設計と実装 68

Slide 69

Slide 69 text

2.3 Batch Architecture: Design and Implementation バッチアーキテクチャの設計と実装 69

Slide 70

Slide 70 text

What the Batch Design Must Handle: Requirements, Constraints, Bottlenecks This project This section Requirements Real-time reads / monthly import within a day 250M rows within a day / redo safely Constraints Limited extra budget and engineering effort — Bottlenecks Writing 250M records Aurora write speed and load → Design the batch from the requirements and bottlenecks バッチ設計で見るのは、要件( 1日以内・安全にやり直せる)とボトルネック(既存 Aurora への書き込み) 70

Slide 71

Slide 71 text

Premises ● Delivered to S3 as compressed CSV ● Batches run as ECS tasks 前提は2つ。納品は S3 の圧縮CSVで、バッチは ECS タスクで動かす 71

Slide 72

Slide 72 text

The Overall Flow まずは素朴な流れから。ダウンロード → 解凍して読み取り → DBへ書き込む 72

Slide 73

Slide 73 text

Two Questions Arise from the Requirements and Bottlenecks Q1 Processing time Q2 Recovery Question Can we import within a day? How do we handle re-runs? Driven by Requirement, bottleneck Requirement この流れに要件とボトルネックを当てると、問いが 2つ出てくる。1日以内に取り込めるか、リランをどうするか 73

Slide 74

Slide 74 text

Q1. Can We Import Within a Day? Q1 Processing time Q2 Recovery Question Can we import within a day? How do we handle re-runs? Driven by Requirement, bottleneck Requirement まずは Q1、1日以内に取り込めるか 74

Slide 75

Slide 75 text

How Do We Import Within a Day? 2.5億件を1日以内に取り込むには、どうする? 75

Slide 76

Slide 76 text

Q1: To Import Within a Day, We Need Parallelism ● Needed: 250M ÷ 86,400 sec ≈ ~2,900 rows/sec ● Measured with serial writes: ~1,000 rows/sec 必要なのは約 2,900件/秒。でも直列で書くと約 1,000件/秒しか出ない 76

Slide 77

Slide 77 text

Q1: To Import Within a Day, We Need Parallelism ● Needed: 250M ÷ 86,400 sec ≈ ~2,900 rows/sec ● Measured with serial writes: ~1,000 rows/sec → It cannot be done without parallel import (3 or more in parallel) 直列だと約70時間かかる。並列で取り込まないと間に合わない( 3並列以上) 77

Slide 78

Slide 78 text

The Answer to Q1 Q1 Processing time Q2 Recovery Question Can we import within a day? How do we handle re-runs? Solution Bulk Upsert in parallel — Q1 の答えは、並列で Bulk Upsert すること 78

Slide 79

Slide 79 text

Q2. How Do We Handle Re-runs? Q1 Processing time Q2 Recovery Question Can we import within a day? How do we handle re-runs? Driven by Requirement, bottleneck Requirement 次は Q2、リランをどうするか 79

Slide 80

Slide 80 text

On Re-runs, We Want Idempotency and to Skip What Is Already Done リランのときは、冪等にして、終わったところは飛ばしたい。性質は「冪等性」と「再開可能性」の 2つ https://www.oreilly.com/library/view/designing-data-intensive-applications/9781491903063/ 80

Slide 81

Slide 81 text

Idempotence まずは冪等性 81

Slide 82

Slide 82 text

Idempotence: With Bulk Upsert, the Same Input Gives the Same Result ● Write with Bulk Upsert keyed on the phone number ● Importing the same data again gives the same result 電話番号をキーに Bulk Upsert で書けば、同じデータを何度取り込んでも結果は変わらない 82

Slide 83

Slide 83 text

Idempotence: With Bulk Upsert, the Same Input Gives the Same Result ● Write with Bulk Upsert keyed on the phone number ● Importing the same data again gives the same result → But reprocessing everything still costs time ただし、これだけだと止まるたびに全件をやり直すコストが残る 83

Slide 84

Slide 84 text

Resumability We want to resume partway, so how do we do it? 次は再開可能性。途中から再開できるようにしたい 84

Slide 85

Slide 85 text

Resumability: How to Record How Far We Got Human Readable Track by byte range Track by split file Not which row By files done どこまで終わったかの記録は、バイト位置だと何行目か分からないけど、分割ファイルなら何個終わったかで 分かる 85

Slide 86

Slide 86 text

Resumability: How to Record How Far We Got Human Readable Track by byte range Track by split file △ ◯ → Make the split file the unit for resuming partway だから、分割ファイルを途中再開の単位にした 86

Slide 87

Slide 87 text

Answers to the Two Questions Q1 Processing time Q2 Recovery Question Can we import within a day? How do we handle re-runs? Solution Bulk Upsert in parallel Idempotence: Bulk Upsert / Resumability: records per file 2つの問いの答えがそろった。並列 Bulk Upsert と、冪等性・ファイル単位の記録 87

Slide 88

Slide 88 text

The Overall Architecture 全体像はこう。分割バッチが解凍・分割して S3 に置き、取り込みバッチが 4並列で MySQL に Bulk Upsert する 88

Slide 89

Slide 89 text

The Bottleneck in the Implementation: a Huge Compressed CSV 実装で向き合ったボトルネックは、解凍前で約 3GBある巨大な圧縮 CSV。そのままメモリに載せると OOM に なる 89

Slide 90

Slide 90 text

Handling the Huge Compressed CSV: Read It Sequentially by Streaming ファイル全体をメモリに載せず、少しずつ読み込んで解凍・分割する(ストリーミング) 90

Slide 91

Slide 91 text

Agenda 3つ目、運用時に発生したボトルネックとの戦い 91

Slide 92

Slide 92 text

3. Fighting the Bottlenecks That Appeared in Production 運用で発生したボトルネックとの戦い 92

Slide 93

Slide 93 text

All Three Bottlenecks Happened in the Import Batch 答え合わせ:分割ファイル単位の取り込みが OOM に、並列取り込みが Deadlock と CPU 使用率の上昇に つながった。全部、取り込みバッチで起きた 93

Slide 94

Slide 94 text

Dealing with OOM 1つ目は OOM 94

Slide 95

Slide 95 text

Containers Killed by OOM ● ● ● What happened: ○ the import batch's ECS task ran out of memory... Analysis: ○ we ran a memory profiler the hard way and found the cause Cause: ○ we were loading the entire split CSV being imported into memory 取り込みバッチのメモリが足りなくなった。 Memory Profiler を地道に回したら、分割 CSVを丸ごとメモリに載 せていた 95

Slide 96

Slide 96 text

How We Fixed the OOM ● ● Read the split files in chunks Peak memory no longer depends on the size of the file 分割ファイルをチャンクで読むようにしたら、ピークメモリがファイルサイズに依存しなくなった 96

Slide 97

Slide 97 text

Dealing with Deadlocks 2つ目は Deadlock 97

Slide 98

Slide 98 text

Parallel Bulk Upsert Produced a Flood of Deadlocks ● ● ● What happened: ○ Frequent deadlocks during bulk upserts Analysis: ○ Investigated locks using performance_schema.data_locks Cause: ○ Under REPEATABLE READ, X locks on the PRIMARY supremum blocked each other's insert intention locks, causing circular waits 並列 Bulk Upsert で Deadlock が大量発生。 data_locks を見ると、REPEATABLE READ で supremum に X ロックが残っていた Scan the QR code to access the GitHub repository with the reproduction script! 98

Slide 99

Slide 99 text

How a Gap Lock Causes a Deadlock (One Example) 2つのトランザクションが末尾の gap にロックを持ったまま、お互いの INSERT を待って循環待ちになる https://dev.mysql.com/doc/refman/8.4/en/innodb-locking.html 99

Slide 100

Slide 100 text

How We Fixed the Deadlocks ● ● ● Lowered that Tx's isolation level: REPEATABLE READ → READ COMMITTED No more X lock on the supremum pseudo-record Downside: read skew can occur ○ Given our requirements, this problem cannot happen, so we accepted it 分離レベルを READ COMMITTED に変えて、supremum のロックを取らないようにした。読み取りスキュー は今回の要件では起きないので許容 https://speakerdeck.com/occhi/prevent-concurrency-problems-a-practical-introduction-to-transactions?slide=101 100

Slide 101

Slide 101 text

Changing the Transaction Isolation Level from the Default 変更は、トランザクションに isolation: :read_committed を足すだけ 101

Slide 102

Slide 102 text

As Is (REPEATABLE READ): an X Lock Stays on the PRIMARY Supremum REPEATABLE READ だと、PRIMARY の supremum に X ロックが残る 102

Slide 103

Slide 103 text

To Be (READ COMMITTED): the Lock on the Supremum Is Gone READ COMMITTED にすると、supremum へのロックが消える 103

Slide 104

Slide 104 text

CPU usage on the RDBMS keeps climbing 3つ目は、RDBMS の CPU 使用率の上昇 104

Slide 105

Slide 105 text

Rising CPU on the RDBMS ● ● What happened: ○ As write traffic from users grew, the Writer DB's CPU usage kept climbing What we found: ○ We were Bulk Upserting all 250M rows every month ○ Of each monthly import, fewer than 5 million rows actually need updating ユーザーの書き込みが増えるにつれて Writer の CPU が上がり続けた。毎月 2.5億件全部を書いていたが、 毎月の取り込みで実際に更新したいのは 500万件未満だった 105

Slide 106

Slide 106 text

How We Fixed the Rising CPU Usage ● ● Approach: ○ Narrowed down the Bulk Upsert targets ○ Changed DB writes from 4 parallel to serial Details ○ Narrowed down what to update inside the decompress-and-split batch, greatly reducing the amount written ■ Change in update targets: from all 250M rows to fewer than 5 million ○ This let the serial import finish within a day 分割バッチで更新対象を絞って、書き込む量を大幅に減らした。これで直列でも 1日以内に取り込めるように なった Happy to talk about how we chose what to update at the after-party! 106

Slide 107

Slide 107 text

Key Takeaways まとめ 107

Slide 108

Slide 108 text

Solve Even Complex Problems by Going Back to the Basics 複雑な課題も、基本に立ち返って解決しよう。要件・制約を整理し、ボトルネックを特定し、根拠をもって設計・ 実装を決める 108

Slide 109

Slide 109 text

Thank you for listening

Slide 110

Slide 110 text

Appendix: Aurora MySQL: Reads and Writes Were Both Checked by Measurement Requirement Conclusion Evidence Reads Under 500ms Our measurement (index lookup) Writes ~10K–50K rows/sec per writer Our reference test → Expected to meet the requirements 付録:MySQL は読み取りを実測済み。書き込みも参考計測から要件を満たせる見込み 110

Slide 111

Slide 111 text

Appendix: DynamoDB: Expected to Meet the Requirements, per the Official Docs Requirement Conclusion Evidence Reads One item by primary key (single-digit ms) AWS: GetItem / On-demand Writes Reference: 4,000 writes/sec AWS: On-demand capacity mode → Expected to meet them, per official sources 付録:DynamoDB は主キーで取れて、公式ドキュメント上は新しいテーブルでも毎秒 4,000件書ける 111

Slide 112

Slide 112 text

Appendix: Elasticsearch: Expected to Meet the Requirements, per the Official Docs and Benchmark Requirement Conclusion Evidence Reads One doc by ID; even complex searches P50 36ms, P99 316ms Get API / Stress test Writes Reference: ~51,700 docs/sec Elastic Search Labs: Serverless stress test → Expected to meet them, per official sources 付録:Elasticsearch も ID で取れて、公式ベンチマークでは秒間 5万件超の取り込み(条件は今回と違う) 112

Slide 113

Slide 113 text

Appendix: Cost Estimates at Selection Time (with Assumptions) ● ● ● ● ● All are estimates from selection time. Their assumptions differ, so totals are not compared side by side Aurora MySQL: storage and I/O added to the existing cluster ○ Expected to fit in $1,000/month; not yet calculated from measurements DynamoDB (update the existing table): ~$186/month for writes + storage ○ Assuming items of 1KB or less, 1 WRU each, ~260M items DynamoDB (S3 Import): priced on uncompressed size; ~$1.37 came from the compressed size and is not settled Elasticsearch (Elastic Cloud Serverless): ~$105/month ○ Search VCU ~$78, ingest ~$10, storage ~$17 (assumed VCU usage) 付録:選定時のコスト概算(前提条件つき) 113

Slide 114

Slide 114 text

Appendix: DynamoDB Trade-offs by Import Method 付録:DynamoDB の取り込み方式( S3 Import と API 書き込み)のトレードオフ 114

Slide 115

Slide 115 text

Appendix: Relational + JSON: Still One Row per Phone Number 付録:Relational + JSON なら、1電話番号 = 1行のまま載る 115

Slide 116

Slide 116 text

Appendix: Key-Value: Just GET by Phone Number 付録:Key-Value なら、電話番号で GET するだけ 116

Slide 117

Slide 117 text

Appendix: Document: The Data Model Fits as Is 付録:Document なら、決めたデータモデルがそのまま入る 117

Slide 118

Slide 118 text

Appendix: Graph: It Can Represent This, but Uses None of Its Strengths 付録:Graph でも表現はできるが、強みを使わない 118

Slide 119

Slide 119 text

Appendix: Premise: We Launch Batches in Two Ways, and Both Start ECS Tasks 付録:社内のバッチの起動方式は 2種類。どちらも ECS タスクを起動する 119

Slide 120

Slide 120 text

Appendix: We Launch with ① Scheduled Batches on Airflow 付録:起動方式は、 Airflow の定期バッチにした 120

Slide 121

Slide 121 text

Appendix: Airflow Controls the Order of the Two Batches 付録:Airflow で、分割バッチ → 取り込みバッチの順に実行する 121

Slide 122

Slide 122 text

Appendix: We Parallelize with Multiple Threads Managing start and completion Multiple processes Multiple threads Needs a separate mechanism Stays within one process's thread pool → We chose threads: management stays inside the import batch 付録:並列化の手段は複数スレッドにした。管理が取り込みバッチの中で完結するから 122

Slide 123

Slide 123 text

Appendix: We Set the Degree of Parallelism by Measuring Import Time and DB Load 3 parallel 4 parallel Import time ~22 hours ~14 hours DB CPU usage ~10% higher ~15% higher → Balancing import time and DB load, we chose 4 付録:並列度は、取り込み時間と DB 負荷を計測して 4並列にした 123

Slide 124

Slide 124 text

Appendix: The Import Batch Implementation (Excerpt) 付録:取り込みバッチの実装(抜粋) 124