sql - Global revision without locking -
given set of rules, possible implement in sql?
- two transactions don't modify same rows should able run concurrently. no locks should occur (or @ least use should minimized as possible).
- transactions can read committed data.
- a revision defined integer value in system.
- a new transaction must able increment , query new revision. revision applied every rows transaction modifies.
- no 2 transactions can share same revision.
- a transaction x committed before transaction y must have revision lower 1 assigned transaction y.
i want use integer revision in order optimize how query changes since specific revision. this:
select * [dummytable] [dummytable].[revision] > clientrevision
my current solution uses sql table [globalrevision] single row [lastrevision] keep latest revision. transactions' isolation level set snapshot.
the problem solution [globalrevision] table single row [lastrevision] becomes point of contention. because must increment revision @ start of transaction can apply new revision modified rows. keep lock on [lastrevision] row throughout duration of transaction, killing concurrency. though 2 concurrent transactions modify totally different rows, cannot executed concurrently (rule #1: failed).
is there pattern in sql solve kind of issue? 1 solution use guids , keep history of revisions (like git revisions) less easier having integer can compare see if revision newer one.
update:
the business case create baas system (backend service) data synchronization between client , server. here use cases kind of system:
- client while online modifies asset, pushes update server, server updates db [this question relates to], server sends update notifications interested clients synchronize local data new changes.
- client connects server, client requests pull server, server finds changes applied after client's revision , return them client, client applies changes , sets new revision.
- ...
as can see, global revision lets me put revision on every changes committed on server , revision, can determine updates need sent clients depending on specific revision.
this needs scale multiple thousands of users can push updates in parallel , changes must synchronized other connected users. longer takes execute transaction, longer takes other users receive change notifications.
i want avoid as possible contention reason. not expert in sql want make sure there not missing let me easily.
probably easiest thing try use sequence
revision number, assuming you're @ sql 2012 or newer. lighter-weight way of generating auto-incrementing value can use revision id per rules. acquiring them @ scale should far less subject contention issues describe using full-fledged table.
you need know end revision number gaps if given transaction rolled back, because sequence
values operate outside of transactional scope. article:
sequence numbers generated outside scope of current transaction. consumed whether transaction using sequence number committed or rolled back.
if can relax requirement integer revision number , settle knowing data @ given point in time, might able use change data capture, or, in sql 2016, temporal tables. both of these technologies allow "turn time" , see data looked @ known timestamp.
Comments
Post a Comment