Hi Team, Is there any default distributed lock imp...
# server
b
Hi Team, Is there any default distributed lock implementation in ktor ?
e
I don’t think so, but you might check out Apache Zookeeper if you’re on Kotlin/JVM for that
b
Thanks for the response, I was looking for Kotlin APIs in non blocking way.
t
I can recommend Hazelcast's fenced lock. Check it out 😄
b
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
I wrote one using postgres locks
b
let me tyr this
b
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
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
2: Communicate to multiple replicas for certain case: ex: cleaning cache in multiple replicas
b
Don't use locks for scheduling activities
b
ok
that is what i am looking for optimum solution in Ktor
b
I can't help you with ktor
b
which handles cluster co-ordination, simillar to Hazelcast, simplified and performant version
Sure Brian, Thanks for your information
t
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
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
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
You can use memcache CAS too
b
let me check
t
@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
Someone recommend a redis-based distributed lock
in java/kotlin
t
Redis + Redisson's redlock
b
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
Totally agree with you, ZK rules. Too bad managed solutions are scarce/basically non-existant.