Search code examples
kdb

How to push tables through socket


Considering the setup of kdb+ tick, how do the tables get pushed through the sockets?

In tick, it's possible to subscribe with a process (let's say) a to the tickerplant, which will then proceed to push the data of the subscribed 'tickers' to a as new data arrives.

I would like to do the same but I was wondering how. As far as I know, inter-process communication between q process is just the ability to transport commands from one process to the other, such that the commands will be executed on the other.

So how is it then possible to transport a complete table between processes?

I know the method which does this in tick is .u.pub and .u.sub, but it's not clear to me how the tables are transported between the processes.

So I have two questions:

  • How does kdb+ tick do this?
  • How can I push a table from one process to the other in general?

Solution

  • Let's understand the simple process of doing this:

    We have one server 'S' and one client 'C'. When 'C' calls .u.sub function, that function code connects to 'S' using its host and port and call a specific function on 'S' (lets say 'request') with subscription parameters.

    On getting this request, 'S request' function makes following entries to its subscribtion table which it maintains for subscription request.

    -> Host and port of Client(incoming request)

    -> Subscription params (for ex. clients send sym `VOD.L for subscription)

    Now when 'S' gets any data update from feed, it goes thorugh it's subscription table and check the entries whose subscription param column value (sym in our case) matches with incoming data. Then it makes connection to each of them using their host and port from table and call their 'upd' function with new data.

    Only thing is, client should have 'upd' function defined on their side.

    This is a very basic process. KDB+ uses this with extra optimizations and features. For ex. more optimized structure for maintaining subscription table,log maintenance, replaying logs, unsubscription ,recovery logic, timer for publishing and lot more.

    For more details, you can check definition of functions in 'u' namespace.