Sometimes KeyDB instances need to be loaded with a big amount of preexisting or user generated data in a short amount of time, so that millions of keys will be created as fast as possible.
This is called a mass insertion, and the goal of this document is to provide information about how to feed KeyDB with data as fast as possible.
#Use the protocol, Luke
Using a normal KeyDB client to perform mass insertion is not a good idea for a few reasons: the naive approach of sending one command after the other is slow because you have to pay for the round trip time for every command. It is possible to use pipelining, but for mass insertion of many records you need to write new commands while you read replies at the same time to make sure you are inserting as fast as possible.
Only a small percentage of clients support non-blocking I/O, and not all the clients are able to parse the replies in an efficient way in order to maximize throughput. For all of these reasons the preferred way to mass import data into KeyDB is to generate a text file containing the KeyDB protocol, in raw format, in order to call the commands needed to insert the required data.
For instance if I need to generate a large data set where there are billions of keys in the form: `keyN -> ValueN' I will create a file containing the following commands in the KeyDB protocol format:
Once this file is created, the remaining action is to feed it to KeyDB
as fast as possible. In the past the way to do this was to use the
netcat with the following command:
However this is not a very reliable way to perform mass import because netcat
does not really know when all the data was transferred and can't check for
supports a new mode called pipe mode that was designed in order to perform
Using the pipe mode the command to run looks like the following:
The keydb-cli utility will also make sure to only redirect errors received from the KeyDB instance to the standard output.
#Generating KeyDB Protocol
The KeyDB protocol is extremely simple to generate and parse, and is documented here. However in order to generate protocol for the goal of mass insertion you don't need to understand every detail of the protocol, but just that every command is represented in the following way:
<cr> means "\r" (or ASCII character 13) and
<lf> means "\n" (or ASCII character 10).
For instance the command SET key value is represented by the following protocol:
Or represented as a quoted string:
The file you need to generate for mass insertion is just composed of commands represented in the above way, one after the other.
The following Ruby function generates valid protocol:
Using the above function it is possible to easily generate the key value pairs in the above example, with this program:
We can run the program directly in pipe to keydb-cli in order to perform our first mass import session.
#How the pipe mode works under the hood
The magic needed inside the pipe mode of keydb-cli is to be as fast as netcat and still be able to understand when the last reply was sent by the server at the same time.
This is obtained in the following way:
- keydb-cli --pipe tries to send data as fast as possible to the server.
- At the same time it reads data when available, trying to parse it.
- Once there is no more data to read from stdin, it sends a special ECHO command with a random 20 byte string: we are sure this is the latest command sent, and we are sure we can match the reply checking if we receive the same 20 bytes as a bulk reply.
- Once this special final command is sent, the code receiving replies starts to match replies with these 20 bytes. When the matching reply is reached it can exit with success.
Using this trick we don't need to parse the protocol we send to the server in order to understand how many commands we are sending, but just the replies.
However while parsing the replies we take a counter of all the replies parsed so that at the end we are able to tell the user the amount of commands transferred to the server by the mass insert session.