Part 4 of 5 · Building Kommercly

Moving Kommercly Off Vercel and Neon Onto My Own Server

Kommercly now runs on a server I rent, not on Vercel and Neon. Same URLs, same pages, same shops. Nobody had to change anything, which was the goal.

This post is how I moved it and, more usefully, what went wrong on the way. Quite a lot did.

Why move at all

Vercel Pro was $20 a month. Neon was a second bill on top, and it grows with traffic. Kommercly is a store builder, so every shop’s page view is traffic, and more shops means a bigger bill. I did not like a cost curve that goes up exactly when things go well.

Meanwhile I already pay for one VPS with 4 CPUs and 8 GB of RAM. It runs ProcureIndex and had plenty of room. Kommercly uses about 234 MB of RAM. It was a bad deal to rent a second place for something that small.

So the rough saving is $20 to $40 a month, and the cost stops growing with traffic. The price is that I now look after the server myself. More on that at the end.

What it looks like now

  • The app. A Next.js standalone build, run as a systemd service with a memory cap, so a leak cannot eat the server.
  • kommercly.com and *.kommercly.com. They reach the server through a Cloudflare Tunnel.
  • Shops with their own domain. Caddy handles these with on-demand TLS. A certificate is only issued if the domain is saved on a shop. Before issuing, Caddy asks an endpoint in the app, and the app says yes only for domains it knows. Without that check, anyone could point a domain at my server and make me request certificates for it.
  • Connecting a domain. A shop owner adds one CNAME to connect.kommercly.com. That record never changes, so I can move the server later without asking anyone to touch their DNS. Apex domains cannot use a CNAME, so they use an A record.
  • Database. Postgres on the same box.
  • Cron jobs. They became systemd timers.
  • Backups. Nightly.

Moving the data

I ran pg_dump against Neon and restored into Postgres on the server. Neon ran Postgres 17 and the server runs 16, so I needed the version 17 client to read the dump. That was the first small surprise.

Then I did the boring and important part: I compared row counts per table. 13 tables, all matched. 180 shops, 795 products, 249 orders and about 18,000 analytics events.

A matching count does not prove the data is identical, but a mismatch would have told me something was wrong. It is cheap, so I do it every time.

A staged cutover

I did not flip everything at once. First I ran the whole thing on a staging subdomain. I marked it noindex so Google would not see two copies of every shop and treat them as duplicates.

When staging looked right, I did the real switch:

  1. A final copy of the data, late at night when almost nobody was ordering.
  2. The DNS switch in Cloudflare straight after.

Afterwards I checked Neon for any rows that had arrived during the switch. Zero. Nothing had landed on the old side.

What went wrong

This is the part I would want to read, so here it is in full.

1. I could not read my own secrets

Vercel marks some environment variables as sensitive and will not show them again. Revealing any value needed an email code, sent to an old inbox I can no longer get into.

So I could not copy the production secrets across. I had to make new ones. The result: everyone was signed out and had to sign in once more, and three sellers had to enter their courier and Facebook keys again. That was my mistake.

The lesson is plain. Keep your own copy of your production secrets in a password manager. Do not let a hosting dashboard be the only place they exist.

2. A bulk delete took two records I needed

In Cloudflare I selected the old A records and deleted them together. The bulk delete also removed two TXT records: the email SPF record and a Google verification record. I noticed within minutes and put them back, so nothing broke for anyone. It could have been mail quietly going to spam for days.

Delete DNS records one at a time. Slower, and worth it.

3. Shop subdomains returned 500

For a few minutes every shop subdomain returned a 500 error.

The cause was a header. Behind Cloudflare and Caddy, the app saw X-Forwarded-Proto: https. Its middleware rewrites then tried to call the app itself over https on localhost, where there is no https. So the app was failing to talk to itself.

The fix was to strip that header at the proxy. The same bug came back later for custom domains, and the same fix worked. I now check this first whenever I see a strange 500 behind a proxy.

4. Telegram kept talking to the old server

Kommercly sends me alerts through a Telegram bot, with an approve button for payments. After the switch, pressing approve failed with a 401.

Telegram had cached the old Vercel IP in its own DNS and kept sending button presses there. Re-registering the webhook fixed it. Nothing was wrong with my server. The outside world just had not caught up.

5. I locked myself out of SSH

I made a few bad key logins and tripped fail2ban, which did its job and banned me. For a while I could not reach my own server. fail2ban was right and I was the intruder.

6. The first build ran out of memory

The first GitHub Actions build died with an out of memory error. Node’s default heap is about 2 GB, and a big Next.js build wants more. I raised the limit and it passed.

How deploys work now

I push to main. After that:

  1. GitHub builds the app. It needs no database at build time. A few pages used to query the database while building. They now render on request, with caching.
  2. GitHub uploads the build using a locked-down deploy key that can only hand over a release.
  3. The server runs migrations, swaps a symlink to the new release, and health-checks it.
  4. If the health check fails, it rolls back by itself.
  5. A Telegram message tells me ✅ or ❌.

The deploy key cannot open a shell. It can only hand over a release. But a release is code that runs on my server, so I still guard that key like a password.

Then I made shop pages cacheable

Once everything ran on one box, I looked at what a shop page actually costs. Every visit to a shop rendered the page from scratch, even though every visitor sees the same page. The data underneath was cached, but the page itself was not. Next.js treated the shop route as fully dynamic because it has a parameter in the URL and nothing told it otherwise.

So I split the pages into two groups:

  • Same for everyone: a shop’s home page and its product pages. These are now cached per shop, and the response says public, s-maxage=60, stale-while-revalidate=600.
  • Different per person: admin, checkout, order pages, sign-in, the API. These stay private, no-store.

Two rules kept this safe:

  1. A cached page never sets a cookie. If it did, one visitor’s cookie could end up in a page served to someone else.
  2. Every cache entry belongs to one shop. I tested that editing one shop never changed what another shop served.

When an owner edits a product, changes settings or syncs their Google Sheet, that shop’s cache is cleared straight away, so changes show up within seconds. The other shops stay cached.

The result at the server: a shop page went from about 0.08 seconds to about 0.02. Four times faster, and most visits now never run the page code at all.

Along the way I found a small hole. The app decided which shop a request belonged to partly from a header, and a visitor could send that header themselves on the main site. Nothing bad could come of it today, but I now strip that header unless the app set it itself.

The result

Same URLs and same SEO. Pages load in about 0.3 to 0.8 seconds through Cloudflare’s Dhaka edge. The bill is lower and no longer tracks traffic.

The trade-off I am not hiding

One server is a single point of failure. Vercel was not. If this box dies, Kommercly dies with it until I fix it. Nightly backups on the same machine are not enough, so I added two things right after the move. Every night the database dumps also go to Cloudflare R2, with a week of daily copies and a month of weekly ones, and I test a restore from there instead of trusting it. And every five minutes a check from outside the server hits the site and messages me on Telegram if it goes down, and again when it comes back.

If you are thinking of making this move: it is not hard, but do it with your secrets saved somewhere you control, delete DNS records one at a time, and do not trust the first 200 you see on staging.


Kommercly is a phone-first store builder for Bangladeshi f-commerce sellers. Free plan, 0% commission, orders to your WhatsApp. kommercly.com

Written by . Software engineer from Bangladesh. I ship products end to end, from the browser to the board: code generation, firmware and PCB tooling at IDEEZA, and my own products ProcureIndex, Akhra and Kommercly. Also farms, cycles and fights MMA.

Published

Get new logs by email. No spam, unsubscribe anytime.