<s>
sync	B-Language
is	O
a	O
standard	O
system	B-Operating_System
call	I-Operating_System
in	O
the	O
Unix	B-Application
operating	I-Application
system	I-Application
,	O
which	O
commits	O
all	O
data	O
in	O
the	O
kernel	B-Operating_System
filesystem	B-Application
to	O
non-volatile	B-General_Concept
storage	I-General_Concept
buffers	B-General_Concept
,	O
i.e.	O
,	O
data	O
which	O
has	O
been	O
scheduled	O
for	O
writing	O
via	O
low-level	O
I/O	B-General_Concept
system	B-Operating_System
calls	I-Operating_System
.	O
</s>
<s>
Higher-level	O
I/O	B-General_Concept
layers	O
such	O
as	O
stdio	B-Language
may	O
maintain	O
separate	O
buffers	B-General_Concept
of	O
their	O
own	O
.	O
</s>
<s>
As	O
a	O
function	O
in	O
C	B-Language
,	O
the	O
sync( )	O
call	O
is	O
typically	O
declared	O
as	O
void	O
sync(void )	O
in	O
<unistd.h>	O
.	O
</s>
<s>
The	O
system	B-Operating_System
call	I-Operating_System
is	O
also	O
available	O
via	O
a	O
command	B-Application
line	I-Application
utility	O
also	O
called	O
sync	B-Language
,	O
and	O
similarly	O
named	O
functions	O
in	O
other	O
languages	O
such	O
as	O
Perl	B-Language
and	O
Node.js	B-Language
(	O
in	O
the	O
fs	O
module	O
)	O
.	O
</s>
<s>
The	O
related	O
system	B-Operating_System
call	I-Operating_System
fsync( )	O
commits	O
just	O
the	O
buffered	O
data	O
relating	O
to	O
a	O
specified	O
file	B-Application
descriptor	I-Application
.	O
</s>
<s>
Some	O
Unix	B-Application
systems	I-Application
run	O
a	O
kind	O
of	O
flush	O
or	O
update	O
daemon	B-Operating_System
,	O
which	O
calls	O
the	O
sync	B-Language
function	O
on	O
a	O
regular	O
basis	O
.	O
</s>
<s>
On	O
some	O
systems	O
,	O
the	O
cron	B-General_Concept
daemon	B-Operating_System
does	O
this	O
,	O
and	O
on	O
Linux	B-Application
it	O
was	O
handled	O
by	O
the	O
pdflush	O
daemon	B-Operating_System
which	O
was	O
replaced	O
by	O
a	O
new	O
implementation	O
and	O
finally	O
removed	O
from	O
the	O
Linux	B-Application
kernel	B-Operating_System
in	O
2012	O
.	O
</s>
<s>
Buffers	B-General_Concept
are	O
also	O
flushed	O
when	O
filesystems	B-Application
are	O
unmounted	B-Application
or	O
remounted	O
read-only	O
,	O
for	O
example	O
prior	O
to	O
system	O
shutdown	O
.	O
</s>
<s>
In	O
order	O
to	O
provide	O
proper	O
durability	B-General_Concept
,	O
databases	O
need	O
to	O
use	O
some	O
form	O
of	O
sync	B-Language
in	O
order	O
to	O
make	O
sure	O
the	O
information	O
written	O
has	O
made	O
it	O
to	O
non-volatile	B-General_Concept
storage	I-General_Concept
rather	O
than	O
just	O
being	O
stored	O
in	O
a	O
memory-based	O
write	O
cache	O
that	O
would	O
be	O
lost	O
if	O
power	O
failed	O
.	O
</s>
<s>
PostgreSQL	B-Application
for	O
example	O
may	O
use	O
a	O
variety	O
of	O
different	O
sync	B-Language
calls	O
,	O
including	O
fsync( )	O
and	O
fdatasync( )	O
,	O
in	O
order	O
for	O
commits	O
to	O
be	O
durable	O
.	O
</s>
<s>
Turning	O
off	O
the	O
fsync	B-Language
requirement	O
can	O
therefore	O
greatly	O
improve	O
commit	O
performance	O
,	O
but	O
at	O
the	O
expense	O
of	O
potentially	O
introducing	O
database	O
corruption	O
after	O
a	O
crash	O
.	O
</s>
<s>
Databases	O
also	O
employ	O
transaction	B-General_Concept
log	I-General_Concept
files	O
(	O
typically	O
much	O
smaller	O
than	O
the	O
main	O
data	O
files	O
)	O
that	O
have	O
information	O
about	O
recent	O
changes	O
,	O
such	O
that	O
changes	O
can	O
be	O
reliably	O
redone	O
in	O
case	O
of	O
crash	O
;	O
then	O
the	O
main	O
data	O
files	O
can	O
be	O
synced	O
less	O
often	O
.	O
</s>
<s>
To	O
avoid	O
any	O
data	O
loss	O
return	O
values	O
of	O
fsync( )	O
should	O
be	O
checked	O
because	O
when	O
performing	O
I/O	B-General_Concept
operations	I-General_Concept
that	O
are	O
buffered	O
by	O
the	O
library	O
or	O
the	O
kernel	B-Operating_System
,	O
errors	O
may	O
not	O
be	O
reported	O
at	O
the	O
time	O
of	O
using	O
the	O
write( )	O
system	B-Operating_System
call	I-Operating_System
or	O
the	O
fflush( )	O
call	O
,	O
since	O
the	O
data	O
may	O
not	O
be	O
written	O
to	O
non-volatile	B-General_Concept
storage	I-General_Concept
but	O
only	O
be	O
written	O
to	O
the	O
memory	O
page	B-General_Concept
cache	I-General_Concept
.	O
</s>
<s>
Errors	O
from	O
writes	O
are	O
instead	O
often	O
reported	O
during	O
system	B-Operating_System
calls	I-Operating_System
to	O
fsync( )	O
,	O
msync( )	O
or	O
close( )	O
.	O
</s>
<s>
Prior	O
to	O
2018	O
,	O
Linux	B-Application
's	O
fsync( )	O
behavior	O
under	O
certain	O
circumstances	O
failed	O
to	O
report	O
error	O
status	O
,	O
change	O
behavior	O
was	O
proposed	O
on	O
23	O
April	O
2018	O
.	O
</s>
<s>
Hard	O
disks	O
may	O
default	O
to	O
using	O
their	O
own	O
volatile	O
write	O
cache	O
to	O
buffer	B-General_Concept
writes	O
,	O
which	O
greatly	O
improves	O
performance	O
while	O
introducing	O
a	O
potential	O
for	O
lost	O
writes	O
.	O
</s>
<s>
Tools	O
such	O
as	O
hdparm	B-Application
-F	O
will	O
instruct	O
the	O
HDD	O
controller	O
to	O
flush	O
the	O
on-drive	O
write	O
cache	O
buffer	B-General_Concept
.	O
</s>
<s>
The	O
performance	O
impact	O
of	O
turning	O
caching	O
off	O
is	O
so	O
large	O
that	O
even	O
the	O
normally	O
conservative	O
FreeBSD	B-Operating_System
community	O
rejected	O
disabling	O
write	O
caching	O
by	O
default	O
in	O
FreeBSD	B-Operating_System
4.3	O
.	O
</s>
<s>
In	O
SCSI	B-Architecture
and	O
in	O
SATA	O
with	O
Native	O
Command	O
Queuing	O
(	O
but	O
not	O
in	O
plain	O
ATA	O
,	O
even	O
with	O
TCQ	O
)	O
the	O
host	O
can	O
specify	O
whether	O
it	O
wants	O
to	O
be	O
notified	O
of	O
completion	O
when	O
the	O
data	O
hits	O
the	O
disk	O
's	O
platters	O
or	O
when	O
it	O
hits	O
the	O
disk	O
's	O
buffer	B-General_Concept
(	O
on-board	O
cache	O
)	O
.	O
</s>
<s>
Assuming	O
a	O
correct	O
hardware	O
implementation	O
,	O
this	O
feature	O
allows	O
the	O
disk	O
's	O
on-board	O
cache	O
to	O
be	O
used	O
while	O
guaranteeing	O
correct	O
semantics	O
for	O
system	B-Operating_System
calls	I-Operating_System
like	O
fsync	B-Language
.	O
</s>
<s>
Although	O
Linux	B-Application
enabled	O
NCQ	O
around	O
2007	O
,	O
it	O
did	O
not	O
enable	O
SATA/NCQ	O
FUA	O
until	O
2012	O
,	O
citing	O
lack	O
of	O
support	O
in	O
the	O
early	O
drives	O
.	O
</s>
<s>
Firefox	O
3.0	O
,	O
released	O
in	O
2008	O
,	O
introduced	O
fsync	B-Language
system	B-Operating_System
calls	I-Operating_System
that	O
were	O
found	O
to	O
degrade	O
its	O
performance	O
;	O
the	O
call	O
was	O
introduced	O
in	O
order	O
to	O
guarantee	O
the	O
integrity	O
of	O
the	O
embedded	O
SQLite	B-Language
database	O
.	O
</s>
<s>
Linux	B-Operating_System
Foundation	I-Operating_System
chief	O
technical	O
officer	O
Theodore	O
Ts'o	O
claims	O
there	O
is	O
no	O
need	O
to	O
"	O
fear	O
fsync	B-Language
"	O
,	O
and	O
that	O
the	O
real	O
cause	O
of	O
Firefox	O
3	O
slowdown	O
is	O
the	O
excessive	O
use	O
of	O
fsync	B-Language
.	O
</s>
<s>
On	O
some	O
rather	O
common	O
Linux	B-Application
configurations	O
,	O
especially	O
using	O
the	O
ext3	B-Application
filesystem	I-Application
in	O
the	O
"	O
data	O
=	O
ordered	O
"	O
mode	O
,	O
calling	O
fsync	B-Language
does	O
n't	O
just	O
flush	O
out	O
the	O
data	O
for	O
the	O
file	O
it	O
's	O
called	O
on	O
,	O
but	O
rather	O
on	O
all	O
the	O
buffered	O
data	O
for	O
that	O
filesystem	B-Application
.	O
</s>
