Prev | Current Page 167 | Next

Brad Ediger

"Advanced Rails"


??? Are you prepared to have every record with a duplicate or missing key rejected?
Or might it be desirable to have the system hold invalid data for a time until it is
corrected?
There are some valid situations for using composite keys, though. A good example is
in multimaster replication. One big problem in asynchronous multimaster replication
is synchronizing primary key sequences. If you insert two records at roughly the
same time to two master servers, there must be some mechanism to ensure that the
two servers issue different values for the primary keys on each record, lest problems
ensue when the records are replicated.
The composite-key solution to the problem of multimaster sequences is to issue each
server an ID and use that as part of the key; then each server can maintain its own
sequence independently of the others. The two records could have primary keys of
{ServerA, 5} and {ServerB, 5} and there would be no conflict. Note that this is a legitimate
use of composite keys, since the keys are meaningless (relative to the data
being stored in attributes).
Advanced Database Features | 113
For situations such as this, Dr Nic Williams has made composite keys work with
ActiveRecord. The composite_primary_keys gem is available at http://compositekeys.
rubyforge.org/.
As an example, consider the multimaster sequence problem discussed previously.
We have an Order model that is replicated between two servers using multimaster
replication.


Pages:
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179