blog.dopana

Back

Architecting a social network platform like Twitter/X or Threads demands high write throughput, real-time newsfeed generation, and low-latency media distribution worldwide.

Using the Cloudflare Edge Ecosystem, we can design a completely Serverless Social Network backed by Cloudflare Workers, R2 Object Storage, D1 Database, KV, and Durable Objects.

1. Core Design Challenges#

Key system design requirements for a social network include:

  • Newsfeed Generation (Fanout Service): Delivering posts efficiently to thousands or millions of followers.
  • Media Storage & Delivery: Storing images/videos with zero egress fees and fast CDN caching.
  • Low-Latency Edge Caching: Reading timelines and user profiles near the user.

2. System Architecture#

The overall architecture leverages five Cloudflare primitives:

  1. Cloudflare Workers: Edge API Gateway handling authentication, post creation, and feed queries.
  2. Cloudflare R2: Object storage for media assets with zero egress cost.
  3. Cloudflare D1: Serverless SQLite database managing relational data (Users, Posts, Follows).
  4. Cloudflare KV: Edge key-value cache storing user timeline feeds.
  5. Durable Objects (DO): Stateful background engine for feed fanout processing and like/comment aggregations.
+--------------+       HTTP / Upload      +---------------------+
|  User Client | <----------------------> | Cloudflare Workers  |
+--------------+                          +---------------------+
                                            /     |          \
                                  Presigned URL   |           \ Cache Feed
                                        v         v            v
                                   +-------+  +-------+    +-------+
                                   |  R2   |  |  D1   |    |  KV   |
                                   +-------+  +-------+    +-------+
                                                  |
                                            Async Event
                                                  v
                                     +--------------------------+
                                     | Durable Object (Fanout)  |
                                     +--------------------------+
text

3. Implementation Details#

3.1. Feed Fanout with Durable Objects & KV#

When a user publishes a new post, a Durable Object worker asynchronously pushes the post ID to the followers’ KV timeline feed.

3.2. Direct Media Upload to Cloudflare R2#

Clients fetch presigned URLs from Workers to upload images and videos directly to R2 without passing heavy binary payloads through Worker memory.

src/media.ts
export async function generateUploadUrl(env: Env, filename: string): Promise<string> {
  // Generate R2 Presigned Upload URL
  const objectKey = `posts/${Date.now()}-${filename}`;
  const signedUrl = await env.MY_R2_BUCKET.createSignedUrl(objectKey, {
    expiresIn: 3600,
    method: 'PUT'
  });

  return signedUrl;
}
typescript

[!NOTE] Cloudflare R2 provides zero egress fee storage, saving massive bandwidth costs for high-resolution image and video distribution.

4. Trade-offs & Limitations#

[!TIP] Advantages:

  • Zero Infrastructure Management: No Kubernetes clusters or Redis instances to maintain.
  • Global Edge Performance: Timelines are cached and served directly at edge locations close to users.

[!WARNING] Limitations:

  • High-Follower Fanout: For accounts with millions of followers, a hybrid approach (Fanout-on-Write for active users + Fanout-on-Read from D1 for inactive users) is required.

5. References#