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.
- Concurrency Control: PostgreSQL row-level locking with the
NOWAIToption prevents deadlocks and ensures data consistency. - Idempotency: Idempotency keys handle duplicate requests gracefully.
- Atomic Transactions: All operations are atomic within transactions, maintaining integrity.
git clone https://github.com/ftteh/golang-wallet-api
cd golang-wallet-apiEnsure Docker and Docker Compose are installed. You can download them from:
https://www.docker.com/products/docker-desktop
docker compose upThis starts the wallet service along with PostgreSQL.
- API Base URL:
http://localhost:8080 - Swagger Docs:
http://localhost:8080/swagger/index.html
docker compose downgo test -v ./...Use Swagger UI at http://localhost:8080/swagger/index.html to interact with the API.
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.
- 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.
- Host:
localhost - Port:
5433 - Maintenance database:
walletapp - Username:
walletuser - Password:
walletpass
docker exec -it wallet_postgres bash -c "psql -U walletuser -d walletapp"This opens a PostgreSQL shell inside the container.
- 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.