Backend Development 18 min read

Cache Consistency Strategies, Java Fundamentals, and Common Backend Interview Questions

This article explains cache consistency problems and solutions such as double‑delete and retry mechanisms, dives into Java fundamentals like HashMap internals and thread pools, and presents several backend interview coding questions with examples and code snippets.

Selected Java Interview Questions
Selected Java Interview Questions
Selected Java Interview Questions
Cache Consistency Strategies, Java Fundamentals, and Common Backend Interview Questions

Project Part: A list of typical interview questions covering project background, data flow, traffic analysis, anomaly detection, and cache‑database consistency (e.g., whether to update the database first or the cache first).

Fundamentals: Detailed explanations of HashMap implementation (bucket, linked list, red‑black tree), default capacity, thread‑unsafe scenarios, and how JDK 1.8 resolves them; differences between arrays and linked lists; common thread pool types (newCachedThreadPool, newFixedThreadPool, newSingleThreadExecutor, newScheduledThreadPool) and their parameters; volatile keyword characteristics; synchronization challenges without using synchronized/volatile; MySQL table design considerations; SQL WHERE clause tips.

Cache Consistency Discussion: Overview of why updating the cache after the database can cause dirty data, followed by three update strategies:

Update database then update cache.

Delete cache then update database.

Update database then delete cache.

Analysis of each strategy’s drawbacks, especially in concurrent scenarios, and introduction of the double‑delete (delayed) strategy with sample code:

public void write(String key, Object data) {
    redis.delKey(key);
    db.updateData(data);
    Thread.sleep(1000);
    redis.delKey(key);
}

Guidance on choosing the sleep duration based on read‑operation latency, handling read‑write splitting, and ensuring eventual consistency.

Failure Handling: When the second cache deletion fails, propose two retry mechanisms: (1) send the key to a message queue and repeatedly attempt deletion; (2) use a binlog subscription (e.g., Canal) to capture database changes and trigger cache deletion, with optional asynchronous retry.

Additional Cache Issues: Descriptions and solutions for cache penetration (using placeholder values or Bloom filters), cache avalanche (staggered TTLs, distributed locks, high‑availability), and cache “no‑bottom‑hole” (reducing mget calls, command optimization, connection pooling).

Java Thread Pool Deep Dive: Explanation of each pool type, their creation limits, thread lifecycle, and when threads may die or be recreated.

Interview Coding Problem: Given an integer array nums and an integer k , determine if the array can be split into groups of k consecutive numbers. Includes three examples (two true, one false) and constraints. Sample solution skeleton:

1 <= nums.length <= 10^5
1 <= nums[i] <= 10^9
1 <= k <= nums.length

Overall, the article provides a comprehensive guide for backend engineers preparing for interviews, covering both theoretical concepts and practical code implementations.

Javabackend developmentRedisCache ConsistencyHashMapthread poolInterview Questions
Selected Java Interview Questions
Written by

Selected Java Interview Questions

A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!

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.