stdsquare²
🎓 Kelas
stdsquare / materi / slides / pertemuan-08
Tema
Japan
Arcade
Dark Retro
Font
‹ Daftar slide Pertemuan 8: Database & Serverless (RDS, Aurora, DynamoDB, Lambda)
PEBD6027 · RPS minggu 9 · 2x50 menit

Database & Serverless

RDS · DynamoDB · Aurora · Lambda · Event-driven Architecture

Peta Pembelajaran Hari Ini

Sub-CPMK 8: memilih database cloud dan menerapkan fungsi serverless. Empat indikator.

Jam ke-1
  • RDS PostgreSQL & MySQL managed
  • DynamoDB NoSQL key-value
  • Aurora MySQL & PostgreSQL
Jam ke-2
  • Lambda event-driven function
  • API Gateway + Lambda = REST serverless
  • Hitung biaya Lambda vs EC2
Posisi 14P: setelah networking VPC, hari ini database & serverless. P9 IAM & security.

Database di Cloud: Managed vs Self-Hosted

Cloud mengubah database operations: dari install-sendiri ke managed service. Trade-off cost vs effort.

RDS Uptime SLA
99,95%
Multi-AZ RDS — 4,3 jam downtime maks per tahun
DBA Time Saved
~70%
Studi TODOgroup 2022 (dihedge) — DBA fokus schema bukan ops
Self-hosted DB di EC2 = full control tapi full responsibility: backup, patching, replication. Bank compliance POJK sulit tanpa DBA dedicated.
Bagian 1 · 1/4
Relational Database (RDS)
Diskusi: bila startup fintech butuh transaksi konsisten (ACID) untuk saldo nasabah, pilih RDS atau DynamoDB?

RDS PostgreSQL & MySQL

RDS = managed relational database. PostgreSQL, MySQL, MariaDB, Oracle, SQL Server.

Yang Dikelola AWS
  • Installation & OS patching
  • Automated backup point-in-time
  • Multi-AZ synchronous replication
  • Read replica untuk scaling read
Yang Anda Lakukan
  • Schema design
  • Index optimization
  • Query tuning
  • Connection pooling (PgBouncer)
RDS hemat DBA ops ~70% tapi schema dan query tetap tanggung jawab developer.

Aurora: MySQL & PostgreSQL Cloud-Native

Aurora = engine cloud-native AWS, kompatibel MySQL/PostgreSQL tapi 5x performa (klaim AWS).

Aurora MySQL/PostgreSQL
  • Storage distributed 6 copy/3 AZ
  • Auto-healing storage layer
  • 15 read replica vs 5 di RDS
  • Up to 5x throughput MySQL standar
Aurora Serverless v2
  • Scale otomatis 0,5-128 ACU
  • Cocok beban variable/intermittent
  • Hemat cost beban rendah
  • Multi-AZ support
Aurora lebih mahal per jam dari RDS tapi storage cluster shared. Untuk skala besar, TCO lebih rendah.

Studi Kasus: Tokopedia Migrasi ke Aurora

Tokopedia migrasi core DB dari RDS MySQL ke Aurora untuk skala Harbolnas.

Public case study AWS Tokopedia: pre-Aurora, RDS MySQL skala besar menghadapi bottleneck saat Harbolnas (11.11, 12.12). Migrasi ke Aurora MySQL: read replica 5 ke 15, storage distributed, auto-healing. Hasil (dihedge): throughput Harbolnas naik ~3-5x, failover recovery 30s (vs manual 10-15 mnt), DBA fokus schema bukan ops.
Pra-Aurora
  • RDS MySQL bottleneck
  • Manual failover 10-15 menit
  • 5 read replica max
  • Maintenance window tight
Pasca-Aurora
  • Throughput 3-5x naik
  • Failover otomatis 30 detik
  • 15 read replica
  • Storage auto-scaling
Pelajaran Bisdig: pemilihan database bukan teknis semata — biaya downtime Harbolnas jauh lebih besar dari premium Aurora.
Bagian 2 · 2/4
NoSQL DynamoDB
Diskusi: bila Gojek butuh simpan lokasi driver real-time update 1000x/detik per driver, pilih DB apa?

DynamoDB Anatomi

DynamoDB = NoSQL key-value fully managed. Skala massive, latency milidetik.

Konsep Inti
  • Table: kumpulan item
  • Item: 1 row (max 400KB)
  • Partition key (wajib)
  • Sort key (opsional, untuk composite)
Karakteristik
  • Latency single-digit milidetik
  • Throughput unlimited (auto-scale)
  • No schema (NoSQL)
  • TTL item otomatis
DynamoDB cocok: session, IoT data, gaming leaderboard, real-time analytics. Tidak cocok: transaksi ACID multi-table.

DynamoDB Capacity Mode

Dua mode: provisioned (manual) atau on-demand (auto). Trade-off cost vs simplicity.

AspekProvisionedOn-Demand
Cost ModelRCU/WCU per jam (cheap saat steady)Per request (murah saat variable)
Auto-scalingYa, tapi capacity planBuilt-in, instant
Best ForBeban predictable/steadyBeban unpredictable/spiky
ContohBackend aplikasi internalHarbolnas spike, IoT burst
Hemat CostBeban konstan: 3-5xBeban spiky: 30-50%
Mulai dari on-demand (murah & simple), optimize ke provisioned saat beban stabil terlihat.

Hitung dari Nol: DynamoDB vs RDS untuk Session Store

Aplikasi web 100K MAU, 1M session/bulan, 1KB per session, TTL 24 jam. DynamoDB vs RDS?

KomponenDynamoDB On-DemandRDS db.t4g.micro
Write 1M/bulan1M WRU ÷ 1000 × $1,25$1,25
Read 5M/bulan5M RRU ÷ 1000 × $0,25$1,25
Storage 100 GB100 × $0,25$25/bulan
Server cost$15/bulan (instance)
Multi-AZBuilt-in+100% (multi-AZ RDS)
BackupBuilt-in PITR+20% storage
Total$27,50/bln$45-60/bln
Latency read5-10 ms (single-digit)2-20 ms (bervariasi)
Harga AWS public (dihedge). DynamoDB menang di latency konsisten + built-in scaling, RDS menang di query SQL kompleks.
Bagian 3 · 3/4
Lambda Serverless
Diskusi: bila startup punya cron job image resize 1000x/hari, pilih EC2 atau Lambda?

Lambda Anatomi

Lambda = function serverless. Trigger oleh event, auto-scale, pay-per-use.

Konsep Inti
  • Function: kode + runtime
  • Handler: entry point function
  • Trigger: event source (S3, API GW)
  • Execution: max 15 menit
Karakteristik
  • Auto-scale 0 → ribuan concurrent
  • Cold start 100-500ms (Java lebih lama)
  • Stateless (gunakan DynamoDB/S3)
  • Memory 128 MB - 10 GB
Lambda cocok: event-driven, burst, intermittent. Tidak cocok: long-running, stateful, WebSocket.

API Gateway + Lambda = REST Serverless

Pola paling populer serverless: API Gateway routing ke Lambda function.

API Gateway
  • Routing HTTP/REST/HTTP API
  • Throttling & rate limit
  • Authentication (Cognito, Lambda Authorizer)
  • WAF integration untuk security
Lambda Handler
  • Parse request event
  • Business logic
  • Query DynamoDB/RDS
  • Return response JSON
Pola ini dipakai Gojek untuk webhook event handler dan Tokopedia untuk image upload trigger.

Event-Driven Architecture: SQS, SNS, EventBridge

Event-driven = komponen terpisah, async via message bus. Loose coupling, scalable.

SQS (Queue)
  • Message queue pull-based
  • Decouple producer/consumer
  • At-least-once delivery
  • Use: order processing async
SNS (Topic)
  • Pub/sub push-based
  • 1 fan-out ke banyak subscriber
  • Filter via attribute
  • Use: notification broadcast
EventBridge
  • Event bus central
  • Schema registry
  • Cross-service routing
  • Use: workflow orchestration
Event-driven = loose coupling + scalable. Trade-off: debugging sulit, eventual consistency.
Bagian 4 · 4/4
Penutup & Pemilihan Teknologi
Diskusi: bila startup fintech baru mulai dengan 10 user, pilih apa semua stack dari hari ini?

Lambda Pricing Model

Lambda charge per request + duration. Free tier sangat generous untuk startup.

KomponenFree TierPaid Tier
Request1 juta/bulan gratis$0,20 per 1 juta
Compute (GB-detik)400K GB-detik/bln$1,00 per 1 juta GB-detik
Contoh 1M req 1s 128MBGratis semua1M × 128/1024 × 1s × $1/1M = $0,125
Contoh 10M req 5s 256MB$3,05 + $3,125 = $6,17/bln
Provisioned Concurrency$0,0000156/GB-detik (slalu on)
Lambda murah untuk beban spiky. Untuk beban konstan 24/7, EC2/Fargate sering lebih murah.

Pemilihan Database: Decision Framework

Tidak ada satu DB untuk semua. Framework pemilihan berdasar use case.

Use CasePilihan UtamaAlasan
Transaksi ACID (saldo, order)RDS PostgreSQL / AuroraACID multi-table konsisten
Session/CookieElastiCache RedisSub-ms latency, TTL built-in
Lokasi real-time (Gojek)DynamoDB / Redis GeoMassive write, single-digit ms
Document JSON fleksibelDynamoDB / DocumentDBSchema-less, scale horizontal
Data lake analyticsS3 + AthenaPetabyte, pay-per-query
Search full-textOpenSearch ServiceInverted index, ranking
Aturan Bisdig: pilih yang paling sederhana yang bisa scale. Migration DB mahal, jangan premature optimize.

Lambda Cold Start & Mitigasi

Cold start = latency 100-500ms saat Lambda instantiate baru. Penting dipahami untuk latensi-sensitive.

Penyebab Cold Start
  • Function baru pertama invoke
  • Setelah idle beberapa menit
  • Java/C# lebih lama (JVM startup)
  • VPC-enabled Lambda dulu lebih lama (kini OK)
Mitigasi
  • Provisioned Concurrency (warm)
  • Smaller package (Java ggrave), Python cepat
  • SnapStart Java (Amazon 2023)
  • EventBridge Scheduler keep-warm
Latensi kritis (API mobile user): provisioned concurrency wajib. Latensi long-tail OK: on-demand cukup.

Ringkasan Kunci Pertemuan 8

Hari ini Anda menguasai database cloud (relational + NoSQL) dan Lambda serverless.

Relational vs NoSQL
RDS Aurora untuk ACID. DynamoDB untuk massive throughput.
Lambda
Event-driven, pay-per-request. Cocok burst/intermittent, bukan konstan.
Event-Driven
SQS queue + SNS pub/sub + EventBridge bus = loose coupling.

Tiga lensa P8: ACID vs throughput · Lambda pay-per-request · event-driven loose coupling.

Persiapan P9: IAM & Security. Baca Wittig bab 5 IAM. Kuis 5 menit soal RDS multi-AZ vs Aurora.