Meaning, they don't have to be distributed. I'm thinking about using memcached or redis for that. Probably the latter one. What I'm concerned about is "we've got to free some memory, so we'll delete this key/value before it expired" thing. But I'm open to other suggestions as well.
How to implement non-distributed locks which expire?
524 Views Asked by x-yuri At
1
There are 1 best solutions below
Related Questions in CONCURRENCY
- Entity Framework Code First with Fluent API Concurrency `DbUpdateConcurrencyException` Not Raising
- How to return blocking queue to the right object?
- How to ensure data synchronization across threads within a "safe" area (e.g not in a critical section) without locking everything
- Breakpoint "concurrency" in Intellij
- java, when (and for how long) can a thread cache the value of a non-volatile variable?
- Reentrancy and Reentrant in C?
- How to do many simultaneous jsoup sessions (Spring boot project and concurrancy)
- Using multiple threads to print statements sequentially
- Interrupting long working thread
- Usage of C++11 std::unique_lock<std::mutex> lk(myMutex); not really clear
- Using getOrElseUpdate of TrieMap in Scala
- Concurrency of JPA when same DB used by other applications
- erlang processes and message passing architecture
- Erratic StampedLock.unlock(long) behaviour?
- Jersey Client, memory leak, static and concurrency
Related Questions in REDIS
- start redis with supervisor
- How to do Mass insertion in Redis using JAVA?
- RedisResponseException: Unknown reply on multi-request
- Redis / Get all keys & values from redis with prefix
- Remove a member from multiple sets in Redis
- Using memcached or Redis on aws-elasticache
- Get Socket Object by Id with node, redis-adapter and socket.io
- how can i save a complex json as string in redis and retrieve it as unescaped legit json object
- How to specify versions on PIP when installing a python package with it's dependencies
- Eloquent model for Redis
- Is exists check required before calling StringSet method of StrackExchange.Redis
- Predis: Pros and Cons of the two cluster strategies
- hmset redis with result from mysqlDB
- does redis cluster support transactions ?
- How change redis to be persistent
Related Questions in LOCKING
- Excel 2013, Cells Will Not Protect
- Persist the value of one column in two simultaneous update
- Expired time for acquiring lock pentaho
- JPA JPQL Lock not working
- Erratic StampedLock.unlock(long) behaviour?
- Hazelcast Distributed Lock with iMap
- Hibernate increments version on both sides of many to many association
- I want to sleep while holding a mutex
- Simultaneous DML operations on same table based on different where clause
- Penalty of AtomicVariables over locks in Java
- Java combine explicit locks with synchronized methods
- lock function in php only for one user
- Multiple Simultaneous file writes with HMC4 on Classic ASP
- Java Multithreading - What Really Happens When Accessing A "Locked" Object?
- Mysql table simultaneous update of rows in single table
Related Questions in MEMCACHED
- Search for a key in django.core.cache
- how to install memcached PECL on centos 7 server
- Using memcached or Redis on aws-elasticache
- Laravel/Lumen: Could not establish Memcached connection
- Memcached on Windows (wamp) with Laravel
- ZF2 authentication session storage in memcached
- Using two cache providers in Play Framework at the same time?
- Not able to install php memcache
- Best solution / practice for temp files with Google App Engine PHP?
- Memory occupied by a quite large array MySQL result set in PHP?
- What means that expiration time in Memcached client is set to zero?
- Memcached and PHP Sessions in multiple servers
- recommendation for basic memory caching across process
- mysql memcached
- How to cache a very dynamic website in PHP?
Related Questions in CONCURRENT-PROGRAMMING
- Two-Lock Concurrent Queue Algorithm implementation issue
- How React PHP handles async non-blocking I/O?
- Increase same counter while concurrency
- How to implement non-distributed locks which expire?
- java - stealing bits from references
- Java Framework for managing Tasks
- How to use ExecutorService of java concurrent programming?
- Active Object Pattern in Concurrent Java 1.5+
- Eclipse link JPA Select Query returns null field value
- how can write code to download in a parallel way?
- java reuse an executor
- scala akka: random deadLetters while no actor is asked to stop
- How to read data on the main thread from a file being read on a worker thread
- Javafx Updating UI from a Thread without direct calling Platform.runLater
- Java 8: Parallel FOR loop
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
tl;dr Use ready-made solution, suggested by developers.
So, I decided not to use
memcachedfor the purpose. Since it's a caching server. I don't see a way to ensure that it doesn't delete my keys because it's out of memory. With,redisthat's not an issue as long asmaxmemory-policy = noeviction.There are 3 links I want to share with you. They are basically 3 ways, that I now know, to solve the issue. As long as you have
redis >= 2.6.0that is.redis >= 2.6.12
If you've got
redis >= 2.6.12, you're lucky and can simply usesetnxcommand with its new optionsexandnx:But we can't just delete the lock in the end, if we are to allow for critical section taking longer then we expected (
>= ttl). Consider the following situation:For that not to happen we are going to store current timestamp as a value of the lock. Then, knowing that Lua scripts are atomic (see Atomicity of scripts):
However, is it possible for two clients to have equal
nowvalues? For that all the above actions should happen within one second andttlmust be equal to0.Resulting code:
expire
The other solution I found here. You simply make the value expire with
expirecommand:So, only
acquire_lockfunction changes:getset
And the last one is described again in documentation. Marked with "left for historical reasons" note.
This time we store timestamp of the moment when the lock is to expire. We store it with
setnxcommand. If it succeeds, we've acquired the lock. Otherwise, either someone else's holding the lock, or the lock has expired. Be it the latter, we usegetsetto set new value and if the old value hasn't changed, we've acquired the lock:What makes me uncomfortable here is that we seem to have changed someone else's
expires_atvalue, don't we?On a side note, you can check which
redisis it that you're using this way:Some debugging functions: