How to Implement database read/write splitting in Node. js sequencee
I. Preface
When building highly concurrent Web applications, in addition to the load balancing scheme adopted at the application layer, the database must also support high availability and high concurrency. Many database optimization schemes are used: through master-slave replication (Master-Slave), And then read/write splitting (MySQL-Proxy) To improve the database's concurrent load capacity.
1. replication option and read/write splitting
Sequelize Supports read/write separation. To implement read/write separation, you can create one for each read and write respectively.SequelizeInstance, the more convenient way isreplicationSelect read/write database respectively.
ToSequelizeUsing read/write replication, You can initializeSequelizeSometimesreplicationOption to pass an object. This objectread,writeTwo attributes.writeIs a single object (that is, writing is processed by a single server), andreadIs an array containing objects (that is, read by multiple servers ). Eachread,writeThe server can contain the following attributes:
·host -Database server host
·port -Host port of the Database Server
·username -Verify the user name
·password -Verify the password
·database -Database to be connected
2. sequencee read/write separation example
In multiple database clusters that use master-slave replicationreplicationObjectreadAttribute. This attribute is an array in which one or more server connection copies can be input. Read operations are equivalent to operations on slave nodes in the database cluster.SELECTQuery (read ). WhilereplicationObjectwriteA property is an object that indicates a server connection. A write operation is equivalent to a master node. It processes all insert, update, and delete operations (write operations ).
Var sequencee = new sequencee ('database', null, null, {dialect: 'mysql', port: 3306 replication: {read: [{host: '123. 168.1.33 ', username: 'itbilu. com ', password: 'pwd'}, {host: 'localhost', username: 'root', password: null}], write: {host: 'localhost', username: 'root', password: null }}, pool: {// if you want to rewrite the link pool, modify maxConnections: 20, maxIdleTime: 30000},} in the pool option },})
All integrity settings apply to all node replicas, so you do not need to specify them for each instance. In the preceding example, the database name and port number are applied to all node copies, and the user name and password options are also applicable. If a node copy does not use global settings, you must specify it separately in the replication option.
Note:Sequelize Data Synchronization (replication) between master-slave replication nodes and nodes is not set. These operations are actually performed by MySQL (or the database you are using. WhileSequelize Only responsible for writing or reading data from the master and slave nodes.
Sequelize The connection pool is used to manage node copies.
The default options are:
{ maxConnections: 10, minConnections: 0, maxIdleTime:1000}
Summary
The above is all about this article. I hope this article will help you in your study or work. If you have any questions, please leave a message.