designing a cache aggregation layer for a high-concurrency Key-Value (KV) storage system. The underlying storage medium is Non-Volatile Memory (NVM).
- Random write performance is poor.
- Sequential write performance is excellent.
- Goal: Convert random concurrent insertion operations into batched, sequential writes to optimize NVM performance.
need to implement two core functions:
- insert(key, value):
- Must be thread-safe in a high-concurrency environment
- Restriction: You cannot use a Global Lock, as it would become a major performance bottleneck.
- flush():
- Must efficiently collect and aggregate data inserted by multiple threads.
- Must write the aggregated data to the underlying storage in a single, sequential batch.
- Thread Safety: How to ensure safety without global locks (e.g., lock-free mechanisms or fine-grained locking).
- High-Efficiency Insertion: How to manage the data buffer to handle rapid ingestion.
- Efficient Aggregation (flush): How to gather data from multiple threads and execute a batch sequential write.
- Currently DO NOT need to consider data retrieval (read) or indexing structures; focus strictly on the write path (aggregation and persistence).
- Assume the NVM write interface is a standard memory copy (memcpy) to a persistent memory region.
- Ensure the code reflects high-concurrency control principles and guarantees data integrity during the flush operation.
- High Concurrency
- Cache Aggregation
- NVM (Non-Volatile Memory)
- Thread-Safe
- Global Lock
- Fine-grained Lock
- Lock-free
- Batch Sequential Write
clang++ main.cpp -o main -std=c++20 -pthread
./main
hexdump -c ./nvm_store.bin | head