4 ms·
Here's one design that's nice to do for servers in GC'd functional languages, and hard to achieve without GC. Have the state of your application be a persistent
by ruuda 4y ago
Here's one design that's nice to do for servers in GC'd functional languages, and hard to achieve without GC. Have the state of your application be a persistent data structure (one that you can efficiently update such that both the old and new version remain available, as opposed to in-place mutation). Then hold a "current state" pointer, that gets updated atomically. Endpoints that only read the state can read the pointer and then complete the request with a consistent view of the state, while writes can be serialized, build the new state in the background, and when it's done, swap the pointer to "publish" the new state. This way reads get a consistent view without blocking writes, and writes do not block reads. (Unlike an implementation where you mutate the state in-place, and you would need to protect it with a mutex.) It is possible to do this in non-gc'd languages too, but then persistent data structures are unwieldy, and cloning the full state for every update may be prohibitively expensive.
- Lvl999Noob 4y agoDo you mean this? https://github.com/jonhoo/left-right https://github.com/jonhoo/left-right I am not sure of the performance or implementation difficulty but the data structure seems to be what you are talking about.
- ruuda 4y agoNot exactly, but it looks similar. With what I was talking about you can have as many versions around as you need. An example would be using (TVar (HashMap k v)) in Haskell.