Webhook

 Webhook? 

Easy example:

- Delivery arrival notification: When deliveries arrive, the notification is sent
- Bank notification: When a transaction is made, the notification is sent

These are the principle of "Webhook"!

Technical definition -> Reverse API or Event-driven HTTP callback

When a certain event happens, an external service sends an HTTP request to our server to notify us in real time via a webhook!


Traditional way: Polling

fun checkPaymentStatus() {

    while (true) {

        val status = stripeApi.getPaymentStatus(paymentId)  // API calling

        if (status == "completed") {

            processPayment()

            break

        }

        Thread.sleep(5000)  // check every 5 secs

    }

}

problem

- wasting recourses - keep calling API
-  lack of real-time - 5 secs delay
- API limited - Rate limit excess risk
- cost increasement - charge per API calling


Modern way: Webhook 

@PostMapping("/webhook/stripe")

  fun handleStripeWebhook(@RequestBody payload: String) {

      // Stripe calls this endpoint when event is driven

      val event = parseEvent(payload)

      if (event.type == "payment_intent.succeeded") {

          processPayment()

      }

  }

Pros

- real time - event immediate notification
- efficiency - only when needed
- scalable
- cost reduction - minimize API calling



Comments

Popular posts from this blog

@ModelAttribute vs @RequestBody in Validation

Side Project(a self-imposed 3-day "Hackathon" challenge)

Google: The King is Back