Java: JDBC Gateway Microservice with Chronicle Queue

Tram Ho

1. Deep dive into low-latency microservices

Together we will build a microservice that can run in its own JVM, which can execute JDBC queries and updates through persistent request and result queues.

I would consider this a Gateway Service because it interacts with a system that is outside the microservice model.

2. What does this service do?

This service supports two functions executeQuery and executeUpdate. These methods mirror the same methods as PreparedStatement except that the result is passed as a message

Two asynchronous request handling functions are declared in the following interface:

Two asynchronous result handling functions are declared in the following interface:

3. Components wrapped as a Service

Look at the executorUpdate method:

You can see that every input message produces an output message with the result. This will be useful later on to restart the service from where it started and monitor its progress, as well as obtain the results.

How to wrap this as a service:

(1) Create a topic with a meaningful name. We use ExecutorService in case we want to do something more complex with it later.

(2) Add this quest to the group

(3) Create a proxy to write to the output queue

(4) Start reading after the last message is processed successfully.

(5) Read one message at a time.

(6) Add a helper method to create a logger to this service’s input

(7) Add a helper method to read the output of this service.

4. How does it perform?

I’ve tested this writing to HSQLDB is pretty fast, even writing to a file. Using it as a Service, though, can be useful for very explosive activity as we can handle a lot more requests in a period of time.

The performance test writes 200k messages as fast as possible and waits for them all to complete. The first is the average delay to write each request and the second is the average time to get the results.

Thời gian trung bình để ghi mỗi bản cập nhật 1,5 us, thời gian trung bình để thực hiện mỗi bản cập nhật 29,7 us

Although HSQLDB can persist over 33k updates per second , (1/29.7 us), the service plan can handle write bursts of more than 660k writes per second . (1/1.5 us) This represents a 20x improvement in the continuous throughput it can support.

5. How long can a burst be?

Both Linux and Windows tend to perform well when up to 10% of main memory is “dirty” or not written to disk. For example, if you have 256 GB, you may have 25 GB of “dirty” data. Even so, if the concurrency is faster than the service consumes, but slow enough that the disk subsystem can keep up, your contigs may exceed the main memory size. In a word, if your message is 256 bytes long, the service can be slower than a billion messages and it won’t run out of memory or fail. The main limitation in this case is the free disk space you have. At the time of posting, you can buy a 1 TB Enterprise SSD for under $600, and Samsung is selling a 16 TB SSD. I expect hosting costs to continue to drop.

6. Conclusion

Building a microservice by wrapping a component with an asynchronous API with a transport for messaging in and out worked without too much hassle.

The best way to go fast is to do less work.

Share the news now

Source : Viblo