Read replicas setup for database read scaling

Our company is engaged in the development, support and maintenance of sites of any complexity. From simple one-page sites to large-scale cluster systems built on micro services. Experience of developers is confirmed by certificates from vendors.
Development and maintenance of all types of websites:
Informational websites or web applications
Business card websites, landing pages, corporate websites, online catalogs, quizzes, promo websites, blogs, news resources, informational portals, forums, aggregators
E-commerce websites or web applications
Online stores, B2B portals, marketplaces, online exchanges, cashback websites, exchanges, dropshipping platforms, product parsers
Business process management web applications
CRM systems, ERP systems, corporate portals, production management systems, information parsers
Electronic service websites or web applications
Classified ads platforms, online schools, online cinemas, website builders, portals for electronic services, video hosting platforms, thematic portals

These are just some of the technical types of websites we work with, and each of them can have its own specific features and functionality, as well as be customized to meet the specific needs and goals of the client.

Our competencies:
Development stages
Latest works
  • image_website-b2b-advance_0.png
    B2B ADVANCE company website development
    1212
  • image_web-applications_feedme_466_0.webp
    Development of a web application for FEEDME
    1161
  • image_websites_belfingroup_462_0.webp
    Website development for BELFINGROUP
    852
  • image_ecommerce_furnoro_435_0.webp
    Development of an online store for the company FURNORO
    1041
  • image_crm_enviok_479_0.webp
    Development of a web application for Enviok
    822
  • image_bitrix-bitrix-24-1c_fixper_448_0.png
    Website development for FIXPER company
    815

Setting Up Read Replicas for Database Read Scaling

Read Replicas — database replicas that accept only SELECT queries. Allow horizontal scaling of read load without changing application architecture.

Use cases

  • Separate load: API requests read from replicas, transactions write to master
  • Analytical queries (complex JOINs, aggregations) go to dedicated replica
  • Different applications use different replicas (CMS backend, public API, reports)

PostgreSQL: multiple replicas

# Create replica via pg_basebackup
pg_basebackup -h master-host -U replication_user \
  -D /var/lib/postgresql/14/main -P -Xs -R

Routing in application

Laravel (PHP)

// config/database.php
'pgsql' => [
    'read' => [
        ['host' => '192.168.1.11'],
        ['host' => '192.168.1.12'],
    ],
    'write' => [
        'host' => '192.168.1.10',
    ],
    'sticky' => true,
]

Node.js (Sequelize)

const sequelize = new Sequelize('myapp', 'user', 'password', {
  dialect: 'postgres',
  replication: {
    read: [
      { host: '192.168.1.11', port: 5432 },
      { host: '192.168.1.12', port: 5432 },
    ],
    write: { host: '192.168.1.10', port: 5432 },
  },
})

pgBouncer read/write split

# pgbouncer.ini
[databases]
myapp = host=192.168.1.10 port=5432 dbname=myapp
myapp_read = host=192.168.1.11,192.168.1.12 port=5432 dbname=myapp

Manage replication lag

Critical issue: write → immediate read may read stale data if replica lags.

-- On master: send LSN to client
SELECT pg_current_wal_lsn();

Pattern in application:

def read_after_write(lsn):
    replica = get_replica()
    if replica.is_caught_up(lsn):
        return replica.execute(query)
    else:
        return master.execute(query)

AWS RDS Read Replicas

aws rds create-db-instance-read-replica \
  --db-instance-identifier myapp-replica-1 \
  --source-db-instance-identifier myapp-master \
  --db-instance-class db.r6g.large

Monitoring replicas

-- On master: status of all replicas
SELECT application_name, client_addr, state,
       round(EXTRACT(EPOCH FROM replay_lag)) as replay_lag_sec
FROM pg_stat_replication;

Timeline

Setup of two Read Replicas with load balancing in application — 1–2 business days.