Files
mariadb-connector-nodejs/documentation/pipelining.md
rusher fdc2db9118 permit using pipelining option at query level
pipelining documentation
2018-03-23 14:27:00 +01:00

46 lines
1.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

## Option "pipelining"
With traditional synchronous drivers, queries are send one by one, waiting for result before sending next one.
Communication with server following a requestresponse messaging pattern.
That is not very effective when having to request many query at the same time.
Node.js is efficient because of asynchrone concept, let's follow this rules.
Pipelining is "optimistic send": Connector will send queries one after another, preserving FIFO order.
This is particularly efficient when client is distant from server.
#### Example :
create a basket with 3 items
```javascript
connection.beginTransaction();
connection.query("INSERT INTO BASKET(customerId) values (?)", [1], (err, res) => {
//must handle error if any
const basketId = res.insertId;
try {
connection.query("INSERT INTO BASKET_ITEM(basketId, itemId) values (?, ?)", [basketId, 100]);
connection.query("INSERT INTO BASKET_ITEM(basketId, itemId) values (?, ?)", [basketId, 101]);
connection.query("INSERT INTO BASKET_ITEM(basketId, itemId) values (?, ?)", [basketId, 102], (err) => {
//must handle error if any
connection.commit();
});
} catch (err) {
connection.rollback();
//handle error
}
});
```
#### Network exchanges :
<p align="center">
<img src="./misc/pipelining.png">
</p>
Using standard client-server protocol (aka "ping-pong"), driver communicate with database following a requestresponse messaging pattern.
When sending a command, connector won't send any until response is available from input socket.
Using option "pipelining", commands are send by bulk, saving network latency.
The Inconvenient is that if an error occur on first/second command, following command are already send to database.
In that sense that pipelining is "optimistic".