{
  "id": 1687880,
  "title": "Refactoring the views system in YOUR gift!",
  "url": "https://urgent.news/2026/08/18/refactoring-the-views-system-in-your-gift",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-08-18T10:16:11.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/codemaster_121482/refactoring-the-views-system-in-your-gift-4kno"
  },
  "original_language": "en",
  "account": "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.\n\nThe 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.\n\nTo 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.\n\nNext, 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.\n\nA 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.",
  "summary": "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…",
  "key_points": [
    "Refactored views system replaces manual loop with bulkWrite operations",
    "Eliminates N+1 problem and prevents view duplication",
    "Ordered: false ensures operations continue even if one fails"
  ],
  "editors_take": null,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}