The Guts 'n' Glory of Database Internals: Merging Transactions
Merging in transactions is one of the core components of any scalable database. Read on to find out how this works.
Join the DZone community and get the full member experience.Join For Free
a repeating choice that we have to make in databases is to trade off performance for scale. in other words, in order to process more requests per time frame, we need to increase the time it takes to process a single request. let's see how this works with the case of transaction commits.
in the simplest model, a transaction does its work, prepares itself to be committed, writes itself to the journal, and then notifies the client that it has successfully committed. note that the most important part of the process is writing to the journal, which is how the transaction maintains durability. it also tends to be, by far, the most expensive part of the operation.
this leads to a lot of attempts to try and change the equation. i talked about one such option when we talked about the details of the transaction journal , having a journal actor responsible for writing the transaction changes as they happen, and amortize the cost of writing them over time and many transactions. this is something that quite a few databases do, but that does have certain assumptions.
to start with, it assumes that transactions are relatively long, and spend a lot of their time waiting for network i/o. in other words, this is a common model in the sql world, where you have to open a connection, make a query, then make another query based on the results of that, etc. the idea is that you parallelize the cost of writing the changes to the journal along with the time it takes to read/write from the network.
but other databases do not use this model. most nosql databases use the concept of a single command (which may aggregate commands), but they don’t have the notion of a long conversation with the client. so there isn’t that much of a chance to spread the cost of writing to the journal on the network.
instead, a common trick is transaction merging. transaction merging relies on the observation that i/o costs are no actually linear to the amount of i/o that you use. oh, sure, writing 1kb is going to be faster than writing 10 mb. but it isn’t going to be two orders of magnitude faster. in fact, it is actually more efficient to make a single 10mb write than 1024 writes on 1 kb. if this makes no sense, consider buying groceries and driving back to your house. the weight of the groceries has an impact on the fuel efficiency of the car, but the length of the drive is of much higher importance in terms of how much fuel is consumed. if you buy 200 kg of groceries (you are probably planning a hell of a party) and drive 10 kb home, you are going to waste a lot less fuel than if you make 4 trips with 50 kg in the trunk.
so what is transaction merging? put simply, instead of calling the journal directly, we queue the operation we want to make, and let a separate thread run through all the concurrent operations. here is the code:
def mergetransactionthreadproc(): buffer = buffer() while true: buffer.clear() result = dequeueoperation() if result.success is false: waitforadditionaloperations() continue buffer.write(result.operation, result.notification) max = time.time() + 1 while time.time() < max: result = dequeueoperation() if result.success is false: break buffer.write(result.operation, result.notification) journal.syncbuffer(buffer) buffer.notifyalloperationsaboutsuccessfuljournalsync()
the secret here is that if we have a load on the system, by the time we read from the queue, there are going to be more items in there. this means that when we write to the journal file, we’ll write not just a single operation (a trip back and forth to the grocery store), but we’ll be able to pack a lot of those operations immediately (one single trip). the idea is that we buffer all of the operations, up to a limit (in the code above, we use time, but typically you would also consider space), and then flush them to the journal as a single unit.
after doing this, we can notify all of the operations that they have been successfully completed, at which point they can go and notify their callers. we traded off the performance of a single operation (because we now need to be more complex and pay more i/o) in favor of being able to scale to a much higher number of operations. if your platform support async, you can also give up the thread (and let some other request run) while you are waiting for the i/o to complete.
the number of i/o requests you are going to make is lower, and the overall throughput is higher. just to give you an example, in one of our tests, this single change moved us from doing 200 requests/second (roughly the maximum number of fsync()/sec that the machine could support) to supporting 4,000 requests per second (20x performance increase)!
Published at DZone with permission of Oren Eini, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.