Skip to content

zxuhan/like-app-backend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

High-Performance Like System

A high-concurrency like/thumbs-up system built with modern backend technologies, designed to handle massive traffic with optimized caching strategies and asynchronous processing.

Project Overview

Built with Spring Boot 3 + Redis + Apache Pulsar, this system handles massive traffic for social media-style like operations. It showcases enterprise-level solutions for cache penetration, hot key detection, eventual consistency, and real-time monitoring—capable of processing thousands of concurrent like requests with sub-100ms latency.

Tech Stack: Spring Boot 3 · MyBatis-Plus · Redis · Apache Pulsar · Caffeine · HeavyKeeper


Key Features

Intelligent Multi-Level Caching with Hot Key Detection

Implements a three-tier cache architecture (Caffeine → Redis → MySQL) with the HeavyKeeper algorithm to automatically identify and cache the top-100 most accessed keys. Only promotes keys to local cache after exceeding 10 accesses, minimizing memory overhead while maintaining cache coherence across all layers.

Asynchronous Processing with Message Queue

Event-driven architecture where all write operations are published to Pulsar, decoupling API responses from database writes. Batch consumer processes up to 1,000 messages with intelligent deduplication, applies exponential backoff retry strategies, and routes failed messages to Dead Letter Queue. This approach reduce database writes by batching operations.

Atomic Operations via Lua Scripting

Redis Lua scripts provide atomic read-check-write operations for like status, eliminating the need for distributed locks. Scripts validate user state before modifying counters, achieving zero lock contention.

Data Reconciliation System

Daily scheduled job (2:00 AM) compares Redis (source of truth) against MySQL using cursor-based scanning (1,000 keys per batch) to detect missing database records. Automatically republishes compensation events to Pulsar for any discrepancies found, with error logging and performance tracking for monitoring.

Screenshot 2025-10-09 at 21 31 17

Project Structure

thumb-backend/
├── src/main/java/com/zxuhan/thumb/
│   ├── config/
│   ├── constant/
│   ├── controller/
│   │   ├── BlogController.java                # Blog list/detail with like status
│   │   ├── ThumbController.java               # Like/unlike API + Prometheus metrics
│   │   └── UserController.java                # Login & session management
│   │
│   ├── job/
│   │   ├── SyncThumb2DBJob.java               # Scheduled sync (every 10s)
│   │   ├── SyncThumb2DBCompensatoryJob.java   # Cleanup orphaned temp keys (daily 2am)
│   │   └── ThumbReconcileJob.java             # Redis↔MySQL reconciliation (daily 2am)
│   │
│   ├── listener/thumb/
│   │   ├── ThumbConsumer.java                 # Pulsar batch consumer (1000 msgs/batch)
│   │   └── msg/ThumbEvent.java                # Event POJO (userId, blogId, type, timestamp)
│   │
│   ├── manager/cache/
│   │   ├── CacheManager.java                  # Multi-level cache coordinator
│   │   ├── HeavyKeeper.java                   # Top-K hot key detection (Count-Min-Sketch variant)
│   │   ├── TopK.java                          # Interface for hot key algorithms
│   │   └── Item.java                          # (key, count) record
│   │
│   ├── mapper/
│   ├── model/
│   ├── service/impl/
│   │   ├── ThumbServiceImpl.java              # Synchronous + local cache only
│   │   ├── ThumbServiceRedisImpl.java         # Time-sliced Redis sync
│   │   └── ThumbServiceMQImpl.java            # Async Pulsar (production mode)
│   │
│   └── util/
│       └── RedisKeyUtil.java                  # Key builders (thumb:{userId}, thumb:temp:HH:mm:ss)
│
├── src/main/resources/
└── pom.xml                                    

Quick Start

Prerequisites

  • JDK 21
  • MySQL 8.0+
  • Redis 6.0+
  • Apache Pulsar 2.10+
  • Maven 3.8+

Setup

1. Database Initialization

mysql -u root -p thumb_db < src/main/resources/create_table.sql

2. Start Redis&Pulsar

# Start Redis
redis-stack-server

# Or if you use Homebrew
brew services start redis

# Start Pulsar
pulsar standalone

3. Configuration

Update src/main/resources/application.yml:

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/thumb_db
    username: your_username
    password: your_password
  data:
    redis:
      database: 0
      host: localhost
      password: your_redis_password
      timeout: 5000
      port: 6379

4. Build&Run

# Clone repository
git clone <your-repo-url>
cd thumb-backend

# Build (skips tests for quick start)
mvn clean package -DskipTests

# Run application
java -jar target/thumb-backend-1.0.0.jar

# Or run in development mode
mvn spring-boot:run

5. Access Application

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages