I deployed a PostgreSQL instance in k8s with a master pod and a single replica pod. I want to know how can I perform read queries on the read replica and write queries on the master? Currently, all the requests I send to the DB are served by the master (after connecting to the master). Do I need to explicitly connect to the read replica for queries to go there or is there some mechanism in Postgres which can route traffic to the read replica?
There is no reliable way to automatically redirect read requests to the standby, since in PostgreSQL you cannot reliably tell if an SQL statement writes or not (example: you can create a function that deletes rows and call it in a SEKECT
statement). You will have to teach your application to connect to the standby to perform read requests.
Note that horizontal scaling in that fashion comes with some problems:
since replication is asynchronous, and replication conflicts can delay replay, changes on the primary might not be visible on the standby right away
if you have long-running queries on the standby, they might be canceled due to replication conflicts, and you have to retry them