I use bash. I'm sure zsh has something similar as hooks.
This is my setup.
PROMPT_COMMAND variable is set to a function that "history 1", pulling the last command entered. That command is piped into a command line tool that pushes a single line into a Redis list. The list is trimmed to no more than 10000 entries.
Ctrl-R is bound to a function that uses a tool to connect to Redis, fetches the list, finds uniques, and pushes them as the input to fzf. The selected command becomes a shell variable BUFFER. Finally, READLINE_LINE=${BUFFER} and READLINE_POINT=${#READLINE_LINE}, thus inserting the ${BUFFER} into the command line and moving the cursor to the end of it.
This allows me to edit the pulled command.
All systems/vms point to the same redis running on a server and using the same list key, thus allowing for a shared command line history. If the server is unreachable, the hooked functions use shell's history instead of the redis server to fetch last set commands( fewer lines, obviously ). In additional to that failure to connect to redis server switches makes the shell prompt two lines where the first line says '[Command history server unreachable/shell server cli tools not found]' and the second line becomes a standard prompt.
I have been running this for 2 years with zero issues. Initially I thought doing connect->push->disconnect will be too slow, but I'm not noticing it much, even over the Wifi. I've been thinking of converting this service into a container to add an archive of when, where and what was typed in addition to the current setup.