<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Davide Pedone on Medium]]></title>
        <description><![CDATA[Stories by Davide Pedone on Medium]]></description>
        <link>https://medium.com/@davide.pedone?source=rss-a83be73fff27------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*ib-abgYy613lBPEfW3u9Yw.jpeg</url>
            <title>Stories by Davide Pedone on Medium</title>
            <link>https://medium.com/@davide.pedone?source=rss-a83be73fff27------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 17:02:18 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@davide.pedone/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Load test a Kafka application with Gatling]]></title>
            <link>https://medium.com/twodigits/load-test-a-kafka-application-with-gatling-04dbcb7baac5?source=rss-a83be73fff27------2</link>
            <guid isPermaLink="false">https://medium.com/p/04dbcb7baac5</guid>
            <category><![CDATA[load-testing]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[apache-kafka]]></category>
            <category><![CDATA[gatling]]></category>
            <dc:creator><![CDATA[Davide Pedone]]></dc:creator>
            <pubDate>Mon, 22 Jul 2024 06:43:02 GMT</pubDate>
            <atom:updated>2024-07-22T06:43:02.489Z</atom:updated>
            <content:encoded><![CDATA[<h4>A brief description of how I’ve modified a Gatling plugin to suit our needs and create a meaningful load test scenario for our application</h4><p>Recently I’ve been asked to setup a load test on a really interesting project which I’m working on and I would like to tell you about but…<em>it’s classified. I could tell you, but then I’d have to kill you</em>. So, for yours (and mine) safety, allow me to be a bit generic describing the architecture and what we want to achieve: we have an application that consumes messages from a Kafka topic “A”, it does some processing calling different APIs and publishing intermediate and then final status update to a Kafka topic “B”. It goes without saying that we want to know how long it will take for a message to reach the final status and how many message per second our application can process.</p><figure><img alt="Quote from the movie Top Gun." src="https://cdn-images-1.medium.com/max/400/0*h8k_33yOgmLNISNw.gif" /><figcaption>Image from <a href="https://giphy.com/">https://giphy.com/</a></figcaption></figure><p>Back in days when I started working on it the most recently updated third party plugin listed on Gatling docs was, you don’t say, <a href="https://github.com/Tinkoff/gatling-kafka-plugin">Gatling Kafka Plugin</a> from Tinkoff.</p><h4>Matching request and response</h4><p>Out of the box the plugin allows to match messages based on the payload or providing a function to extract the desired information (key, field from payload, etc) but neither of the two options could work for us because we need two different functions, one for the request and one for the response. A small addition to the protocol builders</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fe1dd53796007f41fdf0f2af0d027d81/href">https://medium.com/media/fe1dd53796007f41fdf0f2af0d027d81/href</a></iframe><p>allows to easily customise the match logic:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/75dc928a780873823e076dc0579b0c69/href">https://medium.com/media/75dc928a780873823e076dc0579b0c69/href</a></iframe><p>I’ve then started a local Kafka cluster, all required applications and setup a scenario, and within a few minutes I was able to run the first test. Everything went smoothly, results met expectations so I was almost ready to close my task but, as a good boy, I first tried to run some scenarios against DEV environment and unfortunately the Gatling report showed only failures. The “<em>it works on my machine</em>” curse struck again.</p><h4><strong>Investigating and fixing the failures</strong></h4><p>The first possible cause off the top of my head was related to offset: locally I had an empty topic, in DEV environment the consumer topic contains thousands of old messages and, since according to <a href="https://kafka.apache.org/37/documentation/streams/developer-guide/config-streams.html#default-values">kafka stream docs</a> default value for <em>auto.offset.reset </em>is<em> earliest,</em> I’ve set the property to <em>latest</em> and give the test another go. The outcome was slightly better but some messages were still reported as failure and also the response time section looked very concerning.</p><p>A failure means that Gatling was not able to find a message in consumer to topic for a given produced message, so since that I don’t trust my code that much I thought that somehow under heavy load my application was not able to process some of the consumed messages. I checked the logs: no errors or issue. I was even able to see the messages on kafka topic, so what was going on? Let’s have a look at plugin source code, in particular at KafkaRequestReplyAction scala class:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fd94ec9855d29dc7aec2aa55df95ed6b/href">https://medium.com/media/fd94ec9855d29dc7aec2aa55df95ed6b/href</a></iframe><p>As you can see here in the send callback we simply call the tracker’s track method in order to listen for a reply. So far so good, let’s check then TrackersPool class:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/18beccce73ff763506c9ee9bd62563d6/href">https://medium.com/media/18beccce73ff763506c9ee9bd62563d6/href</a></iframe><p>Ok, now it’s clear. When the track method is called for the first time the ConcurrentHashMap is empty, so it has to create first a KafkaStream but this could require some time. No big deal locally with only 1 empty partition, but could be an issue on DEV/higher environments with multiple partitions and thousands of messages, mTLS, network latency, etc.</p><p>I’ve then edited the TrackersPool class, adding a method to get the tracker for a given topic and a KafkaStreamStateListener. The idea is maybe not elegant but simple: instantiate the stream and wait for it to be in the <em>RUNNING</em> status before start sending messages. A few runs against DEV environment confirmed that I was then able to match all the sent messages. Then it was time to tackle what I hope is the latest problem, response time.</p><p>Again, inspecting the code in TrackersPool class clearly highlights why the response time in report was way more high than expected. As you can see on line 35 of TrackersPool.scala class, when the plugin creates the MessageConsumed object it considers the current timestamp, meaning that: given <em>x </em>the timestamp of the sent message, <em>y</em> the timestamp of the response produced by our application under test and <em>z </em>the timestamp of when Gatling has consumed the message, then in the report we will see the value of <em>z-x </em>instead of <em>y-z </em>that would makes much more sense for our testing purposes. The CustomProcessor class I’ve created and injected into the stream contains the “old” logic for notifying Gatling that a message has been consumed, but gives us access to the Kafka record object and therefore the timestamp.</p><h4>Make life easier for the QA team</h4><p>As mentioned earlier, the tested application produces multiple messages to publish processing updates. Some of them are just informative, some of them are warnings due temporary errors/issues, some of them are actually a final state. For our load testing purposes, the QAs not only wanted that the scenario to match the “final” state exclusively but also to be able to differentiate between success and failure. These QAs…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/590/1*pTpsn6QEwUgzY6uFsBKbzA.jpeg" /><figcaption>Developers will never admit it but they ❤ QA team</figcaption></figure><p>Seriously, those requests were legit and allows to have meaningful test scenarios. To ignore the intermediate messages the generic KafkaMatcher came to the rescue. In the responseMatch function we can easily implement our own logic to return the matching byte array only for final success messages but this leads to a problem: the failure messages are not matched and are reported as failed in the Gatling report, making them indistinguishable from real unmatched messages.</p><p>I’ve then enhanced/fixed the existing KafkaCheck.Simple to allow defining a condition to determine whether a matched message should be considered a success or a failure, and to pass a custom error message that will show up in the Gatling report.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f1c947993f4339ef91cc2cf83db35df4/href">https://medium.com/media/f1c947993f4339ef91cc2cf83db35df4/href</a></iframe><p>Let me know what do you think about it or propose a better solution in the comments. You can find all the described changes <a href="https://github.com/davidepedone/gatling-kafka-plugin/pull/2/files#diff-510fdd304b6b85ea1a3fc8c3b09ab3e8df67361c6efe5bd1dc2304bb697be88b">in this MR</a> on GitHub.</p><p><a href="https://github.com/davidepedone/gatling-kafka-plugin">GitHub - davidepedone/gatling-kafka-plugin: Plugin for support Kafka in Gatling</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=04dbcb7baac5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/twodigits/load-test-a-kafka-application-with-gatling-04dbcb7baac5">Load test a Kafka application with Gatling</a> was originally published in <a href="https://medium.com/twodigits">NEW IT Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Keep your git commit history clean with squash]]></title>
            <link>https://medium.com/twodigits/keep-your-git-commit-history-clean-with-squash-f4886790a4c9?source=rss-a83be73fff27------2</link>
            <guid isPermaLink="false">https://medium.com/p/f4886790a4c9</guid>
            <category><![CDATA[git-squash]]></category>
            <category><![CDATA[clean-code]]></category>
            <category><![CDATA[git]]></category>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[git-rebase]]></category>
            <dc:creator><![CDATA[Davide Pedone]]></dc:creator>
            <pubDate>Wed, 30 Mar 2022 08:38:46 GMT</pubDate>
            <atom:updated>2022-03-30T08:38:46.568Z</atom:updated>
            <content:encoded><![CDATA[<h4>Don’t let your git history become a mess</h4><p>It’s 8 am and you are ready to work on a new task. You pull the latest updates from the main branch and checkout a new branch (if not, shame on you), eager to start coding. You make some changes, commit. Keep working, another change then another commit. I guess you got the point.</p><p>You might think that’s nothing wrong with it, but also all your team mates will do the same and in a jiffy your git history will be a mess.</p><p>Still can’t see the problem? Ok, imagine that after a while you need to investigate to find which commit caused an issue looking through an endless list of a useless commit. Of course git bisect can come to the rescue but without a clean history it can be ineffective.</p><p>I strongly believe that git history is part of code documentation and it should effectively describe what was changed and why. So there is no reason to have a git commit history that looks like this:</p><figure><img alt="Sample git history for a new feature" src="https://cdn-images-1.medium.com/max/764/1*MyJg1xjEb8rz_3Hkk-h3mQ.png" /><figcaption>Typical commit messages overflow</figcaption></figure><p>You have 5 commits, all related to a single feature and 4 of them have no reason to exist since they don’t provide any useful information about what you have added or changed. Wouldn’t it be better to have just one?</p><h4>What is git squash</h4><p>Squashing <em>n</em> commits will create a new commit obtained by combining all the selected commits. A picture is worth a thousand words:</p><figure><img alt="How commit history will looks like after squashing last 2 commits" src="https://cdn-images-1.medium.com/max/1024/1*aa3TtxfmHjv-t9-A88MddA.png" /><figcaption>Visual representation of how squashing 2 commit affects git tree</figcaption></figure><h4>Looks cool, how can I do it?</h4><p>Easy tiger, there is an important thing we need to consider first. We are about to rewrite the history here, so it will be safe as long as we are working on our local machine or we are the only one working on a branch. In fact, squashing already pushed commits will fail unless a --force option will be provided.</p><p>That being said it’s time to squash! We will use git rebase command, with an interactive option. A rebase is a way to replay commits, one by one, on top of a branch. An interactive rebase is a way to replay commits, one by one, choosing interactively what to do with each one. Possible actions are:</p><ul><li>pick (p): use commit</li><li>reword (r): use commit, but edit the commit message</li><li>edit (e): use commit, but stop for amending</li><li>squash (s): use commit, but meld into the previous commit</li><li>fixup (f): like “squash”, but discard this commit’s log message</li><li>exec (x): run command (the rest of the line) using shell</li><li>drop (d): remove a commit</li></ul><p>Now we have two options here: specify how many commits we want to squash git rebase -i HEAD~[number of commits]or pick our target commit by his hash git rebase -i [hash of &quot;target&quot; commit]. No matter what option do you choose, we are saying to git that we want to do an interactive rebase, so your configured editor will pop up showing something like that:</p><figure><img alt="Interactive rebase task list" src="https://cdn-images-1.medium.com/max/1024/1*4-oMoVb3mm9UbGOpiE22AA.png" /><figcaption>Interactive rebase task list</figcaption></figure><p>Commits are shown in order so keep in mind that we will need to pick at least one commit before the commits we want to squash (other actions are out of scope for this post). Saving changes and closing the file will open a new editor window where we can work on the message for the new commit that will be created. Once we are happy with it just save &amp; close and we are done. Obviously don’t forget to push it to remote.</p><h4>Make life easier</h4><p>If you plan to use this approach I strongly suggest to setup git in order to “reduce” required work when we perform an interactive rebase. First of all, configure autosquash with git config --global rebase.autosquash true. From now on you if you already know that your next commit(s) will be squashed you can simply prepend a commit message like these: git commit -m &#39;squash! commit message goes here&#39; (same applies to other actions like fixup! , edit! , …). Doing so will end up in an already pre-populated task list for interactive rebase, saving you some time fighting with vim.</p><h4>Nice, but still a lot of work</h4><p>Ok, someone is being lazy here. If you like the idea of having a clean commit history but you don’t want to do a rebase for every feature branch you can simply use the --squash option when merging: git merge --squash. You will get the same result but with slightly less control over what you are doing.</p><p>Also, services like GitHub, GitLab, and Bitbucket offer the possibility to select squash as a merge strategy for pull requests.</p><h4>Wrap it up</h4><ul><li>git history is part of the documentation, leave it as you would like to find it</li><li>Be extra careful when performing rebase on a shared branch</li><li>Consider using/force squash as the default merge strategy</li><li>Write a meaningful commit message</li></ul><p>As for most of the best practices in software development, squashing commits will be effective only if shared and adopted by all of the team members.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f4886790a4c9" width="1" height="1" alt=""><hr><p><a href="https://medium.com/twodigits/keep-your-git-commit-history-clean-with-squash-f4886790a4c9">Keep your git commit history clean with squash</a> was originally published in <a href="https://medium.com/twodigits">NEW IT Engineering</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Cursor based pagination with Spring Boot and MongoDB]]></title>
            <link>https://medium.com/@davide.pedone/cursor-based-pagination-with-spring-boot-and-mongodb-bca6446f3b1f?source=rss-a83be73fff27------2</link>
            <guid isPermaLink="false">https://medium.com/p/bca6446f3b1f</guid>
            <category><![CDATA[pagination]]></category>
            <category><![CDATA[spring-boot-2]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[mongodb]]></category>
            <category><![CDATA[cursor-based-pagination]]></category>
            <dc:creator><![CDATA[Davide Pedone]]></dc:creator>
            <pubDate>Mon, 23 Nov 2020 19:49:49 GMT</pubDate>
            <atom:updated>2020-11-23T19:49:49.055Z</atom:updated>
            <content:encoded><![CDATA[<h4>Say goodbye to offset pagination with few lines of code</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xKBuNttD2yi-fRO1kqqfFg.jpeg" /><figcaption>Photo by <a href="https://www.pexels.com/it-it/@pixabay">Pixabay</a> on <a href="https://www.pexels.com/">Pexels</a></figcaption></figure><p>According to Facebook <em>“Cursor-based pagination is the most efficient method of paging and should always be used where possible”. </em>I’m gonna have a quick look into how to use it in your Spring Boot based application.</p><p>Building a RESTful Web Service is quite easy with Spring Boot, all we have to do is map entities, create a repository and a controller. If the repository extends PagingAndSortingRepository, as MongoRepository does, Spring Data REST recognises some URL parameters that will influence the page size and starting page number. Let’s write some code and see what happens.</p><p>Define an Entity, a Repository and a simple Controller:</p><pre>@Data<br>@Document(collection = &quot;tweets&quot;)<br>public class Tweet {<br>    private ObjectId id;<br>    private String author;<br>    private String content;<br>    private Long createdAt;<br>}</pre><pre>public interface TweetRepository extends MongoRepository&lt;Tweet, ObjectId&gt; {<br>}</pre><pre>@GetMapping(value = &quot;/paged&quot;)<br>public Page&lt;Tweet&gt; getPaged(Pageable pageable) {<br>   return tweetRepository.findAll(pageable);<br>}</pre><p>Now a GET request like these http://myapi.com/tweets?size=10&amp;page=1 will be translated by Spring in the following query against the database:</p><p>db.tweets.find().skip(10).limit(10); to retrieve the result and:</p><p>db.tweets.count(); to retrieve pagination metadata, such as total elements and total page numbers.</p><p>This approach works fine for most use cases but there are some caveats you should be aware of.</p><ul><li><strong>missing elements: </strong>if the dataset changes during page navigation you could miss some element. Deleting an element in a delivered page will shift elements of the next page up</li><li><strong>result set scan: </strong>as reported by <a href="https://docs.mongodb.com/manual/reference/method/cursor.skip/">MongoDB documentation</a> the cursor.skip() method requires the server to scan from the beginning of the input results set before beginning to return results. As the offset increases, cursor.skip() will become slower.</li><li><strong>count: </strong>counting all elements has an impact on performance</li></ul><h3>A better approach: cursor based pagination (a.k.a. range queries)</h3><p>Imagine a cursor as a bookmark to a document in the database. When we ask the server for the first page of data, we of course get the data, as well as an additional information about this bookmark. Passing the bookmark in the next query allows the server to simply continue where he left off in the previous request.</p><blockquote>Range queries can use indexes to avoid scanning unwanted documents, typically yielding better performance as the offset grows compared to using cursor.skip() for pagination.</blockquote><h4>Choosing the “bookmark”</h4><p>Again, MongoDB documentation comes to the aid:</p><blockquote>Use this procedure to implement pagination with range queries:</blockquote><blockquote>Choose a field such as _id which generally changes in a consistent direction over time and has a unique index to prevent duplicate values</blockquote><blockquote>Query for documents whose field is less (or more) than the start value using the $lt (or $gt) and cursor.sort() operators</blockquote><blockquote>Store the last-seen field value for the next query.</blockquote><p>Ok, we know that _id is unique and sortable so we can use this field as a bookmark (from now on continuationToken). Really? Maybe not.</p><p>ObjectId value increases over time but we can’t be sure that sorting a collection by _id reflects creation order of the documents. In fact, ObjectId only contains a 4 byte timestamp value and it is generated client side by the driver. Also, as happens in a real case scenario, we want our API to offer the ability to sort results not just by id. For our Tweets API we can choose to sort results by author or by creationDate but both these fields can’t guarantee uniqueness. An author could write more than one tweet, and two or more tweets could have been created at the same time.</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fgiphy.com%2Fembed%2FW3a0zO282fuBpsqqyD%2Ftwitter%2Fiframe&amp;display_name=Giphy&amp;url=https%3A%2F%2Fgiphy.com%2Fgifs%2FFriends-episode-15-friends-tv-the-one-where-estelle-dies-W3a0zO282fuBpsqqyD&amp;image=https%3A%2F%2Fmedia3.giphy.com%2Fmedia%2FW3a0zO282fuBpsqqyD%2Fgiphy.gif&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=giphy" width="435" height="362" frameborder="0" scrolling="no"><a href="https://medium.com/media/e58bc96b5bce6c83ec377ed79d00e368/href">https://medium.com/media/e58bc96b5bce6c83ec377ed79d00e368/href</a></iframe><p>The solution? Use two fields at a time (id+author, id+creationDate, …) as continuationToken.</p><p>Our new GET request will look like this: http://myapi.com/tweets?size=10&amp;continuationToken=abc_def&amp;sort=author where <em>abc</em> and <em>def</em> respectively represents the ObjectId and the author of the last entity of the previous response.</p><p>We need to produce a query like this:</p><pre>db.tweets.find({<br>  $or: [<br>    { author: { $gt: &quot;def&quot; } },<br>    { author: &quot;def&quot;, _id: { $gt: &quot;abc&quot; } }<br>]})<br>.sort({ author: 1, _id: 1 })<br>.limit(11);</pre><p>Basically we are searching all documents, sorted by author and id, that satisfy one of the following conditions:</p><ul><li>author: {$gt: &quot;def&quot;}</li><li>author: &quot;def&quot; AND _id: {$gt: &quot;abc&quot;}</li></ul><p>The first one allows us to move on to next author if all tweets by “def” were already returned, the latter acts as a tiebreaker in case more entities by author “def” need to be retrieved. We asked for a page with 10 elements but we queried the database for 11 elements. Doing so we are able to determine if there are more items that needs to be retrieved simply checking if the results set size is greater than page size.</p><h4>Trade off</h4><p>As we said before, the field used for range queries must be unique and adding the _id to the chosen sort field will guarantee uniqueness even if the sort field is null. So we can sort by any field right? Wrong.</p><p>Let’s assume we have a collection containing the following elements:</p><pre>[<br>  {<br>    &quot;_id&quot;: &quot;1&quot;,<br>    &quot;birthday&quot;: &quot;2000/1/1&quot;,<br>    &quot;name&quot;: &quot;abc&quot;<br>  },<br>  {<br>    &quot;_id&quot;: &quot;4&quot;,<br>    &quot;birthday&quot;: &quot;2001/1/1&quot;,<br>    &quot;name&quot;: &quot;def&quot;<br>  },<br>  {<br>    &quot;_id&quot;: &quot;2&quot;,<br>    &quot;birthday&quot;: &quot;2020/1/1&quot;,<br>    &quot;name&quot;: &quot;ghi&quot;<br>  },<br>  {<br>    &quot;_id&quot;: &quot;3&quot;,<br>    &quot;name&quot;: &quot;jkl&quot;<br>  }<br>]</pre><p>and we want to retrieve all records of the collection sorted by birthday with a page size of 3. We can easily imagine the sort result by birthday and id. The first record is the one with _id:2, then _id:4, then _id:1 and then _id:3.</p><p>Here is our query:</p><pre>db.persons.find({})<br>.sort({ birthday: -1, _id: -1 })<br>.limit(4);</pre><p>We have asked for a page with size of 3 elements, result set contains (all) 4 records from database so we know that there is a next page. Ok, run the query to retrieve it:</p><pre>db.persons.find({<br>  $or: [<br>    { birthday: { $lt: &quot;2000/1/1&quot; } },<br>    { birthday: &quot;2000/1/1&quot;, _id: { $lt: &quot;1&quot; } }<br>]})<br>.sort({ birthday: -1, _id: -1 })<br>.limit(4);</pre><p>But none of the 4 documents matches these conditions, so we know from the first query that there are more than 3 document but we can’t retrieve them.</p><blockquote>Lesson learned: NEVER sort by fields that may contain null values</blockquote><h4>Putting things together</h4><p>To make things easier I’ve written a <a href="https://github.com/davidepedone/spring-cursor-pagination">small library</a>. Just add it to the list of our project dependencies and go back to IDE to see how to use it.</p><pre>&lt;dependency&gt;<br>    &lt;groupId&gt;it.davidepedone&lt;/groupId&gt;<br>    &lt;artifactId&gt;spring-cursor-pagination-mongodb&lt;/artifactId&gt;<br>    &lt;version&gt;1.1.1&lt;/version&gt;<br>&lt;/dependency&gt;</pre><p>Create a TweetSearchFilter class to add filter capabilities to our controller</p><pre>public class TweetSearchFilter {<br><br>   private String author;</pre><pre>   // getter/setter<br>}</pre><p>And a TweetPaginationService class to customize the query (configSearchQuery) and provide information to base class to build the continuationToken.</p><pre>public class TweetPaginationService extends CursorPaginationService&lt;Tweet, TweetSearchFilter&gt; {<br><br>   public TweetPaginationService(MongoOperations mongoOperations) {<br>      super(mongoOperations, List.<em>of</em>(&quot;createdAt&quot;, &quot;author&quot;), Tweet.class);<br>   }<br><br>   @Override<br>   public void configSearchQuery(Query query, TweetSearchFilter filter, Principal principal) {<br>      Optional.<em>ofNullable</em>(filter.getAuthor()).ifPresent(a -&gt; {<br>         query.addCriteria(<em>where</em>(&quot;author&quot;).is(a));<br>      });<br>   }<br><br>}</pre><p>Finally update the TweetController:</p><pre>@GetMapping<br>public ContinuationTokenSlice&lt;Tweet&gt; getAll(TweetSearchFilter tweetSearchFilter, CursorPageable cursorPageable) throws Exception {<br>   return tweetPaginationService.executeQuery(tweetSearchFilter, cursorPageable);<br>}</pre><p>Now a GET request to <a href="http://myapi.com/posts?size=10">http://myapi.com/tweets?author=xyz&amp;sort=createdAt&amp;direction=DESC&amp;size=10</a> will produce something like this:</p><pre>{<br>    &quot;content&quot;: [<br>     [....]<br>        {<br>            &quot;id&quot;: &quot;5ed6a45d0038622837ba60da&quot;,<br>            &quot;author&quot;: &quot;xyz&quot;,<br>            &quot;content&quot;: &quot;......&quot;,<br>            &quot;createdAt&quot;: 1590585400521<br>        }<br>    ],<br>    &quot;continuationToken&quot;: &quot;4LFv0w30RzTOTFRy2RzRz15Wu2in73AfwM00KFhqVKSxqF9-yDMAZG8Jx7NpDoUz6oo97Vjssum5k6h7nw9o0g==&quot;,<br>    &quot;numberOfElements&quot;: 10,<br>    &quot;size&quot;: 10<br>}</pre><p>The continuationToken is encrypted and contains a hash of the previous query in order to avoid alteration of parameters and consequently unexpected results. The decrypted value is composed of 4 parts, separated by an underscore character: <em>a5b2111b666cd3bd5689effc1eda29b6_5ed6a45d0038622837ba60da_createdAt_1590585400521.</em></p><p>The first part represents the MD5 hash of the previous query string excluding page size, the second part is the id of the last returned object, the third is the name of the sort field, the fourth is the sort field value of the last returned object and the latter is the sort value class. As explained before Id, sort field and sort value allow the library to build the next page query:</p><pre>db.tweets.find({<br>  $or: [<br>    { createdAt: { $lt: 1590585400521} },<br>    { createdAt: 1590585400521,<br>      _id: { $lt: &quot;5ed6a45d0038622837ba60da&quot; }<br>    }<br>]})<br>.sort({ createdAt: -1, _id: -1 })<br>.limit(10);</pre><p>Full source code is available on github.</p><p><a href="https://github.com/davidepedone/spring-cursor-pagination-demo">davidepedone/spring-cursor-pagination-demo</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=bca6446f3b1f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>