Installs into .claude/skills of the current project.
Sponsors
SSkills DirectorySkills Directory
Know which skills are safe — weekly.
SSkills DirectorySkills Directory
Is that skill safe to install?
Are you the author of Linkly?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/iii-hq-linkly-e2b42152)
<!-- generated by iii-skill-render. DO NOT EDIT (changes here are overwritten on the next render). Edit docs/next/tutorials/linkly/persistence.mdx. -->
Linkly's links live in `state`, which you set to in-memory back in Chapter 1. Restart the engine and
everything is gone. In this chapter you add a `database` worker (SQLite) that holds the durable
record of links and a timestamped row for every click on a short code. `state` stays in the picture
as a fast read cache in front of the database.
<Info>
`state` can also persist on its own (`store_method: file_based` with a `file_path`). This chapter
uses a dedicated `database` worker instead, which gives you durable storage plus SQL to query it.
</Info>
## Add the database worker
State is a fast cache, but you also want a durable record you can run SQL over: every link, and a
timestamped row each time someone follows one. Uncomment the Ch. 3 block in `worker-compose.yaml`,
and uncomment the `- database` line in the `start_after` list under `link`:
```yaml worker-compose.yaml
link:
worker: path://./link
start_after:
- sandbox-libkrun
# Ch. 3: the schema is created at startup, after database is up.
- database
scripts:
build: iii call sandbox::build worker=link
run: iii call sandbox::start worker=link
database:
worker: package://database
version: "latest"
config_name: database
working_dir: .
config_override:
databases:
primary:
pool:
acquire_timeout_ms: 5000
idle_timeout_ms: 30000
max: 10
url: sqlite:./data/iii.db
```
{/* TODO: Improve this doc once we can add workers w/ config in compose. Move users to compose::add. */}
<Note>
For simplicity and ease of configuration we've pre-included the database configuration and are
instructing you to uncomment entries and restart the entire project. This is not necessary, you
can also:
1. Add a worker with `iii call compose::add worker=database`
2. Configure it in `worker-compose.yaml`
3. Restart that worker with `iii call compose::restart worker=database`.
In this way you can modify a running system without restarting the entire thing.
</Note>
<Info>
The database worker supports more than SQLite, refer to the [`database` worker
docs](https://workers.iii.dev/workers/database) for all supported databases.
</Info>
Restart the project with:
```bash
iii call compose::restart
```
The links you created in Chapters 1 and 2 lived only in the in-memory `state`: the restart clears
them, and the new `links` table starts empty. Once this chapter's code is in, create again with
`POST /links` any link you still need.
The worker will be in charge of defining its own schema. We'll build up the necessary changes to
`link/src/index.ts` in pieces.
### Define the database
First add the `DB` constant **near the top of `link/src/index.ts`**:
```typescript {4} src/index.ts
import { InvocationError, registerWorker } from "iii-sdk";
import { Logger } from "@iii-dev/helpers/observability";
const DB = "primary"; // Matches the database name in worker-compose.yaml
```
### Make link storage persistent
Now we're going to adapt the existing `link::create` and `link::resolve` functions so that they
write and read from our new database while using our state worker as a hot cache.
#### Create a schema
Add an `ensureSchema()` function to `link/src/index.ts` that creates both tables on startup. The
database worker accepts SQL through its `database::execute` function. The database worker can
register a moment after `link`, so add a few retries:
<Note>
`start_after` in `worker-compose.yaml` controls worker start order. In this case we are having the
`link` worker `start_after` database. This helps with reliability but it does not guarantee that
the `database` worker will be ready when the `link` worker starts.
</Note>
```typescript src/index.ts
async function ensureSchema(): Promise<void> {
for (let attempt = 1; ; attempt++) {
try {
await worker.call({
function_id: "database::execute",
payload: {
db: DB,
sql: "CREATE TABLE IF NOT EXISTS links (code TEXT PRIMARY KEY, url TEXT NOT NULL, created_at TEXT NOT NULL)",
},
});
await worker.call({
function_id: "database::execute",
payload: {
db: DB,
sql: "CREATE TABLE IF NOT EXISTS clicks (id INTEGER PRIMARY KEY AUTOINCREMENT, code TEXT NOT NULL, clicked_at TEXT NOT NULL)",
},
});
logger.info("database: ready");
return;
} catch (err) {
if (attempt >= 30) throw err;
await new Promise((resolve) => setTimeout(resolve, 1000));
}
}
}
ensureSchema().catch((err) => logger.error("database: schema init failed", { error: String(err) }));
```
#### Setup database writing
**Modify `link::create`** to write to both the database (durable record) and `state` (hot cache).
The `code` column is the table's primary key, so the database itself refuses a second link with the
same code. `ON CONFLICT(code) DO NOTHING` turns that refusal into an insert of zero rows instead of
a database error, and `database::execute` reports the count as `affected_rows`. Zero means the code
is taken: `link::create` throws the same error as in Chapter 1, so `POST /links` keeps answering
`409` with `link::create`'s own message rather than the database's.
```typescript src/index.ts {4-17}
worker.registerFunction("link::create", async (payload: { url: string; code?: string }) => {
const code = payload.code ?? makeCode();
const url = /^https?:\/\//i.test(payload.url) ? payload.url : `https://${payload.url}`;
// ON CONFLICT DO NOTHING turns a taken code into zero inserted rows instead
// of a database error, so link::create can say so in its own words.
const { affected_rows } = await worker.call<
{ db: string; sql: string; params: string[] },
{ affected_rows: number }
>({
function_id: "database::execute",
payload: {
db: DB,
sql: "INSERT INTO links (code, url, created_at) VALUES (?, ?, ?) ON CONFLICT(code) DO NOTHING",
params: [code, url, new Date().toISOString()],
},
});
if (affected_rows === 0) throw new Error(`code \`${code}\` is already taken`);
await worker.call({
function_id: "state::set",
payload: { scope: "links", key: code, value: { url } },
});
logger.info("link created", { code, url });
return { code, url };
});
```
#### Setup database retrieval
**Modify `link::resolve` to check the cache first**; on a miss, fall back to the database and warm
the cache for the next read. It's easiest to replace the existing `link::resolve` function with our
new version:
```typescript src/index.ts
worker.registerFunction("link::resolve", async (payload: { code: string }) => {
const cached = await worker.call<{ scope: string; key: string }, { url: string } | null>({
function_id: "state::get",
payload: { scope: "links", key: payload.code },
});
if (cached) {
logger.info("link resolved", { code: payload.code, found: true });
return { url: cached.url };
}
const { rows } = await worker.call<
{ db: string; sql: string; params: string[] },
{ rows: Array<{ url: string }> }
>({
function_id: "database::query",
payload: { db: DB, sql: "SELECT url FROM links WHERE code = ?", params: [payload.code] },
});
const url = rows[0]?.url ?? null;
if (url) {
await worker.call({
function_id: "state::set",
payload: { scope: "links", key: payload.code, value: { url } },
});
}
logger.info("link resolved", { code: payload.code, found: !!url });
return { url };
});
```
## Add click tracking
Since we have a database now, you can start click tracking. Make a new function
(`link::record_click`) to do that and save it to the database. The next chapter will move this work
onto a queue so that it can run without touching the redirect's logic. Add it below `link::resolve`:
```typescript src/index.ts
worker.registerFunction(
"link::record_click",
async (payload: { code: string; clicked_at: string }) => {
await worker.call({
function_id: "database::execute",
payload: {
db: DB,
sql: "INSERT INTO clicks (code, clicked_at) VALUES (?, ?)",
params: [payload.code, payload.clicked_at],
},
});
return { recorded: true };
},
);
```
### Update `http::redirect` to call `link::record_click`
**Now update `http::redirect` to call it directly**, right before returning the redirect:
```typescript src/index.ts {14-18}
worker.registerFunction("http::redirect", async (req) => {
const code = req.path_params.code;
const { url } = await worker.call<{ code: string }, { url: string | null }>({
function_id: "link::resolve",
payload: { code },
});
if (!url) {
return {
status_code: 404,
body: { error: "link not found" },
headers: { "Content-Type": "application/json" },
};
}
// This call waits for link::record_click to finish; Chapter 4 moves the work to a queue
await worker.call({
function_id: "link::record_click",
payload: { code, clicked_at: new Date().toISOString() },
});
return { status_code: 302, headers: { Location: url } };
});
```
<Note>
The database write for clicks adds latency to every redirect. The next chapter moves it onto a
durable queue that removes the latency while also adding recovery from database failures.
</Note>
### Try the click tracking
Now let's see the click tracking in action. Save the file, create a few links, and simulate a click
on each:
```bash
for n in $(seq 1 3); do
curl -s -X POST http://127.0.0.1:3111/links \
-H 'Content-Type: application/json' -d "{\"url\":\"https://iii.dev\",\"code\":\"iii-example-$n\"}"
curl -s -o /dev/null "http://127.0.0.1:3111/s/iii-example-$n"
done
```
The durable history is now queryable with SQL:
```bash
iii call database::query db=primary sql="SELECT COUNT(*) AS clicks FROM clicks WHERE code LIKE 'iii-example-%'"
```
```json
{
"columns": [
{
"name": "clicks",
"type": ""
}
],
"row_count": 1,
"rows": [
{
"clicks": 3
}
]
}
```
<Info>
Did you know that `--help` works with function id's as well? Try running: `iii call
database::query --help` to see what arguments `database::query` accepts.
</Info>
Run the loop a second time and each `POST /links` answers `409`, because the codes are taken:
```json
{ "error": "code `iii-example-1` is already taken" }
```
The redirects still go through, so the click count above grows by three on every run.
## Conclusion
Linkly's links are now durable: the database is the source of truth, `state` keeps lookups fast, and
every redirect appends a timestamped row to the `clicks` table. But that row is written on the
redirect's hot path, so a slow database write slows the redirect. Next, in
[Ch. 4: Make it durable](./durable-execution), you move that write onto a queue so redirects stay
fast.