Application Demonstrating Snapshot (MVCC) Reads
The following quickstart example demonstrates the effect of the snapshot read vs. traditional reads while a writer is active.
The use case being demonstrated here is an application where writes are critical. Meaning, a request for write should not be delayed by a wait for a write lock because a reader (or multiple readers) accessing the database. This example will demonstrate the effects of a traditional read lock blocking the writer’s access compared to a snapshot read transaction which allows a reader access to a ‘moment in time view’ of the contents of the database without impeding the writer.
Program Mode Description
The source for this example is in the shared/RDM/GettingStarted/c-core/core38Example
directory. The example consists of three parts:
Writer
The --writer
mode is an application with a TFS embedded in the application. The application also enables the ‘listener’ functionality so that other applications can connect to the embedded TFS in this application. The application continuously writes to the database using one write per transaction.
The schema for the database used in this example (core38Example.sdl
) defines the table being used for writes as a "circular table" with a maximum of 10,000 rows. The application uses a database with a "circular table" for demonstration ease of use to prevent the system from running out of disk space.
By default, the application will wait zero (0) seconds for a write lock when requested. This means that when a write lock is requested and cannot be immediately granted, the lock request will fail.
In the source code for core38Example
, refer to the writerProcess()
function at approximately line 151.
Reader
The --read
mode will attempt to connect to the database using a remote TFS connection. The application will attempt to take a read lock and, if successful, will read all the rows in the database sequentially. When the reading is complete, the read lock will be released, and the application will perform the same action again.
In the source code for core38Example
, refer to the readerProcess()
function at approximately line 101.
Snapshot
The --snapshot
mode will attempt to connect to the database using a remote TFS connection. The application will attempt to start a snapshot read and, if successful, will read all the rows in the database sequentially. When the reading is complete, the snapshot read will be released, and the application will perform the same action again.
While the "snapshot read" is active, the writer can continue to create new rows in the database as well as delete rows as needed in this circular table being used. The "snapshot read" once active, will see ONLY the data that was available when the "snapshot read" started. Deleted rows will not appear deleted and new rows will not exist until the "snapshot read" is ended and retaken.
In the source code for core38Example
, refer to the snapshotProcess()
function at approximately line 212.
Next sections: