This is a talk that I had given at an Emacs meetup hosted by Helpshift.
In this post, I have tried explaining what Consistent Hashing is, when it is needed and how to implement it in Clojure. Consistent hashing has many use cases. I have chosen the use case of distributed caching for this post.
Almost all applications today use some kind of caching. Caches help reduce the number of requests served by your database and improve latency. Initially, your application would have one cache node sitting over your database. On read paths, it will be checked if data is available on the cache node, if not, you would go to the database and populate the data on your cache node. On write paths, you would first update the database and then your cache. Or let the cached item expire with some TTL according to your consistency requirements. With this setup, you are using your cache node as a superfast index to look up hot items and absorb traffic that would have otherwise gone to your database.
But as your application usage grows, your cache node is also going to get overwhelmed and soon enough, you will need multiple cache nodes. When you have multiple nodes, you will need to decide how you are going to divide data between those nodes.
One very simple strategy to divide data between cache nodes is to take an integer hash of the cache key and then take the mod by the number of cache nodes.Read More
In this post, I have tried to cover what nREPL is, what nREPL Middleware are and why we need them. We will also look at the middleware provided by
cider-nrepland in the end, we will write our own custom middleware.
If you write Clojure, you are most probably using nREPL as that is the most popular REPL out there. But it is not the only REPL out there.
The Clojure REPL
Clojure itself has its own REPL. To start the default Clojure REPL, just run the
clojureon your terminal. You will see a REPL prompt. This is the default Clojure REPL.
(println "Hello, world!")and you will see it works just like any other REPL.
But there are a few problems with this REPL. It does not support some basic features like autocompletion or line editing (visiting previously typed text with up arrow)
Line editing will work if you use the
cljcommand, but it is just a wrapper over
If you read
man clojure, you will also notice that there is no easy way to start this REPL on a socket. So if you are using this REPL, you cannot connect to it from remote machines.
So the default REPL clearly cannot be used as your daily development REPL. That is where the need for other types of REPLs comes in.
Network REPL (nREPL) is a huge improvement over the default Clojure REPL. nREPL has a client server design. As the name suggests, an nREPL server can be started on a socket so that remote clients can connect to it. It also gives you support for autocompletion, symbol lookups and many more things.Read More