Back to Projects

Reddit Engine

Actor model based reddit-like engine with REST API interface and real-time messaging capabilities.

2024-10-20156 stars8.2k views
GoProtocol BuffersREST APISQLite

A comprehensive social media platform backend implementing Reddit-like functionality using modern Go patterns and protocols.

Project Overview

The Reddit Engine demonstrates advanced backend architecture patterns through a scalable, actor-based system that handles social media interactions efficiently.

Core Features

1. Actor Model Architecture

Built on the actor model pattern for:

  • Concurrent message processing
  • Fault isolation between components
  • Scalable parallel execution
  • State management without shared memory

2. RESTful API Design

Comprehensive REST API supporting:

  • User management and authentication
  • Post creation and management
  • Comment threading and replies
  • Voting systems (upvotes/downvotes)
  • Subreddit management

3. Real-time Messaging

WebSocket-based real-time features:

  • Live comment updates
  • Instant vote counting
  • Real-time notifications
  • Active user presence

Technical Architecture

Go Implementation

package main import ( "context" "database/sql" "encoding/json" "log" "net/http" "github.com/gorilla/mux" "github.com/gorilla/websocket" _ "github.com/mattn/go-sqlite3" ) type RedditEngine struct { db *sql.DB upgrader websocket.Upgrader actors map[string]*Actor } type Actor struct { ID string Messages chan Message State interface{} } type Message struct { Type string `json:"type"` Payload interface{} `json:"payload"` }

Database Schema

SQLite database with optimized schema:

-- Users table CREATE TABLE users ( id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT UNIQUE NOT NULL, email TEXT UNIQUE NOT NULL, password_hash TEXT NOT NULL, karma INTEGER DEFAULT 0, created_at DATETIME DEFAULT CURRENT_TIMESTAMP ); -- Posts table CREATE TABLE posts ( id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL, content TEXT, author_id INTEGER REFERENCES users(id), subreddit_id INTEGER REFERENCES subreddits(id), upvotes INTEGER DEFAULT 0, downvotes INTEGER DEFAULT 0, created_at DATETIME DEFAULT CURRENT_TIMESTAMP ); -- Comments table with threading CREATE TABLE comments ( id INTEGER PRIMARY KEY AUTOINCREMENT, content TEXT NOT NULL, author_id INTEGER REFERENCES users(id), post_id INTEGER REFERENCES posts(id), parent_id INTEGER REFERENCES comments(id), upvotes INTEGER DEFAULT 0, downvotes INTEGER DEFAULT 0, created_at DATETIME DEFAULT CURRENT_TIMESTAMP );

Protocol Buffers

Used Protocol Buffers for efficient serialization:

syntax = "proto3"; package reddit; message Post { int64 id = 1; string title = 2; string content = 3; int64 author_id = 4; int64 subreddit_id = 5; int32 upvotes = 6; int32 downvotes = 7; int64 created_at = 8; } message Comment { int64 id = 1; string content = 2; int64 author_id = 3; int64 post_id = 4; int64 parent_id = 5; int32 upvotes = 6; int32 downvotes = 7; int64 created_at = 8; } service RedditService { rpc CreatePost(CreatePostRequest) returns (Post); rpc GetPost(GetPostRequest) returns (Post); rpc CreateComment(CreateCommentRequest) returns (Comment); rpc VotePost(VoteRequest) returns (VoteResponse); }

API Endpoints

User Management

POST /api/v1/users/register POST /api/v1/users/login GET /api/v1/users/profile PUT /api/v1/users/profile DELETE /api/v1/users/account

Post Operations

GET /api/v1/posts # List posts POST /api/v1/posts # Create post GET /api/v1/posts/{id} # Get specific post PUT /api/v1/posts/{id} # Update post DELETE /api/v1/posts/{id} # Delete post POST /api/v1/posts/{id}/vote # Vote on post

Comment Operations

GET /api/v1/posts/{id}/comments # Get post comments POST /api/v1/posts/{id}/comments # Create comment PUT /api/v1/comments/{id} # Update comment DELETE /api/v1/comments/{id} # Delete comment POST /api/v1/comments/{id}/vote # Vote on comment

Performance Features

Caching Strategy

  • In-memory caching for hot posts
  • Redis integration for session management
  • Query result caching for popular content
  • CDN integration for static assets

Database Optimization

  • Indexed queries for fast lookups
  • Connection pooling for efficiency
  • Query optimization with EXPLAIN analysis
  • Batch operations for bulk updates

Real-time Processing

// WebSocket handler for real-time updates func (r *RedditEngine) handleWebSocket(w http.ResponseWriter, req *http.Request) { conn, err := r.upgrader.Upgrade(w, req, nil) if err != nil { log.Printf("WebSocket upgrade error: %v", err) return } defer conn.Close() // Subscribe to real-time updates updates := make(chan Message, 100) r.subscribeToUpdates(updates) for { select { case msg := <-updates: conn.WriteJSON(msg) case <-req.Context().Done(): return } } }

Testing Strategy

Unit Tests

func TestCreatePost(t *testing.T) { engine := setupTestEngine() defer engine.cleanup() post := &Post{ Title: "Test Post", Content: "This is a test post", AuthorID: 1, SubredditID: 1, } created, err := engine.CreatePost(post) assert.NoError(t, err) assert.Equal(t, post.Title, created.Title) assert.Greater(t, created.ID, int64(0)) }

Integration Tests

  • API endpoint testing with HTTP clients
  • Database integration testing
  • WebSocket functionality testing
  • Load testing for performance validation

Performance Benchmarks

  • Concurrent user simulation
  • Database query performance
  • Memory usage profiling
  • Network latency testing

Security Features

Authentication & Authorization

  • JWT-based authentication
  • Role-based access control
  • Session management
  • Password hashing with bcrypt

Data Protection

  • SQL injection prevention
  • XSS protection
  • CSRF tokens
  • Rate limiting

Input Validation

func validatePost(post *Post) error { if len(post.Title) < 3 || len(post.Title) > 300 { return errors.New("title must be between 3 and 300 characters") } if len(post.Content) > 10000 { return errors.New("content cannot exceed 10000 characters") } return nil }

Deployment & Operations

Docker Configuration

FROM golang:1.21-alpine AS builder WORKDIR /app COPY go.mod go.sum ./ RUN go mod download COPY . . RUN CGO_ENABLED=1 GOOS=linux go build -o reddit-engine FROM alpine:latest RUN apk --no-cache add ca-certificates sqlite WORKDIR /root/ COPY --from=builder /app/reddit-engine . COPY --from=builder /app/migrations ./migrations EXPOSE 8080 CMD ["./reddit-engine"]

Monitoring & Logging

  • Structured logging with structured JSON
  • Metrics collection with Prometheus
  • Health check endpoints
  • Error tracking and alerting

Results & Impact

Performance Metrics

  • Response time: < 100ms for 95% of requests
  • Throughput: 1000+ concurrent users
  • Uptime: 99.9% availability
  • Resource usage: Efficient memory and CPU utilization

Learning Outcomes

This project provided deep insights into:

  • Concurrent programming patterns in Go
  • Distributed system design
  • Real-time web applications
  • Database optimization techniques
  • API design best practices

The experience reinforced the importance of choosing the right tools and patterns for scalable backend systems.