This page is intended to set you on the right path to tuning RocksDB if you are a Java user.

Introductory information on how to set up and tune RocksDB as a C++ user is to be found in: Setup Options and Basic Tuning. Further, detailed information on how to tune RocksDB as a C++ user is to be found in: RocksDB Tuning Guide. You should refer to that guide for definitive information.

Java Tuning Example

This is a translation of the tuning of two example parameters using Java, instead of C++. It is based on the one in Setup Options and Basic Tuning. With this example in hand, you should be able to translate any of the tuning parameters described there into their Java equivalent.

Write Buffer Size

This can be set either per Database and/or per Column Family.

Global Write Buffer Size

This is the maximum write buffer size used throughout the database. It is overridden when the option is set for a column family.

It represents the amount of data to build up in memory (backed by an unsorted log on disk) before converting to a sorted on-disk file. The default is 64 MB.

You need to budget for 2 x your worst case memory use. If you don’t have enough memory for this, you should reduce this value. Otherwise, it is not recommended to change this option.

In C++ the option is set thus

  1. options.write_buffer_size = 64 << 20;

An equivalent piece of Java, wrapped with database opening, is this:

  1. final int WRITE_BUFFER_SIZE = 64 << 20;
  2. final Options options = new Options().setWriteBufferSize(WRITE_BUFFER_SIZE);
  3. try (final RocksDB db = RocksDB.open(options,
  4. dbFolder.getRoot().getAbsolutePath())) {
  5. // do some work
  6. } catch (final RocksDBException rocksDBException) {
  7. rocksDBException.printStackTrace();
  8. }

Because this option is a mutable option, you can also set it during a run of the database

  1. try (final RocksDB db = RocksDB.open(new Options(),
  2. dbFolder.getRoot().getAbsolutePath())) {
  3. // do some work
  4. final int WRITE_BUFFER_SIZE = 64 << 20;
  5. final MutableColumnFamilyOptions mutableOptions =
  6. MutableColumnFamilyOptions.builder()
  7. .setWriteBufferSize(WRITE_BUFFER_SIZE)
  8. .build();
  9. db.setOptions(mutableOptions);
  10. } catch (final RocksDBException rocksDBException) {
  11. rocksDBException.printStackTrace();
  12. }

Column Family Write Buffer Size

This is the maximum write buffer size used for an individual Column Family. You can set this option at database open, or you can modify it later.

In C++ the option is set thus

  1. cf_options.write_buffer_size = 64 << 20;

Set the mutable column family option:

  1. try (final RocksDB db = RocksDB.open(new DBOptions(),
  2. dbFolder.getRoot().getAbsolutePath(), columnFamilyDescriptors, columnFamilyHandles)) {
  3. // do some work
  4. final int WRITE_BUFFER_SIZE = 64 << 20;
  5. final MutableColumnFamilyOptions mutableOptions =
  6. MutableColumnFamilyOptions.builder()
  7. .setWriteBufferSize(WRITE_BUFFER_SIZE)
  8. .build();
  9. final ColumnFamilyHandle newColumnFamilyHandle = columnFamilyHandles.get(1);
  10. db.setOptions(newColumnFamilyHandle, mutableOptions);
  11. } catch (final RocksDBException rocksDBException) {
  12. rocksDBException.printStackTrace();
  13. }

Max Total WAL Size

This option applies to the whole database only.

  1. options.max_total_wal_size = 64 << 28;

You can set it at open

  1. final long MAX_TOTAL_WAL_SIZE = 64L << 28;
  2. final Options options = new Options().setMaxTotalWalSize(MAX_TOTAL_WAL_SIZE);
  3. try (final RocksDB db = RocksDB.open(options,
  4. dbFolder.getRoot().getAbsolutePath())) {
  5. // do some work
  6. } catch (final RocksDBException rocksDBException) {
  7. rocksDBException.printStackTrace();
  8. }

or, because it is a mutable database option, you can set it at runtime, like this

  1. try (final RocksDB db = RocksDB.open(new Options(),
  2. dbFolder.getRoot().getAbsolutePath())) {
  3. // do some work
  4. final long MAX_TOTAL_WAL_SIZE = 64L << 28;
  5. final MutableDBOptions mutableDBOptions =
  6. MutableDBOptions.builder()
  7. .setMaxTotalWalSize(MAX_TOTAL_WAL_SIZE)
  8. .build();
  9. db.setDBOptions(mutableDBOptions);;
  10. } catch (final RocksDBException rocksDBException) {
  11. rocksDBException.printStackTrace();
  12. }