Handle concurrent queries in KDB with Port Reuse
KDB allows for port reuse enabling multiple processes to share a single port. The kernel will attempt to distribute the load evenly across worker processes. However, not all queries are equal and could cause clients wait on busy worker while while other remain idle.
To deal with busy servers, a client wraps their query in a fetch
function that makes a one-shot query with a short timeout window. When the "timeout" error occurs, the client will reattempt to connect to the server. It will repeat this until the remote process is able to service the request.
/ enable port reuse
q -p rp,5000
q -p rp,5000
/ client fetch function
fetch:{[hp;pld]@[{`::[(x;100);y]}[hp];pld;{[f;err]$[(0N!err)~"timeout";get f;'err]}[(.z.s;hp;pld)]]}
/ Usage
fetch["::5000"] ({system"sleep 30";`done};`)
fetch["::5000"] "1+1"