How To Build Flash Sale App with GoLang and Couchbase

Life at Mapan
Sep 6, 2018 · 4 min read
Source: Mapan’s Creative Team

This is how I describe flash sale. It’s like when you are very hungry, you decide to go to a restaurant and you see all your favorite dish that you want to eat on the menu. And then one day, there is a big promo for your favorite dish at some restaurants, there is a discount within a very limited time. What are you gonna do?

Limited stock or the effort people have to give in obtaining items makes them feel that it relates to their pride, especially when they get the items. People rush whenever there is a flash sale at a store, both offline and online, where millions of users throw themselves into the flash sale.

It is a big challenge for software engineers to build a reliable software for flash sale because there are so many factors to consider. But I will not go too deep on how to build flash sale app end-to-end.

Source: https://on.msnbc.com/2GWcZRF

We all know the that hottest language programming right now is GoLang. In short, GoLang is easy to read or write yet so fast, statically typed, has huge open source community, powerful on concurrency and also easy to scale. Even though GoLang is a pragmatic or opinionated language in which everyone can have their own style in writing program with it, it will tackle most of the challenge that we have in today’s modern tech stack as long as we have clean codes.

As our front and the middle layer of our apps is fast we need a persistent layer that has fast performance too. Yup, I found that Couchbase and GoLang swipe right each other on Tinder. The absence of NoSQL Document Oriented to store data in Couchbase makes most of software engineers think that it is not suitable for storing transaction data though. Still, it is worth to try!

For example, we are now trying to build a FlashSale() function on backend flash sale apps where every time the function is called, it will decrement the available stock quantity of an item on the warehouse. Here is a simple architecture diagram, and early implementation:

Simple FlashSale Architecture Diagram
func FlashSale(reqData Request) (error) {
itemObj, err := cb.FindItemByKey(reqData.itemKey)
if err != nil {
return err
}
if itemObj.AvailableQty < 1 {
return exc.NewNotEnoughQtyException()
}
itemObj.AvailableQty -= reqData.Qty

err = cb.Upsert(&itemObj)
if err != nil {
return err
}
return nil
}

Problem

Implementation

func FlashSale(reqData Request) (error) {
for true {
itemObj, CAS, err := cb.FindItemByKey(reqData.itemKey)
if err != nil {
return err
}
if itemObj.AvailableQty < 1 {
return exc.NewNotEnoughQtyException()
}
itemObj.AvailableQty -= reqData.Qty

err = cb.Replace(&itemObj, CAS)
switch err.(type) {
case *exc.CasNotMatched:
continue
default:
return err
}
break
}
return nil
}

Voilà~~ Now our function will never break our stock data on production. Performance? Leave it to GoLang and Couchbase. With 2 node clusters and low standard Google Cloud Platform template server, FlashSale() function have around 1000–1150 Throughput Per Second. Anyway, the code above is only a snippet, not a full code because it will be too long for this post. The full code can be found here.

Enjoy!


Article was written by Hifnie Bilfash. You can also check his other articles here.

Are you curious about how we contribute to society with the help of technology? Send your latest CV to recruitment@ruma.co.id and let’s see what we can do together!

Life at Mapan

A purposeful workplace with a mission to increase access, dignity and income for low-income communities through technology.

Life at Mapan

Written by

Life at Mapan

A purposeful workplace with a mission to increase access, dignity and income for low-income communities through technology.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade