Tungsten Connector - The Secret to SmartScale Success

The BackStory

SmartScale is a special mode of the Tungsten Connector (aka Tungsten Proxy), which allows read requests to be intelligently routed to either a replica or the primary to ensure that the data returned is not stale.

This feature is available when the Connector is running in the application-layer Proxy mode. SmartScale will check the Primary for the latest write and compare that to the read replicas. If the data the read wants has been replicated to a replica, the read will go there, otherwise the read goes to the Primary to ensure that the information is not stale.

The Question

Recently, a customer asked the following:

“With SmartScale enabled, with no load, reads to go to a replica. However, after adding just a small load (and the replicas have almost no latency), all reads are going to the primary. Why?”

The Answer

This is expected behavior due to the way SmartScale works!

When you connect through the connector for the first time to a database, and don't issue a write operation, all reads will go to the primary, until you issue a write with this same session ID, which will initialize the session. From there, reads will start to go to slaves.

The Connector polls for the current seqno of all replicas every 3 seconds, then compares these numbers to the seqno of last write in the session.

This customer had set DATABASE as the session ID. This means that (even if the load is light), when writes to the same schema are within 3 seconds or less, reads will go to primary.

The Solution

The secret to SmartScale success is selecting the proper session ID, along with ensuring that queries are not wrapped in a BEGIN/COMMIT as a transaction. SmartScale sends all transactions to the Primary.

After application analysis, this customer determined that the Hibernate ORM was in use. SmartScale was not a good fit because all events were automatically wrapped in a BEGIN/COMMIT by Hibernate, forcing all queries to be routed to the Primary.

Tuning Tips

Tuning of the SmartScale session ID will improve the load balancing results.

Many customers select CONNECTION or USER instead of DATABASE as the best sessionId.

For the best performance, determine the application behavior and find the best session ID match.

Below are examples of which session ID to select when:

=> Use Case: user1 does very few writes but reads heavily, while user2 does most of the writes

connector-smartscale-sessionid=USER

=> Use Case: connections are well isolated and do not need the data freshly written by other, parallel connections

connector-smartscale-sessionid=CONNECTION

=> Use Case: data written in schema1 rarely changes and is read heavily, while schema2 receives most of the writes

connector-smartscale-sessionid=DATABASE

 

A Few Details

SmartScale will send to the primary any read queries done by the application that have a preceeding "set autocommit=0".

By default, SmartScale does not include the primary in the read pool because the goal is to reduce load on the primary to allow for the most writes possible. A future version will allow SmartScale to include the primary via configuration option.

Get It Done:

To Enable SmartScale

First, pick a session ID. Possible session IDs are:

  • DATABASE: applications will see write operations made to the same database that it is connected to. Reads from other databases might be outdated depending on the Replica latency.
  • USER: all connections invoked as the same database user will read data consistent with the writes made by the current user. Other users' data might be outdated.
  • CONNECTION: only writes made by the current connection are guaranteed to be read consistently. Writes from other connections might be outdated.
  • PROVIDED_IN_DBNAME: Allows you to specify a variable sessionid in the database connection string. An application, typically PHP, can pass its own session id to make SmartScale even more efficient.

Add two lines to the tungsten.ini file, then run tpm update:

connector-smartscale=true
connector-smartscale-sessionid={SESSIONID_FROM_ABOVE_LIST}
connector-bridge-mode=false

Once updated, you should see the following in `tungsten-connector/conf/connector.properties`

sessionId={SESSIONID_FROM_ABOVE_LIST}

To Disable SmartScale

Edit the tungsten.ini file, then run tpm update:

connector-smartscale=false

This will leave the Connector in “regular” Proxy mode.

To Enable Bridge Mode

Edit the tungsten.ini file, then run tpm update:

connector-bridge-mode=true
connector-smartscale=false

Wrap-Up

The secret to SmartScale success is selecting the proper session ID, and not using transactions for read requests.

For more information about SmartScale, please visit our online documentation: https://docs.continuent.com/tungsten-clustering-6.1/connector-routing-smartscale.html

About the Author

Eric M. Stone
COO

Eric is a veteran of fast-paced, large-scale enterprise environments with 35 years of Information Technology experience. With a focus on HA/DR, from building data centers and trading floors to world-wide deployments, Eric has architected, coded, deployed and administered systems for a wide variety of disparate customers, from Fortune 500 financial institutions to SMB’s.

Add new comment