actu-image
Inżynieria oprogramowania - 11 maj 2026

kdb+ Tick Architecture: Building a Real-Time Market Data System using Java and q [Part 3]

Welcome back to the third and final part of our series on building a kdb+ tick architecture. In the previous parts, we explored the core concepts and successfully launched a full ecosystem including the Tickerplant, RDB, HDB, and Gateway.

Now, we are stepping outside the q ecosystem to integrate our architecture with Java. Using the javakdb driver, we will build services capable of querying, subscribing, and publishing data to our tick system.


The Java Client for kdb+

The javakdb driver is a lightweight data marshaller that allows Java applications to communicate with kdb+ over TCP/IP. It revolves around a single class c and four essential methods:

  • c(): Establish a connection.
  • c.k(): Send a synchronous message (wait for response).
  • c.ks(): Send an asynchronous message (fire and forget).
  • c.close(): Terminate the connection.

Prerequisite: Ensure your tick architecture is running (Tickerplant on port 5010, RDB on port 5011) before launching your Java services.


Querying kdb+ from Java

The most common use case is fetching data from the RDB. In Java, a kdb+ table is represented by a c.Flip object.

// Connect to RDB
c conn = new c("localhost", 5011, "");

// Execute query
c.Flip result = (c.Flip) conn.k("select from trade where sym=`MSFT");

// Access data
int rowCount = c.n(result.y[0]);
for (int i = 0; i < rowCount; i++) {
    System.out.println(c.at(result.y[1], i)); // Print symbol column
}

Subscribing to Live Data

To receive real-time updates, your Java service must subscribe to the Tickerplant. The conn.k() method (without arguments) blocks until a new message is pushed by the TP.

// Subscribe to all symbols on the trade table
conn.k(".u.sub", "trade", "");

while (true) {
    Object response = conn.k(); // Blocking call
    Object[] msg = (Object[]) response;
    c.Flip data = (c.Flip) msg[2];
    // Process live data...
}

Publishing to the Tickerplant

You can also act as a Feed Handler by pushing data to the Tickerplant using asynchronous calls (ks).

// Send random trade data every second
Object[] row = new Object[]{ new c.Timespan(), "AAPL", 150.25, "B" };
conn.ks(".u.upd", "trade", row);

Acting as a Server for kdb+

Instead of Java polling kdb+, you can set up a Java ServerSocket. This allows kdb+ to push data directly to your application, turning your service into a passive consumer.

ServerSocket ss = new ServerSocket(5050);
c conn = new c(ss); // Accept connection from q
while (true) {
    Object msg = conn.k();
    // Handle pushed data from q script
}

Bridging Ecosystems

By mastering the Java-to-kdb+ bridge, you can now integrate high-performance tick data into broader enterprise microservices and custom analytics pipelines.

Need help optimizing your kdb+ infrastructure or building robust Java wrappers? Our consultants are ready to assist.

Talk to a MARGO Expert
What is c.Flip in the javakdb driver?

A c.Flip represents a kdb+ table. It consists of an array of column names (x) and an array of arrays containing the column data (y).

Should I use sync (k) or async (ks) calls for publishing?

For high-frequency publishing (Feed Handler), async calls (ks) are preferred to avoid blocking the sender while waiting for an acknowledgment.

Can Java handle kdb+ specific types like Timespan?

Yes, the driver includes specific classes like c.Timespan or c.Month to ensure perfect type mapping between the two languages.