<?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>Forem: SelfDevTV</title>
    <description>The latest articles on Forem by SelfDevTV (@selfdevtv).</description>
    <link>https://forem.com/selfdevtv</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F558506%2F0af02818-dc68-4a55-b973-73f85639f0e5.png</url>
      <title>Forem: SelfDevTV</title>
      <link>https://forem.com/selfdevtv</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/selfdevtv"/>
    <language>en</language>
    <item>
      <title>What I learned by using Prisma with PostgreSQL</title>
      <dc:creator>SelfDevTV</dc:creator>
      <pubDate>Tue, 12 Jan 2021 14:26:31 +0000</pubDate>
      <link>https://forem.com/selfdevtv/what-i-learned-by-using-prisma-with-postgresql-2b6c</link>
      <guid>https://forem.com/selfdevtv/what-i-learned-by-using-prisma-with-postgresql-2b6c</guid>
      <description>&lt;h2&gt;
  
  
  Welcome everyone to my first Blog post ever.
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Let me introduce myself quickly:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I'm a youtuber that creates videos about re-creating existing applications (for example a &lt;code&gt;reddit-clone&lt;/code&gt;). In the past I worked a lot with react and node / express + mongodb as backend. &lt;/p&gt;

&lt;p&gt;So SQL and further PostgreSQL is very new to me.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In my last project I've decided to use this stack:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Next.js&lt;/li&gt;
&lt;li&gt;AWS RDB (with PostgreSQL)&lt;/li&gt;
&lt;li&gt;Prisma as ORM&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I'm pretty new to hosting something in general on AWS and I'm also using Prisma the first time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;With that said here is what I've learned:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Vercel&lt;/code&gt; deploys hosted sites per default to SFO1 (San Francisco) region&lt;/li&gt;
&lt;li&gt;I deployed my RDB on AWS to Frankfurt Gateway (since I thought it will be the fastest solution because I'm from Austria)&lt;/li&gt;
&lt;li&gt;I also chose the free tier and created nothing that a naked PostgreSQL database&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;So what I'm trying to say here is the following:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Having the db in another region then the hosted application is a bad idea - which you will see later&lt;/li&gt;
&lt;li&gt;Having a naked PostgreSQL db without a &lt;a href="https://www.pgbouncer.org/" rel="noopener noreferrer"&gt;PGBouncer&lt;/a&gt; is also not a good idea performance wise&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So I deployed my db to AWS and my Next.js App to Vercel.&lt;/p&gt;

&lt;p&gt;This is my main api route and all I do (the db has 2 posts total with 0 comments):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const sub = await prisma.subreddit.findUnique({
      where: { name: String(req.query.name) },
      include: {
        posts: {
          include: { subreddit: true, user: true, votes: true },
        },
        joinedUsers: true,
      },
    });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I turned on &lt;code&gt;Vercel&lt;/code&gt;Analytic and waited a day and this is what I got:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F3yrk9r35dealib9siqge.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F3yrk9r35dealib9siqge.PNG" alt="blog1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see the performance is horrible for my simple application and loading just 2 posts. &lt;/p&gt;

&lt;p&gt;I further added a /performance route that does just this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var t0 = performance.now();
    const sub = await prisma.subreddit.findUnique({
      where: { name: String(req.query.name) },
      include: {
        posts: {
          include: { subreddit: true, user: true, votes: true },
        },
        joinedUsers: true,
      },
    });
    var t1 = performance.now();

    const timeItTook = t1 - t0;
    console.log("Fetching full Sub took " + timeItTook + " milliseconds.");
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The result was always between 3 to 5 seconds. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Fetching full Sub took 3358.1404000520706 milliseconds.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;At first I thought it's a problem with &lt;code&gt;Prisma&lt;/code&gt; and more specific it's &lt;code&gt;include&lt;/code&gt;syntax. I somehow thaught that nested Queries are slow. &lt;/p&gt;

&lt;p&gt;So I opened an issue here: &lt;a href="https://github.com/prisma/prisma/issues/4884" rel="noopener noreferrer"&gt;https://github.com/prisma/prisma/issues/4884&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The awesome contributors took a closer look and found the problem(s):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The datbase was hosted on one side of the world&lt;/li&gt;
&lt;li&gt;And the Application that connects to it on the other side of the world&lt;/li&gt;
&lt;li&gt;It also has no bouncer (for handling many session since it's serverless / lambda functions)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;As I read that, it instantly clicked!&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;At first I wanted to create a RDB Proxy, I think it's called on AWS - but it didn't give me the option to chose the right database. I decided to use &lt;code&gt;Digital Ocean&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;So I headed over to Digital Ocean and created an account + db hosted in San Francisco. &lt;br&gt;
It also gave me the suggestion to add a "connection pool" (that's basically the same as &lt;code&gt;PGBounce&lt;/code&gt; - as far as my understanding goes. That was also very new to me and didn't even know existed or I needed.&lt;/p&gt;

&lt;p&gt;Next I ...&lt;/p&gt;

&lt;p&gt;...changed the &lt;code&gt;.env&lt;/code&gt; database url to Digital Oceans one.&lt;/p&gt;

&lt;p&gt;...Then I migrated the Schema with &lt;code&gt;yarn prisma migrate dev --preview-feature&lt;/code&gt; and deployed the app to Vercel.&lt;/p&gt;

&lt;p&gt;The performance route is now between  20 - 30 ms (compared to about 3000ms before). &lt;/p&gt;

&lt;p&gt;This is how it looks on Analytics:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fz3clwf88q2v30emvnj5g.PNG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fz3clwf88q2v30emvnj5g.PNG" alt="blog2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So the performance is now absolutely beautiful and I learned a lot in the process. &lt;/p&gt;

&lt;p&gt;Thank you to all the &lt;code&gt;Prisma&lt;/code&gt; contributors to helped me out here. These guys are awesome and I can really say: &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;I love Prisma&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Shameless plug:&lt;/strong&gt; Here is my youtube channel if you are interested: &lt;a href="https://www.youtube.com/channel/UCOo3r8Do1Xa97UfQdqK2MSQ" rel="noopener noreferrer"&gt;https://www.youtube.com/channel/UCOo3r8Do1Xa97UfQdqK2MSQ&lt;/a&gt;&lt;/p&gt;

</description>
      <category>prisma</category>
      <category>postgres</category>
      <category>react</category>
      <category>nextjs</category>
    </item>
  </channel>
</rss>
