Ttoydb online

/// a database you can read end-to-end

The key-value store you can actually understand.

ToyDB is a tiny, durable Redis-style database built from scratch. See every moving part: memory, expiry, TCP, and persistence.

0 dependencies · native Map · RESP over TCP

toydb — local session

toydb> SET session:user_42 online EX 3600

+OK

toydb> GET session:user_42

"online"

toydb> TTL session:user_42

3598

12

keys

1.2k

commands

0ms

memory read

Pure JavaScript

no framework, no magic

AOF durable

replayable on restart

RESP protocol

talk over TCP

Live preview

inspect every key

01 built for understanding

Small enough to trace.
Real enough to use.

The implementation keeps the interesting systems problems visible instead of hiding them behind a client library.

MAP

Native Map keyspace

Every key lives in a plain JavaScript Map. No abstraction layer — the grid you see is the data structure itself.

TTL

Active expiry

A sweeper ticks every 250ms. Watch countdown bars drain and keys vanish the instant their time hits zero.

AOF

Append-only log

Every mutation is serialized as raw RESP. Hit BGREWRITEAOF and watch hundreds of lines collapse into a minimal log.

TCP

Concurrent clients

Open simulated sockets and fire bursts at once. The single-threaded event loop serializes them — just like Redis.

PUB

Pub/Sub fan-out

Subscribe in one session, publish in another, and trace each message as it fans out across channels.

OPS

Real-time metrics

Ops per second, memory, hit/miss ratio and channel counts — sampled every second into live sparklines.

02 under the hood

The life of a command.

Five stages, one event loop. Every command you type follows this exact path — and the console shows each effect as it happens.

  1. 01

    Parse

    The raw line is tokenized, quotes respected, command uppercased.

  2. 02

    Dispatch

    The command table routes to a handler; arity is checked.

  3. 03

    Mutate

    The Map is updated and an expiry deadline is recorded.

  4. 04

    Persist

    Writes are appended to the AOF as RESP arrays.

  5. 05

    Reply

    A typed reply — +OK, bulk string, integer or error — returns.

appendonly.aof

*3
$3

SET

$9

visits:42

$1

7

# 412 lines → 38 after BGREWRITEAOF

03 durability

Crash-proof, by replay.

Every write is appended in the same wire format clients speak. Restart means re-reading the log. Compaction rewrites it to the smallest set of commands that rebuilds today's state.

04 command set

Speak Redis.

SET key value [EX s]store a string, optionally expiring
GET keyread a value, counts hit or miss
DEL / EXISTS keyremove or check keys
KEYS patternglob match across the keyspace
TTL / EXPIRE / PERSISTinspect and control expiry
INCR / DECR keyatomic integer counters
SUBSCRIBE / PUBLISHmessage channels
FLUSHALL / DBSIZEwipe or count everything

05 questions

Is there a real server?+

No. The whole engine runs inside your browser tab. The TCP clients are simulated sessions sharing one store — the concepts are identical.

Does data survive a refresh?+

Not by design — it's in-memory. The AOF panel shows exactly what a real server would replay to rebuild state on restart.

Who is this for?+

Anyone curious how Redis-style databases work: students, backend engineers prepping for interviews, or teams teaching systems basics.

Stop reading. Start typing.

The console is already running. Your first key is one SET away.

Open the live console →