Google Compute Engine instances have high-performance, enterprise-class memory that you can use to run your applications. You can allocate some of this memory to create a RAM disk with exceptionally low latency and high throughput. RAM disks work well when your application expects a file system structure and cannot simply store its data in memory. RAM disks alone do not provide any storage redundancy or flexibility, so it is best to use RAM disks in combination with other instance storage options.
RAM disks share instance memory with your applications. If your instances do not have enough memory to contain RAM disks and your applications, create instances with high-memory machine types or upgrade your existing instances to add more memory.
Before you begin
- If you want to use the command-line examples in this guide:
- Install or update to the latest version of the gcloud command-line tool.
- Set a default region and zone.
- If you want to use the API examples in this guide, set up API access.
- Read about the difference between RAM disks and other Compute Engine storage options.
Creating a RAM disk
You can create a RAM disk with the tmpfs tool, which is installed by default
on many Linux operating systems.
If your instance does not have enough available memory, you can optionally change the instance machine type to a machine type with more memory.
Connect to your instance through SSH. For this example, go to the VM instances page and click the SSH button next the instance where you want to add a RAM disk.
Create a mount point for your RAM disk.
$ sudo mkdir /mnt/ram-diskCreate and mount a new
tmpfsRAM disk. You must determine a value for thesizeproperty that meets your storage requirements without competing with your applications for memory or expending all of the available memory. For this example, the instance has an1-highmem-32machine type with 208 GB of memory, so a50gRAM disk size is appropriate.$ sudo mount -t tmpfs -o size=50g tmpfs /mnt/ram-diskAdd the RAM disk to the
/etc/fstabfile so that the device automatically mounts again if you restart the instance:$ echo 'tmpfs /mnt/ram-disk tmpfs nodev,nosuid,noexec,nodiratime,size=50G 0 0' | sudo tee -a /etc/fstab
Deleting a RAM disk
You can unmount a tmpfs RAM disk just like any other volume. This deletes the
RAM disk and any data that is stored in it. For this example, remove a RAM disk
that is mounted at /mnt/ram-disk:
$ sudo umount /mnt/ram-disk
Automatically backing up RAM disk data between instance restarts
You can back up a RAM disk before your instance restarts to preserve the RAM disk data until the instance starts up again. Back up your data to a persistent disk to preserve it.
Create and mount a persistent disk to use as a backup disk for your RAM disk. Make sure the disk is big enough to contain the information in the RAM disk.
Create a shutdown script for your instance with an
rsynccommand that writes the RAM disk contents to the backup volume. For this example, use thegcloudtool to add theshutdown-scriptmetadata to the instance with the RAM disk mounted at/mnt/ram-diskand the persistent disk mounted at/mnt/ram-disk-backup.gcloud compute instances add-metadata example-instance --metadata shutdown-script="#! /bin/bash rsync -a --delete --recursive --force /mnt/ram-disk/ /mnt/ram-disk-backup/ EOF"Optionally, you can also create a startup script that restores the files back to the RAM disk when the instance starts again. Use the
gcloudtool to add thestartup-scriptmetadata to the instance.gcloud compute instances add-metadata example-instance --metadata startup-script="#! /bin/bash rsync -a --recursive --force /mnt/ram-disk-backup/ /mnt/ram-disk/ EOF"


