Skip to content

Instantly share code, notes, and snippets.

@bennadel
Created October 11, 2019 12:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bennadel/0fbbbab7aeae788cbd0c70da58063d1a to your computer and use it in GitHub Desktop.
Save bennadel/0fbbbab7aeae788cbd0c70da58063d1a to your computer and use it in GitHub Desktop.
Thought Experiment: Splitting A Single Data Table Up Into High-Writes And High-Reads For Better Performance In MySQL
UPDATE
user u
SET
u.lastRequestAt = UTC_TIMESTAMP()
WHERE
u.id = ?
;
SELECT
u.id,
u.name,
u.email,
u.lastRequestAt
FROM
user u
WHERE
u.id = ?
;
<cfscript>
// Read the session from Redis.
userSession = redis.hgetall( "sessions:#userID#:#sessionToken#" );
// Optimistically update the in-memory value for presence.
userSession.lastRequestAt = now();
// ASYNCHRONOUSLY log the request timestamp to both Redis and MySQL.
runAsync(
() => {
// Update the write-through cache.
redis.hset(
"sessions:#userID#:#sessionToken#",
"lastRequestAt",
userSession.lastRequestAt
);
// Update the underlying database record.
userPresenceGateway.update({
userID: userSession.id,
lastRequestAt: userSession.lastRequestAt
});
}
);
</cfscript>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment