Redis & Caching (50 Qs)•Part 3: Advanced Internals, Scale & Production Patterns (Senior / Lead)
What causes Fork Latency and Copy-On-Write (COW) Memory Spikes during RDB/AOF saves?
senior LevelredisAdvanced Internals
In Simple Words:
When executing BGSAVE or BGREWRITEAOF, Redis calls fork() to spawn a child process:
- Linux duplicates page tables. If the instance uses 32 GB of RAM with thousands of memory pages,
fork()can stall the main thread for 50-200ms! - Copy-On-Write (COW): Memory pages are shared between parent and child until written to. If the application has heavy write traffic during the save, the OS copies entire modified memory pages, potentially doubling the server's RAM consumption!
- Ensure servers have at least 2x memory allocated or disable Transparent Huge Pages (THP).