Sobes.tech
Middle — Senior

Enhancing the resilience of a high-traffic service

livecode

Task condition

It is necessary to develop a overload protection mechanism for a service capable of processing up to 500,000 requests per minute, while ensuring that the returned result remains актуальным for 10 seconds. A solution is required that will reduce the load, prevent overloads, and provide fast and stable delivery of fresh data.

func getBtcCourseHandler(w http.ResponseWriter, r *http.Request) { 
    // Generate a random number instead of the real BTC rate.
    _, err := fmt.Fprintf(w, "The simulated BTC course is: %.2f USD", getCourse())
    if err != nil {
        log.Print(err)
    }
}

func main() {
    http.HandleFunc("/getCourse", getBtcCourseHandler)

    fmt.Println("Server is running on port 8080...")
    err := http.ListenAndServe(":8080", nil)
    if err != nil {
        fmt.Println("Server failed:", err)
    }
}

func getCourse() float64 {
    rand.Seed(time.Now().UnixNano())
    // Volatility from 30000-80000
    btcCourse := rand.Float64()*50000 + 30000
    return btcCourse
}