https://kotlinlang.org logo
Title
b

Brindasanth

02/07/2020, 11:38 PM
Hi Team, Is there any default distributed lock implementation in ktor ?
e

Evan R.

02/08/2020, 1:43 PM
I don’t think so, but you might check out Apache Zookeeper if you’re on Kotlin/JVM for that
b

Brindasanth

02/08/2020, 2:40 PM
Thanks for the response, I was looking for Kotlin APIs in non blocking way.
t

TormentedDan

02/08/2020, 4:25 PM
I can recommend Hazelcast's fenced lock. Check it out 😄
b

Brindasanth

02/08/2020, 4:48 PM
Thanks, we used Hazelcast and Atomix, but startup time is wost, complex configurations and kills performance. I thought this kind of API can be handled effectively by kotlin coroutines as a Ktor platform solution.
b

Brian Dilley

02/08/2020, 4:49 PM
I wrote one using postgres locks
b

Brindasanth

02/08/2020, 4:51 PM
let me tyr this
b

Brindasanth

02/08/2020, 5:01 PM
It is informative, what about the performance?, How it behaves for long time based lock? say locking for 1 hrs, having request volume min 500 per hour, If want to use the lock for scheduling activities, How can I use this?
let me put list of requirements:
1: Lock when processing dependent resources .
b

Brian Dilley

02/08/2020, 5:03 PM
Not so good for long locks, it requires you keep the connection acquired for the duration of the lock.
But 500 per hour is nothing, it can handle much more than that
Based on your questions alone so far, it'll probably do the job just fine for you.
b

Brindasanth

02/08/2020, 5:05 PM
2: Communicate to multiple replicas for certain case: ex: cleaning cache in multiple replicas
b

Brian Dilley

02/08/2020, 5:05 PM
Don't use locks for scheduling activities
b

Brindasanth

02/08/2020, 5:05 PM
ok
that is what i am looking for optimum solution in Ktor
b

Brian Dilley

02/08/2020, 5:05 PM
I can't help you with ktor
b

Brindasanth

02/08/2020, 5:06 PM
which handles cluster co-ordination, simillar to Hazelcast, simplified and performant version
Sure Brian, Thanks for your information
t

TormentedDan

02/08/2020, 5:07 PM
I recently implemented distributed locks using pg advisory locks, so far everything's been going well in prod. The main hurdle you'll have to overcome is a way to execute raw pg statements directly, once you've got a way to do this reliably then you're basically good to go.
For my implementation I generate a Long by hashing a unique key generated by the objects I want to lock, these objects implement a Lockable interface just so we can get the key.
A way to avoid stale locks is to use the transactional version of the locks but you'll have to deal with transactions, which can be a bit messy.
b

Brindasanth

02/08/2020, 5:12 PM
let me explore that, Sorry for so many question, I am working Edge IOT/Telecommunication use cases, Accessing core network and DB solution is not reliable and not performant.
some times it may be server less functions, automatically connecting to peers and communicate each other
Ktor has good startup time and consumes less memory
only missing is co-ordinations
for fail over / scaling usecases and
t

TormentedDan

02/08/2020, 5:16 PM
I'd also look into using redisson + redis which in my experience just flies. Managed redis instances are a bit more expensive than their relational counterparts but usually you should be good with tiny instances if all you're using it for is distributed coordination. Tho, take into account that redisson's redlock is clock skew susceptible so it's not a 'correct' distributed lock mechanism but rather a 'correct enough most of the time''
b

Brian Dilley

02/08/2020, 5:17 PM
You can use memcache CAS too
b

Brindasanth

02/08/2020, 5:17 PM
let me check
t

TormentedDan

02/08/2020, 5:20 PM
@Brian Dilley we used to use memcached in prod but... To quote memcached developers themselves, distributed locking using memcached is ghetto af. We spent some time redoing all our distributed components after encountering too much data inconsistency that could only be explained by faulty distributed coordination. So far, we're really glad to have ditched memcached.
r

rrva

02/09/2020, 1:58 PM
Someone recommend a redis-based distributed lock
in java/kotlin
t

TormentedDan

02/09/2020, 7:58 PM
Redis + Redisson's redlock
b

Brian Dilley

02/09/2020, 8:04 PM
I've used a ton of different solutions for this, and really nothing beats Zookeeper for performance and reliability - just has the downside of managing a ZK cluster ;/
We use redis currently, also as a message bus with it's pub/sub
t

TormentedDan

02/10/2020, 3:13 PM
Totally agree with you, ZK rules. Too bad managed solutions are scarce/basically non-existant.