andrew-bierman / PackRat

PackRat is a versatile adventure planner tailored for outdoor enthusiasts. It simplifies the process of organizing trips from a simple day hike to cross-country journeys.
https://packrat.world/
Other
23 stars 36 forks source link

Exploring Alternatives for ORM, Moving Backend to the Edge for Improved Performance and Scalability #507

Closed andrew-bierman closed 1 month ago

andrew-bierman commented 9 months ago

We have been working on transitioning our backend infrastructure to the edge via Cloudflare Workers. The initial migration from a Node.js environment with MongoDB and Mongoose to Hono + Prisma with MongoDB was aimed at leveraging the benefits of serverless architecture. For the most part, this was a success. However, after thorough testing and evaluation, it has become evident that Prisma's performance is suboptimal for handling queries on the edge. Additionally, as our project data has evolved to be more relational, MongoDB's limitations have become more apparent.

Issue:

The primary concern is the significant performance bottleneck we are experiencing with Prisma, especially for complex queries. This not only affects the efficiency of our backend operations but also could potentially impact the user experience.

Proposed Solution:

To address these challenges, I am considering migrating our backend to use Drizzle and D1. Drizzle, as a lightweight ORM, and D1, as a SQLite database, could provide the necessary performance improvements and better support for relational data models.

Action Items:

Research and Evaluation: We need to thoroughly research Drizzle and D1, assessing their compatibility with our current stack, performance benefits, and how they handle relational data compared to MongoDB.

Team Feedback: I encourage the team to provide feedback on this proposed migration, especially concerning potential challenges and experiences with Drizzle and D1.

References and Resources:

Seeking Input:

I would appreciate any insights or experiences the team has with Drizzle and D1, especially regarding large-scale, relational data handling in a serverless environment.

andrew-bierman commented 9 months ago

Awais I think the handling of duplicate ids could very well be an issue, that’s a good idea.

I’m not convinced switching the stack will solve all problems, but it addresses 2 hunches I have as for why this is so slowed down.

Relationships: With a query like getPublicPacks, we are doing something like 4-6 different populates via nested relationships. Which I understand is not a small number, but realistically does not seem that ridiculous. But assuming this adds a noticeable overhead to the query, we can count it as a negative.

Cold start: Then factor in we have a 500ms-multiple second cold start from prisma, link to reported issue I saw: https://www.youtube.com/watch?v=3P7jnolWfHw (this is not exact issue but demonstrates what we are talking about)

Mongo without direct connection: Then another few seconds to communicate with mongodb via prisma accelerate, adds essentially the same slow mongo connection you see when starting mongoose on node server, but now for every request.

Prisma mongo queries are much worse than mongoose, and might not utilize indexes fully seen here: https://github.com/prisma/prisma/issues/16916

But even on like findById queries, I’m seeing it take quite some time. Which leads me to believe one or multiple of the above is taking place.

Overall I’m totally open to solutions that aren’t ripping out ORM and starting over - but I do feel like some of the groundwork we did on this prisma will translate. Its just the schema and service files that we need to switch over I believe if we go this route.

algora-pbc[bot] commented 8 months ago

🎉🎈 @raghavyuva has been awarded $600! 🎈🎊