Use case to sync SQL server with memstate #113 #115
Replies: 5 comments 4 replies
-
@rofr I am looking for one-way sync: SQL Server -> MemState So my answers are:
|
Beta Was this translation helpful? Give feedback.
-
Here's one way to do it. For each table you want to replicate, create a trigger in the db that captures the change and writes to an events table. In the process where memstate is running, poll the events table and update the in-memory model. Now you can start migrating reads from the db to memstate. A nice thing about this pattern is that you can start from zero and grow the in-memory model gradually. To rebuild state during startup you can either load memstate normally from backing storage or do an initial read from the database tables. If reading from the database you don't need any backing storage for memstate. |
Beta Was this translation helpful? Give feedback.
-
@rofr Do you any sample or start point for implementing this approach? |
Beta Was this translation helpful? Give feedback.
-
Here's an embryo to get started on the sql side, note that this could be generated from an Entity Framework model. -- An example table
create table Product(
Id int identity(1,1) not null primary key,
Name varchar(max) not null,
Description varchar(max) not null,
UnitPrice decimal(18, 2) not null,
)
go
create table MemstateChangeLog (
Id int not null primary key identity(1,1),
TableName varchar(max) not null,
Operation char(1) not null, -- 'U' or 'D'
TransactionId int not null default current_transaction_id(),
JsonData varchar(max)
)
go
create trigger OnProductsUpserted
on Product after insert, update as
declare @json varchar(max)
select @json = (select p.* from product p join inserted i on p.Id = i.Id for json auto)
insert MemstateChangeLog(TableName, Operation, JsonData)
values ('Product', 'U', @json)
go
create trigger OnProductsDeleted
on Product after delete as
declare @json varchar(max)
select @json = (select id from deleted for json auto)
insert MemstateChangeLog(TableName, Operation, JsonData)
values('Product', 'D', @json)
go
-- Setup completed, now lets do some inserts, updates and deletes
-- should be empty
select * from MemstateChangeLog
insert product(name, description,unitprice) values('1kg Cod', 'Lean white fish', 200)
insert product(name, description,unitprice) values('1kg Salmon', 'Pink and healthy', 150)
update product -- multiple rows affected
set unitprice = unitprice * 1.1
where Id < 5
delete product where id > 999980
-- And the results...
select * from MemstateChangeLog
|
Beta Was this translation helpful? Give feedback.
-
https://docs.microsoft.com/en-us/sql/relational-databases/track-changes/track-data-changes-sql-server?view=sql-server-ver15 |
Beta Was this translation helpful? Give feedback.
-
Hi,
Is it valid use case for using to sync memstate with SQL server tables?
How it can be achieved?
Hi @AlexSenchenko
If the question is "Can I use SQL Server as a backing store?" the answer is yes, use the Memstate.SqlStreamStore storage provider. This means that the command journal can be persisted to any RDBMS supported by SqlStreamStore
For replication/sync there are a few different patterns that can be applied depending on the requirements. Is it one-way or two-way replication? If one-way, which direction? Sync or async replication? Consistent or eventually consistent?
I've just activated the discussions feature on GH, can you add a question there elaborate on what kind of replication you have in mind?
Beta Was this translation helpful? Give feedback.
All reactions