· 4 min read

Inside a race room: three lifetimes and a chat

A room's record, its lobby listing and its live presence each have their own clock. Plus the chat that follows new messages without stealing your place.

Shivank Pandey

"Active room" can mean three things: a room record exists, the room shows in the public lobby, or sockets are connected to it right now. Typestrix keeps those in three different places with three different clocks. Knowing the split explains how rooms work, and why a room can vanish from the lobby while people are still racing in it.

Three clocks

Pick a step, or use the arrows, to walk through the diagram.

The three clocks are independent: none of them looks at the others.

Postgres stores the invitation target

Creating a room needs a signed-in session and a body that passes roomSchema from @repo/shared. The row holds the name, an eight-character invite code, mode, mode option, creator (userId), privacy flag, and timestamps. isPrivate defaults to true both in the schema and in the route, so a room is unlisted unless the creator asks otherwise.

Codes come from nanoid's customAlphabet over 32 characters with I, O, 0 and 1 removed, so a code read aloud or off a screen is hard to mistype. That gives 32^8, about 1.1 trillion, possible codes. The @unique constraint on code catches a collision, but the route does not retry: a collision would surface as a generic "Failed to create room" 500. At this code space that is very unlikely, but it is a different policy from player tags, which do retry.

The creator is stored on the row. The current host is a separate Redis value, because hosting moves when people leave.

The lobby is a recent list, not a live one

export function getActivePublicRooms(): Promise<Room[]> {
    const since = new Date(Date.now() - ACTIVE_ROOM_WINDOW_MS);
    return prisma.room.findMany({
        where: { isPrivate: false, createdAt: { gte: since } },
        take: 20,
        orderBy: { createdAt: "desc" },
    });
}

The /multiplayer page (server-rendered, signed-in only) and the public room API both call this one function. It never joins Redis presence, so:

  • a listed room can be empty, and
  • a busy room older than six hours is not listed.

"Recent public rooms" is the accurate name; "rooms with people in them" is not. The @@index([isPrivate, createdAt(sort: Desc)]) matches the filter and the order exactly, so Postgres reads the newest public rows straight off the index instead of scanning the table.

Private means unlisted

A private room is left out of the lobby. Anyone with its code can still join, so the code works like a shared link rather than a password. That keeps inviting friends as simple as pasting a code, which is the point of a casual race room.

Live presence belongs to Redis

Pick a step, or use the arrows, to walk through the diagram.

Step through a join, a host handoff, and the last member leaving.

All three keys expire a day after their last write. The keys are coordination data. The room's long-term record is the Postgres row.

Saving settings is two writes, not one

Pick a step, or use the arrows, to walk through the diagram.

Two separate writes: one durable, one live.

This is not one transaction across Postgres and Redis. If the database write succeeds and the socket message never arrives, the room record has the new settings while players see the old ones until they reload. Keeping the durable write first means the worst case is a stale screen, never lost settings.

A chat that does not steal your place

A room chat has two jobs that pull against each other: stay on the newest message, and let someone read older ones without being yanked back down. The chat keeps one small piece of state for that, "is the reader at the bottom?", instead of scrolling on every message.

Pick a step, or use the arrows, to walk through the diagram.

Step through one incoming message. The dashed path is a reader scrolled up.

Your own messages only appear once the server echoes them back, so the chat never shows something the room did not actually receive. The slow-mode countdown on the send button mirrors the host's setting so you are not left guessing, but the server is what enforces it.

There is no chat history on the server: messages travel over the same Pub/Sub channel as race events, which keeps nothing. Joining late starts with an empty chat, which suits a room whose whole life is measured in hours.

Cleanup is daily, by creation time

A scheduled job removes rooms once a day. It deletes by creation time, not by last activity or updatedAt, so a room that people are actively using is deleted on schedule like any other. It does not touch Redis; the presence keys expire on their own clock.

Keep reading