Node.js Sequelize How to realize the database reading and writing separation _node.js

Source: Internet
Author: User
Tags connection pooling port number

First. Introduction

When building highly concurrent Web applications, in addition to the application layer to adopt a load balancing scheme, the database must also support high availability and high concurrency. More database optimization solutions are used: master-slave replication (Master-Slave) to synchronize data, and then read-write separation (MySQL-Proxy) to improve the concurrent load capacity of the database.
1. Replication option is separated from reading and writing
Sequelize supports read / write separation. To achieve read / write separation, you can create a Sequelize instance for read and write respectively. A more convenient way is to specify the read / write database through the replication option when creating the instance.

To use read / write replication in Sequelize, you can sometimes pass an object to its replication option when initializing Sequelize. This object has two properties, read and write. write is a single object (ie: a single server handles writes), and read is an array containing objects (ie: multiple servers handle reads). Each read and write server can contain the following attributes:

     · Host-the host of the database server
     · Port-the host port of the database server
     · Username-authenticate the username
     · Password-verify password
     · Database-the database to connect to

2. Sequelize read / write separation example
In multiple database clusters using master-slave replication, you can set it in the read attribute of the replication object. This attribute is an array in which one or more server connection copies can be passed in. The read operation is equivalent to the operation of the slave node in the database cluster, it will handle all SELECT query operations (read operations). The write attribute of the replication object is an object that represents the server connection. The write operation is equivalent to the master node, and it handles all insert, update, and delete operations (write operations).

var sequelize = new Sequelize ('database', null, null, {
 dialect: 'mysql',
 port: 3306
 replication: {
  read: [
   {host: '192.168.1.33', username: 'itbilu.com', password: 'pwd'},
   {host: 'localhost', username: 'root', password: null}
  ],
  write: {host: 'localhost', username: 'root', password: null}
 },
 pool: {// If you need to rewrite the link pool, please modify it in the pool option
  maxConnections: 20,
  maxIdleTime: 30000
 },
})
All the overall settings will apply to all node copies, so there is no need to specify each instance separately. In the above 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 the global setting, you need to specify it separately in the replication option.

Note: 

Sequelize does not set up master-slave replication nodes and data synchronization (replication) between nodes. These operations are actually done by MySQL (or the database you use). Sequelize is only responsible for writing or reading data from the master and slave nodes.

Sequelize uses connection pooling to manage node copies.

The default options are:

{
 maxConnections: 10,
 minConnections: 0,
 maxIdleTime: 1000
}
to sum up

The above is the entire content of this article. I hope that the content of this article can bring some help to everyone's study or work. If you have any questions, you can leave a message to communicate.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.