Skip to content

7fantasy7/not-sale

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

NOT Back Contest - Flash Sale Service

For https://contest.notco.in/dev-backend

            ##                            ( )_
           #||#            ___      _ //  |  _)   ___    _ //  (_)   ___
          # || #         /  _  \  / _//\  | |   / ___) / _//\  | | /  _  \
         #  ||  #        | ( ) | ( (//  ) | |_ ( (___ ( (//  ) | | | ( ) |
        ##  ||  ##       (_) (_)  \//__/   \__) \____) \//__/  (_) (_) (_)
       ##   ||   ##               //                   //                 
      ##    ||    ##                    
     ##     ||     ##    Probably nothing.             
    ##################   

About

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.

Task Description

  1. Every hour, a new flash sale of 10,000 items begins. Each item’s name and image are generated at runtime
  2. 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 /health that 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

Technical details

Assumptions (beyond the brief)

  • 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.

Libraries and Technologies

  • 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

Building and Running

Prerequisites

  • Docker
  • Docker Compose

Build and Run

  1. Clone the repository:
git clone https://github.com/7fantasy7/not-sale.git
cd not-sale-back
  1. Start the services using Docker Compose:
docker-compose up -d --build

You should have all services up and running:
compose.png

The docker-compose configuration includes health checks for PostgreSQL and Redis services, and the application will only start after these dependencies are healthy.

  1. The service will be available at http://localhost:8080

  2. You can check the application health status at http://localhost:8080/health

Performance Testing

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.

k6 Performance Tests

Project uses k6 for performance testing, as it allows scripting to verify real user scenarios (checkout + purchase flow)

To run the performance tests:

Install k6

macOS
brew install k6

Other platforms

see installation instructions and performance-specific README

Run the tests:

K6_WEB_DASHBOARD=true K6_WEB_DASHBOARD_EXPORT=html-report.html k6 run dev/perf.js

View the results

Open the generated dev/html-report.html file in your web browser to view the performance test results.

License

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.

About

Not sale

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors