Blog › What one Node process holds in memory: the state behind a chat server
What one Node process holds in memory: the state behind a chat server
A chat service looks like it has a lot of machinery. For a service the size of Chatzyo, the surprising truth is that nearly everything it knows at any moment fits in a handful of variables inside one Node.js process. That makes it simple. It also decides what happens on the worst day: the day the process restarts.
What the server remembers
Chatzyo's matching server keeps these as ordinary in-memory objects:
| What it remembers | What it's for |
|---|---|
| A set of people looking for a video chat | Who is searching |
| The waiting list | Who gets matched next |
| A map from each connection to its current room | Knowing who is paired, and cleaning up when a pairing ends |
| A map of private rooms and who is in them | Letting two people share a room and noticing when one leaves |
| The names people have chosen, each held by one connection | Making sure two people never use the same name at once |
| Short-lived relay credentials issued to each connection | Reusing them until they are close to expiring, instead of asking Cloudflare again |
| Token counters for each connection | Ignoring a connection that sends events far faster than normal use |
| Recent pairings: who was matched with whom, kept for about two minutes after a pairing ends | Checking a report against what really happened |
| Counters of how often each network opens connections or files reports | Limits and abuse control |
| People a reporter or blocker should not be matched with again | Keeping a reported or blocked person away from that connection |
The server also contains code for text rooms and private calls that I inherited from the first Chatzyo site; it is switched off on this one. There is no database behind any of it. When a person connects, entries appear. When they disconnect, the server removes them. The server does keep separate log files of joins and reports, but it never writes pairings, rooms or waiting lists to disk, so none of that state survives when the process stops.
Why it's built this way
For this kind of service, in-memory state has real advantages. Every lookup is instant. There is nothing to configure, back up or keep in sync. A pairing is useful only while both people are connected, so storing it adds work and no value. The data are also about people who are there right now; a database full of last Tuesday's waiting lists would be clutter.
What happens on a restart
A restart wipes all of it at once:
- Every connection drops. Each browser sees its connection close, and Socket.IO starts trying to reconnect.
- The waiting list is empty. Nobody who was waiting is remembered.
- Every pairing is forgotten. The server doesn't tell the partners that their pairing ended, because the process that knew about it no longer exists.
- Temporary mutes vanish. Anything that lived only in memory goes with it.
For the people connected, the effect depends on the client. A browser that re-requests a match on reconnect recovers within seconds and lands in a fresh pairing. A browser that doesn't will sit on a frozen screen until its owner presses Next. For a call in progress, the two browsers are connected directly and may keep sending video for a while after the server disappears; the people just see a conversation that has no server behind it anymore.
This is why Chatzyo.live treats the server's memory as disposable. It assumes that after any reconnect, the server knows nothing about it, and starts again.
One process means one pool
Because the waiting list lives in one process, everyone has to connect to that same process to be matched. This is also why a pool belongs to one server. Chatzyo.live and Chatzyo.in run separate servers, so each has its own waiting list. Matching is a property of the server a page points at.
The flip side is that you can't add a second server for capacity without more work. Two processes would have two separate waiting lists, and a person on one could never be matched with a person on the other. Scaling this design means moving the waiting list into something both processes can see, such as Redis, or routing everyone to the right process.
The rule of thumb
Keep state in memory when it lasts as long as a connection and is useless afterwards. Move it out of memory when losing it would hurt someone: an account, a record of a decision, a safety report. A chat server's pairings are the first kind. A moderation record is the second, and that's the one to keep out of a variable.
For how a pairing is made and torn down, see random matching on Socket.IO.