1

I need to store an integer in a persistent media. The only option i can think is the disk.. so i'm trying to write a file in the cwd.

This must be done as quickly as possible, and need to assume that the files is really synced in the HD, because this must be power off safe.

I have tried a lot of approaches, and i cant get less than 200ms to write this file.

What i have tried:

  1. Write the content in the NAME of the file:

    File file = new java.io.File(".1234567890");
    file.createNewFile();
    
  2. Write the file Using FileOutputStream;

    fd = new FileOutputStream(".flush", false);
    fd.write(1234567890);
    fd.flush();
    fd.getFD().sync();
    fd.close();
    

I know the target machine is really bad, but i'm trying to optimize this. Any advice?

fabriciols
  • 959
  • 1
  • 12
  • 25

1 Answers1

4

The problem is the speed at which you can write to disk. Take the off the requirement to persist to disk it it will take less than 1/1000th of the time.

If you want to write to actual disk faster, you need a faster disk. e.g. A fast SSD can write in 50 micro-seconds.

The only way to beat the speed to disk, is to send a redundant copy to another machine on the assumption that both machines won't die at the same time. This may mean using an independent power supplies with UPS.

If you do this you can get a message to another system, round trip in well under 20 micro-seconds.


As @Thomas suggested, you can use a memory mapped file. WHile this shouldn't be any faster, it does give you more control over exactly what is persisted. e.g. the open and close of the file isn't needed.

In advance you could

RandomAccessFile memoryMappedFile = new RandomAccessFile(".flush", "rw");

// 4K small buffer
MappedByteBuffer buffer = memoryMappedFile.getChannel().map( FileChannel.MapMode.READ_WRITE, 0, 4 << 10);

when you need to write

buffer.writeLong(12345678L);
buffer.force();

as the data is persisted to disk, you don't need to close except to save resources.

So if you only time the write and force, this is the minimum you can do to persist to a disk.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130