Utuk Backend Story HNG11 Stage 0 Task

Uwakmfon Utuk - Jun 30 - - Dev Community

I was working on a slack clone and my objective was to replicate the status update feature. The feature works as follows:

  • User specifies emoji, short text and chooses expiry time (e.g. 1 hour, 4 hours, 1 day, 1 month or Don't Clear).
  • This status (emoji + short text) should then be displayed on their profile page for the expiry time duration.
  • The status must be cleared after the specified duration, except the user selects expiry_time="Don't clear".

There are 2 components for this feature: CRUD (simple part) and the expiration (tricky at the time).

I quickly implemented the CRUD because it was easy, so I'll focus on talking about my implementation of expiration.

My (not so great) idea was to use goroutines (yes, the backend was written in Go). Once a status update happens, with duration specified as X minutes, I spawn a thread that basically sleeps for X minutes and clears the status from the db. This worked when I tested it for small duration and is technically a correct implementation.
However, it is a horrible way of implementing this for 2 reasons:

  1. Volatility! If the application crashes, the sleeping thread dies and never gets to clear the status from the db.
  2. Not Scalable! At every instance you have a separate thread for every user that has a status with an expiry time. This means 10 users = 10 threads, 1000 users = 1000 threads; 1million users = 1million threads. Phew! Horrible!

What to do instead? Feel free to think about it yourself if you have some backend experience or just stay tuned for part 2 of this blog post.

I joined HNG11 because I want to have fun building projects and making new friends along the way! I am also a (proud?) member of HNG Premium

.