Skip to main content

Overview

Column families provide a way to logically partition a RocksDB database. Each column family can have its own options and configuration, while sharing the same Write-Ahead Log (WAL). This enables efficient multi-tenancy and data segregation within a single database.

ColumnFamilyDescriptor

Describes a column family with its name and options.
std::string
Name of the column family. The default column family name is stored in kDefaultColumnFamilyName.
ColumnFamilyOptions
Configuration options specific to this column family

ColumnFamilyHandle

Handle to access a specific column family within the database.

GetName

const std::string&
Returns the name of the column family associated with this handle

GetID

uint32_t
Returns the ID of the column family

GetDescriptor

ColumnFamilyDescriptor*
Output parameter filled with up-to-date descriptor
This call may lock and release DB mutex to access up-to-date CF options. Pointer-typed options cannot be referenced longer than the original options exist.

GetComparator

const Comparator*
Returns the comparator of the column family

Creating Column Families

CreateColumnFamily

Create a single column family.
const ColumnFamilyOptions&
Options for the new column family
const std::string&
Name of the column family to create
ColumnFamilyHandle**
Output parameter for the column family handle
Creating many column families one-by-one is not recommended due to quadratic overheads (e.g., writing a full OPTIONS file for all CFs after each creation). Use CreateColumnFamilies() or DB::Open() with create_missing_column_families=true instead.

CreateColumnFamilies (Same Options)

Bulk create column families with the same options.
const std::vector<std::string>&
Names of column families to create
std::vector<ColumnFamilyHandle*>*
Output vector for column family handles
In case of error, the request may succeed partially. The handles vector will contain handles for successfully created column families.

CreateColumnFamilies (Different Options)

Bulk create column families with individual options.
const std::vector<ColumnFamilyDescriptor>&
Descriptors for each column family to create

Opening Database with Column Families

Open with All Column Families

You must open ALL column families in the database. Use ListColumnFamilies() to get the list of existing column families.

ListColumnFamilies

Get list of all column families in a database.
const DBOptions&
Database options (primarily for env)
const std::string&
Path to the database
std::vector<std::string>*
Output vector filled with column family names (ordering is unspecified)

Deleting Column Families

DropColumnFamily

Mark a column family for deletion.
This only records a drop record in the manifest and prevents the column family from flushing and compacting. The column family is not fully removed until all handles are destroyed.

DropColumnFamilies

Bulk drop multiple column families.
Request may succeed partially. Use ListColumnFamilies() to check the result.

DestroyColumnFamilyHandle

Release and deallocate a column family handle.
A column family is only fully removed once it is:
  1. Dropped via DropColumnFamily()
  2. All handles have been destroyed via DestroyColumnFamilyHandle()
You must call this before closing the DB (except for DefaultColumnFamily() handle).

Using Column Families

Reading and Writing

Most DB operations accept an optional ColumnFamilyHandle parameter.

Atomic Writes Across Column Families

Use WriteBatch to write atomically across multiple column families.

Iterating Column Families

Column Family Options

Each column family can have independent configuration.

Best Practices

When to Use Column Families

Good Use Cases

  • Multi-tenancy (one CF per tenant)
  • Different data types with different access patterns
  • Time-series data (one CF per time bucket)
  • Different durability/performance requirements

Avoid When

  • Very large number of column families (>100)
  • Frequently creating/deleting column families
  • All data has similar characteristics
  • Simple key prefixes would suffice

Performance Considerations

Shared WAL: All column families share the same Write-Ahead Log, so a slow flush in one CF can affect others.Compaction: Each column family has independent compaction, which can help or hurt depending on your workload.Memory: Each column family has its own memtable(s), so memory usage scales with number of CFs.

Common Patterns

Default Column Family

Every database has a default column family that always exists.
You cannot drop or destroy the default column family handle.