create account

Making Market-Viewr More Robust with Caching and Retries by thecrazygm

View this thread on: hive.blogpeakd.comecency.com
· @thecrazygm ·
$8.78
Making Market-Viewr More Robust with Caching and Retries
Hey everyone,

I've been keeping an eye on my server logs recently and noticed something I hate to see: the gunicorn worker for my **Market-Viewr** app was periodically dying and being restarted. The application was unstable under load, and it was time to investigate why.


![It works so much faster!](https://files.peakd.com/file/peakd-hive/thecrazygm/EoEwMZiUTxMXEjeDZ2CgouAkHfQqfFdmZjv7ZNAqPGt39mmPTfi21u34Q2DhsZKBhjH.png)


After digging into the logs and doing some performance profiling, the culprit became clear. The application was making frequent, expensive API calls to fetch token richlists from Hive-Engine, especially when a user requested a CSV export. I realized the CSV export was making a _separate, fresh pull_ of the entire richlist, even if the web page had just loaded it. This was inefficient and, for very large richlists, was causing the worker to time out or run out of resources.

To fix this and make the whole application more stable and performant, I've implemented two key backend improvements: **caching** and **retry logic**.

#### 1. Smart Caching with a Fallback

The biggest improvement was adding a caching layer. In simple terms, caching stores the result of an expensive operation (like an API call) for a set period. If another request for the same data comes in during that period, the app serves the stored result instead of hitting the API again. This is massively more efficient.

I used the `flask-caching` library and set it up with a clever fallback system:

- It first tries to connect to a **Redis** server, which is a fast, in-memory database perfect for caching.
- If Redis is unavailable for any reason, it automatically falls back to a **SimpleCache** that just stores the data in the application's memory.

This ensures the app still benefits from caching even if the Redis service isn't running. I've applied different cache timeouts to different pages – for example, the homepage with the list of all tokens is cached for longer, while a specific token's market page has a shorter cache time to keep the data fresher.

#### 2. Resilient API Calls with Retry Logic

The second issue is that sometimes API nodes can have a temporary blip or a network connection can fail. To make the app more resilient to these transient errors, I've configured the `requests` library to automatically retry failed requests.

If a connection to the Hive-Engine API fails, the app will now automatically try again a few times with a short delay between attempts. This means a temporary network hiccup is much less likely to result in an error page for the user.

#### The Result: A Faster, More Stable App

These backend changes should result in a noticeably better experience for you:

- **Faster Load Times:** Many pages will now load almost instantly from the cache.
- **Increased Reliability:** The site should be much more stable and less prone to errors or timeouts, especially when viewing tokens with large richlists.

While I was in there, I also made a small quality-of-life tweak to sort the richlist by the total holdings (staked + balance) by default, which gives a more accurate picture of a user's position.

These kinds of infrastructure improvements aren't as flashy as a new feature, but they are crucial for a good user experience. I hope you find the updated Market-Viewr to be a smoother and more reliable tool!

As always,
Michael Garcia a.k.a. TheCrazyGM
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 119 others
properties (23)
authorthecrazygm
permlinkmaking-market-viewr-more-robust-with-caching-and-retries
categoryhive-186392
json_metadata{"app":"peakd/2025.6.1","format":"markdown","tags":["dev","tribes","archon","proofofbrain","pimp","oneup"],"users":[],"image":["https://files.peakd.com/file/peakd-hive/thecrazygm/EoEwMZiUTxMXEjeDZ2CgouAkHfQqfFdmZjv7ZNAqPGt39mmPTfi21u34Q2DhsZKBhjH.png"]}
created2025-06-15 09:39:06
last_update2025-06-15 09:39:06
depth0
children11
last_payout2025-06-22 09:39:06
cashout_time1969-12-31 23:59:59
total_payout_value4.044 HBD
curator_payout_value4.739 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length3,415
author_reputation92,994,816,973,558
root_title"Making Market-Viewr More Robust with Caching and Retries"
beneficiaries
0.
accountthecrazygm.bank
weight1,500
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id143,367,282
net_rshares33,371,999,857,256
author_curate_reward""
vote details (183)
@ecoinstant ·
$0.02
Little by little we shall take over the ~~world~~ blockexplorer!  

!PAKX
!PIMP
!PIZZA
👍  , ,
properties (23)
authorecoinstant
permlinkre-thecrazygm-sxwqb5
categoryhive-186392
json_metadata{"tags":["hive-186392"],"app":"peakd/2025.6.1","image":[],"users":[]}
created2025-06-15 17:09:06
last_update2025-06-15 17:09:06
depth1
children1
last_payout2025-06-22 17:09:06
cashout_time1969-12-31 23:59:59
total_payout_value0.012 HBD
curator_payout_value0.012 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length86
author_reputation850,031,356,003,154
root_title"Making Market-Viewr More Robust with Caching and Retries"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id143,373,188
net_rshares91,802,710,938
author_curate_reward""
vote details (3)
@pakx ·
<center><table><tr></tr><tr><td><center><img src='https://files.peakd.com/file/peakd-hive/pakx/PakX-logo-transparent.png'><p><sup><a href='https://hive-engine.com/?p=market&t=PAKX'>View or trade </a> <code>PAKX</code> tokens.</sup></p></center></td><td><center>@ecoinstant, PAKX has voted the post by @thecrazygm. (2/2 calls)</p><br><br><p>Use !PAKX command if you hold enough balance to call for a @pakx vote on worthy posts! More details available on <a href='/@pakx'>PAKX Blog</a>.</p></center></td></tr></table></center>
properties (22)
authorpakx
permlinkre-ecoinstant-1750007443
categoryhive-186392
json_metadata"{"tags": ["pakx", "hivepakistan"], "app": "HiveDiscoMod"}"
created2025-06-15 17:10:42
last_update2025-06-15 17:10:42
depth2
children0
last_payout2025-06-22 17:10:42
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length524
author_reputation59,753,308,115
root_title"Making Market-Viewr More Robust with Caching and Retries"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id143,373,212
net_rshares0
@hivebuzz ·
Congratulations @thecrazygm! You have completed the following achievement on the Hive blockchain And have been rewarded with New badge(s)

<table><tr><td><img src="https://images.hive.blog/60x60/https://hivebuzz.me/badges/postallweek.png"></td><td>You have been a buzzy bee and published a post every day of the week.</td></tr>
</table>

<sub>_You can view your badges on [your board](https://hivebuzz.me/@thecrazygm) and compare yourself to others in the [Ranking](https://hivebuzz.me/ranking)_</sub>
<sub>_If you no longer want to receive notifications, reply to this comment with the word_ `STOP`</sub>

properties (22)
authorhivebuzz
permlinknotify-1750049176
categoryhive-186392
json_metadata{"image":["https://hivebuzz.me/notify.t6.png"]}
created2025-06-16 04:46:15
last_update2025-06-16 04:46:15
depth1
children0
last_payout2025-06-23 04:46:15
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length607
author_reputation369,449,178,429,507
root_title"Making Market-Viewr More Robust with Caching and Retries"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id143,381,062
net_rshares0
@mytechtrail ·
$0.06
Is your site available to the public? Can I get the URL?
👍  , , , ,
properties (23)
authormytechtrail
permlinkre-thecrazygm-sxwrxh
categoryhive-186392
json_metadata{"tags":["hive-186392"],"app":"peakd/2025.6.1","image":[],"users":[]}
created2025-06-15 17:44:06
last_update2025-06-15 17:44:06
depth1
children2
last_payout2025-06-22 17:44:06
cashout_time1969-12-31 23:59:59
total_payout_value0.028 HBD
curator_payout_value0.027 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length56
author_reputation19,007,675,085,002
root_title"Making Market-Viewr More Robust with Caching and Retries"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id143,373,708
net_rshares198,884,494,226
author_curate_reward""
vote details (5)
@thecrazygm ·
$0.11
Absolutely, the main site is https://TheCrazyGM.com

The one I'm talking about today is: https://market.thecrazygm.com 
👍  , , , ,
properties (23)
authorthecrazygm
permlinkre-mytechtrail-sxwsqt
categoryhive-186392
json_metadata{"tags":["hive-186392"],"app":"peakd/2025.6.1","image":[],"users":[]}
created2025-06-15 18:01:42
last_update2025-06-15 18:01:42
depth2
children1
last_payout2025-06-22 18:01:42
cashout_time1969-12-31 23:59:59
total_payout_value0.056 HBD
curator_payout_value0.055 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length119
author_reputation92,994,816,973,558
root_title"Making Market-Viewr More Robust with Caching and Retries"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id143,373,976
net_rshares399,840,950,839
author_curate_reward""
vote details (5)
@mytechtrail ·
Thanks.
👍  
properties (23)
authormytechtrail
permlinkre-thecrazygm-sxxasb
categoryhive-186392
json_metadata{"tags":["hive-186392"],"app":"peakd/2025.6.1","image":[],"users":[]}
created2025-06-16 00:31:24
last_update2025-06-16 00:31:24
depth3
children0
last_payout2025-06-23 00:31:24
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length7
author_reputation19,007,675,085,002
root_title"Making Market-Viewr More Robust with Caching and Retries"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id143,378,547
net_rshares66,663,352,609
author_curate_reward""
vote details (1)
@pizzabot ·
<center>PIZZA!


$PIZZA slices delivered:
@ecoinstant<sub>(2/20)</sub> tipped @thecrazygm 


<sub>Come get [MOON](https://moon.hive.pizza)ed!</sub></center>
properties (22)
authorpizzabot
permlinkre-making-market-viewr-more-robust-with-caching-and-retries-20250615t170928z
categoryhive-186392
json_metadata"{"app": "pizzabot"}"
created2025-06-15 17:09:27
last_update2025-06-15 17:09:27
depth1
children0
last_payout2025-06-22 17:09:27
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length156
author_reputation7,566,038,174,751
root_title"Making Market-Viewr More Robust with Caching and Retries"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id143,373,194
net_rshares0
@stekene ·
$0.20
I really like Market-Viewr!
I utilize it mainly to view token peice history over a longer peiod in time which isn't available on hive-engine / tribaldex.

Great app!
👍  , , , , , , , , , ,
properties (23)
authorstekene
permlinkre-thecrazygm-sxxpno
categoryhive-186392
json_metadata{"tags":["hive-186392"],"app":"peakd/2025.6.1","image":[],"users":[]}
created2025-06-16 05:52:42
last_update2025-06-16 05:52:42
depth1
children0
last_payout2025-06-23 05:52:42
cashout_time1969-12-31 23:59:59
total_payout_value0.100 HBD
curator_payout_value0.098 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length165
author_reputation152,648,132,489,429
root_title"Making Market-Viewr More Robust with Caching and Retries"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id143,381,758
net_rshares717,253,028,644
author_curate_reward""
vote details (11)
@thecrazygm ·
$0.18
> While I was in there, I also made a small quality-of-life tweak to sort the richlist by the total holdings (staked + balance) by default, which gives a more accurate picture of a user's position.


@herman-german Thanks for pointing this out, should have fixed your issue as well.
👍  , , , , , ,
properties (23)
authorthecrazygm
permlinkre-thecrazygm-sxw5o1
categoryhive-186392
json_metadata{"tags":["hive-186392"],"app":"peakd/2025.6.1","image":[],"users":["herman-german"]}
created2025-06-15 09:43:15
last_update2025-06-15 09:43:15
depth1
children1
last_payout2025-06-22 09:43:15
cashout_time1969-12-31 23:59:59
total_payout_value0.092 HBD
curator_payout_value0.092 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length282
author_reputation92,994,816,973,558
root_title"Making Market-Viewr More Robust with Caching and Retries"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id143,367,324
net_rshares650,419,819,375
author_curate_reward""
vote details (7)
@herman-german ·
$0.20
Looking great now. Thanks for adding these fix 💪
👍  , , , , , , ,
properties (23)
authorherman-german
permlinkre-thecrazygm-sxw6oj
categoryhive-186392
json_metadata{"tags":["hive-186392"],"app":"peakd/2025.6.1","image":[],"users":[]}
created2025-06-15 10:05:09
last_update2025-06-15 10:05:09
depth2
children0
last_payout2025-06-22 10:05:09
cashout_time1969-12-31 23:59:59
total_payout_value0.102 HBD
curator_payout_value0.101 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length48
author_reputation17,413,738,323,227
root_title"Making Market-Viewr More Robust with Caching and Retries"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id143,367,579
net_rshares718,414,117,783
author_curate_reward""
vote details (8)
@tydynrain ·
$0.15
I just tried Market-Viewr out again, and it's looking and working great! Congratulations on the fixes and refinements! 😁 🙏 💚 ✨ 🤙 
👍  , , , , , , , ,
properties (23)
authortydynrain
permlinkre-thecrazygm-2025615t183952395z
categoryhive-186392
json_metadata{"tags":["dev","tribes","archon","proofofbrain","pimp","oneup"],"app":"ecency/3.2.0-vision","format":"markdown+html"}
created2025-06-16 04:39:54
last_update2025-06-16 04:39:54
depth1
children0
last_payout2025-06-23 04:39:54
cashout_time1969-12-31 23:59:59
total_payout_value0.073 HBD
curator_payout_value0.072 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length129
author_reputation202,742,791,604,578
root_title"Making Market-Viewr More Robust with Caching and Retries"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id143,380,991
net_rshares515,919,685,826
author_curate_reward""
vote details (9)