How to properly create a table for user messages?

I have a table with user channels in the Apache Cassandra database. And queries responsible for getting the list of channels and getting data about the channel. One of the queries requires ALLOW FILTERING, we think that the best option is to make a duplicate of the table, only it will have a primary key ((channel_id, bucket), user_id), and then the question arose, is there something like links in Apache Cassandra, so that the database does not have to constantly write and update data in both tables. If you know a solution where this does not need to be done, then I will be very grateful for the solution

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>create table channels
(
user_id bigint,
bucket int,
channel_id bigint,
flags int,
permissions bigint,
type int,
primary key ((user_id, bucket), channel_id)
)
</code>
<code>create table channels ( user_id bigint, bucket int, channel_id bigint, flags int, permissions bigint, type int, primary key ((user_id, bucket), channel_id) ) </code>
create table channels
(
    user_id     bigint,
    bucket      int,
    channel_id  bigint,
    flags       int,
    permissions bigint,
    type        int,
    primary key ((user_id, bucket), channel_id)
)
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>SELECT * FROM channels WHERE user_id = :user_id AND bucket = :bucket
</code>
<code>SELECT * FROM channels WHERE user_id = :user_id AND bucket = :bucket </code>
SELECT * FROM channels WHERE user_id = :user_id AND bucket = :bucket
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>SELECT * FROM channels WHERE channel_id = :channel_id ALLOW FILTERING
</code>
<code>SELECT * FROM channels WHERE channel_id = :channel_id ALLOW FILTERING </code>
SELECT * FROM channels WHERE channel_id = :channel_id ALLOW FILTERING

The suggestion is to create two tables and manage them on the application level. It’s called Denormalization; let me explain.

Regarding ALLOW FILTERING:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>SELECT * FROM channels WHERE channel_id = :channel_id ALLOW FILTERING
</code>
<code>SELECT * FROM channels WHERE channel_id = :channel_id ALLOW FILTERING </code>
SELECT * FROM channels WHERE channel_id = :channel_id ALLOW FILTERING

This query scans the entire dataset because channel_id is not part of the partition key. ALLOW FILTERING is a Cassandra anti-pattern. It causes inefficiencies like cluster-wide reads, timeouts, and high resource consumption.

Regarding the suggestion of duplicating the table:

Duplicating the table is the correct way, but you seem to look for “links” (like foreign keys) to automatically synchronize both tables. Cassandra doesn’t support this because it is not a relational database and embraces denormalization.

Regarding Cassandra itself:

Cassandra is designed for fast writes and scales horizontally (Write-Optimised). Duplicating data across tables is encouraged for different query patterns because writes are cheap, but reads can be expensive.
Imagine having hundreds of nodes in your cluster. You want your query to involve only the minimum amount of nodes both when you write and when you read your data, so you can easily scale horizontally if needed.
Cassandra’s schema should be designed for your queries. If you have two queries (user_id and channel_id), you will need two tables with appropriate primary keys to optimize for both.

Proposed solution:

Create two tables:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>CREATE TABLE channels_by_user (
user_id bigint,
bucket int,
channel_id bigint,
flags int,
permissions bigint,
type int,
PRIMARY KEY ((user_id, bucket), channel_id)
);
</code>
<code>CREATE TABLE channels_by_user ( user_id bigint, bucket int, channel_id bigint, flags int, permissions bigint, type int, PRIMARY KEY ((user_id, bucket), channel_id) ); </code>
CREATE TABLE channels_by_user (
    user_id     bigint,
    bucket      int,
    channel_id  bigint,
    flags       int,
    permissions bigint,
    type        int,
    PRIMARY KEY ((user_id, bucket), channel_id)
);

and

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>CREATE TABLE channels_by_channel_id (
channel_id bigint,
bucket int,
user_id bigint,
flags int,
permissions bigint,
type int,
PRIMARY KEY (channel_id, bucket)
);
</code>
<code>CREATE TABLE channels_by_channel_id ( channel_id bigint, bucket int, user_id bigint, flags int, permissions bigint, type int, PRIMARY KEY (channel_id, bucket) ); </code>
CREATE TABLE channels_by_channel_id (
    channel_id  bigint,
    bucket      int,
    user_id     bigint,
    flags       int,
    permissions bigint,
    type        int,
    PRIMARY KEY (channel_id, bucket)
);

Write to both tables simultaneously at the application level. Since writes in Cassandra are fast, this duplication will not significantly affect performance.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật