Skip to content

ftteh/golang-wallet-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Wallet Service

A Go-based wallet service offering APIs for managing user wallets, including deposit, withdrawal, and fund transfers. Built using the Gin framework and PostgreSQL, with a focus on concurrency, data integrity, and idempotency.

Overview

Design Highlights

  • Concurrency Control: PostgreSQL row-level locking with the NOWAIT option prevents deadlocks and ensures data consistency.
  • Idempotency: Idempotency keys handle duplicate requests gracefully.
  • Atomic Transactions: All operations are atomic within transactions, maintaining integrity.

Setup & Running the Service

1. Clone the Repository

git clone https://github.com/ftteh/golang-wallet-api
cd golang-wallet-api

2. Install Docker & Docker Compose

Ensure Docker and Docker Compose are installed. You can download them from:

https://www.docker.com/products/docker-desktop

3. Start Services

docker compose up

This starts the wallet service along with PostgreSQL.

4. Access the API

  • API Base URL: http://localhost:8080
  • Swagger Docs: http://localhost:8080/swagger/index.html

5. Stop Services

docker compose down

Testing

Automated Testing

go test -v ./...

Manual Testing

Use Swagger UI at http://localhost:8080/swagger/index.html to interact with the API.

Code Review Guidance

Focus on the following:

  • Concurrency Handling: Implementation of row-level locking and retry logic.
  • Idempotency: Correct use of idempotency keys.
  • Code Structure: Organization, clarity, and maintainability of the code.

Concurrency Handling – Technical Explanation

  • Database Row Locking: Uses GORM's clause.Locking{Strength: "UPDATE"} to lock rows during transactions.
  • NOWAIT Option: Fails fast when encountering locked rows to prevent deadlocks.
  • Retry Logic: Retries transactions that fail due to locks using exponential backoff.
  • Idempotency Keys: Prevents duplicate transactions by associating a unique key with each request.
  • Atomic Transactions: Ensures that all steps in a transaction either succeed together or fail together.

These techniques ensure the service remains safe and consistent under high concurrency.

Accessing the Database

Option 1: Using pgAdmin

  • Host: localhost
  • Port: 5433
  • Maintenance database: walletapp
  • Username: walletuser
  • Password: walletpass

Option 2: Using psql via Docker

docker exec -it wallet_postgres bash -c "psql -U walletuser -d walletapp"

This opens a PostgreSQL shell inside the container.

Areas for Improvement

  • Increase test coverage, especially for edge cases and errors.
  • Improve logging for easier debugging.
  • Add authentication and authorization to secure API endpoints.
  • Implement encryption where appropriate.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published