6 ms·
High-performance .NET by example: Filtering bot traffic
- throwasehasdwi 9y agoBlocking access based on arbitrary user agent strings is a really bad idea. Every single bad bot will avoid known user agent strings or pretend to be Google, so you're only blocking well behaved ones. Plus there's thousands of browser versions out there, so there's a very good chance you're blocking some users for no reason. The proper way to do this is to block by IP, based on behavior. Block IPs slowing down the site or throw up a captcha like cloudflare does. Blocking bots sounds great but it just brings Google one step closer to a monopoly. Even good bots just pretend to be people nowadays because lots of people are implementing naive site protection strategies. Edited: to be less mean
- John23832 9y ago> single dumbest idea I agree with your sentiment, but you should try to be a little more constructive.
- alexandrnikitin 9y agoYes, you're right. There are many ways to block robots: IP, UA, behaviour analysis. An advertising company has to have UA based filtering to be compliant with standards. However, the focus of the blog post is on performance rather on how to block bots.
- deleted 9y ago[deleted]
- cm2187 9y agoPlus it's baking planned obsolescence into your code. As soon as you stop updating it, it will start blocking newer browser editions and versions.
- krzrak 9y ago> Every single bad bot will avoid known user agent strings If I was writing a bot, I would set user agent to some well known and very popular value, i.e. newest Chrome on Windows, or something like that.
- jlg23 9y agoIn theory you are right, but in reality 99% of the rogue bots are actually just some scraper tools were ignorant users changed sane defaults to "make it go faster". They usually don't have enough knowledge to even understand that they are routed into a black hole - not to speak of being able to do something about it. Disclaimer: Getting rid of those idiots^wmisguides poor souls is part of my job description.
- throwasehasdwi 9y agoThen why not block based on bots that ignore robots text or make requests too quickly like I suggested? Looking for sub-strings in the UA header is a hack at best.
- jlg23 9y agoThey all require one to keep state information on the server side and with overall little benefit. Yes, I do what you proposed, but in hindsight it was a waste of time to set that up: 99% simply don't know what they are doing and therefore easy to catch. I don't care about this one person who knows to change the user agent sent, those are the ones you can usually talk to and they'll happily throttle their crawlers. The other 99 are the problem - and they are a problem that can be solved pretty well by simple string matching.
- Twirrim 9y agoThe article is not about filtering bad bots. It even says so right near the top: >We won’t cover black bots because it is a huge topic with sophisticated analysis and Machine learning algorithms. We will focus on the white and grey bots that identify themselves as such. This is about not wasting time & effort showing advertising banners to good bots.
- NKCSS 9y agoI'd probably store cached results for Dictinary<int, HashSet<string>> allowed, notAllowed; where int == length of the user agent. This should probably be blazing fast as well instead of keep doing those lookups.
- alexandrnikitin 9y agoI doubt that exactly that will work. There are tens of thousands of different UAs (maybe 100K). Perhaps some kind of tiny (few CPU cache lines) cache for most popular UAs could help. But again: measure, measure, measure :)
- NKCSS 9y agoPublish your test set and I can look at it :)
- alexandrnikitin 9y agoI'm afraid I can't do that because of proprietary data. I think I can come up with analogous tests using open data. I'll let you know ;)
- andrewgrowles 9y agoA FST seems like a good fit for this problem. I believe it will be much more compact than the Aho-Corasick algorithm trie structure. Depends on the size of the dictionary.
- alexandrnikitin 9y agoI have that feeling too. But I found it harder to implement, especially with fallback failure references.
- andrewgrowles 9y agoI'd look for a library and not write one from scratch. Lucene [1] and OpenFST [2] are great implementations. I haven't used C#, so I don't know if bindings exist or not. Also you may find this talk useful [3] (Particularly slide 11). Great write up by the way. Really thorough on the benchmarking! [1] https://lucene.apache.org/core/4_1_0/core/org/apache/lucene/util/fst/package-summary.html https://lucene.apache.org/core/4_1_0/core/org/apache/lucene/... [2] http://www.openfst.org/twiki/bin/view/FST/WebHome http://www.openfst.org/twiki/bin/view/FST/WebHome [3] https://www.slideshare.net/lucenerevolution/text-tagging-with-finite-state-transducers https://www.slideshare.net/lucenerevolution/text-tagging-wit...
- senorjazz 9y agoRather than block on UA, just add some honeypots. An invisible link. Any bot that pulls that page gets blocked as scrapers tend to pull all links from the page and follow. Use the robots.txt to ban the pulling of specific pages. Bots 99% of the time ignore robots, so if they pull it: block Check how quickly pages are pulled. If passes a threshold: block
- alexandrnikitin 9y agoYes, using honeypots is one of the ways to identify bots. But that wasn't the focus of the post. I'll add some clarification.
- marklit 9y agoI've seen bot traffic claiming to be recent versions of Firefox from residential IPs in the Ukraine pulling robots.txt. Sometimes this is one of the few clues to go on.
- pc86 9y agoI'm pretty sure the point of the article was performance testing of C#, not best practices for banning bots...
- hdhzy 9y agoExcellent post showing how to correctly improve code w.r.t. performance using the scientific method: hypothesis, measuring the baseline, change, measuring effect with real tools, real code. Thanks for sharing!
- Someone1234 9y agoI also found it an interesting post in that it kind of inadvertently proves that for most situations you shouldn't optimise to this extent. Meaning, yes, the OP got impressive performance improvements but the code is also completely unreadable and utilises unsafe code sections which could expose you to security problems/memory leaks/memory corruption. Not to mention they've recreated and will need to maintain an in-house version of the Dictionary class. Their first optimisation (from Enumerator to List and Any() to Count()) are something every codebase could use. Most of their other optimisations make the code a maintenance minefield. Plus programmers are expensive. Hardware is cheap. Why spent time on harder code to write that's also harder to maintain in the medium to long term when instead you could just throw money at hardware and call it a day? Just food for thought, not really a criticism in and of itself. PS - Please don't take this post too seriously. I am not really being critical, just playing devil's advocate. I actually enjoyed the linked article a lot.
- drewmate 9y agoI wonder if good VCS could solve this problem. For instance, if he documented each step with comments/commits in the code as well as he did for this blog post, it would be easy to go back and see not only why he did what he did, but the much more readable (albeit less performant) original code. It seems like most git guis are pretty commit-focused. I'm not sure of any way to do it in the command line (though it must be possible) but it would be nice to highlight a section of code in your IDE and have git give you a history of just those lines (or that function) as far back as you want to go.
- osd 9y agoNo, no no no. I cannot believe that you are honestly saying a 2x increase to throughput in production is something you "shouldn't take seriously" because the code isn't as readable as it was before. Programmers are expensive. Hardware is cheap. That doesn't justify completely throwing out the window any performance increasing changes just because a fresh college grad won't be able to understand what's going on within 10 minutes.
- doubleplusgood 9y agoI did something similar with nginx, the data file from 51degrees and some lua code; each instance only handles 10-20k requests/sec so no clever optimization was needed.
- oblio 9y agoWould you mind posting the Lua code?
- doubleplusgood 9y agoSure, but there's nothing really special, just some json parsing and some variable assignments back into an internal nginx request. I'm on mobile at the moment but I'll follow up on this in the next 24 hours.
- doubleplusgood 9y agoHi, I've made a gist[0]; feel free to get in touch via GH if you'd like to discuss it further. [0] - https://gist.github.com/marklr/ae0c2f1eb61855d13cde6cef6bf63541 https://gist.github.com/marklr/ae0c2f1eb61855d13cde6cef6bf63...
- tener 9y agoSo, the industry standard requires them not to serve ads to the bots... which means they have implemented the ad blocking themselves?
- frik 9y agoGood post! A lot of manual work with various perf tools. What's a bit missing is some production performance monitoring (APM) that gives you such data, with no manual interaction.
- alexandrnikitin 9y agoI intend to write a separate blog post about low-overhead production monitoring (not sure when it happen though)
- brilliantcode 9y agowhat if the "grey" traffic came from residential IP addresses using a normally distributed range of user agents? How would you reliable distinguish them from regular traffic?
- Benfromparis 9y agoBasically, we are using two sort of technics : technical and behavior. Technical : if the UserAgent claim to be a regular browser (let say Chrome 43) we will check on network level if the client implement http protocol like Chrome 43 usually do and on the JS side if the Javascript render is correct for Chrome. In case it's a real Chrome, we will check if the Browser is controlled by automation Tool. Behavior : we will check if the path of requests is regular according to the website usage. Disclaimer: I'm working at https://datadome.co https://datadome.co, a bot protection tool.
- alexandrnikitin 9y agoIt depends. Usually do nothing if that traffic is very low. There's no reliable way to do that. Honeypots and behavior analysis are very useful here.