Distributed Caching Architectures
Handling Data Discrepancies
Cache Penetration: Occurs when queries request data existing neither in the cache nor the database. Mitigation strategies include:
- Storing null values for missing keys with short expiration times to prevent redundant DB hits.
- Implementing a Bloom Filter to probabilistically verify data existence before querying deeper storage layers.
Cache Breakdown: Happens when a highly accessed key expires simultaneously, flooding the backend database. Solutions involve:
- Applying a mutex lock during the expiration window so only one thread fetches and restores the value.
- Configuring hot keys to have indefinite lifespans or longer expiration windows.
Cache Avalanche: Multiple keys expire at once, or the entire cache cluster fails, overwhelming the database. Prevention methods:
- Randomize expiration timestamps for related keys.
- Utilize distributed cluster topology for redundancy.
- Implement circuit breakers and rate limiting on the business layer.
- Employ multi-tier caching using local in-process stores alongside remote cache servers.
Data Consistency Models
Ensuring alignment between cache and database requires specific patterns:
- Delayed Double Delete: Clear cache, update DB, then wait briefly and clear cache again.
- Message Queue: Publish deletion/update events asynchronously to ensure eventual consistency.
- Binlog Monitoring: Tools like Cana listen to database transaction logs to trigger cache invalidation automatically.
Persistence Mechanisms
Redis supports two primary models:
- RDB (Snapshot): Saves memory state to disk at intervals. Offers faster recovery but risks losing recent write operations.
- AOF (Append Only): Logs every write command. Ensures durability but results in larger file sizes and potentially slower recovery.
Memory Management Strategies
When configured max memory is reached, eviction policies determine which keys are removed:
- No Eviction: Prevents writes once memory limits are hit.
- TTL Based: Preferring keys closer to expiration (
volatile-ttl). - Random Removal: Selecting random keys regardless of TTL status.
- LRU/LFU: Reomving based on access frequency or recency (
allkeys-lru,allkeys-lfu).
Distributed Synchroniztaion
Achieving locking across nodes typically relies on SET key value NX EX. Key considerations:
- Ensure expiration time is set to avoid deadlock scenarios.
- Perform acquisition and expiration atomically.
- Verify ownership before releasing locks to ensure cleanup only occurs if the lock holder actually holds it.
- Implement Watchdog mechanisms to renew lease times automatically for long-running processes.
- For high availability, consider RedLock algorithms distributing locks across multiple independent instances.
Network Topology
- Master-Slave: Handles read/write separation via replication.
- Sentinel: Monitors master health and automates failover.
- Cluster: Enables horizontal scaling through sharding.
Database Optimization
Performance Tuning
Identify bottlenecks using slow query logs. Common offenders include complex aggregations, deep pagination, and unindexed joins.
Analyze execution plans using EXPLAIN:
- Check
keyusage and index length (key_len). - Review scan types (
type) for full table scans. - Look for "Using index" vs "Using temporary" indicators in the
extracolumn.
Index Structures
Relational databases typically employ B+ Trees due to efficient range queries and stable disk I/O performance compared to binary trees.
- Clustered Index: Stores row data within the tree itself (one per table).
- Non-Clustered Index: Separate leaf node pointers referencing the clustered index.
- Covering Index: Retrieves all needed data directly from the index, eliminating the need to lookup table rows.
Write Optimization
To optimize deep pagination (LIMIT offset, size), join the result with a subquery filtering IDs first:
SELECT main_data.* FROM target_table AS main_data
INNER JOIN (
SELECT id FROM target_table ORDER BY id LIMIT 9000 OFFSET 10
) AS filtered_ids ON main_data.id = filtered_ids.id;
Isolation and Logging
- ACID: Supported via locks and MVCC (Multi-Version Concurrency Control).
- Redo Log: Records physical page modifications to guarantee durability after a crash.
- Undo Log: Maintains previous data versions for rollback and consistent reads.
- Isolation Levels: Address phenomena like Dirty Reads, Non-Repeatable Reads, and Phantom Reads.
Enterprise Framework Internals
Spring AOP and Transactions
Notification types include Around, Before, After, Returning, and Throwing. Common Transaction Pitfalls:
- Methods lacking public visibility.
- Static methods bypassing proxies.
- Catching exceptions within the same class without rethrowing.
- Self-invocation calls skipping proxy mechanisms.
- Declared exception types not matching rollback rules (usually only unchecked exceptions trigger rollbacks by default).
Lifecycle Management: Bean creation follows: Instantiation -> Property Injection -> Aware Interfaces -> Pre-BPP -> Initialization -> Post-BPP -> Usage -> Destruction.
Circular Dependencies: Resolved via three-level caching (Singleton Objects, Early Singles, Object Factories). Constructor injection remains a blocker for circular references.
Request Processing Pipeline
- Front Controller (
DispatcherServlet) intercepts requests. - Handler Mapping locates the appropriate controller.
- Handler Adapter invokes the logic.
- ViewResolver maps the return
ModelAndViewto a template. - Response rendering occurs.
Auto-Configuration: Triggered by @EnableAutoConfiguration, scanning META-INF/spring.factories for conditional bean definitions.
MyBatis Workflow
- Configuration loads into
SqlSessionFactory. - Sessions manage database connections.
- Executor interfaces handle query caching and statement preparation.
- Result mapping converts SQL results to objects.
- Lazy Loading uses dynamic proxies (
CGLib) to defer initialization until property access.
Spring Cloud Features
Idempotency: Achieved via unique tokens combined with Redis checks, database unique constraints, or optimistic locking. Scheduling: XXL-JOB provides distributed job management, fault tolerance, and sharding capabilities.
Concurrency and JVM Memory
Thread Safety
synchronized utilizes monitors tracking owners and waiting queues.
- Biased Locking: Optimizes for uncontended synchronization.
- Lightweight Locking: Uses CAS on stack frames.
- Heavyweight Locking: Escalates to OS mutex contention.
AQS (AbstractQueuedSynchronizer)
Foundation for ReentrantLock and others. Maintains a FIFO queue and a state integer managed via Compare-And-Swap (CAS) to control resource access.
Memory Layout
- Program Counter: Thread-local instruction pointer.
- Heap: Shared object storage (Young Gen, Old Gen, Metaspace).
- Stack: Frame storage for method execution variables.
- Method Area: Loaded classes, constants, static fields.
Class Loading: Involves Loading, Verification, Preparation, Parsing, and Initialization steps before bytecode execution begins.