memcp: An open-source high-performance column-based in-memory database as an alternative to proprietary analytical databases

memcp: An open-source high-performance column-based in-memory database as an alternative to proprietary analytical databases

In the last 13 years, there has been a lot of innovation in the field of column-based databases. This is largely thanks to a German ERP manufacturer who decided to distance themselves from the market leader and do their own thing. It became a success story. It is time for open source to catch up.

In the commercial database world, column-based databases have long since established themselves. The open-source world is still lagging behind with MariaDB and PostgreSQL. This hinders not only open-source projects but all products that rely on open-source databases.

For this reason, in this article we present the memcp , a column-based in-memory database that can handle both OLAP and OLTP workloads efficiently.

Advantages of column-based databases over row-based storage

Especially more complex applications such as ERP systems often have tables with over a hundred columns. Most of the time, many columns do not even contain a value. However, row-based databases must reserve memory for every storage cell accordingly.

At the same time, there are many database queries that only work on 2 columns - usually a foreign key used for filtering and a value column used for summing. Example:

SELECT SUM(lineItem.quantity * lineItem.price) FROM lineItem WHERE lineItem.line = [ID]

In this SQL query, for example, the total sales amount is calculated from a list of sales - a quite common use case. The crux: If you use a row-based database like MySQL, you always have to read the complete database rows from the hard drive to multiply the two columns quantity and price. The columns unit, item number, description, VAT rate, etc. are simply "attached" to the data record.

Column-based storage does not have this problem. In a column-based storage, all columns are stored separately from each other in memory. While this makes reading a single "complete" row slightly slower, the overall performance of the system increases more significantly in comparison.

Column-based databases also allow for compression and optimization

While database rows are structured very complexly - a database row has numerous columns and each column can have a different data type - column-based storage is simpler: You simply have a huge homogeneous list of numbers.

This makes calculating with large amounts of data easier. For example, numbers can be "bit-compressed", meaning: If the column only contains zeros and ones, you don't need 64 bits, but only a single bit to represent the number.A memory saving of up to 64 times!

The same applies to strings: They can be "dictionary-compressed". If the Gender column, for example, only contains the alternating values "male" and "female", traditional databases store the complete string. With dictionary compression, you would assign a number to each word and could store the values with a memory consumption of 1 bit per value instead of 8 bytes.A saving of 256 times!

The balance between OLAP and OLTP

For OLTP workloads, however, row-based storage still has the upper hand. A typical OLTP query would be, for example, creating a new record or changing a single piece of data.

In a row-based database, you only change the one row - if you use a pre-compressed column storage instead, it may be that you have to resolve the entire compression, reallocate the memory, and rebuild the entire column storage.

But there is a solution for that: You simply combine both worlds.

In the so-called Main Store (engl. "Main Storage") the old unchanged data is stored column-wise. The main storage is performant for statistics and never changes. Indices are also created in the main storage to enable faster data searching.

In the Change Storage (engl. "Delta Storage") instead, deletion marks as well as row-based new or changed data records are stored. Inserting into the change storage is very fast because no compression is applied.

At certain intervals, the changes from the change storage are subsequently "solidified" by building a new main storage from the old main storage and the change storage. Thus, if the interval is 15 minutes, full OLAP performance is available for all new data after 15 minutes at the latest, while OLTP workloads can still be written to the change storage with full performance.

Taming OLAP workloads through distributed computing

A major user of OLAP databases are so-called BI tools (Business Intelligence). These programs allow for the analysis and sifting of data. The problem here: the more data that accumulates, the slower the analytics become. A click in the BI program then takes several seconds up to minutes or even hours.

However, a solution is possible: Analytical database queries can be very easily parallelized. Instead of one CPU calculating the sum over all data records, the memory can be divided, for example, across 8 different computing nodes. The command to calculate the sum is then distributed across all 8 nodes, and the result is then calculated from the 8 partial sums.

If you want, for example, every click in the BI tool to take no longer than 100ms of calculation time, you can calculate exactly what the ratio of RAM to CPU cores must be. In this way, you could assign 1 GiB of RAM to one CPU core.

In terms of products, you would, for example, sell a mini-server consisting of a Raspberry Pi with 4 cores and 8 GiB of RAM for the first 8 GiB of data in the database. As soon as the user has more than 8 GiB of data, they buy a second mini-server and so on - so that for a defined amount of memory, sufficient calculation time is always available to also "process" the memory.

Rethinking databases

In addition to considerations for the best possible storage, compression, and parallelization, the communication with the outside world should also be reconsidered.

Most databases have a SQL-frontend so that the application can communicate with the storage. However, there are also other query languages, such as SPARQL, a language with which one can query RDF data from the semantic web. With this graph-based query language, it is possible to query, for example, wiki data - for example, the names of all capitals with more than 1 million inhabitants located in a country with the form of government democracy. Various NoSQL databases also have their own completely unique query languages.

Currently, it was the case that such alternative databases would have to be programmed from scratch, as there is no unified storage engine. With memcp, it is different:

memcp first implements a column-based storage engine that can be read from and populated with data using a simple interface. At the same time, memcp functions as an HTTP server and accepts requests. Depending on the plugin, one can now establish any endpoints on the HTTP server that can access the data.

For example, an SQL endpoint could translate incoming SQL queries into the internal query language and return the results row by row. The same is possible for the SPARQL query language.

At the same time, however, one could already program complete REST APIs directly in the database. This would eliminate the separation of processes between application and database, allowing for another significant increase in performance - especially if the database data is closely linked with "specialized algorithms" - e.g., an AI.

The workhorse here is the time-tested functional programming language Scheme, a LISP dialect characterized by its simplicity and extensibility. We have in the memcp even developed a serialization routine for Scheme, so that calculation tasks could be interrupted in the middle of calculation, sent over the network, and continued on the next computer.

Conclusion

While memcp will not revolutionize the world of in-memory databases - that has already been done by the proprietary project - it will open the technology for free applications and thus advance humanity. And of course - (and this is now my personal motivation) - to rob the main competitor of Launix of its competitive advantage.

Categories