Skip to content

emedvede/zilch-microservice

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 

Repository files navigation

ZILCH CARD MICROSERVICE

A microservice which accepts incoming Purchases against Zilch Cards and manages Transactions.

Description

A Rest API to access Zilch Card accounts with the current balance of a user. Purchase can be executed against Zilch Card what will create 4 Transactions. The amount in Purchase will be equally distributed against all 4 transactions. Each Transaction will be saved within a week difference Due Date starting from the first which has a due date of Purchase request.

The balance of Zilch Card can be modified by registering transactions on the account, either debit transactions (removing funds) or credit transactions (adding funds).

A Purchase will only succeed if there are sufficient funds on Zilch Card for the first transaction (balance - Purchase_amount/4 >= 0).

Unique global transaction id must be provided when registering a transaction on an account.

Unique global Purchase id must be provided when creating a Purchase.

It is also possible get current balance of a Zilch Card and get transactions.

Api requirements and running instructions

  1. Java 8
  2. Maven 3 to build the application. This will activate Flyway to run db creation scripts.
  3. Download and install PostgreSQL server (version 9.5 or higher). It is available here: https://www.postgresql.org/download/

Client can be installed from https://www.pgadmin.org/download/ for example

  1. Connect to the Postgres server and create database - zilch.
  2. For the database zilch create schema - schema_zilch, using SQL:
CREATE SCHEMA schema_zilch AUTHORIZATION postgres;

The user, schema and database can be different, but then it needs to be configured in the

zilch-microservice/src/main/resources/application.properties

See

spring.datasource.url = jdbc:postgresql://localhost:5432/zilch
spring.datasource.username = postgres
spring.datasource.password = postgres
spring.datasource.driver-class-name=org.postgresql.Driver
spring.jpa.properties.hibernate.default_schema = schema_zilch
  1. From the root folder of the application run:
mvn clean install
  1. Start application by:
mvn spring-boot:run

All tables, reference data and some dummy data will be created and inserted by Flyway.

Or instead you can run the jar file and run scripts manually against schema_card:

java -jar target/zilch-microservice-1.0-SNAPSHOT.jar
  1. To check that application started successfully go to:
http://localhost:8080/zilch/test

This should produce result:

Hello from Zilch microservice!

The port can be also configured from

zilch-microservice/src/main/resources/application.properties

see 'server.port' property

Api endpoints

Examples of all requests can be found in:

zilch-microservice/src/main/resources/examples/Zilch-microservice.postman_collection.json

Http GET endpoints:

  1. http://localhost:8080/zilch/cards Gets all cards Some cards are generated after the first start of the application by Flyway.

  2. http://localhost:8080/zilch/cards/{id} Gets card with id={id}

  3. http://localhost:8080/zilch/cards/user?userId={user} Gets list of cards by user

  4. http://localhost:8080/zilch/cards/{id}/transactions Gets list of transactions by card id Some transactions are generated after the first start of the application by Flyway.

  5. http://localhost:8080/zilch/purchases Lists all purchases

  6. http://localhost:8080/zilch/cards/{id}/purchases Lists purchases by cardId

Http POST endpoints:

  1. http://localhost:8080/zilch/cards With the following JSON in the body:
{
"currency":"{currency}",
"userId":"{userId}"
}

Creates new Zilch card. e.g.

{
"currency":"GBP",
"userId":"new-user"
}

Will create new card with currency GBP and userId=new-user. The currency id should be present in the reference table 'currency'.

  1. http://localhost:8080/zilch/purchases With the following JSON in the body:
{
"globalId":"{id}",
"shopId": "{shopId}",
"currency":"{currency}",
"cardId": "{cardId}",
"amount":"{amount}",
"description":"description"}

Creates new Purchase and associates 4 Debit transactions with it with equal amount. Purchase amount is equally spread among 4 transactions. One transactions will be executed against Zilch Card. Three other transactions will be scheduled for the future 3 weeks. In case there is not enough funds on Zilch card all transactions and Purchase will be discaded. e.g.

{
"globalId":"444",
"shopId": "ZARA",
"currency":"GBP",
"cardId": "2",
"amount":"100",
"description":"bought trousers"}
  1. http://localhost:8080/zilch/transactions With the following JSON in the body:
{"globalId":"557",
"currency":"GBP",
"cardId": "2",
"amount":"20",
"transactionTypeId":"C",
"description":"add money"
}

for credit transaction.

{"globalId":"558",
"currency":"GBP",
"cardId": "2",
"amount":"20",
"transactionTypeId":"D",
"description":"add money"
}

for debit transaction. Creates transaction. 'globalId' must be unique. 'currency','transactionTypeId' and 'cardId' must be present in the db. 'currency' should be the same as in card.

Technology used

  • PostgreSQL database, which has good concurrency support, also has ACID compliance and can be replicated.
  • Spring Boot, including Spring Data JPA for JPA based repositories.
  • Undertow, a web server to run the application.
  • Flyway,tool db migration
  • logback + slf4j for logging.
  • Gson from com.google.code.gson to serialize objects to JSON.

Support of the aspects:

  1. Transactions on service and repository level ensure atomicity.
  2. Identifiers for entities and primary keys in the db ensure idempotency. As well as unique globalIds for card transactions.
  3. Scalability: This can be solved by installing card microservice application on several hosts, with Postgres server running on it own host/hosts(can be distributed). Load balancer (e.g. NGINX) will share requests between the application instances and provide high-availability. Shared cache can be configured for Spring application. For example, Spring supports Reddis, which can be used as shared cache. But since this application is targeted on transactions creation, it might not be useful, because we have to update cache too often.
  4. Concurrency: Postgres has good concurrency support. Undertow threads count can be configured in the application.properties.
server.undertow.worker-threads
server.undertow.io-threads

This numbers should be configured based on the properties of particular host, where the application runs. In the application there are no shared objects, so there should not be concurrency issues.

Features not implemented

  1. Security (Information Exchange)

  2. Not full test coverage, especially for Purchase workflow

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published