For https://contest.notco.in/dev-backend
## ( )_
#||# ___ _ // | _) ___ _ // (_) ___
# || # / _ \ / _//\ | | / ___) / _//\ | | / _ \
# || # | ( ) | ( (// ) | |_ ( (___ ( (// ) | | | ( ) |
## || ## (_) (_) \//__/ \__) \____) \//__/ (_) (_) (_)
## || ## // //
## || ##
## || ## Probably nothing.
##################
A high-throughput flash-sale service built with Go, Redis, and PostgreSQL.
This service sells exactly 10,000 items every hour,
with a two-step purchase process to ensure reliability and prevent overselling.
- Every hour, a new flash sale of 10,000 items begins. Each item’s name and image are generated at runtime
- Implement a sale flow that:
- Receives a first request:
POST /checkout?user_id=%user_id%&id=%item_id%,
generates a unique code for the user and returns the code. All checkout attempts must be persisted - Receives a second request:
POST /purchase?code=%code%, verifies the code, and "sells" the item to the user - Provides a health endpoint at
GET /healththat checks the status of database and Redis connections - Ensures exactly 10,000 items are sold each sale—no over‑ or underselling
- Limits each user to a maximum of 10 items per sale
- Even next sale is started, the previous sale is still active until there are items available.
- User can checkout more then 10 times on the same sale, but only 10 items can be purchased.
Imaging going back or refreshing the page scenario. - The service doesn't contain a state for performance "hacks", assuming it can be run in clustered mode.
- 10,000 items are not that big of a deal, so preferring strong consistency over performance.
- Not overcomplicating the code with unnecessary abstractions, keeping it simple and readable.
- Go-chi router - A lightweight, idiomatic and composable router for building Go HTTP services.
- Go-redis — Redis client for Go
- Pgx — PostgreSQL driver and toolkit for Go, best choice even it brings more deps, as lig/pq is not well supported
- Docker
- Docker Compose
- Clone the repository:
git clone https://github.com/7fantasy7/not-sale.git
cd not-sale-back- Start the services using Docker Compose:
docker-compose up -d --buildYou should have all services up and running:

The docker-compose configuration includes health checks for PostgreSQL and Redis services, and the application will only start after these dependencies are healthy.
-
The service will be available at http://localhost:8080
-
You can check the application health status at http://localhost:8080/health
The project includes performance tests to evaluate the application's behavior under load. These tests focus on the checkout and purchase flow, simulating multiple concurrent users.
Project uses k6 for performance testing, as it allows scripting to verify real user scenarios (checkout + purchase flow)
brew install k6see installation instructions and performance-specific README
K6_WEB_DASHBOARD=true K6_WEB_DASHBOARD_EXPORT=html-report.html k6 run dev/perf.jsOpen the generated dev/html-report.html file in your web browser to view the performance test results.
This project is licensed under the Apache License 2.0. You are free to use, modify, and distribute this software for any purpose, including commercial applications, as long as you comply with the terms of the license.