How do I ensure that data is securely and reliably written to disk?

219 篇文章 2 订阅
214 篇文章 1 订阅

https://access.redhat.com/solutions/43912

 SOLUTION 已验证 - 已更新 2014年六月19日22:32 - 

English 

环境

  • Red Hat Enterprise Linux 4
  • Red Hat Enterprise Linux 5
  • Red Hat Enterprise Linux 6

问题

  • It is important for many applications to ensure that data is safely written to the disk and that in the event of a power failure the data would still be there when power is restored. Data can be written out as part of a transaction with another data source and once it is written to disk the data is deleted from the data source. Therefore, it is imperative to know that the information is secure on the disk, otherwise the data could be lost forever.

决议

There are 3 levels of caching involved in writes to disk in Red Hat Enterprise Linux :

1. Application caching in Glibc :

  • Here a successful completion of fflush() should guarantee that the data is out of user space. But, you still have to worry about power failure, kernel error, disk-system caches when this occurs.

2. File system caching:

  • The file system holds information in memory (kernel page cache) before committing to disk. This can be flushed to hardware using the fsync() call.
  • You can chose to use O_DIRECT. This will bypass the filesystem cache leaving the application to mange the cache itself. Databases use this interface but there have been issues with this interface. See this link for more details.

3. Hardware caching:

  • The hardware can cache file write operations before physically writing to disk. Write caching can be disabled on some drives :

    • IDE drives can be queried using hdparm -I; write caching is enabled if there is a * next to Write cache. hdparm -W can be used to turn off write caching.
    • SCSI drives can be queried using sdparm. Use sdparm --get=WCE to check whether the write cache is enabled and sdparm --clear=WCEto disable it.
  • See the following discussions on the Postgress project for more reading on dealing with this :

  • Another option is to enable write barriers in the file system. But there have been big performance issues with barriers. See here and here for more information on write barriers.

An application that is sensitive to data integrity should use our supported file system mode - data ordered mode - and using fsync() on the file when you want data to persist. We do support O_DIRECT, but that is more complex to get right. You also need to understand your storage and how it works - you can definitely lose data even if you do everything right in the app & we do it right in the kernel, but the storage is misconfigured or buggy.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值