Subscription Management Service
A Go backend for recurring subscriptions, built so a subscription only moves through valid lifecycle states and every request and background task can be traced end to end.
What It Is
A backend for recurring subscriptions: sign-up, billing, renewals, reminders and expiry. The HTTP API handles what users do, and a scheduler with Asynq workers on Redis handles what happens over time. MongoDB stores the data. OpenTelemetry traces every request and background task to Jaeger, metrics go to Prometheus and logs to Loki, all linked in Grafana.
Why It Exists
Subscription bugs cost real money: a user charged after canceling, or a subscription renewed twice. I built this to rule those out in the design, through the compiler where Go allows it and through state checks everywhere else, and so that any failure can be followed from the request or task that caused it.
Key Decisions
- API-Facing and Worker-Facing Interfaces — Each service exposes an
Externalinterface for the API and anInternalone for the scheduler and workers. HTTP controllers only getExternal, so the compiler keeps them from calling renewal or expiry. - State Checks on Every Transition — Only active subscriptions can be canceled or renewed, only canceled ones can expire, and only expired ones can be deleted. Workers re-check state before acting, and a renewal is refused once the next period is already billed, so a retried task can't renew twice.
- Transactions Without the Driver in the Domain — Creating or renewing a subscription writes its bill and the subscription in one MongoDB transaction through an injected
TxnFn. Services never import themongopackage, and their unit tests run without a database. - Trace Context Across the Queue — The scheduler writes W3C trace context into each task's headers, and the worker middleware continues it, so one trace follows a renewal from the scheduler tick through the worker.
- Fail-Open Rate Limiting — Per-IP limits live in Redis. When Redis is down, requests still go through; the error is recorded on the request's span, and an atomic
CompareAndSwapguard logs it at most once a minute instead of on every request. - Client IP Behind Proxies — If
RemoteAddris a public address, forwarding headers are ignored. Behind a proxy,X-Forwarded-Foris read right to left and the first public address wins, so entries a client adds on the left don't count.
Trade-offs & Scope Management
Choices I made on purpose, and what each one costs:
- Modular Monolith, Not Microservices — Domains talk through interfaces, so they can be split out later. Until then, one binary avoids network hops and the cost of running several services.
- Availability Over Strict Rate Limits — Failing open means a Redis outage lets unlimited traffic through while it lasts. For this API, staying up matters more.
- Real MongoDB in Integration Tests — Testcontainers needs Docker and takes about 15 seconds, against about 3 for the unit tests, but query filters and sort orders are checked against the real database, not my assumptions about the driver.
- Cross-Domain Reads Through Services — The user service asks the subscription service, never its repository, so each domain keeps its schema to itself. The cost is an extra interface method for each read another domain needs.
Go · Asynq · Redis · MongoDB · OpenTelemetry · Prometheus · Jaeger · Loki · Grafana · Testcontainers

