Is there a default query caching on RDS Proxy for MySQL databases ?

0

Hello,

We've created one RDS MySQL database and added a proxy to handle the connections from multiple lambdas to this database. We've noticed a strange behaviour when querying data that has been updated very recently in the database (in the last minute) :

  • the same query does not yield the same result if we run it on the proxy or on the database directly
  • there seems to be some kind of cache but no configuration on the proxy or database is set to enable anything like that

Has anyone also met this case ? Am I missing some crucial data in the documentation ?

Best regards,

asked 7 months ago605 views
1 Answer
4

Hi cramarokoto,

Please go through the below steps once i hope it will helps to resolve your issue.

Check Read Replica Configuration:

  • Verify if read replicas are being used and whether the proxy is routing queries to them. You can configure the RDS Proxy to use only the primary instance for read and write operations if consistency is critical.

Review Proxy Settings:

  • Ensure that the RDS Proxy settings do not have any configurations that might introduce caching. Although RDS Proxy itself does not cache query results, it might interact with other layers that do.

Verify Transaction Isolation Levels:

  • Ensure that your transaction isolation levels are set correctly. You can check and set the isolation level in your queries or application settings.
-- Check current isolation level
SELECT @@transaction_isolation;

-- Set isolation level to READ COMMITTED (example)
SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;

Use Consistent Read Settings:

When performing read operations that require the latest data, use the FOR UPDATE or LOCK IN SHARE MODE clauses to ensure you get the most recent data.

-- Example of a consistent read with FOR UPDATE
SELECT * FROM your_table WHERE condition FOR UPDATE;

Investigate Query Execution Plans:

  • Compare the execution plans of queries run directly on the database versus through the proxy. Differences in execution plans might indicate why results differ.

Disable or Adjust Connection Pooling:

  • If necessary, adjust the connection pooling settings in RDS Proxy to ensure that transactions are committed and visible immediately across all connections.

Example Configuration Adjustments

Disabling Read Replicas for Consistent Reads:

In the RDS Proxy configuration, you can specify that all queries go to the primary instance to ensure you get the most up-to-date data.

{
    "DBProxyEndpoint": "your-proxy-endpoint",
    "DBProxyTargetGroup": {
        "TargetGroupName": "default",
        "TargetGroupArn": "arn:aws:rds:us-west-2:123456789012:targetgroup:mydbproxy:default",
        "ReadOnly": false
    }
}

Setting Isolation Level in Lambda Function:

  • When connecting to the database from a Lambda function, set the appropriate isolation level at the start of each connection session.

import pymysql

connection = pymysql.connect(
    host='your-db-host',
    user='your-username',
    password='your-password',
    db='your-database',
    autocommit=True
)

with connection.cursor() as cursor:
    cursor.execute("SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;")
    cursor.execute("SELECT * FROM your_table WHERE condition;")
    result = cursor.fetchall()
    print(result)

EXPERT
answered 7 months ago
  • Thank you very much for that quick answer, I'll check each point and accept your answer if any of the steps fixes my issue.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions