This repository contains a minimal social feed API that demonstrates Cassandra as a query-driven, append-optimized database. The stack is designed to run locally inside a kind Kubernetes cluster running on one physical machine: a 7-node local Kubernetes cluster (1 control-plane, 6 workers), a six-node Cassandra ring modeled as two datacenters with three racks each, one FastAPI app, schema bootstrap via a Kubernetes Job, MetalLB for LoadBalancer IP allocation, and ingress-nginx for HTTP routing.
This demo is intentionally centered on the data model rather than feature breadth. Cassandra is a good fit here because the core read paths are narrow, predictable, and ordered by time:
- fetch the latest posts for one user
- fetch the latest home-feed items for one user
- fetch the followers of one author during fanout
Those access patterns map naturally to Cassandra partitions and clustering columns. Instead of normalizing data and joining it later, the service writes denormalized rows that make each read path a single-partition query.
Stores the canonical user identifier and username.
CREATE TABLE users (
user_id uuid PRIMARY KEY,
username text
);Supported query:
- look up whether a user exists by
user_id
Stores follower relationships partitioned by the followed user.
CREATE TABLE followers_by_user (
user_id uuid,
follower_id uuid,
followed_at timestamp,
PRIMARY KEY ((user_id), follower_id)
);Supported query:
- get all followers of a user during fanout
Stores a user’s profile timeline as a time-ordered wide row.
CREATE TABLE posts_by_user (
user_id uuid,
post_time timeuuid,
post_id uuid,
body text,
PRIMARY KEY ((user_id), post_time)
) WITH CLUSTERING ORDER BY (post_time DESC);Supported query:
- get the newest posts for one user
Stores each user’s precomputed home feed as immutable events.
CREATE TABLE feed_by_user (
user_id uuid,
event_time timeuuid,
actor_id uuid,
post_id uuid,
body text,
PRIMARY KEY ((user_id), event_time)
) WITH CLUSTERING ORDER BY (event_time DESC);Supported query:
- get the newest feed items for one user
The partition key determines where the data lives and which rows a query can target efficiently.
- In
posts_by_user, the partition key isuser_id. One user’s posts live together. - In
feed_by_user, the partition key is alsouser_id. One user’s feed is one partition. - In
followers_by_user, the partition key is the followed user’suser_id, which makes follower lookups efficient during fanout.
The clustering key determines order within a partition.
post_timeorders posts newest-first inside a user’s profile partition.event_timeorders feed items newest-first inside a user’s feed partition.
This is the core Cassandra idea the project is trying to highlight: model tables around the exact reads you need.
When a user creates a post, the API does three things synchronously:
- inserts the post into
posts_by_user - reads the author’s followers from
followers_by_user - inserts one feed row into
feed_by_userfor each follower
That makes home-feed reads cheap because they are just a single-partition lookup. The tradeoff is duplicate storage and more expensive writes.
User deletion is implemented as a soft delete rather than a synchronous multi-table cleanup.
- the
usersrow gets adeleted_attimestamp - deleted users no longer appear in normal user reads
- deleted users cannot create posts or follows
- follower lists filter out deleted followers
- feed reads filter out items whose
actor_idnow points at a deleted user
This keeps writes simple and avoids a cross-table delete workflow in the request path. Old denormalized rows may still exist in Cassandra, but the API stops serving them.
The app can optionally log Cassandra query traces for every repository-backed endpoint. This is not a relational EXPLAIN plan. Instead, it uses Cassandra request tracing and logs which coordinator handled the query plus the recorded trace events.
Available env vars:
CASSANDRA_TRACE_ENABLED=true|falseCASSANDRA_TRACE_SAMPLE_RATE=0.0-1.0CASSANDRA_TRACE_LOG_EVENTS=true|falseCASSANDRA_TRACE_MAX_WAIT_SECONDS=5
When enabled, the app logs:
- one HTTP request log line with
request_id, method, path, status, and whether Cassandra tracing was on - one Cassandra trace log line per repository query
- optional trace event lines if
CASSANDRA_TRACE_LOG_EVENTS=true
The deployment manifest currently enables tracing in app.yaml. Because the local cluster now runs six Cassandra nodes across two logical datacenters, the coordinator field is useful for demonstrating how the driver routes requests around the ring.
- The service duplicates post content into follower feeds.
- Popular accounts create the classic Cassandra "celebrity problem" because one post can fan out to many followers.
- The demo uses a six-node Cassandra ring for local development, but it is still not production-grade.
- There is no pagination, authentication, ranking, or async fanout in this version.
- Usernames are not unique and follow relationships are idempotent only because the Cassandra primary key overwrites duplicates.
POST /users
Content-Type: application/json
{
"username": "alice"
}POST /follow
Content-Type: application/json
{
"follower_id": "2c6d3f77-89dc-4a5d-a6af-349f03de7c6b",
"followed_id": "f90e4a5d-f4bc-40c7-89cc-7e72a2467d84"
}POST /posts
Content-Type: application/json
{
"user_id": "f90e4a5d-f4bc-40c7-89cc-7e72a2467d84",
"body": "Hello world"
}GET /usersThis endpoint is for demo convenience. In Cassandra it requires scanning the users table, so it is not the query-first path the rest of the schema is optimized for.
GET /users/{user_id}DELETE /users/{user_id}This marks the user deleted. It does not synchronously remove every follower, post, or feed row from Cassandra.
GET /follow?followed_id={user_id}GET /users/{user_id}/posts?limit=50GET /posts?user_id={user_id}&limit=50GET /users/{user_id}/feed?limit=50GET /feed?user_id={user_id}&limit=50app/: FastAPI app, route handlers, fanout service, config, and Cassandra repositoryschema.cql: keyspace and tablesk8s/: raw manifests for kind, MetalLB config, Cassandra, schema bootstrap, app deployment, and ingresstests/: bash demo script for exercising the live API
- Docker
kindkubectl- Python 3.12+
- Docker networking that allows the host to reach the
kindbridge subnet
This repo’s Makefile defaults to the current ingress-nginx cloud manifest and MetalLB native manifest as of March 21, 2026:
https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.14.3/deploy/static/provider/cloud/deploy.yaml
https://raw.githubusercontent.com/metallb/metallb/v0.15.3/config/manifests/metallb-native.yaml
If either upstream version changes, override INGRESS_NGINX_MANIFEST or METALLB_MANIFEST when you run make up.
make upThis creates the kind cluster, installs MetalLB, installs ingress-nginx, starts a six-node Cassandra ring, initializes the schema, and deploys the API.
The kind cluster itself is now multi-node, but all of those Kubernetes nodes are still Docker containers on this same machine. Cassandra is pinned to the 6 worker nodes and modeled as:
dc1/rack1,dc1/rack2,dc1/rack3dc2/rack1,dc2/rack2,dc2/rack3
The default MetalLB pool is 172.19.255.200-172.19.255.250. That assumes the standard Docker kind bridge subnet on Linux. If your local Docker network uses a different subnet, update k8s/metallb-config.yaml before running make up.
make ingress-ipThen map your hostname to that IP:
<INGRESS_IP> social-feed.local
make deployThis rebuilds the image, loads it into kind, reapplies the app manifests, and restarts the social-feed-api deployment. It does not re-run Cassandra or schema bootstrap.
If you are upgrading an existing install to the 2-datacenter, 6-rack topology, rebuild the data plane with:
make clean
make upCheck the workload state:
kubectl get nodes
kubectl get pods -n social-feed
kubectl get pods -n social-feed -o wide
kubectl get ingress -n social-feed
kubectl logs job/social-feed-schema-init -n social-feed
kubectl get svc -n ingress-nginx ingress-nginx-controller
kubectl exec -n social-feed cassandra-dc1-rack1-0 -- nodetool statusThen call the health endpoint:
curl http://social-feed.local/healthzIf you do not want to edit host resolution, you can still call the ingress IP directly:
curl http://<INGRESS_IP>/healthz -H 'Host: social-feed.local'Run the bash demo script against the live ingress:
make testTo show multi-node Cassandra behavior directly, compare the ring view and the coordinator fields in the app logs:
kubectl exec -n social-feed cassandra-dc1-rack1-0 -- nodetool status
kubectl logs -n social-feed deployment/social-feed-api --tail=200 | grep cassandra_tracenodetool status should show three UN nodes in dc1 and three UN nodes in dc2, with racks rack1, rack2, and rack3 in each datacenter. The trace logs include coordinator=<pod-ip> values that identify which Cassandra node coordinated each request.
kubectl get nodes should show one control-plane node and six worker nodes for the local kind cluster. kubectl get pods -n social-feed -o wide lets you see which Kubernetes node each Cassandra pod landed on.
This resolves the current ingress LoadBalancer IP and runs smoke_live.sh with the correct base URL and Host header.
If you want a direct shell smoke test, run:
tests/smoke_live.shIt resets the app, creates Alice/Bob/Carol, adds follow relationships, creates posts, and prints the resulting profile/feed responses with curl and jq. You can override the target with SOCIAL_FEED_BASE_URL and SOCIAL_FEED_HOST_HEADER.
With the API reachable, the required demo sequence is:
- create Alice, Bob, and Carol
- Bob follows Alice
- Carol follows Alice
- Alice creates a post
- fetch Alice’s posts
- fetch Bob’s feed
- fetch Carol’s feed
Example with curl and jq:
BASE_URL=http://social-feed.local
ALICE_ID=$(curl -s "$BASE_URL/users" \
-H 'Content-Type: application/json' \
-d '{"username":"alice"}' | jq -r '.user_id')
BOB_ID=$(curl -s "$BASE_URL/users" \
-H 'Content-Type: application/json' \
-d '{"username":"bob"}' | jq -r '.user_id')
CAROL_ID=$(curl -s "$BASE_URL/users" \
-H 'Content-Type: application/json' \
-d '{"username":"carol"}' | jq -r '.user_id')
curl -s "$BASE_URL/follow" \
-H 'Content-Type: application/json' \
-d "{\"follower_id\":\"$BOB_ID\",\"followed_id\":\"$ALICE_ID\"}"
curl -s "$BASE_URL/follow" \
-H 'Content-Type: application/json' \
-d "{\"follower_id\":\"$CAROL_ID\",\"followed_id\":\"$ALICE_ID\"}"
curl -s "$BASE_URL/posts" \
-H 'Content-Type: application/json' \
-d "{\"user_id\":\"$ALICE_ID\",\"body\":\"Hello world\"}"
curl -s "$BASE_URL/users/$ALICE_ID/posts?limit=50"
curl -s "$BASE_URL/users/$BOB_ID/feed?limit=50"
curl -s "$BASE_URL/users/$CAROL_ID/feed?limit=50"The repo keeps a single shell-based demo script in smoke_live.sh. It is meant for manual showcasing rather than Python-based automated testing.