Skip to content

Instantly share code, notes, and snippets.

View johnsonlu's full-sized avatar

Johnson Lu johnsonlu

  • Shanghai, China
View GitHub Profile
@johnsonlu
johnsonlu / avoid-distributed-transactions.md
Created December 15, 2023 08:07 — forked from rponte/avoid-distributed-transactions.md
THEORY: Distributed Transactions and why you should avoid them (2 Phase Commit , Saga Pattern, TCC, Idempotency etc)

Distributed Transactions and why you should avoid them

  1. Modern technologies won't support it (RabbitMQ, Kafka, etc.);
  2. This is a form of using Inter-Process Communication in a synchronized way and this reduces availability;
  3. All participants of the distributed transaction need to be avaiable for a distributed commit, again: reduces availability.

Implementing business transactions that span multiple services is not straightforward. Distributed transactions are best avoided because of the CAP theorem. Moreover, many modern (NoSQL) databases don’t support them. The best solution is to use the Saga Pattern.

[...]

@johnsonlu
johnsonlu / SkipWhenPreviousJobIsRunningAttribute.cs
Created November 16, 2023 08:21 — forked from odinserj/SkipWhenPreviousJobIsRunningAttribute.cs
SkipWhenPreviousJobIsRunningAttribute.cs
// Zero-Clause BSD (more permissive than MIT, doesn't require copyright notice)
//
// Permission to use, copy, modify, and/or distribute this software for any purpose
// with or without fee is hereby granted.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
// OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
function Set-Progress {
param( [int]$Percent )
if ($Progress -lt 100) {
Write-Host -NoNewline "`e]9;4;1;${Percent}`e\"
}
else {
Write-Host -NoNewline "`e]9;4;0`e\"
}
}
@johnsonlu
johnsonlu / graphLookup.js
Created November 23, 2021 08:48 — forked from matinrco/graphLookup.js
Brief example of MongoDB graphLookup to work with tree structure such as e-commerce product categories
// Insert some data
db.product_categories.insertMany([
{
_id: 1,
name: 'Products',
parent_id: null
},
{
_id: 2,
name: 'Digital & Electronics',
@johnsonlu
johnsonlu / ByteRangesRouteConstraint.cs
Created June 24, 2021 06:13 — forked from theuntitled/ByteRangesRouteConstraint.cs
ASP.NET Core 3 Partial Content Response (Status Code 206) for electron-updater
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
namespace Example
{
public class ByteRangesRouteConstraint : IRouteConstraint
{
public bool Match(HttpContext httpContext, IRouter route, string routeKey, RouteValueDictionary values, RouteDirection routeDirection)
{
if (!values.TryGetValue("url", out var url) || url == null)
@johnsonlu
johnsonlu / How to sync fork with original repo.md
Last active February 15, 2019 08:47 — forked from CristinaSolana/gist:1885435
Keeping a fork up to date

1. Clone your fork:

git clone [email protected]:YOUR-USERNAME/YOUR-FORKED-REPO.git

2. Add remote from original repository in your forked repository:

cd into/cloned/fork-repo
git remote add upstream git://github.com/ORIGINAL-DEV-USERNAME/REPO-YOU-FORKED-FROM.git
git fetch upstream