Where the Data Lives When Your API Is Online

Render's free web service wipes its files every time it redeploys, restarts or spins down, so a SQLite file there holds demo data that the API seeds at startup when the database is empty, and keeping data for real takes a persistent disk or a hosted database such as PostgreSQL, which cost money.

Journeyman · FastAPI · REST API · Updated

By

After this lesson you can

  • Explain why a free host's wiped disk makes an online SQLite file demo data, and name what keeps data for real
  • Seed demo data at startup only when the database is empty, so an API on a wiped disk starts with rows to show

Key takeaways

  • A free Render web service loses every file it wrote whenever it redeploys, restarts or spins down, and its SQLite file goes with them.
  • So the online API holds demo data. The lifespan adds demo rows when the database is empty, and the link always has something to show.
  • Seeding only an empty database keeps the rows you've changed on your own computer, where nothing wipes the file.
  • A persistent disk or a hosted database such as PostgreSQL keeps data for real. A disk needs a paid service, and a free Render Postgres database expires after 30 days.

The trails API keeps its trails in a SQLite file, trails.db, beside its code. On your laptop that file stays put for years. On a free host it doesn't, and an API that doesn't plan for it shows hikers an empty list. Before you put anything online, you need to know where its data will live and how long it lasts, because a user who loses their data rarely comes back.

A disk that forgets

Render's free web service gives your API a disk to write to, but its documentation is plain about what happens to it: files the service writes "are lost every time the service redeploys, restarts, or spins down." A disk that's wiped like this is called an ephemeral filesystem ("ephemeral" means short-lived).

Each of those three happens often:

  • A redeploy happens on every push to your repository.
  • A spin-down happens after 15 minutes with no requests, so a quiet night wipes it.
  • A restart can come at any time: the docs say Render "might restart a Free web service at any time."

Think of a whiteboard in a shared meeting room: anything you write there is gone after the cleaners come through. The trails API's trails.db is written on that whiteboard. A ranger who closed Cedar Falls Loop at noon could find it open again by evening.

A day of the trails API on a free host, left to right: it starts and adds the 6 demo trails; at noon a ranger closes Cedar Falls Loop; after 15 minutes with no requests it spins down and its files are wiped; the next request wakes it, it adds the 6 demo trails again, and Cedar Falls Loop is open

Demo data, seeded at startup

The API can't stop the wipe, but it can start well after one. Each start begins with an empty trails.db, so the lifespan from Startup, Shutdown and a Health Check runs the migrations, then adds the six demo trails. Starting rows like these are called seed data:

def seed_demo_trails(conn):
    count = conn.execute("SELECT COUNT(*) FROM trails").fetchone()[0]
    if count > 0:
        return 0
    conn.executemany(
        "INSERT INTO trails (slug, name, miles, status, report_date, report_note) "
        "VALUES (:slug, :name, :miles, :status, :report_date, :report_note)",
        DEMO_TRAILS,
    )
    conn.commit()
    return len(DEMO_TRAILS)

executemany runs the INSERT once for each dictionary in DEMO_TRAILS, filling each :name placeholder from the key with the same name. The check at the top matters: on your own computer, where nothing wipes the file, a trail you changed yesterday stays changed, because the database isn't empty.

Here's the whole story in one small script, with a temporary folder standing in for the host's disk and os.remove standing in for the wipe:

import os
import sqlite3
import tempfile

DEMO_TRAILS = [("cedar-falls-loop", "open"), ("lookout-ridge", "muddy")]


def start_api(path):
    conn = sqlite3.connect(path)
    conn.execute("CREATE TABLE IF NOT EXISTS trails (slug TEXT PRIMARY KEY, status TEXT)")
    if conn.execute("SELECT COUNT(*) FROM trails").fetchone()[0] == 0:
        conn.executemany("INSERT INTO trails VALUES (?, ?)", DEMO_TRAILS)
        conn.commit()
    return conn


def cedar_falls(conn):
    return conn.execute("SELECT status FROM trails WHERE slug = 'cedar-falls-loop'").fetchone()[0]


path = os.path.join(tempfile.mkdtemp(), "trails.db")
conn = start_api(path)
conn.execute("UPDATE trails SET status = 'closed' WHERE slug = 'cedar-falls-loop'")
conn.commit()
print("Before the restart:", cedar_falls(conn))
conn.close()

os.remove(path)  # what a restart on a free host does to the disk
conn = start_api(path)
print("After the restart:", cedar_falls(conn))
conn.close()
Before the restart: closed
After the restart: open

The API came back with trails to show, and the ranger's change is gone. That's what "demo data" means.

What keeps data for real

Two things keep data through every restart. Both cost money, and this course creates neither:

  • A persistent disk: a disk attached to the service whose files survive deploys and restarts. Render's docs say only a paid service can have one; free web services can't.
  • A hosted database: a database server that runs on its own, such as PostgreSQL. Your API connects to it over the network, with its address and password in an environment variable. Render's free Postgres database expires 30 days after it's made, and Render deletes it, data and all, unless it's moved to a paid plan within 14 days.

The SQL you've learned carries over to PostgreSQL almost line for line. The connection changes; the queries mostly don't.

Say so plainly

A free link is a demo, not a home for real people's data. Say so where people will read it: the trails API's README says the live API runs on demo data, and that Render wipes it on every redeploy, restart and spin-down. Don't collect names, emails or anything a person would miss on the free tier. When a project needs to keep real data, that's the moment to choose a persistent disk or a hosted database, and to read its price first.

Sources

  • Render, Deploy for Free: a free web service's files are lost every time it redeploys, restarts or spins down; it spins down after 15 minutes without traffic; Render might restart it at any time; free web services can't have a persistent disk; free Render Postgres databases expire 30 days after creation.

Check yourself

2 questions to try on the lesson page.

So what

Knowing where your data lives, and how long it lasts there, is what lets you put an API online honestly: a demo that resets itself cleanly, not an app that quietly loses what people trust it with.

Key terms

Related