<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Seif Eid</title>
    <description>The latest articles on DEV Community by Seif Eid (@eidcoder28).</description>
    <link>https://dev.to/eidcoder28</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4252489%2F5690c571-9614-44bf-b8c8-5bf2602ab217.png</url>
      <title>DEV Community: Seif Eid</title>
      <link>https://dev.to/eidcoder28</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/eidcoder28"/>
    <language>en</language>
    <item>
      <title>Fixing Session Leaks in Axion's MongoDB Transactions</title>
      <dc:creator>Seif Eid</dc:creator>
      <pubDate>Wed, 18 Nov 2026 19:16:29 +0000</pubDate>
      <link>https://dev.to/eidcoder28/fixing-session-leaks-in-axions-mongodb-transactions-1fmo</link>
      <guid>https://dev.to/eidcoder28/fixing-session-leaks-in-axions-mongodb-transactions-1fmo</guid>
      <description>&lt;p&gt;Hey friends! 👋🏻&lt;/p&gt;

&lt;p&gt;I wanted to share another cool backend fix I implemented in my microforum, Axion. This time, it involves MongoDB transactions, session management, and taking advantage of Express 5's upgraded error handling to keep my code clean.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why Use Transactions in the First Place?&lt;/strong&gt; When you're building a platform like Axion, some actions require multiple database operations to happen together. For example, in the post-deletion route, I need to remove related documents like bookmarks, likes, and comments all at once.&lt;/p&gt;

&lt;p&gt;Transactions act as a way to execute a set of database operations where if a single operation fails, everything rolls back and no operations execute afterwards. This guarantees data consistency.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Bug I Encountered: Session Leaks.&lt;/strong&gt; When running a transaction in MongoDB, you must start a client session and end it when you are finished. My code was written sequentially, assuming it would always reach the cleanup phase at the bottom. But if my &lt;code&gt;withTransaction&lt;/code&gt; block crashed early, &lt;code&gt;session.endSession()&lt;/code&gt; wouldn't get called. This left some sessions running in the background, which slowly drained resources and caused memory leaks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;My Solution: Using&lt;/strong&gt; &lt;code&gt;try/finally&lt;/code&gt; &lt;strong&gt;in Express 5.&lt;/strong&gt; The fix was straightforward, but I wanted to implement it without adding unnecessary boilerplate code. I wrapped the session management inside a &lt;code&gt;try/finally&lt;/code&gt; block:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;session&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;mongoose&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;startSession&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;withTransaction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// My atomic database operations...&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;finally&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;endSession&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The beauty of the &lt;code&gt;finally&lt;/code&gt; block is its absolute guarantee: no matter what happens inside the &lt;code&gt;try&lt;/code&gt; block, the code inside &lt;code&gt;finally&lt;/code&gt; is always executed. This ensures &lt;code&gt;session.endSession()&lt;/code&gt; runs every time, eliminating the session leak problem.&lt;/p&gt;

&lt;p&gt;Oh, yeah, and since I'm using Express 5, any exception thrown inside an async route is automatically caught by Express and routed to my global error-handling middleware. I no longer need those repetitive &lt;code&gt;catch&lt;/code&gt; blocks!&lt;/p&gt;

&lt;p&gt;Thanks for hanging out and reading through! How do you structure your database transactions to keep them clean and leak-free? Let's talk in the comments below. See you next time! 🌟&lt;/p&gt;

</description>
      <category>backend</category>
      <category>database</category>
      <category>mongodb</category>
      <category>node</category>
    </item>
    <item>
      <title>How I Used Aggregation Pipelines to Fix No-Op Updates and Keep Error Clarity in Axion</title>
      <dc:creator>Seif Eid</dc:creator>
      <pubDate>Wed, 18 Nov 2026 19:14:41 +0000</pubDate>
      <link>https://dev.to/eidcoder28/how-i-used-aggregation-pipelines-to-fix-no-op-updates-and-keep-error-clarity-in-axion-4hpk</link>
      <guid>https://dev.to/eidcoder28/how-i-used-aggregation-pipelines-to-fix-no-op-updates-and-keep-error-clarity-in-axion-4hpk</guid>
      <description>&lt;p&gt;Hey friends! 👋🏻&lt;/p&gt;

&lt;p&gt;I wanted to share a cool backend fix I implemented while adding the comments editing feature to my microforum, Axion. It's about handling no-op updates cleanly without breaking error handling.&lt;/p&gt;

&lt;p&gt;In the past, some of my editing routes would update the content unconditionally. Even if the old and new content were exactly the same, the database would still run the update, hit the disk, and change the timestamps. It was inefficient honestly.&lt;/p&gt;

&lt;p&gt;And letting MongoDB update the timestamps on identical content breaks data integrity. It lies to you—saying the last update happened at X time when nothing actually changed. Plus, it would return &lt;code&gt;modifiedCount === 1&lt;/code&gt; just because a timestamp field got changed, which ruins the tracking of actual content changes.&lt;/p&gt;

&lt;p&gt;When adding comment updates, I wanted to avoid this behavior entirely.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Naive Approach.&lt;/strong&gt; I could have simply used &lt;code&gt;{ content: { $ne: content } }&lt;/code&gt;. But this creates a blindspot in my logic. If the database returns 0 matched documents, you can't differentiate between two completely different scenarios:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Does the document actually exist and belong to the logged-in user?&lt;/li&gt;
&lt;li&gt;Did the query just fail because the new content matches the old content?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To give a proper response to the client, I needed to know the difference.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;My Solution: Aggregation Pipeline Updates.&lt;/strong&gt; Instead of a basic update, I relied on an aggregation pipeline inside the update operation. And that's my logic:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It first finds a document created by the current logged-in user where the &lt;code&gt;_id&lt;/code&gt; matches the given comment ID.&lt;/li&gt;
&lt;li&gt;If found, it updates the content and timestamps only if the old content differs from the new content.&lt;/li&gt;
&lt;li&gt;Finally, it checks &lt;code&gt;matchedCount === 0&lt;/code&gt;. If it's 0, we know that the document wasn't found (or the user doesn't own it) and can throw a proper 404 error here.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Oh, yeah, and to end this cleanly, I included a &lt;code&gt;changed&lt;/code&gt; field in the HTTP response, setting its value to &lt;code&gt;modifiedCount !== 0&lt;/code&gt;. This tells the frontend exactly whether an actual database write occurred or if it was a harmless no-op.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Handling the "(Edited)" Badge Properly.&lt;/strong&gt; While doing this, I also needed a way for the frontend to show an "(Edited)" badge on updated comment cards.&lt;/p&gt;

&lt;p&gt;I didn't want to rely on a single boolean like &lt;code&gt;isEdited: true&lt;/code&gt; because it doesn't provide the metadata of &lt;em&gt;when&lt;/em&gt; the update happened if I ever need it. I also rejected the idea of just comparing &lt;code&gt;createdAt&lt;/code&gt; against &lt;code&gt;updatedAt&lt;/code&gt;. Like, what if a background script or admin tool ever modifies a document for maintenance? &lt;code&gt;updatedAt&lt;/code&gt; changes, and the system would falsely tell users the comment was edited when it wasn't.&lt;/p&gt;

&lt;p&gt;So, I came up with a cleaner approach: adding a third field called &lt;code&gt;editedAt&lt;/code&gt;. For non-edited comments, it stays &lt;code&gt;null&lt;/code&gt;. The moment a real edit happens, it stores the exact timestamp of that edit. Maybe I don't show the edit timestamp to users right now, but if I ever want to in the future, the data will be there. It's a matter of handling "what if" scenarios cleanly from day one.&lt;/p&gt;

&lt;p&gt;At the end of the day, losing precise error handling isn't worth saving a few lines of code.&lt;/p&gt;

&lt;p&gt;Thanks for hanging out and reading through! How do you handle these annoying no-op updates and edit tracking in your backend? Let's talk in the comments below. See you next time! 🌟&lt;/p&gt;

</description>
      <category>backend</category>
      <category>database</category>
      <category>mongodb</category>
      <category>performance</category>
    </item>
  </channel>
</rss>
