From Frustration to Fulfillment: How I Recently Identified and Solved a Back-end Problem
Table of contents
In this article, I will be narrating how I solved a recent, difficult problem during the development of the Cashboard web app.
Cashboard is a personal finance tool that allows users to track their accounts, income sources, expenses, savings, and investments. If you're interested in interacting with it, you can view it here.
How I Identified the Problem
While I was developing the Cashboard API, I was interested in making sure the accounts stay up-to date. For example, when an income source is added, a corresponding transaction was created and the associated account's balance was incremented. This would be simple in theory by carrying out the operations independently.
However, when an error occurred, the database would be inconsistent. For example, the income would be created and the account would not be updated.
How I Solved the Problem
- Using my existing knowledge
Due to my experience with SQL, I knew about transactions which allow dependent operations to be executed all at once or not at all. I started looking for ways I could execute this in Mongoose/MongoDB.
- Finding documentation🤗
I consulted Google and found various documentations on MongoDB sessions. The documentation was dense making it hard for me to understand the process. Therefore, I opted for a few Medium articles which were simplified.
I changed my code to incorporate sessions and I was pretty excited to test it.
- Testing🤨
I restarted my server and everything was fine. Once I tested the income route, the app crashed due to an error.
So back to square one(but not quite).
- Incompatible database😞
I searched the error on Google and found out that transactions cannot be executed in a local instance of MongoDB. This was because they need replica sets which are not available in the local instance of MongoDB without modifying the setup.
I tried modifying the MongoDB server on my laptop to incorporate replica sets which led to more errors that I could not seem to figure out.
In the midst of my frustrations, I found a Stack Overflow post that suggested using a remote cluster because they have replica sets already set up.
So I created a remote cluster on MongoDB Atlas and connected to it.
- Solved🎉
I tested the app while using the remote database and it worked!
Yay!🤩
If you're interested in learning more about ACID transactions in Mongoose/MongoDB, you can check out this article.
Conclusion
This whole process showed me that it's important to leverage my existing knowledge from other technologies when I am debugging. The process would have been more frustrating if I didn't know about transactions. I would have possibly ignored the error and thought it wouldn't have effect in production.
I am a full-stack software engineer and technical writer from Kenya. I have recently joined the HNG internship where I get to work on projects and collaborate in teams. I am interested in this opportunity because I get to use my skills in a fast-paced environment and network with other professionals.
The HNG internship is a free fast-paced remote internship based in Nigeria for intermediate and experienced professionals in various tracks including,
front-end web development,
mobile development,
back-end development,
UI/UX,
video marketing,
devops,
data analytics,
product management.
I am participating in the front-end and back-end tracks. I am excited to learn and grow as a developer in this journey.
There's also a premium version of the HNG internship that has the following perks:
certificate on completion of the program,
networking opportunities,
mock interviews,
CV reviews,
reference letters,
and many more
If you're interested in learning more about the program, you can view their website.