New York kdb Training 2019
Spaces still available. Register now to learn Kdb+.

Kdb IPC Inter-Process Communication

Communication amongst kdb processes is simple, efficient and supports both synchronous and asynchronous communication.

Contents

Kdb IPC Client Server Calls

The Basics

To start q on port 5000 we could use the command q -p 5000, or we can use \p 5000 from within the q console.

From one q instance hopen can be used to open a handle to another q process. Straight from the code kx website the formats supported are:

q)h:hopen `:10.43.23.198:5010                    / using ipaddress
q)h:hopen `:mydb.us.com:5010                     / using hostname
q)h:hopen `::5010                                / localhost
q)h:hopen (`:mydb.us.com:5010:elmo:sesame;10000) / full argument list, 10 second timeout
        

Message Formats

Once we have a connection there are two message formats:

handle "message"Send a string, the entire message is evaluated on the server
handle (function; arg1; arg2..)Send a list where the first item is either a
  • locally defined function
  • symbol/string for a remotely defined function
The remaining items in the list are arguments for the function.

The string format is useful when entering commands manually at the console, however since it doesn't allow passing generated values easily, the list format is more useful.

(A)Synchronous messaging

Once we have a handle we can either send a message:

Synchonouslyhandle "message"Send message, wait on and return the result.
Asynchonouslyneg[handle] "message"Send message, return to processing next statement immediately
do not wait on or return a result.

Messages that require a response like function calls or select statements will normally use the synchronous form while message that need not return like inserting updates to a table will be performed asynchronously. Inside kdb there is a messaging thread which handles the actual sending of data. All messaging is actually TCP, the messaging thread takes care of message queueing/buffering. The processing loop of kdb itself is single threaded.

Example client/server communication

Client

Server - q -p 5000

Important Variables - .z.W .z.w .z.a .z.u

.z.W Dictionary of open handles, to a list of message sizes.
e.g. 3 -> 100 200 means handle 3 has two messages queued, size 100 then 200 in bytes
.z.w When called inside a synchronous call, gives the return handle back to the caller.
.z.a Returns the current IP address, if called inside a remote call this will be the remote IP address.
.z.u Return the current users name, if called inside a remote call this is the name supplied when the connection was created.

Message Handlers - .z.pg .z.ps

When a message arrives into kdb it is processed by message handlers, these have a default setting but can be custom set by the user. For example we could override the the handler for synchronous message handling by defining .z.pg ourselves. By default .z.pg returns the value of the query. Handlers include:

.z.pg Synchronous or Get call handler, x argument is the incoming message.
Default handling is {value x} ie parse and run the query, returning result.
.z.ps Asynchronous or Set call handler, x argument is the incoming message.
Default handling is {value x;} ie parse and run the query, returning no result.
.z.po Port Open, called when a new connection is made.
.z.pc Port Close, called when an existing connection is closed.
.z.ph HTTP GET called for web requests on port 80.
.z.pp HTTP POST
.z.ws Web Sockets (new in kdb 3.0)