Payments · Resilience

A mobile-money gateway that fails safely

Rebuilding the service that moves customer deposits and withdrawals through the MTN MoMo Open API, so that a slow or failing provider can no longer take the platform down with it — or lose a payment callback.

Client
XENO Investment
Role
Core engineer on the rebuild and the platform migration
Period
2022 — 2026
Status
In production
  • Java 25
  • Spring Boot 4.1
  • Resilience4j
  • Kafka
  • Redis
  • k6
  • WireMock
>90%of client deposits now arrive through the MTN and Airtel mobile-money integrations.

01 Problem

Every deposit and withdrawal in the investment platform ultimately becomes a call to a mobile-money provider. The original client was written for the happy path: no connect or read timeouts, no retry policy, no circuit breaker, and a fresh OAuth token fetched on every single call.

When the provider slowed down, request threads queued behind it and the slowness spread upstream. When the message broker was briefly unavailable, provider callbacks — the only signal that money had actually moved — could be dropped.

02 Constraints

  • Money-moving calls must never be duplicated by an automatic retry.
  • Downstream consumers already depend on existing Kafka topics and payload shapes; they could not change in lockstep.
  • Several tenants and products share the gateway; one misbehaving product must not starve the others.
  • Customer phone numbers and request bodies must stay out of logs.
  • The provider was moving customers to a new platform during the rebuild.

03 Architecture

Request and callback path
  1. 01
    Platform requestDeposit or withdrawal with a unique reference ID; duplicates rejected with 409.
  2. 02
    Token cacheOAuth tokens cached and refreshed ahead of expiry instead of per call.
  3. 03
    Bulkhead + breakerIsolated per tenant × product; 2 s connect, 10 s read budgets.
  4. 04
    Provider APINew platform primary, legacy platform as fallback.
  5. 05
    Callback → outboxCallback persisted to a Redis outbox before publishing.
  6. 06
    KafkaIdempotent producer, acks=all, existing topics and payloads byte-for-byte.
  7. 07
    Republish + reconcilePending events republished; status pulled for anything unconfirmed.

04 Decisions

No automatic retries on POST

Chose
Writes are attempted once and reconciled; only idempotent GETs retry, once, with backoff.
Instead of
Retry everything with exponential backoff.
Why
A retried disbursement that the provider actually processed is a double payment. Reconciliation is slower but never wrong.

Isolation per tenant × product

Chose
Separate circuit breaker and bulkhead instances keyed by tenant and product.
Instead of
One global breaker for the provider.
Why
A global breaker turns one product's outage into everyone's outage. Fine-grained isolation keeps healthy flows moving.

Outbox before broker

Chose
Persist each callback to an outbox, publish asynchronously, and republish anything left pending.
Instead of
Publish directly to Kafka inside the callback handler.
Why
The callback is the source of truth that money moved; it must survive a broker outage rather than depend on one.

Byte-for-byte topic compatibility

Chose
Keep existing topics and payloads exactly, enforced by tests.
Instead of
Introduce new, cleaner event contracts.
Why
Lets the gateway be replaced independently of every consumer — the migration ships without a coordinated release.

05 Implementation

  • Rewrote the service from Groovy on Spring Boot 3.3 to Java 25 on Spring Boot 4.1, with the full design captured in a written specification.
  • Built a provider client with cached tokens, explicit timeouts, Resilience4j breakers and bulkheads, and translation of provider errors into a stable internal error model.
  • Added a Redis-backed outbox, a scheduled republish job for pending events, and a reconciliation pull for transactions without a final status.
  • Removed sensitive values from logs and kept callbacks outside tenant filters so the provider can always reach them.
  • Set up k6 scenarios against WireMock stubs with a p99 latency threshold to exercise the client under load.

06 Outcomes

  • More than 90% of client deposits now arrive through the MTN and Airtel mobile-money integrations.
  • Production traffic moved onto the provider's new platform, with the previous platform retained as a fallback.
  • Callback events are published on the existing topics with identical payloads, so no downstream service had to change.
  • A slow provider now consumes a bounded slice of capacity for one tenant and product instead of the whole service's thread pool.

Outcomes are limited to what I can verify and share. Internal names, partners, infrastructure details and production data are deliberately omitted.