Kodeloka
  1. Kodeloka
  2. Kelas Microservices
  3. Materi

Gateway & Routing · Modul 14 dari 24

Belajar Microservices - Backend for Frontend (BFF)

Pattern Backend for Frontend (BFF) di mana gateway khusus dibuat sesuai kebutuhan klien berbeda (Web Desktop, Mobile Android/iOS, Third Party API).

1. Masalah Single Generic Gateway

Aplikasi Web Desktop memiliki layar besar dan koneksi WiFi cepat (membutuhkan data JSON lengkap). Sebaliknya, Aplikasi Mobile (iOS/Android) memiliki layar kecil dan koneksi seluler 4G/5G terbatas (membutuhkan payload JSON sangat ringkas untuk menghemat kuota & baterai).

2. Solusi: Backend for Frontend (BFF) Pattern

Daripada membuat 1 API Gateway universal yang memaksa kompromi, BFF Pattern membuat server gateway yang disesuaikan khusus untuk masing-masing tipe client:

  • Web BFF: Mengembalikan data lengkap beserta komponen HTML/rich widget.
  • Mobile BFF: Melakukan agregasi data, mengkompresi gambar, dan menghapus field yang tidak dipakai di layar HP.
  • Third-Party Public BFF: Dilengkapi rate limiter ketat dan skema autentikasi API Key khusus pihak ketiga.
Arsitektur BFF Pattern
[ Web App ]  ────────> [ Web BFF Server ] ──────┐
                                                  │
[ Mobile App ] ──────> [ Mobile BFF Server ] ───┼──> [ Internal Microservices ]
                                                  │    (Order, User, Payment, etc.)
[ Third Party ] ─────> [ Public API BFF ] ──────┘