title: How to build a simple but reliable API Prepaid Credit System for a microservice stack?
body: <p>We've build a microservice based API product with many different REST endpoints. The api customer should be charged differently regarding to his usage of the different endpoints. We're primary using spring-boot for our services with some netflix-oss components within a kubernetes cluster.</p>
  
  <p>For example: Creating a job with <code>n</code> tasks via the job endpoint <code>/jobs</code> should cost <code>n * job_creation_credit_cost</code> credits. So it can't be done by just tracking the http calls within our API gateway in front of all microservices. </p>
  
  <p>We would like to have some kind of <code>acquire</code> in each microservice, based on the users id or client_id (we are using OAuth2 with OIDC and JWT token) that returns wether the user has enough credits to do the action and if yes, acquire the needed credits for this action.</p>
  
  <p>Since we're in an api context with automated access there can be thousands of parallel requests. So the acquire steps needs to be fast. It should also be possible to refund credits if for example a job fails afterwards (we can't execute the job).</p>
  
  <p>I had some thinkings about the whole thing and thought about building a central microservice that is responsible for credits/auditing and which holds the current credit balances for each customer. This service should have some REST endpoints for acquire and also a connection to a message broker like RabbitMQ (because we are already using it internally).</p>
  
  <p>Now every service like <code>job-service</code> could call the <code>credit-service</code> by http(all actions possible) or message broker(only for charging, not for asking for acquire). This <em>credit-service</em> should be connected to a mongodb and maybe also a redis as cache layer to improve performance for some actions where it is possible so that the http endpoints can answer as fast as possible (since we're blocking the customers request).</p>
  
  <p>Also a point to think about are <strong>optimistic locking exceptions</strong> that might occur in mongodb context if we have much concurrent read/write to some balances and also the usage of http protocol. Maybe http is too slow and we should use something different.</p>
  
  <p>So how would you build such credit/balance-system? Maybe you also know about some open-source solutions or examples?</p>