Refactoring the views system in YOUR gift!
In the past, link views were cached in Redis. After 10 seconds , all the views were flushed using a manual loop. The old flow looked like this: Loop through every key (ID). Update its views separately via updateOne . Move to the next until the loop ends. Why This Is Wrong: N+1 Problem: As the number of key operations grows , it runs many separate trips . Under very heavy traffic, the system can…
Refactoring the views system for a gift application involved addressing several key issues with the previous approach. In the past, link views were cached in Redis and flushed after 10 seconds using a manual loop. This outdated method would iterate through every key (ID), updating views separately via updateOne and progressing to the next until the loop concluded.
The primary problems with this method were twofold. Firstly, the N+1 problem arose, where the system made numerous separate trips as the number of key operations increased. Under heavy traffic, this could exhaust system resources and lead to crashes. Secondly, views were never properly cleared after flushing, resulting in duplicated view counts. Additionally, the system lacked validation to check for keys that required updating, leading to unnecessary empty operations.
To rectify these issues, the refactoring strategy adopted bulkWrite operations instead of individual updateOne operations. This modification simplified the process—when there were 100 links viewed, the system executed just one operation rather than 100. The new flow began by retrieving views using hGetAll. The system then checked if valid data existed for processing; if not, the operation was skipped entirely.
Next, the ID operations were mapped, a step designed to be lightweight and suitable for niche applications such as this markdown previewer for developers. The request was sent to the database, where the updates were made using bulkWrite. Crucially, if the request reached the database and was acknowledged, the old views were subsequently deleted. This ensured that even if the server crashed midway through processing, the data would not be lost.
A notable adjustment in the refactored process was the addition of ordered: false. This was implemented to prevent the failure of a single operation from stopping remaining operations from executing, as well as to allow previous operations to be rolled back if necessary. For those who appreciated these improvements, the developer encouraged a contribution by giving a star on GitHub.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.