Backend Development 14 min read

Implementing a Like/Unlike Feature with Spring Cloud, Redis Caching, and Periodic Persistence

This article demonstrates how to design and implement a high‑frequency like/unlike functionality in a Spring Cloud application by using Redis as a cache, persisting data to MySQL at two‑hour intervals, and automating the sync with Quartz scheduled jobs.

Java Captain
Java Captain
Java Captain
Implementing a Like/Unlike Feature with Spring Cloud, Redis Caching, and Periodic Persistence

The tutorial explains a complete solution for a like/unlike feature built on Spring Cloud, where user actions are first stored in Redis and later persisted to a relational database every two hours to reduce database load.

1. Redis cache design and implementation

Docker command to run Redis:

docker run -d -p 6379:6379 redis:4.0.8

Spring Boot integration adds the spring-boot-starter-data-redis dependency in pom.xml and enables caching with @EnableCaching on the main class.

Redis configuration class ( RedisConfig ) creates RedisTemplate<String, Object> and StringRedisTemplate beans using Jackson2JsonRedisSerializer for JSON serialization.

Redis data structures: the solution uses a Hash where each entry key is likedUserId::likedPostId and the value is 1 for like or 0 for unlike, allowing easy retrieval of all like records.

2. Redis service layer

Interface RedisService defines methods to save a like, cancel a like, delete a record, increment/decrement like counts, and fetch all stored data.

Implementation RedisServiceImpl uses RedisTemplate to operate on the hash keys defined in RedisKeyUtils and to scan entries for batch processing.

Utility class RedisKeyUtils builds the composite key and defines constant map names.

Enum LikedStatusEnum provides codes for LIKE (1) and UNLIKE (0).

3. Database design

SQL creates table user_like with columns id , liked_user_id , liked_post_id , status , create_time , and update_time . The corresponding JPA entity UserLike maps these fields.

4. Database operations

Repository UserLikeRepository (not shown) is used by LikedService which defines CRUD methods, pagination queries, and two synchronization methods: transLikedFromRedis2DB and transLikedCountFromRedis2DB .

Implementation LikedServiceImpl retrieves data from RedisService , merges it with existing records, and updates the MySQL tables within transactional boundaries.

5. Periodic persistence with Quartz

Quartz dependency spring-boot-starter-quartz is added. Configuration class QuartzConfig defines a durable JobDetail for LikeTask and a trigger that fires every two hours.

The job class LikeTask extends QuartzJobBean and, when executed, calls the two synchronization methods of LikedService to move likes and like counts from Redis to MySQL.

Additional notes mention the need for atomic operations and a shutdown hook to flush remaining Redis data before the application stops.

Overall, the article provides a step‑by‑step guide, complete code snippets, and architectural rationale for building a scalable like system using Spring Boot, Redis, MySQL, and Quartz.

RedisSpring BootMySQLQuartzlike-system
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.