GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||
GeezerVison is showing apple.github.io — Developer Guide — FoundationDB 8.0.1 documentation Developer GuideFoundationDB’s scalability and performance make it an ideal back end for supporting the operation of critical applications. FoundationDB provides a simple data model coupled with powerful transactional integrity. This document gives an overview of application development using FoundationDB, including use of the API, working with transactions, and performance considerations. Note For operational advice about how to setup and maintain a FoundationDB cluster, see Administration. Data modelFoundationDB’s core data model is an ordered key-value store. Also known as an ordered associative array, map, or dictionary, this is a data structure composed of a collection of key-value pairs in which all keys are unique and ordered. Both keys and values in FoundationDB are simple byte strings. Apart from storage and retrieval, the database does not interpret or depend on the content of values. In contrast, keys are treated as members of a total order, the lexicographic order over the underlying bytes, in which keys are sorted by each byte in order. The combination of the core data model and multikey transactions allows an application to build richer data models and libraries that inherit FoundationDB’s scalability, performance, and integrity. Richer data models are designed by mapping the application’s data to keys and values in a way that yields an effective abstraction and enables efficient storage and retrieval. Note For guidance on mapping richer data models to FoundationDB’s core, see Data Modeling. Namespace managementThe keys in FoundationDB’s key-value store can be viewed as elements of a single, global keyspace. Your application will probably have multiple kinds of data to store, and it’s a good idea to separate them into different namespaces. The use of distinct namespaces will allow you to avoid conflicts among keys as your application grows. Because of the ordering of keys, a namespace in FoundationDB is defined by any prefix prepended to keys. For example, if we use a prefix Note Subspaces employ the tuple layer to provide a convenient syntax for namespaces. SubspacesSubspaces provide the recommended way to define namespaces for different categories of data. As a best practice, you should always use at least one subspace as a namespace for your application data. Note While subspaces can be used directly, they are also returned when creating or opening a directory. Directories are designed to manage related subspaces. Each FoundationDB language binding provides a Subspace class to help use subspaces uniformly. An instance of the class stores a prefix used to identify the namespace and automatically prepends it when encoding tuples into keys. Likewise, it removes the prefix when decoding keys. A subspace can be initialized by supplying it with a prefix tuple: my_space = Subspace(prefix_tuple) It can also optionally take a byte string as a prefix element that will be prepended to all keys packed by the subspace: my_space = Subspace(prefix_tuple, prefix_element) Note A subspace formed with a byte string as a prefix element is not fully compatible with the tuple layer. Keys stored within it cannot be unpacked as tuples. In many of the language bindings, index notation can be used on a subspace to create a nested subspace. The new subspace will have the same prefix extended by the index value: my_space = Subspace(('foo',)) # new_space will have prefix tuple ('foo', 'bar') new_space = my_space['bar'] The For example, suppose your application tracks profile data for your users. You could store the data in a user_space = Subspace(('user',)) @fdb.transactional def set_user_data(tr, key, value): tr[user_space.pack((key,))] = str(value) Subspaces support the as_foundationdb_key method to implicitly pack keys, so you could also write the @fdb.transactional def set_user_data(tr, key, value): tr[user_space[key]] = str(value) Finally, you may want to clear a subspace before working with it (as long as you’re sure it should be empty): @fdb.transactional def clear_subspace(tr, subspace): tr.clear_range_startswith(subspace.key()) DirectoriesFoundationDB provides directories (available in each language binding) as a tool for managing related subspaces. Directories are a recommended approach for administering applications. Each application should create or open at least one directory to manage its subspaces. Directories are identified by hierarchical paths analogous to the paths in a Unix-like file system. A path is represented as a tuple of strings. Each directory has an associated subspace used to store its content. The directory layer maps each path to a short prefix used for the corresponding subspace. In effect, directories provide a level of indirection for access to subspaces. This design has significant benefits: while directories are logically hierarchical as represented by their paths, their subspaces are not physically nested in a corresponding way. For example, suppose we create a few directories with increasing paths, such as: >>> a = fdb.directory.create(db, ('alpha',)) >>> b = fdb.directory.create(db, ('alpha', 'bravo')) >>> c = fdb.directory.create(db, ('alpha', 'bravo', 'charlie')) The prefixes of Paths in the directory layer are always relative. In particular, paths are interpreted relative to the directory in which an operation is performed. For example, we could have created the directories >>> a = fdb.directory.create(db, ('alpha',)) >>> b = a.create(db, ('bravo',)) >>> c = b.create(db, ('charlie',)) We can easily check that the resulting paths are the same as before: >>> a.get_path() (u'alpha',) >>> b.get_path() (u'alpha', u'bravo') >>> c.get_path() (u'alpha', u'bravo', u'charlie') UsageThe directory layer exposes a >>> dir_layer = DirectoryLayer(content_subspace = Subspace(rawPrefix='\x01')) You can use a >>> users = fdb.directory.create(db, ('users',)) >>> users DirectorySubspace(path=(u'users',), prefix='\x157') A directory returned by >>> inactive = users.create(db, ('inactive',)) >>> inactive DirectorySubspace(path=(u'users', u'inactive'), prefix='\x15&') The directory can also be used as a subspace to store content: >>> db[users['Smith']] = '' The directory layer uses a high-contention allocator to efficiently map the path to a short prefix for the directory’s subspace. If the directory was created previously (e.g., in a prior session or by another client), you can open it via its path. Like >>> users = fdb.directory.open(db, ('users',)) It’s often convenient to use a combined >>> products = fdb.directory.create_or_open(db, ('products',)) >>> orders = fdb.directory.create_or_open(db, ('orders',)) As noted above, all of the operations defined for a >>> cancelled_orders = orders.create_or_open(db, ('cancelled',)) Once created, directory paths can be changed using >>> store = fdb.directory.create_or_open(db, ('store',)) >>> users = fdb.directory.move(db, ('users',), ('store', 'users')) >>> products = fdb.directory.move(db, ('products',), ('store', 'products')) A directory path can also be changed via its subspace using >>> orders = orders.move_to(db, ('store', 'orders')) You can list the subdirectories of a directory. >>> fdb.directory.list(db) [u'store'] >>> store.list(db) # or fdb.directory.list(db, ('store',)) [u'orders', u'products', u'users'] >>> store.list(db, ('orders')) [u'cancelled'] Directories can be removed, with or without a prior test for existence: >>> fdb.directory.remove_if_exists(db, ('store', 'temp')) >>> users.remove(db) Although >>> users.exists(db) False >>> store.exists(db, ('products',)) True Subdirectories and nested subspacesIt’s common to have a subset of data that we want to arrange hierarchically under a directory. We can do this in two ways: by creating a subdirectory or a nested subspace. Let’s return to our We may have data that is logically subordinate to our primary data but needs to be handled distinctly. For example, suppose we have inactive users that we want to store separately from our regular users. We could create a subdirectory for them: >>> inactive = users.create(db, ('inactive',)) >>> inactive DirectorySubspace(path=(u'users', u'inactive'), prefix='\x15&') A subdirectory’s data is stored under a prefix unrelated to that of its parent, which allows the prefix to be kept short and makes the subdirectory fast and easy to move. The managed prefix also makes it harder to jointly access data across subdirectories (e.g., you cannot usefully perform a range read across subdirectories). Conversely, we often have data that we want to access as part of a single data set. For example, suppose we want >>> db[ users['ID'][user_ID][lastname][firstname] ] = '' Here, we’re just employing Directory partitionsUnder normal operation, a directory does not share a common key prefix with its subdirectories. As a result, related directories are not necessarily located together in key-space. This means that you cannot use a single range query to read all contents of a directory and its descendants simultaneously, for example. For most applications this behavior is acceptable, but in some cases it may be useful to have a directory tree in your hierarchy where every directory shares a common prefix. For this purpose, the directory layer supports creating partitions within a directory. A partition is a directory whose prefix is prepended to all of its descendant directories’ prefixes. A partition can be created by specifying the byte string ‘partition’ for the layer parameter: >>> partition = fdb.directory.create(db, ('p1',), layer=b'partition') >>> users = partition.create_or_open(db, ('users',)) Directory partitions have the following drawbacks, and in general they should not be used unless specifically needed:
Working with the APIsFoundationDB supports client APIs for Python, Ruby, Node.js, Java, Go, and C. At a high level, each of these APIs support transactions allowing a client to:
Note For details on the language-specific APIs, see the corresponding document under API Reference. Note All keys that start with the byte Key selectorsFoundationDB’s lexicographically ordered data model permits finding keys based on their order (for example, finding the first key in the database greater than a given key). Key selectors represent a description of a key in the database that could be resolved to an actual key by get_key() or used directly as the beginning or end of a range in get_range(). Each language API exposes constructors for four common forms of key selector. For example, in Python:
For example, suppose you want to read a range from a X = tr.get_range(begin, fdb.KeySelector.first_greater_than(end)) You can add or subtract an offset to or from a key selector. For example, in Python: fdb.KeySelector.first_greater_than('apple') + 1 selects the second key following apple. Note The current version of FoundationDB does not resolve key selectors with large offsets in O(1) time. See the Key selectors with large offsets are slow known limitation. All possible key selectors can be constructed using one of the four “common form” constructors and a positive or negative offset. Alternatively, general key selectors can be manually constructed by specifying:
To “resolve” these key selectors FoundationDB first finds the last key less than the reference key (or equal to the reference key, if the equality flag is true), then moves forward a number of keys equal to the offset (or backwards, if the offset is negative). If a key selector would otherwise describe a key off the beginning of the database (before the first key), it instead resolves to the empty key Transaction basicsTransactions in FoundationDBFoundationDB provides concurrency control via transactions, allowing multiple clients to concurrently read and write data in the database with strong guarantees about how they affect each other. Specifically, FoundationDB provides global, ACID transactions with strict serializability using optimistic concurrency. All reads and modifications of key-value pairs in FoundationDB are done within the context of a transaction. A transaction is a small unit of work that is both reliably performed and logically independent of other transactions. In Python, a simple transaction looks like this: @fdb.transactional def example(tr): # Read two values from the database a = tr.get('a') b = tr.get('b') # Write two key-value pairs to the database tr.set('c', a+b) tr.set('d', a+b) example(db) Once a call to the This ensures the following properties, known collectively as “ACID”:
An additional important property, though technically not part of ACID, is also guaranteed:
FoundationDB implements these properties using multiversion concurrency control (MVCC) for reads and optimistic concurrency for writes. As a result, neither reads nor writes are blocked by other readers or writers. Instead, conflicting transactions will fail at commit time and will usually be retried by the client. In particular, the reads in a transaction take place from an instantaneous snapshot of the database. From the perspective of the transaction this snapshot is not modified by the writes of other, concurrent transactions. When the read-write transaction is ready to be committed (read-only transactions don’t get committed and therefore never conflict), the FoundationDB cluster checks that it does not conflict with any previously committed transaction (i.e. that no value read by a transaction has been modified by another transaction since the read occurred) and, if it does conflict, rejects it. Rejected conflicting transactions are usually retried by the client. Accepted transactions are written to disk on multiple cluster nodes and then reported accepted to the client.
Transaction retry loopsIn most client APIs, there is a way of encapsulating a block of code as part of a transaction. For example, in Python, the def example_encapsulated(db): # make a new Transaction object on the database tr = db.create_transaction() while True: try: # Read two values from the transaction snapshot a = tr.get('a') b = tr.get('b') # Write two key-value pairs to the transaction snapshot tr.set('c', a+b) tr.set('d', a+b) # Try to commit the transaction, and wait for the result tr.commit().wait() # Success! return except fdb.FDBError as error: # The transaction conflicted or there was a transient failure. # Ask the FDB API whether and when to retry the error # (Also resets the tr object to be ready to use again) tr.on_error(error).wait() Note that, while the Programming with futuresMany of FoundationDB’s API functions are asynchronous: rather than blocking the calling thread until the result is available they immediately return a future object. A future represents a value (or error) to be available at some later time. For languages in which it’s supported, the simplest approach to futures is implicit blocking. This involves using the future as if it were an object of the result type. For example, the following Python code uses futures to do multiple read operations in parallel, thereby reducing transaction latency. The reads of the ‘A’ and ‘B’ keys will be done in parallel because # tr is a transaction # Read two values from the database in parallel a = tr.get('A') b = tr.get('B') print a+b The addition of By default, FoundationDB supports read-your-writes, meaning that reads reflect the results of prior writes within a transaction. FoundationDB maximizes parallelism of database operations within a transaction, subject to enforcing the sequential semantics of those operations, such as when a key is written before it is read. Another approach to programming with futures in FoundationDB is to set a callback function to be invoked asynchronously when the future is ready. Note Be very careful when mixing callbacks with explicit or implicit blocking. Blocking in a callback on a non-ready future will cause a deadlock. Blocking on anything else or performing CPU intensive tasks will block the FoundationDB client thread and therefore all database access from that client. For further details, see the API reference documentation for your language. Range readsFoundationDB supports efficient range reads based on the lexicographic ordering of keys. Range reads are a powerful technique commonly used with FoundationDB. A recommended approach is to design your keys so that you can use range reads to retrieve your most frequently accessed data. A range read within a transaction returns a container that issues asynchronous reads to the database. The client usually processes the data by iterating over the values returned by the container. Range reads can be specified explicitly by giving the for k, v in tr.get_range('a', 'm'): print(k, v) To define ranges that extend from the beginning the database, you can use the empty string for k, v in tr.get_range('', 'm'): print(k, v) Likewise, to define ranges that extend to the end of the database, you can use the key for k, v in tr.get_range('m', '\xFF'): print(k, v) A range read can also retrieve all keys starting with a given for k, v in tr.get_range_startswith('a'): print(k, v) The API balances latency and bandwidth by fetching data in batches as determined by the streaming mode parameter. Streaming modes allow you to customize this balance based on how you intend to consume the data. The default streaming mode ( for k, v in tr.get_range_startswith('a', streaming_mode=fdb.StreamingMode.small): if halting_condition(k, v): break print(k, v) In some situations, you may want to explicitly control the number of key-value pairs returned. You can use the LIMIT = 100 # adjust according to data characteristics def get_range_limited(tr, begin, end): keys_found = True while keys_found: keys_found = [] for k, v in tr.get_range(begin, end, limit=LIMIT): keys_found.append(k) if keys_found: begin = fdb.KeySelector.first_greater_than(keys_found[-1]) yield keys_found For very large range reads, you can use multiple clients to perform reads concurrently. In this case, you’ll want to estimate sub-ranges of roughly equal size based on the distribution of your keys. The locality functions can be used to derive estimates for the boundaries between sub-ranges. Long-running transactionsFoundationDB does not support long-running transactions, currently defined as those lasting over five seconds. The reasons for this design limitation relate to multiversion concurrency control and are discussed in Anti-Features. You may have certain large operations that you’re accustomed to implementing as long-running transactions with another database. How should you approach implementing your operation in FoundationDB? The key consideration is whether your operation requires global consistency over all its data elements. In many cases, some smaller scope of consistency is acceptable. For example, many analytic computations are defined over a set of entities, such as users, and require consistency only for each entity, not globally across them. In this case, you can decompose the operation into multiple transactions, one for each entity. More generally, the strategy for operations requiring local consistency is to decompose them into a set of short transactions. If your operation really does require global consistency, you can often use an indirection strategy. Here, you write a separate version of the data during the course of the operation and switch to the new version when the operation is done. The switch can be performed transactionally with a single change of a reference. For example, you can store the version reference using a @fdb.transactional def setMostRecentVersion(tr, versionNumber): tr[fdb.tuple.pack(('mostRecentVersion',))] = str(versionNumber) @fdb.transactional def getMostRecentVersion(tr): return tr[fdb.tuple.pack(('mostRecentVersion',))] Your application would then store the relevant data using keys that encode the version number. The application would read data with a transaction that reads the most recent version number and uses it to reference the correct data. This strategy has the advantage of allowing consistent access to the current version of the data while concurrently writing the new version. Working with transactionsAtomic operationsAn atomic operation is a single database command that carries out several logical steps: reading the value of a key, performing a transformation on that value, and writing the result. Different atomic operations perform different transformations. Like other database operations, an atomic operation is used within a transaction; however, its use within a transaction will not cause the transaction to conflict. Atomic operations are ideal for operating on keys that multiple clients modify frequently. For example, you can use a key as a counter and increment/decrement it with atomic @fdb.transactional def increment(tr, counter): tr.add(counter, struct.pack('<i', 1)) @fdb.transactional def decrement(tr, counter): tr.add(counter, struct.pack('<i', -1)) When the counter value is decremented down to 0, you may want to clear the key from database. An easy way to do that is to use @fdb.transactional def decrement(tr, counter): tr.add(counter, struct.pack('<i', -1)) tr.compare_and_clear(counter, struct.pack('<i', 0)) Similarly, you can use a key as a flag and toggle it with atomic @fdb.transactional def toggle(tr, flag): tr.xor(flag, struct.pack('=?',1)) Each atomic operation takes a packed string as an argument (as detailed in the API reference). Atomic operations do not expose the current value of the key to the client but simply send the database the transformation to apply. In regard to conflict checking, an atomic operation is equivalent to a write without a read. It can only cause other transactions performing reads of the key to conflict. By combining its logical steps into a single, read-free operation, FoundationDB can guarantee that the transaction performing the atomic operation will not conflict due to that operation. Note If a transaction uses both an atomic operation and a strictly serializable read on the same key, the benefits of using the atomic operation (for both conflict checking and performance) are lost. Transactions with unknown resultsAs with other client/server databases, in some failure scenarios a client may be unable to determine whether a transaction succeeded. In these cases, Note The Python An idempotent transaction is one that has the same effect when committed twice as when committed once. If your transaction is already idempotent, there is nothing more to worry about. Otherwise, you should consider whether it is acceptable for your transaction to be committed twice. The following suggestions are useful for making your transaction idempotent:
The following example illustrates both techniques. Together, they make a transaction idempotent that otherwise would not have been: # Increases account balance and stores a record of the deposit with a unique depositId @fdb.transactional def deposit(tr, acctId, depositId, amount): # If the deposit record exists, the deposit already succeeded, and we can quit depositKey = fdb.tuple.pack(('account', acctId, depositId)) if tr[depositKey].present(): return amount = struct.pack('<i', amount) tr[depositKey] = amount # The above check ensures that the balance update is executed only once balanceKey = fdb.tuple.pack(('account', acctId)) tr.add(balanceKey, amount) There is experimental support for preventing Conflict rangesBy default, FoundationDB transactions guarantee strict serializability, which results in a state that could have been produced by executing transactions one at a time, even though they may actually have been executed concurrently. FoundationDB maintains strict serializability by detecting conflicts among concurrent transactions and allowing only a non-conflicting subset of them to succeed. Two concurrent transactions conflict if the first to commit writes a value that the second reads. In this case, the second transaction will fail. Clients will usually retry failed transactions. To detect conflicts, FoundationDB tracks the ranges of keys each transaction reads and writes. While most applications will use the strictly serializable isolation that transactions provide by default, FoundationDB also provides several API features that manipulate conflict ranges to allow more precise control. Conflicts can be avoided, reducing isolation, in two ways:
Conflicts can be created, increasing isolation, by explicitly adding read or write conflict ranges. Note add read conflict range behaves as if the client is reading the range. This means add read conflict range will not add conflict ranges for keys that have been written earlier in the same transaction. This is the intended behavior, as it allows users to compose transactions together without introducing unnecessary conflicts. For example, suppose you have a transactional function that increments a set of counters using atomic addition. Atomic operations do not add read conflict ranges and so cannot cause the transaction in which they occur to fail. More precisely, the read version for an atomic operation is the same as transaction’s commit version, and thus no conflicting write from other transactions could be serialized between the read and write of the key. Most of the time, this is exactly what we want. However, suppose there is another transaction that (infrequently) resets one or more counters, and our contract requires that we must advance all specified counters in unison. We want to guarantee that if a counter is reset during an incrementing transaction, then the incrementing transaction will conflict. We can selectively add read conflicts ranges for this purpose: @fdb.transactional def guarded_increment(tr, counters): for counter in counters: tr.add(counter, struct.pack('<i', 1)) if reset(counter): tr.add_read_conflict_key(counter) Snapshot readsSnapshot reads selectively relax FoundationDB’s isolation property, reducing conflicts but making it harder to reason about concurrency. The strictly serializable isolation that transactions maintain by default has little performance cost when there are few conflicts but can be expensive when there are many. FoundationDB therefore also permits individual reads within a transaction to be done as snapshot reads. Snapshot reads differ from ordinary (strictly serializable) reads by permitting the values they read to be modified by concurrent transactions, whereas strictly serializable reads cause conflicts in that case. Consider a transaction which needs to remove and return an arbitrary value from a small range of keys. The simplest implementation (using strictly serializable isolation) would be: @fdb.transactional def remove_one(tr, range): all_kv = tr[range] key, value = random.choice(list(all_kv)) del tr[key] return value Unfortunately, if a concurrent transaction happens to insert a new key anywhere in the range, our transaction will conflict with it and fail (resulting in a retry) because seeing the other transaction’s write would change the result of the range read. FoundationDB is enforcing a stronger contract than we actually need. A snapshot read allows us to weaken the contract, but we don’t want to weaken it too far: it’s still important to us that the actual value we returned existed in the database at the time of our transaction. Adding a conflict range for our key ensures that we will fail if someone else modifies the key simultaneously: @fdb.transactional def remove_one(tr, range): all_kv = tr.snapshot[range] # Snapshot read key, value = random.choice(list(all_kv)) tr.add_read_conflict_key(key) # Add conflict range del tr[key] return value This transaction accomplishes the same task but won’t conflict with the insert of a key elsewhere in the range. It will only conflict with a modification to the key it actually returns. By default, snapshot reads see the effects of prior writes in the same transaction. (This read-your-writes behavior is the same as for ordinary, strictly serializable reads.) Read-your-writes allows transactional functions (such as the above example) to be easily composed within a single transaction because each function will see the writes of previously invoked functions. Note The default read-your-writes behavior of snapshot reads is well-suited to the large majority of use cases. In less frequent cases, you may want to read from only a single version of the database. This behavior can be achieved through the appropriate transaction options. Transaction options are an advanced feature of the API and should be used with caution.
Read-your-writes can be disabled (and re-enabled) within a transaction by using the options:
*
Transaction.options.set_snapshot_ryw_disable()*
Transaction.options.set_snapshot_ryw_enable().A given snapshot read gets read-your-writes behavior unless the disable option has been previously set more times than the enable option in that transaction.
Using snapshot reads is appropriate when the following conditions all hold:
Transaction cancellationThe FoundationDB language bindings all provide a mechanism for cancelling an outstanding transaction. However there are also special transaction options for specifying the conditions under which a transaction should automatically be cancelled. In the following example, a retry loop is combined with transaction options that ensure that the operation will not be attempted more than 6 times or for longer than 3 seconds: @fdb.transactional def example_with_cancellation(tr): # Set maximum number of times that on_error() can be called (implicitly by the decorator). # On the 6th time, on_error() will throw retry_limit_exceeded rather than resetting and retrying. tr.options.set_retry_limit(5) # Cancel transaction with transaction_timed_out after 3 seconds tr.options.set_timeout(3000) # Read two values from the transaction snapshot a = tr.get('a') b = tr.get('b') # Write two key-value pairs to the transaction snapshot tr.set('c', a+b) tr.set('d', a+b) Note The WatchesSometimes you want a client to monitor one or more keys for updates to their values by other clients. An obvious way to implement monitoring is by polling, i.e., periodically reading the key-values to check them for a change. FoundationDB provides watches to monitor keys more efficiently. Watches are created for a specified key and return a future that becomes ready when there’s a change to the key’s value. For example, suppose you have a polling loop that checks keys for changes once a second: def polling_loop(db, keys): @fdb.transactional def read_keys(tr): return {k:tr[k] for k in keys} cache = {k:object() for k in keys} while True: value = read_keys(db) for k in keys: if cache[k] != value[k]: yield (k, value[k]) cache[k] = value[k] time.sleep(1) You can use the loop to dispatch changes to a handler with something like: for k, v in polling_loop(db, ['foo','bar','bat']): handle(k,v) With watches, you can eliminate the sleep and perform new reads only after a change to one of the keys: def watching_read_loop(db, keys): @fdb.transactional def watch_keys(tr): return {k:tr[k] for k in keys}, [tr.watch(k) for k in keys] cache = {k:object() for k in keys} while True: value, watches = watch_keys(db) for k in keys: if cache[k] != value[k]: yield (k, value[k]) cache[k] = value[k] fdb.Future.wait_for_any(*watches) The version with watches will perform fewer unnecessary reads and detect changes with better resolution than by polling. Note Watches guarantee only that a value was changed; they make no guarantee about values you may subsequently read. In particular, there is no guarantee that a value you read will correspond to the change that triggered the watch. Another client may have changed a key back to its original value or to some third value between a watch becoming ready and your subsequent read. For further details, see the discussion of watches in the language-specific document of your choice under API Reference. If you only need to detect the fact of a change, and your response doesn’t depend on the new value, then you can eliminate the reads altogether: def watching_loop(db, keys): @fdb.transactional def watch_keys(tr): return [tr.watch(k) for k in keys] while True: fdb.Future.wait_for_any(*watch_keys(db)) yield Performance considerationsLatencyLike all systems, FoundationDB operates at a low latency while under low load and an increasing latency as the load approaches the saturation point. We have made efforts to allow FoundationDB to operate at a low latency even at moderate loads. However, if FoundationDB is being driven with a “saturating” load (e.g. batch processing), latencies can be become very high as a line forms for requests. In this case, the transactions generating the saturating load should be run with a lower priority, allowing other transactions to skip ahead in line.
There are several places in a typical transaction that can experience database latency:
Throughput requires concurrencyFoundationDB will only reach its maximum performance with a highly concurrent workload. This is a practical consideration that derives mathematically from the ratio of system throughput to system latency (known in queuing theory as Little’s Law). For FoundationDB, a cluster might have a read latency of 1ms and be capable of millions of reads per second. To achieve such a rate, there must therefore be thousands of read requests happening concurrently. Not having enough outstanding requests is the single biggest reason for low performance when using FoundationDB. There are several important techniques for achieving high concurrency:
Minimizing conflictsFrequent conflicts make FoundationDB operate inefficiently and should be minimized. They result from multiple clients trying to update the same keys at a high rate. Developers need to avoid this condition by spreading frequently updated data over a large set of keys. Note As a rule of thumb, if a key will be modified more than 10-100 times per second, a different data model should be considered. In these situations:
Key and value sizesMaintaining efficient key and value sizes is essential to getting maximum performance with FoundationDB. As a rule, smaller key sizes are better, as they can be more efficiently transmitted over the network and compared. In concrete terms, the highest performance applications will keep key sizes below 32 bytes. Key sizes above 10 kB are not allowed, and sizes above 1 kB should be avoided—store the data in the value if possible. Value sizes are more flexible, with 0-10 kB normal. Value sizes cannot exceed 100 kB. Like any similar system, FoundationDB has a “characteristic value size” where the fixed costs of the random read roughly equal the marginal costs of the actual bytes. For FoundationDB running on SSD hardware, this characteristic size is roughly 1 kB for randomly accessed data and roughly 100 bytes for frequently accessed (cached) data. If your keys or values are initially too large, try to revise your data model to make them smaller. Loading dataLoading data is a common task in any database. Loading data in FoundationDB will be most efficiently accomplished if a few guidelines are followed:
Using these techniques, our cluster of 24 nodes and 48 SSDs loads about 3 billion (100 byte) key-value pairs per hour. Implementation DetailsThese following sections go into some of the gritty details of FoundationDB. Most users don’t need to read or understand this in order to use FoundationDB efficiently. How FoundationDB Detects ConflictsAs written above, FoundationDB implements serializable transactions with external consistency. The underlying algorithm uses multi-version concurrency control. At commit time, each transaction is checked for read-write conflicts. Conceptually this algorithm is quite simple. Each transaction will get a read version assigned when it issues the first read or before it tries to commit. All reads that happen during that transaction will be read as of that version. Writes will go into a local cache and will be sent to FoundationDB during commit time. The transaction can successfully commit if it is conflict free; it will then get a commit-version assigned. A transaction is conflict free if and only if there have been no writes to any key that was read by that transaction between the time the transaction started and the commit time. This is true if there was no transaction with a commit version larger than our read version but smaller than our commit version that wrote to any of the keys that we read. This form of conflict detection, while simple, can often be confusing for people who are familiar with databases that check for write-write conflicts. Some interesting properties of FoundationDB transactions are:
How Versions are Generated and AssignedVersions are generated by the process that runs the master role. FoundationDB guarantees that no version will be generated twice and that the versions are monotonically increasing. In order to assign read and commit versions to transactions, a client will never talk to the master. Instead it will get them from a GRV proxy and a commit proxy. Getting a read version is more complex than a commit version. Let’s first look at commit versions:
As mentioned before, the algorithm to assign read versions is a bit more complex. At the start of a transaction, a client will ask a GRV proxy server for a read version. The GRV proxy will reply with the last committed version as of the time it received the request - this is important to guarantee external consistency. This is how this is achieved:
Checking whether the log-system is still writeable can be especially expensive if a clusters runs in a multi-region configuration. If a user is fine to sacrifice strict serializability they can use option-causal-read-risky. Conflict DetectionThis section will only explain conceptually how transactions are resolved in FoundationDB. The implementation will use multiple servers running the Resolver role and the keyspace will be sharded across them. It will also only allow resolving transactions whose read versions are less than 5 million versions older than their commit version (around 5 seconds). A resolver will keep a map in memory which stores the written keys of each commit version. A simplified resolver state could look like this:
Now let’s assume we have a transaction with read version 1200 and the assigned commit version will be something larger than 1340 - let’s say it is 1450. In that transaction we read keys
Note that the resolver didn’t use the write set at all in order to make a decision whether the transaction can commit or not. This means that blind writes (writes to keys without reading them first) will never cause a conflict. But since the resolver will then remember these writes, blind writes can cause future transactions to conflict. Error HandlingWhen using FoundationDB we strongly recommend users to use the retry-loop. In Python the retry loop would look this this: tr = tr.create_transaction() while True: try: # execute reads and writes on FDB using the tr object tr.commit().wait() break except FDBError as e: tr.on_error(e.code).wait() This is also what the transaction decoration in python does, if you pass a
If you use this retry loop, there are very few caveats. If you write your own and you are not careful, some things might behave differently than you would expect. The following sections will go over the most common errors you will see, the guarantees FoundationDB provides during failures, and common caveats. This retry loop will take care of most of these errors, but it might still be beneficial to understand those. Errors where we know the State of the TransactionThe most common errors you will see are errors where we know that the transaction failed to commit. In this case, we’re guaranteed that nothing that we attempted to write was written to the database. The most common error codes for this are:
The good thing about these errors is that retrying is simple: you know that the transaction didn’t commit and therefore you can retry even without thinking much about weird corner cases. The
| ||||||||||||||||||||||
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |