TweakCode
Building Blocks lessons

Databases

How Databases work

Before you start

  • Interfaces

    The redirect is a primary-key read. That lesson writes the API and the row these blocks serve.

    Open lesson

Why it matters

A database choice made from the product name invites unnecessary machinery. This shortener performs only two important operations: insert a mapping and retrieve it from a short code, at about 40 creates per second and a few terabytes after years. Redirect must remain one primary-key read, and every additional index or store must justify a real query.

Example

Pick a store for short links

You need to insert a mapping and later read it by short code. Volume is the estimate from the method course: about 40 writes a second and a few terabytes after years.

Mental model

The idea

Keep one example in view

Pick a store for short links

You need to insert a mapping and later read it by short code. Volume is the estimate from the method course: about 40 writes a second and a few terabytes after years.

Keep this exact scenario and its constraint in view while you build the model.

For roughly 40 mapping inserts per second and a few terabytes, use one relational links table keyed by short code; x7Kp stays a primary-key read, and owner_id earns a secondary index only if owners list their links.

Use a links table whose primary key is code. Creating x7Kp inserts one row; redirecting x7Kp follows the B-tree to that row in only a few page reads, even among billions. A relational primary comfortably handles the estimated write rate and preserves uniqueness for aliases.

Add an owner_id index only if “list my links” is required, because every index adds work and storage to each insert. Full-text search, graph traversal, and large-object retrieval are absent, so search, graph, and object stores would solve queries this row never receives.

Pro

The rest of this is on Pro

The idea above is free. The walkthrough, the cost, and the practice are on Pro.