Keep the data in RAM for microsecond answers, and decide how to survive a restart.
An in-memory database keeps its data in RAM instead of on a disk. RAM answers in microseconds, many times faster than even a fast disk.
A normal database keeps the real data on disk and uses a bit of RAM as a helper (a buffer cache). An in-memory database flips this: RAM is the main home of the data. The structures can be designed for RAM, with no need to arrange data in disk pages, so lookups are very short.
The big question is: what happens when the machine restarts? RAM is wiped. There are four common answers:
| Plan | How it works | You can lose | Cost |
|---|---|---|---|
| None (cache only) | Data lives only in RAM | Everything on restart | Fastest. Needs a source to refill from. |
| Snapshots | Write the whole dataset to disk every N seconds or minutes | Changes since the last snapshot | Low per write, but a snapshot uses CPU and disk |
| Log, flush every second | Append each change to a log, force it to disk once a second | About one second of writes | Small slowdown |
| Log, flush every write | Force every change to disk before saying done | Nothing that was acknowledged | Much slower writes |
| Replication | Send each change to another machine (asynchronous or waiting for it) | Changes the follower did not receive yet (async) | Network and an extra machine |
| Snapshot + log together | Use the snapshot as a base and the log for what came after | Depends on the log flush setting | Faster restart, more files to manage |
Everything must fit in RAM (or in the RAM of the whole cluster). When it does not, you either evict old data (fine for a cache), shard across more machines, or move cold data to a cheaper store.
A simplified model with a steady, fixed write stream and no random numbers. The write speeds and the 0.5 s follower lag are illustrative: real speed depends heavily on the disk, and real products add more options.
| Term | Meaning in easy words |
|---|---|
| In-memory database | A database that keeps its working data in RAM |
| RAM | Fast, temporary memory that is wiped when power is lost |
| Persistence | Saving data to disk so it survives a restart |
| Snapshot | A full copy of the data written to disk at one moment |
| Append-only log (AOF / WAL) | A file where every change is added to the end, to be replayed after a crash |
| fsync / flush | Forcing data from memory buffers onto the physical disk |
| Replication | Copying changes to other machines |
| Eviction | Removing some data to free memory when RAM is full |
| TTL | Time to live: the time after which a key is removed automatically |
| Cold start | A restart where memory is empty and must be reloaded or refilled |
| Data loss window | The amount of recent writes that could disappear in a crash |
| Choice | We gain | We lose |
|---|---|---|
| No persistence | Top speed, simplest | All data on restart |
| Snapshots only | Cheap and compact | Minutes of data at risk |
| Log flushed every second | Good balance | Up to about a second of writes |
| Log flushed every write | No acknowledged loss | A big drop in write speed |
| Asynchronous replica | Fast failover, small loss | Lag means a few recent writes can be lost |
| Synchronous replica | Almost no loss | Each write waits for the network |