Running Cassandra 3.0.16, we observed a major performance regression affecting SELECT DISTINCT keys-style queries against certain tables. Based on some investigation (guided by some helpful feedback from Benjamin on the dev list), we tracked the regression down to two problems.
- One is that Cassandra was reading more data from disk than was necessary to satisfy the query. This was fixed under
CASSANDRA-10657in a later 3.x release.
- If the fix for
CASSANDRA-10657is incorporated, the other is this code snippet in RebufferingInputStream:
The gist of it is that to skip bytes, the stream needs to read those bytes into memory then throw them away. In our tests, we were spending a lot of time in this method, so it looked like the chief drag on performance.
We noticed that the subclass of RebufferingInputStream in use for our queries, RandomAccessReader (over compressed sstables), implements a seek() method. Overriding skipBytes() in it to use seek() instead was sufficient to fix the performance regression.
The performance difference is significant for tables with large values. It's straightforward to evaluate with very simple key-value tables, e.g.:
CREATE TABLE testtable (key TEXT PRIMARY KEY, value BLOB);
We did some basic experimentation with the following variations (all in a single-node 3.11.2 cluster with off-the-shelf settings running on a dev workstation):
- small values (1 KB, 100,000 entries), somewhat larger values (25 KB, 10,000 entries), and much larger values (1 MB, 10,000 entries);
- compressible data (a single byte repeated) and uncompressible data (output from openssl rand $bytes); and
- with and without sstable compression. (With compression, we use Cassandra's defaults.)
The difference is most conspicuous for tables with large, uncompressible data and sstable decompression (which happens to describe the use case that triggered our investigation). It is smaller but still readily apparent for tables with effective compression. For uncompressible data without compression enabled, there is no appreciable difference.
Here's what the performance looks like without our patch for the 1-MB entries (times in seconds, five consecutive runs for each data set, all exhausting the results from a SELECT DISTINCT key FROM ... query with a page size of 24):
and with the fix:
The long initial runs for the uncompressible data presumably come from repeatedly hitting the disk. In contrast to the runs without the fix, the initial runs seem to be effective at warming the page cache (as lots of data is skipped, so the data that's read can fit in memory), so subsequent runs are faster.
For smaller data sets, RandomAccessReader.seek() and RebufferingInputStream.skipBytes() are approximately equivalent in their behavior (reducing to changing the position pointer of an in-memory buffer most of the time), so there isn't much difference. Here's before the fix for the 1-KB entries:
The effect is similar for the 25-KB entries, which might enjoy a slight performance benefit from the patch (perhaps because they're larger than the default buffer size defined in RandomAccessReader). Before:
In short, this looks like a pretty straightforward performance win with negligible cost. (It's worth noting that for our use case, disabling sstable compression is clearly the best solution, but there's still reasonably clear benefit from this minor fix for data sets with larger, compressible values, as well as presumably data sets with a mix of compressible and uncompressible values in environments where storage is limited.)