For performance, Couchbase Server prefers to store and provide all the information to clients using RAM. However, this is not always possible or desirable in an application. Instead, what is required is the 'working set' of information stored in RAM and immediately available for supporting low-latency responses.
Couchbase Server provides support for storing document on disk. This is provided for a number of reasons:
Persistence. Couchbase can be loaded with data, shutdown, and started up again without the data having to be re-loaded from another source. In this way, Couchbase Server more than simply a caching layer on top of an existing database infrastructure.
Faster warm-up times. Other the lifetime of Couchbase Server node there may be soft or hard failures and regular administration tasks that need to take place. By storing information on disk, when you start-up your Couchbase Server node the data can be loaded to 'warm-up' the database with the active dataset so that it can start serving requests for data as quickly as possible.
Disk storage enables you to store datasets larger than the physical RAM size. Couchbase automatically moves data between RAM and disk (asynchronously in the background) in order to keep regularly used information in memory, and less frequently used data on disk.
The process of moving data from RAM to disk is called eviction, and is configured automatically through thresholds set on each configured bucket in your Couchbase Cluster.
The use of disk storage presents an issue in that a client request for a document ID must know whether the information exists or not. Couchbase Server achieves this using metadata structures. The metadata holds information about each documents stored in the database and this information is held in RAM. This means that the server can always return a 'document ID not found' response for an invalid document ID, while waiting (and returning) the data for an item either in RAM (in which case it is returned immediately), or after the item has been read from disk (after a delay, or until a timeout has been reached).
The process of moving information to and from disk is asynchronous. Data is evicted to disk from memory in the background while the server continues to service active requests. Eviction requests are written to an eviction queue to be written to disk in the background. During sequences of high writes to the database, clients will be notified that the server is temporarily out of memory until enough items have been evicted from memory to disk.
Similarly, when the server identifies an item that needs to be loaded from disk because it is not in active memory, the process is handled by a background process that processes the load queue and reads the information back disk and into memory. The client is made to wait until the data has been loaded back into memory before the information is returned.
The asynchronous nature and use of queues in this way enables reads and writes to be handled at a very fast rate, while removing the typical load and performance spikes that would otherwise cause a server running Couchbase Server to produce erratic performance.