The Fail Fast Principle
Key Takeaways
The Fail Fast Principle in software design is demonstrated using a Fast API backend example, showcasing early error detection and specific error messages to prevent localized errors from spreading and breaking other things in the application.
Full Transcript
have you ever been in a situation where there's some error that occurs at your customer's site and then as a result of that some other system fails and then total chaos ensues everything crashes including the main service that the customer is using and then you're scrambling and trying to figure out what happens trying to reproduce the issue basically losing the whole day trying to solve the problem the reason that happened is because you probably didn't apply the fail fast principle today I'm going to talk about what the fail fast principle is and how you can apply it to your code Now fail fast is just one principle of software design if you want to learn more about software design principles check out my software design course at iron. codesm mindset I want to start by showing the setup of the code example so we have a main file here which is a backend using fast API that's what we see right here so there's a couple of functions to help us set up things like a SQL light connector and the payment processor doesn't really matter what these are exactly for this example but they're there and then we have some crud operations in particular on employees so we have create employee we have read all the employees we have read a single employee given an ID we have updates an employee and we have delet an employee and then we have a couple of extra methods that are useful for employees like paying the salary that's kind of useful if you're an employee right and that's basically it so it's a very simple HR System and then each endpoint in the API has controll functions that are defined in another file employees. pii so for example this has a function that creates an employee and if something goes wrong this will also raise an exception now currently not all endpoints in this backend follow the fail fast principle in particular create employee update employee and pay salary so what is the fail fast principle well there's two aspects to it the first is that you should do early error detection so catch problems issues as soon as possible and then stop immediately the second part of the principle is that the error messages that you should return should not be cryptic and generic they should be specific so that you know what is actually wrong with what you did the reason you want to feel fast that you don't want failures to be hidden so that later on they're going to break other things you can apply this principle to many different architecture not just client server architectures though it is quite common there because of its nature of returning data and handling requests that may return data that can harm the application so what's failing fast is going to do is that it will ensure that the system is going to prevent localized errors from spreading and breaking other things in your application which is exactly the example that I gave at the start of the video early problem detection is an example of what goes wrong in create employee in particular related to this salary field here so as you can see we simply create the thing there is no validation whatsoever and that means you can input basically any type of value including negative value so this system allows employees to have a negative salaries basically means that you have to pay to work here and nobody wants that right and having Nega negative salaries in the system can have lots of annoying complications like the payment process are crashing because it can handle negative numbers for example so instead of storing a negative salary and potentially crashing other systems later on you can add validation here to avoid the problem altogether fail fast and the same goes for updating an employee so also there you set a salary that you need to check that it's positive here you see an updated version of the code that actually has a validation check so if it's less than zero then we're going to raise an HTTP exception what you see is that the exception is precise so there is detail we explain what the problem is we say well this value is incorrect you need to provide a positive decimal value and we also Supply a suitable status code not something random like 418 or something so how do you resolve this in common well an important thing you can do is automated testing when you build software you have to ensure that these types of issues are called early on so you need to test your functions your classes with the appropriate fixtures test values to make sure that they don't break under a certain conditions and you can write those unit tests yourself you can also use Library like hypothesis to generate that kind of testing data for you there's a lot of possibilities there but testing is really important there's several other Pros to the fail fast principle the first is that it can actually save you some money in that if you catch errors early in the execution it means that any expensive operations that you need to perform you don't need to do so that's going to save on compute cost a second Pro is that failing fast means that are more transparent to the users and not only the users but also the developers so if you see something is wrong you have a specific error message then it's also easier to find the problem when you want to fix it and finally it's more secure it minimizes the risk of different DS attacks and by avoiding cascading failures in your system you also avoid issues with data Integrity just to name a few things now there's also a con to using the fa fast principle which is that it may appear to your customer that your system is more fragile more Fini key that it returns an error more quickly than other systems might do though this is actually a good thing because the system is more rigid and explicit it also means that you reduce the chance of having issues with data integrity and in the long term that is going to provide a better experience for your customers so A system that fails fast should catch problems detect errors as fast as possible and return a sensible message to the user and this going to help you build software that's more robust and easier to debug in particular example I used Fast API to build my backend if you want to learn more about how to build a back end with fast API check out this video next thanks for watching and see you next time
Original Description
👷 Review code better and faster with my 3-Factor Framework: https://arjan.codes/diagnosis.
'Fail Fast' is an important principle of software design. In this video, I’ll show you why a fast-failing program isn't necessarily a drawback and how you can effectively apply the 'Fail Fast' principle to your advantage.
🔥 GitHub repository: https://git.arjan.codes/2024/tuesday_tips/fail_fast
🎓 ArjanCodes Courses: https://www.arjancodes.com/courses/
🔖 Chapters:
0:00 Intro
0:42 Setup
1:43 Explanation
2:35 Early detection - Find the error as fast as possible
3:08 Quick response on failure
3:45 How to resolve - Automated testing
4:12 Pros
4:52 Cons
5:17 Final thoughts
#arjancodes #softwaredesign #python
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from ArjanCodes · ArjanCodes · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Full stack WEB DEVELOPMENT in 2021 - the ULTIMATE tech stack for FAST web app development
ArjanCodes
FROM PRODUCT IDEA TO SOFTWARE - turn your idea into reality in a few steps
ArjanCodes
Cohesion and Coupling: Write BETTER PYTHON CODE Part 1
ArjanCodes
Build a GLASSMORPHISM React Component - Typescript & Material-UI
ArjanCodes
Observer Pattern Tutorial: I NEVER Knew Events Were THIS Powerful 🚀
ArjanCodes
100% CODE COVERAGE - Think You're Done? Think AGAIN.☝
ArjanCodes
Two UNDERRATED Design Patterns 💡 Write BETTER PYTHON CODE Part 6
ArjanCodes
1000 Subscribers! 🚀 WHY I Started this Channel and WHAT'S NEXT
ArjanCodes
Channel Trailer ArjanCodes - March 2021
ArjanCodes
Exception Handling Tips in Python ⚠ Write Better Python Code Part 7
ArjanCodes
Monadic Error Handling in Python ⚠ Write Better Python Code Part 7B
ArjanCodes
GW BASIC Games I Wrote When I Was a Kid 🎮 Running 30 Year Old Code
ArjanCodes
Why You Should Think About SOFTWARE ARCHITECTURE in Python 💡
ArjanCodes
Uncle Bob’s SOLID Principles Made Easy 🍀 - In Python!
ArjanCodes
QUESTIONABLE Object Creation Patterns in Python 🤔
ArjanCodes
If You’re Not Using Python DATA CLASSES Yet, You Should 🚀
ArjanCodes
CODE ROAST: Yahtzee - New Python Code Refactoring Series!
ArjanCodes
7 UX Design Tips for Developers
ArjanCodes
Going All-in on Software Design in Python + an ANNOUNCEMENT 🎙
ArjanCodes
🎙 Interview with Sybren Stüvel, Developer @ Blender 3D
ArjanCodes
Do We Still Need Dataclasses? // PYDANTIC Tutorial
ArjanCodes
7 Python Mistakes That Instantly Expose Junior Developers
ArjanCodes
Answering Your Most Frequently Asked Python Questions // Q&A 07-2021
ArjanCodes
GitHub Copilot 🤖 The Future of Software Development?
ArjanCodes
More Python Code Smells: Avoid These 7 Smelly Snags
ArjanCodes
Test-Driven Development In Python // The Power of Red-Green-Refactor
ArjanCodes
5 Tips To Keep Technical Debt Under Control
ArjanCodes
Refactoring A Tower Defense Game In Python // CODE ROAST
ArjanCodes
The Factory Design Pattern is Obsolete in Python
ArjanCodes
Why the Plugin Architecture Gives You CRAZY Flexibility
ArjanCodes
Refactoring A Data Science Project Part 1 - Abstraction and Composition
ArjanCodes
Refactoring A Data Science Project Part 2 - The Information Expert
ArjanCodes
Refactoring A Data Science Project Part 3 - Configuration Cleanup
ArjanCodes
Purge These 7 Code Smells From Your Python Code
ArjanCodes
Running A Software Development YouTube Channel
ArjanCodes
Refactoring A PDF And Web Scraper Part 1 // CODE ROAST
ArjanCodes
Refactoring A PDF And Web Scraper Part 2 // CODE ROAST
ArjanCodes
How To Easily Do Asynchronous Programming With Asyncio In Python
ArjanCodes
The Software Designer Mindset
ArjanCodes
NEVER Worry About Data Science Projects Configs Again
ArjanCodes
Powerful VSCode Tips And Tricks For Python Development And Design
ArjanCodes
8 Python Coding Tips - From The Google Python Style Guide
ArjanCodes
What Is Encapsulation And Information Hiding?
ArjanCodes
8 Tips For Becoming A Senior Developer
ArjanCodes
Building A Custom Context Manager In Python: A Closer Look
ArjanCodes
GraphQL vs REST: What's The Difference And When To Use Which?
ArjanCodes
You Can Do Really Cool Things With Functions In Python
ArjanCodes
Announcing The Black VS Code Theme (Launching April 1st)
ArjanCodes
7 DevOps Best Practices For Launching A SaaS Platform
ArjanCodes
Refactoring a Rock Paper Scissors Lizard Spock Game // Code Roast Part 1
ArjanCodes
Refactoring a Rock Paper Scissors Lizard Spock Game // Part 2
ArjanCodes
Things Are Going To Change Around Here
ArjanCodes
Dependency Injection Explained In One Minute // Python Tips
ArjanCodes
How To Setup A MacBook Pro M1 For Software Development
ArjanCodes
A Simple & Effective Way To Improve Python Class Performance
ArjanCodes
How To Write Unit Tests For Existing Python Code // Part 1 of 2
ArjanCodes
How To Write Unit Tests For Existing Python Code // Part 2 of 2
ArjanCodes
Make Sure You Choose The Right Data Structure // Python Tips
ArjanCodes
5 Tips For Object-Oriented Programming Done Well - In Python
ArjanCodes
Next-Level Concurrent Programming In Python With Asyncio
ArjanCodes
More on: API Design
View skill →Related Reads
📰
📰
📰
📰
AI Tools that Actually Pay You Back: A Developer's Guide to Monetizing AI
Dev.to AI
How to Summarize PDFs Programmatically in 2026 (+ a Free No-Code Option)
Dev.to · Ai tools overview
The new ChatGPT macOS app redesign has made basic navigation so much worse
Reddit r/artificial
Three Token-2022 Mints in One Week: Fees, Yield, and Soulbound
Dev.to · atharv shukla
Chapters (9)
Intro
0:42
Setup
1:43
Explanation
2:35
Early detection - Find the error as fast as possible
3:08
Quick response on failure
3:45
How to resolve - Automated testing
4:12
Pros
4:52
Cons
5:17
Final thoughts
🎓
Tutor Explanation
DeepCamp AI