Skip to main content

Overview

The ScyllaDB Rust Driver supports frame compression to reduce the amount of data transmitted over the network. Compression can significantly reduce bandwidth usage, especially for queries that return large result sets. The driver supports two compression algorithms:
  • LZ4: Fast compression with moderate compression ratios
  • Snappy: Very fast compression/decompression with lower compression ratios

Compression Enum

The Compression enum represents the available compression algorithms:
pub enum Compression {
    /// LZ4 compression algorithm.
    Lz4,
    /// Snappy compression algorithm.
    Snappy,
}

Configuring Compression

Compression is configured on the SessionBuilder using the compression() method. By default, compression is disabled (None).

Enabling LZ4 Compression

use scylla::client::session_builder::SessionBuilder;
use scylla::client::session::Session;
use scylla::client::Compression;

let session: Session = SessionBuilder::new()
    .known_node("127.0.0.1:9042")
    .compression(Some(Compression::Lz4))
    .build()
    .await?;

Enabling Snappy Compression

use scylla::client::Compression;

let session: Session = SessionBuilder::new()
    .known_node("127.0.0.1:9042")
    .compression(Some(Compression::Snappy))
    .build()
    .await?;

Disabling Compression

To explicitly disable compression (or revert after setting it):
let session: Session = SessionBuilder::new()
    .known_node("127.0.0.1:9042")
    .compression(None)
    .build()
    .await?;

How Compression Works

Compression Negotiation

When you specify a preferred compression algorithm, the driver will:
  1. Request the specified compression during the connection handshake
  2. If the server supports the requested algorithm, it will be used for all frames
  3. If the server doesn’t support it, the driver will fall back to no compression
The driver negotiates compression separately for each connection. If the database server doesn’t support your preferred compression algorithm, the session will still be established successfully without compression.

What Gets Compressed

Compression applies to the frame body only, not the frame header:
  • Request frames: Query strings, bound values, batch statements, etc.
  • Response frames: Result sets, schema information, error messages, etc.
The frame header (9 bytes) is never compressed and includes a flag indicating whether the body is compressed.

Choosing a Compression Algorithm

LZ4

Best for: General-purpose use with large result sets Advantages:
  • Good compression ratios
  • Fast compression and decompression
  • Widely supported
Use when: You need a balance between compression ratio and performance

Snappy

Best for: Ultra-low latency requirements Advantages:
  • Very fast compression/decompression
  • Low CPU overhead
  • Predictable performance
Use when: CPU efficiency and consistent latency are more important than compression ratio

Performance Considerations

When to Use Compression

Compression is beneficial when:
  • Your network bandwidth is limited
  • You’re transferring large result sets
  • Network latency is higher than compression overhead
  • You’re on a metered network connection

When to Avoid Compression

You may want to disable compression when:
  • You’re on a fast local network (e.g., same datacenter)
  • Your queries return small result sets
  • CPU resources are constrained
  • You need the absolute lowest latency

Benchmarking

Always benchmark with your specific workload. The performance impact of compression varies significantly based on:
  • Data types and patterns
  • Query result sizes
  • Network characteristics
  • Server and client CPU resources

Complete Example

Here’s a complete example demonstrating compression configuration:
use scylla::client::session_builder::SessionBuilder;
use scylla::client::session::Session;
use scylla::client::Compression;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Create a session with Snappy compression
    let session: Session = SessionBuilder::new()
        .known_node("127.0.0.1:9042")
        .compression(Some(Compression::Snappy))
        .build()
        .await?;

    // Create a keyspace
    session.query_unpaged(
        "CREATE KEYSPACE IF NOT EXISTS examples_ks \
         WITH REPLICATION = {'class': 'NetworkTopologyStrategy', 'replication_factor': 1}",
        &[],
    ).await?;

    // Use the keyspace
    session.use_keyspace("examples_ks", false).await?;

    // Create a table
    session.query_unpaged(
        "CREATE TABLE IF NOT EXISTS users (id int PRIMARY KEY, name text, data text)",
        &[],
    ).await?;

    // Insert data - compression reduces the size of the request
    let large_data = "x".repeat(10000);
    session.query_unpaged(
        "INSERT INTO users (id, name, data) VALUES (?, ?, ?)",
        (1, "Alice", large_data),
    ).await?;

    // Query data - compression reduces the size of the response
    let result = session.query_unpaged(
        "SELECT id, name, data FROM users WHERE id = ?",
        (1,),
    ).await?;

    println!("Successfully queried with compression enabled");

    Ok(())
}

Compression String Representation

The Compression enum provides methods for string conversion:
use scylla::client::Compression;
use std::str::FromStr;

// Convert to string
let compression = Compression::Lz4;
assert_eq!(compression.as_str(), "lz4");
assert_eq!(compression.to_string(), "lz4");

// Parse from string
let compression = Compression::from_str("snappy")?;
assert_eq!(compression, Compression::Snappy);

See Also

Connection Overview

Learn about basic connection configuration

TLS/SSL

Configure secure TLS connections

Build docs developers (and LLMs) love