[{"data":1,"prerenderedAt":3443},["ShallowReactive",2],{"post-microservices-mistakes":3,"rel-microservices-mistakes":206,"sib-microservices-mistakes":3371},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"layout":11,"subtitle":12,"image":13,"optimized_image":13,"category":14,"tags":15,"author":19,"paginate":6,"body":20,"_type":200,"_id":201,"_source":202,"_file":203,"_stem":204,"_extension":205},"/posts/microservices-mistakes","posts",false,"","Common Mistakes When Developing Microservices","A guide on the most frequent mistakes made when developing microservices, with practical examples.","2024-02-15T11:00:00.000Z","post","Avoid pitfalls and build robust microservices","/assets/img/uploads/microservices.jpg","code",[14,16,17,18],"microservices","architecture","best practices","jaimedearcos",{"type":21,"children":22,"toc":191},"root",[23,35,42,47,58,64,69,78,84,89,98,104,109,118,124,129,138,144,149,186],{"type":24,"tag":25,"props":26,"children":27},"element","blockquote",{},[28],{"type":24,"tag":29,"props":30,"children":31},"p",{},[32],{"type":33,"value":34},"text","Developing microservices can provide significant advantages such as scalability, flexibility, and rapid deployment. However, the journey to a microservice architecture is filled with potential pitfalls. In this post, we'll explore 5 common mistakes to avoid when developing microservices.",{"type":24,"tag":36,"props":37,"children":39},"h2",{"id":38},"_1-highly-coupled-services",[40],{"type":33,"value":41},"1. Highly Coupled Services",{"type":24,"tag":29,"props":43,"children":44},{},[45],{"type":33,"value":46},"One common mistake is creating tightly coupled microservices, which undermines independence and complicates deployment and updates.",{"type":24,"tag":29,"props":48,"children":49},{},[50,56],{"type":24,"tag":51,"props":52,"children":53},"strong",{},[54],{"type":33,"value":55},"Example",{"type":33,"value":57},": Using a shared library across multiple microservices might initially seem efficient. However, when the library evolves, every microservice depending on it must be updated and redeployed simultaneously, resulting in complex coordination and loss of service independence.",{"type":24,"tag":36,"props":59,"children":61},{"id":60},"_2-ignoring-error-handling-and-resilience",[62],{"type":33,"value":63},"2. Ignoring Error Handling and Resilience",{"type":24,"tag":29,"props":65,"children":66},{},[67],{"type":33,"value":68},"Failures are inevitable in distributed architectures. A frequent error is neglecting robust error handling, automatic retries, circuit breakers, or fallback patterns. Resilience is crucial to preventing cascading failures.",{"type":24,"tag":29,"props":70,"children":71},{},[72,76],{"type":24,"tag":51,"props":73,"children":74},{},[75],{"type":33,"value":55},{"type":33,"value":77},": If a critical payment microservice fails without proper resilience measures, it could halt the entire purchasing flow, negatively impacting user experience and causing economic losses.",{"type":24,"tag":36,"props":79,"children":81},{"id":80},"_3-inadequate-monitoring",[82],{"type":33,"value":83},"3. Inadequate Monitoring",{"type":24,"tag":29,"props":85,"children":86},{},[87],{"type":33,"value":88},"Visibility is crucial in distributed systems. Without a clear monitoring and observability strategy (centralized logs, metrics, and distributed tracing), it's difficult to detect problems early or perform effective diagnostics.",{"type":24,"tag":29,"props":90,"children":91},{},[92,96],{"type":24,"tag":51,"props":93,"children":94},{},[95],{"type":33,"value":55},{"type":33,"value":97},": A performance degradation in one microservice without centralized metrics and tracing could lead to hours wasted trying to pinpoint the specific service causing the bottleneck.",{"type":24,"tag":36,"props":99,"children":101},{"id":100},"_4-deploying-microservices-without-automation",[102],{"type":33,"value":103},"4. Deploying Microservices Without Automation",{"type":24,"tag":29,"props":105,"children":106},{},[107],{"type":33,"value":108},"Rapid and flexible deployment is a significant advantage of microservices, but many teams mistakenly fail to automate these processes early, leading to human errors, inconsistent configurations, and scalability issues. Prioritizing automation with CI/CD is essential.",{"type":24,"tag":29,"props":110,"children":111},{},[112,116],{"type":24,"tag":51,"props":113,"children":114},{},[115],{"type":33,"value":55},{"type":33,"value":117},": Repeated manual deployments can lead to frequent human errors, such as incorrect configurations that cause hard-to-detect production issues.",{"type":24,"tag":36,"props":119,"children":121},{"id":120},"_5-overly-fragmented-microservices",[122],{"type":33,"value":123},"5. Overly Fragmented Microservices",{"type":24,"tag":29,"props":125,"children":126},{},[127],{"type":33,"value":128},"Excessively splitting an application into very small microservices can create more problems than it solves. Microservices should align clearly with business domains and values, avoiding excessive fragmentation that complicates management and operation.",{"type":24,"tag":29,"props":130,"children":131},{},[132,136],{"type":24,"tag":51,"props":133,"children":134},{},[135],{"type":33,"value":55},{"type":33,"value":137},": Creating separate microservices for every minor function, such as managing a simple form or validating an input, can result in significant operational overhead with minimal scalability or performance benefits.",{"type":24,"tag":36,"props":139,"children":141},{"id":140},"further-reading",[142],{"type":33,"value":143},"Further Reading",{"type":24,"tag":29,"props":145,"children":146},{},[147],{"type":33,"value":148},"To deepen your understanding of microservices, consider exploring the following resources:",{"type":24,"tag":150,"props":151,"children":152},"ul",{},[153,166,176],{"type":24,"tag":154,"props":155,"children":156},"li",{},[157],{"type":24,"tag":158,"props":159,"children":163},"a",{"href":160,"rel":161},"https://microservices.io/book",[162],"nofollow",[164],{"type":33,"value":165},"Building Microservices - Sam Newman",{"type":24,"tag":154,"props":167,"children":168},{},[169],{"type":24,"tag":158,"props":170,"children":173},{"href":171,"rel":172},"https://martinfowler.com/microservices/",[162],[174],{"type":33,"value":175},"Microservices Patterns - Chris Richardson",{"type":24,"tag":154,"props":177,"children":178},{},[179],{"type":24,"tag":158,"props":180,"children":183},{"href":181,"rel":182},"https://samnewman.io/books/building_microservices/",[162],[184],{"type":33,"value":185},"Martin Fowler - Microservices Resource Guide",{"type":24,"tag":29,"props":187,"children":188},{},[189],{"type":33,"value":190},"Mastering these concepts will help you design and maintain robust, scalable, and effective microservice architectures.",{"title":7,"searchDepth":192,"depth":192,"links":193},2,[194,195,196,197,198,199],{"id":38,"depth":192,"text":41},{"id":60,"depth":192,"text":63},{"id":80,"depth":192,"text":83},{"id":100,"depth":192,"text":103},{"id":120,"depth":192,"text":123},{"id":140,"depth":192,"text":143},"markdown","content:posts:microservices-mistakes.md","content","posts/microservices-mistakes.md","posts/microservices-mistakes","md",[207,1644,2552],{"_path":208,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":209,"description":210,"date":211,"layout":11,"subtitle":212,"image":213,"optimized_image":213,"category":14,"tags":214,"author":19,"paginate":6,"body":220,"_type":200,"_id":1641,"_source":202,"_file":1642,"_stem":1643,"_extension":205},"/posts/gpt-sql-integration","Implementa un analizador de consultas en lenguaje natural para bases de datos SQL con GPT","Aprende a implementar un sistema que convierte consultas en lenguaje natural en sentencias SQL usando OpenAI GPT, con un tutorial paso a paso y ejemplos prácticos.","2025-01-15T11:00:00.000Z","Transforma preguntas naturales en consultas SQL automatizadas","/assets/img/uploads/gpt-sql-integration.png",[14,215,216,217,218,219],"gpt","openai","sql","ai","nlp",{"type":21,"children":221,"toc":1630},[222,230,236,254,260,265,313,326,342,348,353,578,584,589,925,931,936,1287,1293,1298,1357,1363,1368,1549,1555,1573,1579,1584,1591,1624],{"type":24,"tag":25,"props":223,"children":224},{},[225],{"type":24,"tag":29,"props":226,"children":227},{},[228],{"type":33,"value":229},"Convertir preguntas naturales en consultas SQL de forma automática simplifica la interacción con bases de datos, permitiendo que usuarios sin conocimientos técnicos extraigan datos relevantes. En este tutorial, aprenderás a implementar esta solución utilizando la API de OpenAI GPT.",{"type":24,"tag":36,"props":231,"children":233},{"id":232},"requisitos-previos",[234],{"type":33,"value":235},"Requisitos Previos",{"type":24,"tag":150,"props":237,"children":238},{},[239,244,249],{"type":24,"tag":154,"props":240,"children":241},{},[242],{"type":33,"value":243},"Cuenta y clave API de OpenAI",{"type":24,"tag":154,"props":245,"children":246},{},[247],{"type":33,"value":248},"Python instalado en tu entorno",{"type":24,"tag":154,"props":250,"children":251},{},[252],{"type":33,"value":253},"Base de datos SQL (ej.: PostgreSQL, MySQL)",{"type":24,"tag":36,"props":255,"children":257},{"id":256},"paso-1-configuración-del-entorno",[258],{"type":33,"value":259},"Paso 1: Configuración del entorno",{"type":24,"tag":29,"props":261,"children":262},{},[263],{"type":33,"value":264},"Primero, instala las dependencias necesarias:",{"type":24,"tag":266,"props":267,"children":271},"pre",{"className":268,"code":269,"language":270,"meta":7,"style":7},"language-bash shiki shiki-themes github-dark github-light","pip install openai python-dotenv sqlalchemy psycopg2-binary\n","bash",[272],{"type":24,"tag":14,"props":273,"children":274},{"__ignoreMap":7},[275],{"type":24,"tag":276,"props":277,"children":280},"span",{"class":278,"line":279},"line",1,[281,287,293,298,303,308],{"type":24,"tag":276,"props":282,"children":284},{"style":283},"--shiki-default:#B392F0;--shiki-light:#6F42C1",[285],{"type":33,"value":286},"pip",{"type":24,"tag":276,"props":288,"children":290},{"style":289},"--shiki-default:#9ECBFF;--shiki-light:#032F62",[291],{"type":33,"value":292}," install",{"type":24,"tag":276,"props":294,"children":295},{"style":289},[296],{"type":33,"value":297}," openai",{"type":24,"tag":276,"props":299,"children":300},{"style":289},[301],{"type":33,"value":302}," python-dotenv",{"type":24,"tag":276,"props":304,"children":305},{"style":289},[306],{"type":33,"value":307}," sqlalchemy",{"type":24,"tag":276,"props":309,"children":310},{"style":289},[311],{"type":33,"value":312}," psycopg2-binary\n",{"type":24,"tag":29,"props":314,"children":315},{},[316,318,324],{"type":33,"value":317},"Guarda tu clave API en un archivo ",{"type":24,"tag":14,"props":319,"children":321},{"className":320},[],[322],{"type":33,"value":323},".env",{"type":33,"value":325},":",{"type":24,"tag":266,"props":327,"children":331},{"className":328,"code":329,"language":330,"meta":7,"style":7},"language-env shiki shiki-themes github-dark github-light","OPENAI_API_KEY='tu-api-key'\n","env",[332],{"type":24,"tag":14,"props":333,"children":334},{"__ignoreMap":7},[335],{"type":24,"tag":276,"props":336,"children":337},{"class":278,"line":279},[338],{"type":24,"tag":276,"props":339,"children":340},{},[341],{"type":33,"value":329},{"type":24,"tag":36,"props":343,"children":345},{"id":344},"paso-2-conexión-a-la-base-de-datos",[346],{"type":33,"value":347},"Paso 2: Conexión a la base de datos",{"type":24,"tag":29,"props":349,"children":350},{},[351],{"type":33,"value":352},"Utiliza SQLAlchemy para conectarte a tu base de datos:",{"type":24,"tag":266,"props":354,"children":358},{"className":355,"code":356,"language":357,"meta":7,"style":7},"language-python shiki shiki-themes github-dark github-light","from sqlalchemy import create_engine\nfrom sqlalchemy.orm import sessionmaker\nimport os\nfrom dotenv import load_dotenv\n\nload_dotenv()\n\nDATABASE_URL = os.getenv(\"DATABASE_URL\")\nengine = create_engine(DATABASE_URL)\nSession = sessionmaker(bind=engine)\nsession = Session()\n","python",[359],{"type":24,"tag":14,"props":360,"children":361},{"__ignoreMap":7},[362,387,408,421,443,453,462,470,500,527,560],{"type":24,"tag":276,"props":363,"children":364},{"class":278,"line":279},[365,371,377,382],{"type":24,"tag":276,"props":366,"children":368},{"style":367},"--shiki-default:#F97583;--shiki-light:#D73A49",[369],{"type":33,"value":370},"from",{"type":24,"tag":276,"props":372,"children":374},{"style":373},"--shiki-default:#E1E4E8;--shiki-light:#24292E",[375],{"type":33,"value":376}," sqlalchemy ",{"type":24,"tag":276,"props":378,"children":379},{"style":367},[380],{"type":33,"value":381},"import",{"type":24,"tag":276,"props":383,"children":384},{"style":373},[385],{"type":33,"value":386}," create_engine\n",{"type":24,"tag":276,"props":388,"children":389},{"class":278,"line":192},[390,394,399,403],{"type":24,"tag":276,"props":391,"children":392},{"style":367},[393],{"type":33,"value":370},{"type":24,"tag":276,"props":395,"children":396},{"style":373},[397],{"type":33,"value":398}," sqlalchemy.orm ",{"type":24,"tag":276,"props":400,"children":401},{"style":367},[402],{"type":33,"value":381},{"type":24,"tag":276,"props":404,"children":405},{"style":373},[406],{"type":33,"value":407}," sessionmaker\n",{"type":24,"tag":276,"props":409,"children":411},{"class":278,"line":410},3,[412,416],{"type":24,"tag":276,"props":413,"children":414},{"style":367},[415],{"type":33,"value":381},{"type":24,"tag":276,"props":417,"children":418},{"style":373},[419],{"type":33,"value":420}," os\n",{"type":24,"tag":276,"props":422,"children":424},{"class":278,"line":423},4,[425,429,434,438],{"type":24,"tag":276,"props":426,"children":427},{"style":367},[428],{"type":33,"value":370},{"type":24,"tag":276,"props":430,"children":431},{"style":373},[432],{"type":33,"value":433}," dotenv ",{"type":24,"tag":276,"props":435,"children":436},{"style":367},[437],{"type":33,"value":381},{"type":24,"tag":276,"props":439,"children":440},{"style":373},[441],{"type":33,"value":442}," load_dotenv\n",{"type":24,"tag":276,"props":444,"children":446},{"class":278,"line":445},5,[447],{"type":24,"tag":276,"props":448,"children":450},{"emptyLinePlaceholder":449},true,[451],{"type":33,"value":452},"\n",{"type":24,"tag":276,"props":454,"children":456},{"class":278,"line":455},6,[457],{"type":24,"tag":276,"props":458,"children":459},{"style":373},[460],{"type":33,"value":461},"load_dotenv()\n",{"type":24,"tag":276,"props":463,"children":465},{"class":278,"line":464},7,[466],{"type":24,"tag":276,"props":467,"children":468},{"emptyLinePlaceholder":449},[469],{"type":33,"value":452},{"type":24,"tag":276,"props":471,"children":473},{"class":278,"line":472},8,[474,480,485,490,495],{"type":24,"tag":276,"props":475,"children":477},{"style":476},"--shiki-default:#79B8FF;--shiki-light:#005CC5",[478],{"type":33,"value":479},"DATABASE_URL",{"type":24,"tag":276,"props":481,"children":482},{"style":367},[483],{"type":33,"value":484}," =",{"type":24,"tag":276,"props":486,"children":487},{"style":373},[488],{"type":33,"value":489}," os.getenv(",{"type":24,"tag":276,"props":491,"children":492},{"style":289},[493],{"type":33,"value":494},"\"DATABASE_URL\"",{"type":24,"tag":276,"props":496,"children":497},{"style":373},[498],{"type":33,"value":499},")\n",{"type":24,"tag":276,"props":501,"children":503},{"class":278,"line":502},9,[504,509,514,519,523],{"type":24,"tag":276,"props":505,"children":506},{"style":373},[507],{"type":33,"value":508},"engine ",{"type":24,"tag":276,"props":510,"children":511},{"style":367},[512],{"type":33,"value":513},"=",{"type":24,"tag":276,"props":515,"children":516},{"style":373},[517],{"type":33,"value":518}," create_engine(",{"type":24,"tag":276,"props":520,"children":521},{"style":476},[522],{"type":33,"value":479},{"type":24,"tag":276,"props":524,"children":525},{"style":373},[526],{"type":33,"value":499},{"type":24,"tag":276,"props":528,"children":530},{"class":278,"line":529},10,[531,536,540,545,551,555],{"type":24,"tag":276,"props":532,"children":533},{"style":373},[534],{"type":33,"value":535},"Session ",{"type":24,"tag":276,"props":537,"children":538},{"style":367},[539],{"type":33,"value":513},{"type":24,"tag":276,"props":541,"children":542},{"style":373},[543],{"type":33,"value":544}," sessionmaker(",{"type":24,"tag":276,"props":546,"children":548},{"style":547},"--shiki-default:#FFAB70;--shiki-light:#E36209",[549],{"type":33,"value":550},"bind",{"type":24,"tag":276,"props":552,"children":553},{"style":367},[554],{"type":33,"value":513},{"type":24,"tag":276,"props":556,"children":557},{"style":373},[558],{"type":33,"value":559},"engine)\n",{"type":24,"tag":276,"props":561,"children":563},{"class":278,"line":562},11,[564,569,573],{"type":24,"tag":276,"props":565,"children":566},{"style":373},[567],{"type":33,"value":568},"session ",{"type":24,"tag":276,"props":570,"children":571},{"style":367},[572],{"type":33,"value":513},{"type":24,"tag":276,"props":574,"children":575},{"style":373},[576],{"type":33,"value":577}," Session()\n",{"type":24,"tag":36,"props":579,"children":581},{"id":580},"paso-3-obtener-esquema-de-la-base-de-datos",[582],{"type":33,"value":583},"Paso 3: Obtener esquema de la base de datos",{"type":24,"tag":29,"props":585,"children":586},{},[587],{"type":33,"value":588},"Genera una descripción del esquema que será utilizada por GPT:",{"type":24,"tag":266,"props":590,"children":592},{"className":355,"code":591,"language":357,"meta":7,"style":7},"from sqlalchemy import inspect\n\ndef get_database_schema(engine):\n    inspector = inspect(engine)\n    schema_description = \"\"\n    for table_name in inspector.get_table_names():\n        schema_description += f\"Table: {table_name}\\n\"\n        for column in inspector.get_columns(table_name):\n            schema_description += f\"- {column['name']} ({column['type']})\\n\"\n        schema_description += \"\\n\"\n    return schema_description\n\nschema_description = get_database_schema(engine)\nprint(schema_description)\n",[593],{"type":24,"tag":14,"props":594,"children":595},{"__ignoreMap":7},[596,616,623,641,658,675,698,741,763,848,872,885,893,911],{"type":24,"tag":276,"props":597,"children":598},{"class":278,"line":279},[599,603,607,611],{"type":24,"tag":276,"props":600,"children":601},{"style":367},[602],{"type":33,"value":370},{"type":24,"tag":276,"props":604,"children":605},{"style":373},[606],{"type":33,"value":376},{"type":24,"tag":276,"props":608,"children":609},{"style":367},[610],{"type":33,"value":381},{"type":24,"tag":276,"props":612,"children":613},{"style":373},[614],{"type":33,"value":615}," inspect\n",{"type":24,"tag":276,"props":617,"children":618},{"class":278,"line":192},[619],{"type":24,"tag":276,"props":620,"children":621},{"emptyLinePlaceholder":449},[622],{"type":33,"value":452},{"type":24,"tag":276,"props":624,"children":625},{"class":278,"line":410},[626,631,636],{"type":24,"tag":276,"props":627,"children":628},{"style":367},[629],{"type":33,"value":630},"def",{"type":24,"tag":276,"props":632,"children":633},{"style":283},[634],{"type":33,"value":635}," get_database_schema",{"type":24,"tag":276,"props":637,"children":638},{"style":373},[639],{"type":33,"value":640},"(engine):\n",{"type":24,"tag":276,"props":642,"children":643},{"class":278,"line":423},[644,649,653],{"type":24,"tag":276,"props":645,"children":646},{"style":373},[647],{"type":33,"value":648},"    inspector ",{"type":24,"tag":276,"props":650,"children":651},{"style":367},[652],{"type":33,"value":513},{"type":24,"tag":276,"props":654,"children":655},{"style":373},[656],{"type":33,"value":657}," inspect(engine)\n",{"type":24,"tag":276,"props":659,"children":660},{"class":278,"line":445},[661,666,670],{"type":24,"tag":276,"props":662,"children":663},{"style":373},[664],{"type":33,"value":665},"    schema_description ",{"type":24,"tag":276,"props":667,"children":668},{"style":367},[669],{"type":33,"value":513},{"type":24,"tag":276,"props":671,"children":672},{"style":289},[673],{"type":33,"value":674}," \"\"\n",{"type":24,"tag":276,"props":676,"children":677},{"class":278,"line":455},[678,683,688,693],{"type":24,"tag":276,"props":679,"children":680},{"style":367},[681],{"type":33,"value":682},"    for",{"type":24,"tag":276,"props":684,"children":685},{"style":373},[686],{"type":33,"value":687}," table_name ",{"type":24,"tag":276,"props":689,"children":690},{"style":367},[691],{"type":33,"value":692},"in",{"type":24,"tag":276,"props":694,"children":695},{"style":373},[696],{"type":33,"value":697}," inspector.get_table_names():\n",{"type":24,"tag":276,"props":699,"children":700},{"class":278,"line":464},[701,706,711,716,721,726,731,736],{"type":24,"tag":276,"props":702,"children":703},{"style":373},[704],{"type":33,"value":705},"        schema_description ",{"type":24,"tag":276,"props":707,"children":708},{"style":367},[709],{"type":33,"value":710},"+=",{"type":24,"tag":276,"props":712,"children":713},{"style":367},[714],{"type":33,"value":715}," f",{"type":24,"tag":276,"props":717,"children":718},{"style":289},[719],{"type":33,"value":720},"\"Table: ",{"type":24,"tag":276,"props":722,"children":723},{"style":476},[724],{"type":33,"value":725},"{",{"type":24,"tag":276,"props":727,"children":728},{"style":373},[729],{"type":33,"value":730},"table_name",{"type":24,"tag":276,"props":732,"children":733},{"style":476},[734],{"type":33,"value":735},"}\\n",{"type":24,"tag":276,"props":737,"children":738},{"style":289},[739],{"type":33,"value":740},"\"\n",{"type":24,"tag":276,"props":742,"children":743},{"class":278,"line":472},[744,749,754,758],{"type":24,"tag":276,"props":745,"children":746},{"style":367},[747],{"type":33,"value":748},"        for",{"type":24,"tag":276,"props":750,"children":751},{"style":373},[752],{"type":33,"value":753}," column ",{"type":24,"tag":276,"props":755,"children":756},{"style":367},[757],{"type":33,"value":692},{"type":24,"tag":276,"props":759,"children":760},{"style":373},[761],{"type":33,"value":762}," inspector.get_columns(table_name):\n",{"type":24,"tag":276,"props":764,"children":765},{"class":278,"line":502},[766,771,775,779,784,788,793,798,803,808,813,817,821,826,830,834,839,844],{"type":24,"tag":276,"props":767,"children":768},{"style":373},[769],{"type":33,"value":770},"            schema_description ",{"type":24,"tag":276,"props":772,"children":773},{"style":367},[774],{"type":33,"value":710},{"type":24,"tag":276,"props":776,"children":777},{"style":367},[778],{"type":33,"value":715},{"type":24,"tag":276,"props":780,"children":781},{"style":289},[782],{"type":33,"value":783},"\"- ",{"type":24,"tag":276,"props":785,"children":786},{"style":476},[787],{"type":33,"value":725},{"type":24,"tag":276,"props":789,"children":790},{"style":373},[791],{"type":33,"value":792},"column[",{"type":24,"tag":276,"props":794,"children":795},{"style":289},[796],{"type":33,"value":797},"'name'",{"type":24,"tag":276,"props":799,"children":800},{"style":373},[801],{"type":33,"value":802},"]",{"type":24,"tag":276,"props":804,"children":805},{"style":476},[806],{"type":33,"value":807},"}",{"type":24,"tag":276,"props":809,"children":810},{"style":289},[811],{"type":33,"value":812}," (",{"type":24,"tag":276,"props":814,"children":815},{"style":476},[816],{"type":33,"value":725},{"type":24,"tag":276,"props":818,"children":819},{"style":373},[820],{"type":33,"value":792},{"type":24,"tag":276,"props":822,"children":823},{"style":289},[824],{"type":33,"value":825},"'type'",{"type":24,"tag":276,"props":827,"children":828},{"style":373},[829],{"type":33,"value":802},{"type":24,"tag":276,"props":831,"children":832},{"style":476},[833],{"type":33,"value":807},{"type":24,"tag":276,"props":835,"children":836},{"style":289},[837],{"type":33,"value":838},")",{"type":24,"tag":276,"props":840,"children":841},{"style":476},[842],{"type":33,"value":843},"\\n",{"type":24,"tag":276,"props":845,"children":846},{"style":289},[847],{"type":33,"value":740},{"type":24,"tag":276,"props":849,"children":850},{"class":278,"line":529},[851,855,859,864,868],{"type":24,"tag":276,"props":852,"children":853},{"style":373},[854],{"type":33,"value":705},{"type":24,"tag":276,"props":856,"children":857},{"style":367},[858],{"type":33,"value":710},{"type":24,"tag":276,"props":860,"children":861},{"style":289},[862],{"type":33,"value":863}," \"",{"type":24,"tag":276,"props":865,"children":866},{"style":476},[867],{"type":33,"value":843},{"type":24,"tag":276,"props":869,"children":870},{"style":289},[871],{"type":33,"value":740},{"type":24,"tag":276,"props":873,"children":874},{"class":278,"line":562},[875,880],{"type":24,"tag":276,"props":876,"children":877},{"style":367},[878],{"type":33,"value":879},"    return",{"type":24,"tag":276,"props":881,"children":882},{"style":373},[883],{"type":33,"value":884}," schema_description\n",{"type":24,"tag":276,"props":886,"children":888},{"class":278,"line":887},12,[889],{"type":24,"tag":276,"props":890,"children":891},{"emptyLinePlaceholder":449},[892],{"type":33,"value":452},{"type":24,"tag":276,"props":894,"children":896},{"class":278,"line":895},13,[897,902,906],{"type":24,"tag":276,"props":898,"children":899},{"style":373},[900],{"type":33,"value":901},"schema_description ",{"type":24,"tag":276,"props":903,"children":904},{"style":367},[905],{"type":33,"value":513},{"type":24,"tag":276,"props":907,"children":908},{"style":373},[909],{"type":33,"value":910}," get_database_schema(engine)\n",{"type":24,"tag":276,"props":912,"children":914},{"class":278,"line":913},14,[915,920],{"type":24,"tag":276,"props":916,"children":917},{"style":476},[918],{"type":33,"value":919},"print",{"type":24,"tag":276,"props":921,"children":922},{"style":373},[923],{"type":33,"value":924},"(schema_description)\n",{"type":24,"tag":36,"props":926,"children":928},{"id":927},"paso-4-preparar-la-consulta-con-gpt",[929],{"type":33,"value":930},"Paso 4: Preparar la consulta con GPT",{"type":24,"tag":29,"props":932,"children":933},{},[934],{"type":33,"value":935},"Utiliza la API de OpenAI para transformar preguntas en consultas SQL, incluyendo el esquema de la base de datos:",{"type":24,"tag":266,"props":937,"children":939},{"className":355,"code":938,"language":357,"meta":7,"style":7},"import openai\nimport os\n\nopenai.api_key = os.getenv(\"OPENAI_API_KEY\")\n\ndef generate_sql(question, schema):\n    response = openai.ChatCompletion.create(\n        model=\"gpt-4\",\n        messages=[\n            {\"role\": \"system\", \"content\": f\"Convierte preguntas en lenguaje natural a consultas SQL. Esquema de la base de datos:\\n{schema}\"},\n            {\"role\": \"user\", \"content\": question}\n        ]\n    )\n    sql_query = response.choices[0].message.content.strip()\n    return sql_query\n\nquery = generate_sql(\"¿Cuántos usuarios se registraron el último mes?\", schema_description)\nprint(query)\n",[940],{"type":24,"tag":14,"props":941,"children":942},{"__ignoreMap":7},[943,955,966,973,998,1005,1022,1039,1061,1078,1149,1182,1190,1198,1225,1238,1246,1274],{"type":24,"tag":276,"props":944,"children":945},{"class":278,"line":279},[946,950],{"type":24,"tag":276,"props":947,"children":948},{"style":367},[949],{"type":33,"value":381},{"type":24,"tag":276,"props":951,"children":952},{"style":373},[953],{"type":33,"value":954}," openai\n",{"type":24,"tag":276,"props":956,"children":957},{"class":278,"line":192},[958,962],{"type":24,"tag":276,"props":959,"children":960},{"style":367},[961],{"type":33,"value":381},{"type":24,"tag":276,"props":963,"children":964},{"style":373},[965],{"type":33,"value":420},{"type":24,"tag":276,"props":967,"children":968},{"class":278,"line":410},[969],{"type":24,"tag":276,"props":970,"children":971},{"emptyLinePlaceholder":449},[972],{"type":33,"value":452},{"type":24,"tag":276,"props":974,"children":975},{"class":278,"line":423},[976,981,985,989,994],{"type":24,"tag":276,"props":977,"children":978},{"style":373},[979],{"type":33,"value":980},"openai.api_key ",{"type":24,"tag":276,"props":982,"children":983},{"style":367},[984],{"type":33,"value":513},{"type":24,"tag":276,"props":986,"children":987},{"style":373},[988],{"type":33,"value":489},{"type":24,"tag":276,"props":990,"children":991},{"style":289},[992],{"type":33,"value":993},"\"OPENAI_API_KEY\"",{"type":24,"tag":276,"props":995,"children":996},{"style":373},[997],{"type":33,"value":499},{"type":24,"tag":276,"props":999,"children":1000},{"class":278,"line":445},[1001],{"type":24,"tag":276,"props":1002,"children":1003},{"emptyLinePlaceholder":449},[1004],{"type":33,"value":452},{"type":24,"tag":276,"props":1006,"children":1007},{"class":278,"line":455},[1008,1012,1017],{"type":24,"tag":276,"props":1009,"children":1010},{"style":367},[1011],{"type":33,"value":630},{"type":24,"tag":276,"props":1013,"children":1014},{"style":283},[1015],{"type":33,"value":1016}," generate_sql",{"type":24,"tag":276,"props":1018,"children":1019},{"style":373},[1020],{"type":33,"value":1021},"(question, schema):\n",{"type":24,"tag":276,"props":1023,"children":1024},{"class":278,"line":464},[1025,1030,1034],{"type":24,"tag":276,"props":1026,"children":1027},{"style":373},[1028],{"type":33,"value":1029},"    response ",{"type":24,"tag":276,"props":1031,"children":1032},{"style":367},[1033],{"type":33,"value":513},{"type":24,"tag":276,"props":1035,"children":1036},{"style":373},[1037],{"type":33,"value":1038}," openai.ChatCompletion.create(\n",{"type":24,"tag":276,"props":1040,"children":1041},{"class":278,"line":472},[1042,1047,1051,1056],{"type":24,"tag":276,"props":1043,"children":1044},{"style":547},[1045],{"type":33,"value":1046},"        model",{"type":24,"tag":276,"props":1048,"children":1049},{"style":367},[1050],{"type":33,"value":513},{"type":24,"tag":276,"props":1052,"children":1053},{"style":289},[1054],{"type":33,"value":1055},"\"gpt-4\"",{"type":24,"tag":276,"props":1057,"children":1058},{"style":373},[1059],{"type":33,"value":1060},",\n",{"type":24,"tag":276,"props":1062,"children":1063},{"class":278,"line":502},[1064,1069,1073],{"type":24,"tag":276,"props":1065,"children":1066},{"style":547},[1067],{"type":33,"value":1068},"        messages",{"type":24,"tag":276,"props":1070,"children":1071},{"style":367},[1072],{"type":33,"value":513},{"type":24,"tag":276,"props":1074,"children":1075},{"style":373},[1076],{"type":33,"value":1077},"[\n",{"type":24,"tag":276,"props":1079,"children":1080},{"class":278,"line":529},[1081,1086,1091,1096,1101,1106,1111,1115,1120,1125,1130,1135,1139,1144],{"type":24,"tag":276,"props":1082,"children":1083},{"style":373},[1084],{"type":33,"value":1085},"            {",{"type":24,"tag":276,"props":1087,"children":1088},{"style":289},[1089],{"type":33,"value":1090},"\"role\"",{"type":24,"tag":276,"props":1092,"children":1093},{"style":373},[1094],{"type":33,"value":1095},": ",{"type":24,"tag":276,"props":1097,"children":1098},{"style":289},[1099],{"type":33,"value":1100},"\"system\"",{"type":24,"tag":276,"props":1102,"children":1103},{"style":373},[1104],{"type":33,"value":1105},", ",{"type":24,"tag":276,"props":1107,"children":1108},{"style":289},[1109],{"type":33,"value":1110},"\"content\"",{"type":24,"tag":276,"props":1112,"children":1113},{"style":373},[1114],{"type":33,"value":1095},{"type":24,"tag":276,"props":1116,"children":1117},{"style":367},[1118],{"type":33,"value":1119},"f",{"type":24,"tag":276,"props":1121,"children":1122},{"style":289},[1123],{"type":33,"value":1124},"\"Convierte preguntas en lenguaje natural a consultas SQL. Esquema de la base de datos:",{"type":24,"tag":276,"props":1126,"children":1127},{"style":476},[1128],{"type":33,"value":1129},"\\n{",{"type":24,"tag":276,"props":1131,"children":1132},{"style":373},[1133],{"type":33,"value":1134},"schema",{"type":24,"tag":276,"props":1136,"children":1137},{"style":476},[1138],{"type":33,"value":807},{"type":24,"tag":276,"props":1140,"children":1141},{"style":289},[1142],{"type":33,"value":1143},"\"",{"type":24,"tag":276,"props":1145,"children":1146},{"style":373},[1147],{"type":33,"value":1148},"},\n",{"type":24,"tag":276,"props":1150,"children":1151},{"class":278,"line":562},[1152,1156,1160,1164,1169,1173,1177],{"type":24,"tag":276,"props":1153,"children":1154},{"style":373},[1155],{"type":33,"value":1085},{"type":24,"tag":276,"props":1157,"children":1158},{"style":289},[1159],{"type":33,"value":1090},{"type":24,"tag":276,"props":1161,"children":1162},{"style":373},[1163],{"type":33,"value":1095},{"type":24,"tag":276,"props":1165,"children":1166},{"style":289},[1167],{"type":33,"value":1168},"\"user\"",{"type":24,"tag":276,"props":1170,"children":1171},{"style":373},[1172],{"type":33,"value":1105},{"type":24,"tag":276,"props":1174,"children":1175},{"style":289},[1176],{"type":33,"value":1110},{"type":24,"tag":276,"props":1178,"children":1179},{"style":373},[1180],{"type":33,"value":1181},": question}\n",{"type":24,"tag":276,"props":1183,"children":1184},{"class":278,"line":887},[1185],{"type":24,"tag":276,"props":1186,"children":1187},{"style":373},[1188],{"type":33,"value":1189},"        ]\n",{"type":24,"tag":276,"props":1191,"children":1192},{"class":278,"line":895},[1193],{"type":24,"tag":276,"props":1194,"children":1195},{"style":373},[1196],{"type":33,"value":1197},"    )\n",{"type":24,"tag":276,"props":1199,"children":1200},{"class":278,"line":913},[1201,1206,1210,1215,1220],{"type":24,"tag":276,"props":1202,"children":1203},{"style":373},[1204],{"type":33,"value":1205},"    sql_query ",{"type":24,"tag":276,"props":1207,"children":1208},{"style":367},[1209],{"type":33,"value":513},{"type":24,"tag":276,"props":1211,"children":1212},{"style":373},[1213],{"type":33,"value":1214}," response.choices[",{"type":24,"tag":276,"props":1216,"children":1217},{"style":476},[1218],{"type":33,"value":1219},"0",{"type":24,"tag":276,"props":1221,"children":1222},{"style":373},[1223],{"type":33,"value":1224},"].message.content.strip()\n",{"type":24,"tag":276,"props":1226,"children":1228},{"class":278,"line":1227},15,[1229,1233],{"type":24,"tag":276,"props":1230,"children":1231},{"style":367},[1232],{"type":33,"value":879},{"type":24,"tag":276,"props":1234,"children":1235},{"style":373},[1236],{"type":33,"value":1237}," sql_query\n",{"type":24,"tag":276,"props":1239,"children":1241},{"class":278,"line":1240},16,[1242],{"type":24,"tag":276,"props":1243,"children":1244},{"emptyLinePlaceholder":449},[1245],{"type":33,"value":452},{"type":24,"tag":276,"props":1247,"children":1249},{"class":278,"line":1248},17,[1250,1255,1259,1264,1269],{"type":24,"tag":276,"props":1251,"children":1252},{"style":373},[1253],{"type":33,"value":1254},"query ",{"type":24,"tag":276,"props":1256,"children":1257},{"style":367},[1258],{"type":33,"value":513},{"type":24,"tag":276,"props":1260,"children":1261},{"style":373},[1262],{"type":33,"value":1263}," generate_sql(",{"type":24,"tag":276,"props":1265,"children":1266},{"style":289},[1267],{"type":33,"value":1268},"\"¿Cuántos usuarios se registraron el último mes?\"",{"type":24,"tag":276,"props":1270,"children":1271},{"style":373},[1272],{"type":33,"value":1273},", schema_description)\n",{"type":24,"tag":276,"props":1275,"children":1277},{"class":278,"line":1276},18,[1278,1282],{"type":24,"tag":276,"props":1279,"children":1280},{"style":476},[1281],{"type":33,"value":919},{"type":24,"tag":276,"props":1283,"children":1284},{"style":373},[1285],{"type":33,"value":1286},"(query)\n",{"type":24,"tag":36,"props":1288,"children":1290},{"id":1289},"paso-5-ejecutar-consultas-generadas",[1291],{"type":33,"value":1292},"Paso 5: Ejecutar consultas generadas",{"type":24,"tag":29,"props":1294,"children":1295},{},[1296],{"type":33,"value":1297},"Ejecuta la consulta generada y obtiene resultados:",{"type":24,"tag":266,"props":1299,"children":1301},{"className":355,"code":1300,"language":357,"meta":7,"style":7},"result = session.execute(query)\nfor row in result:\n    print(row)\n",[1302],{"type":24,"tag":14,"props":1303,"children":1304},{"__ignoreMap":7},[1305,1322,1344],{"type":24,"tag":276,"props":1306,"children":1307},{"class":278,"line":279},[1308,1313,1317],{"type":24,"tag":276,"props":1309,"children":1310},{"style":373},[1311],{"type":33,"value":1312},"result ",{"type":24,"tag":276,"props":1314,"children":1315},{"style":367},[1316],{"type":33,"value":513},{"type":24,"tag":276,"props":1318,"children":1319},{"style":373},[1320],{"type":33,"value":1321}," session.execute(query)\n",{"type":24,"tag":276,"props":1323,"children":1324},{"class":278,"line":192},[1325,1330,1335,1339],{"type":24,"tag":276,"props":1326,"children":1327},{"style":367},[1328],{"type":33,"value":1329},"for",{"type":24,"tag":276,"props":1331,"children":1332},{"style":373},[1333],{"type":33,"value":1334}," row ",{"type":24,"tag":276,"props":1336,"children":1337},{"style":367},[1338],{"type":33,"value":692},{"type":24,"tag":276,"props":1340,"children":1341},{"style":373},[1342],{"type":33,"value":1343}," result:\n",{"type":24,"tag":276,"props":1345,"children":1346},{"class":278,"line":410},[1347,1352],{"type":24,"tag":276,"props":1348,"children":1349},{"style":476},[1350],{"type":33,"value":1351},"    print",{"type":24,"tag":276,"props":1353,"children":1354},{"style":373},[1355],{"type":33,"value":1356},"(row)\n",{"type":24,"tag":36,"props":1358,"children":1360},{"id":1359},"paso-6-manejo-de-errores-y-seguridad",[1361],{"type":33,"value":1362},"Paso 6: Manejo de errores y seguridad",{"type":24,"tag":29,"props":1364,"children":1365},{},[1366],{"type":33,"value":1367},"Para evitar problemas con consultas inseguras o errores, siempre valida las consultas generadas por GPT antes de ejecutarlas directamente:",{"type":24,"tag":266,"props":1369,"children":1371},{"className":355,"code":1370,"language":357,"meta":7,"style":7},"def execute_safe_query(sql_query):\n    try:\n        result = session.execute(sql_query)\n        return result.fetchall()\n    except Exception as e:\n        print(f\"Error al ejecutar consulta: {e}\")\n        return None\n\nresults = execute_safe_query(query)\nprint(results)\n",[1372],{"type":24,"tag":14,"props":1373,"children":1374},{"__ignoreMap":7},[1375,1392,1405,1422,1435,1458,1501,1513,1520,1537],{"type":24,"tag":276,"props":1376,"children":1377},{"class":278,"line":279},[1378,1382,1387],{"type":24,"tag":276,"props":1379,"children":1380},{"style":367},[1381],{"type":33,"value":630},{"type":24,"tag":276,"props":1383,"children":1384},{"style":283},[1385],{"type":33,"value":1386}," execute_safe_query",{"type":24,"tag":276,"props":1388,"children":1389},{"style":373},[1390],{"type":33,"value":1391},"(sql_query):\n",{"type":24,"tag":276,"props":1393,"children":1394},{"class":278,"line":192},[1395,1400],{"type":24,"tag":276,"props":1396,"children":1397},{"style":367},[1398],{"type":33,"value":1399},"    try",{"type":24,"tag":276,"props":1401,"children":1402},{"style":373},[1403],{"type":33,"value":1404},":\n",{"type":24,"tag":276,"props":1406,"children":1407},{"class":278,"line":410},[1408,1413,1417],{"type":24,"tag":276,"props":1409,"children":1410},{"style":373},[1411],{"type":33,"value":1412},"        result ",{"type":24,"tag":276,"props":1414,"children":1415},{"style":367},[1416],{"type":33,"value":513},{"type":24,"tag":276,"props":1418,"children":1419},{"style":373},[1420],{"type":33,"value":1421}," session.execute(sql_query)\n",{"type":24,"tag":276,"props":1423,"children":1424},{"class":278,"line":423},[1425,1430],{"type":24,"tag":276,"props":1426,"children":1427},{"style":367},[1428],{"type":33,"value":1429},"        return",{"type":24,"tag":276,"props":1431,"children":1432},{"style":373},[1433],{"type":33,"value":1434}," result.fetchall()\n",{"type":24,"tag":276,"props":1436,"children":1437},{"class":278,"line":445},[1438,1443,1448,1453],{"type":24,"tag":276,"props":1439,"children":1440},{"style":367},[1441],{"type":33,"value":1442},"    except",{"type":24,"tag":276,"props":1444,"children":1445},{"style":476},[1446],{"type":33,"value":1447}," Exception",{"type":24,"tag":276,"props":1449,"children":1450},{"style":367},[1451],{"type":33,"value":1452}," as",{"type":24,"tag":276,"props":1454,"children":1455},{"style":373},[1456],{"type":33,"value":1457}," e:\n",{"type":24,"tag":276,"props":1459,"children":1460},{"class":278,"line":455},[1461,1466,1471,1475,1480,1484,1489,1493,1497],{"type":24,"tag":276,"props":1462,"children":1463},{"style":476},[1464],{"type":33,"value":1465},"        print",{"type":24,"tag":276,"props":1467,"children":1468},{"style":373},[1469],{"type":33,"value":1470},"(",{"type":24,"tag":276,"props":1472,"children":1473},{"style":367},[1474],{"type":33,"value":1119},{"type":24,"tag":276,"props":1476,"children":1477},{"style":289},[1478],{"type":33,"value":1479},"\"Error al ejecutar consulta: ",{"type":24,"tag":276,"props":1481,"children":1482},{"style":476},[1483],{"type":33,"value":725},{"type":24,"tag":276,"props":1485,"children":1486},{"style":373},[1487],{"type":33,"value":1488},"e",{"type":24,"tag":276,"props":1490,"children":1491},{"style":476},[1492],{"type":33,"value":807},{"type":24,"tag":276,"props":1494,"children":1495},{"style":289},[1496],{"type":33,"value":1143},{"type":24,"tag":276,"props":1498,"children":1499},{"style":373},[1500],{"type":33,"value":499},{"type":24,"tag":276,"props":1502,"children":1503},{"class":278,"line":464},[1504,1508],{"type":24,"tag":276,"props":1505,"children":1506},{"style":367},[1507],{"type":33,"value":1429},{"type":24,"tag":276,"props":1509,"children":1510},{"style":476},[1511],{"type":33,"value":1512}," None\n",{"type":24,"tag":276,"props":1514,"children":1515},{"class":278,"line":472},[1516],{"type":24,"tag":276,"props":1517,"children":1518},{"emptyLinePlaceholder":449},[1519],{"type":33,"value":452},{"type":24,"tag":276,"props":1521,"children":1522},{"class":278,"line":502},[1523,1528,1532],{"type":24,"tag":276,"props":1524,"children":1525},{"style":373},[1526],{"type":33,"value":1527},"results ",{"type":24,"tag":276,"props":1529,"children":1530},{"style":367},[1531],{"type":33,"value":513},{"type":24,"tag":276,"props":1533,"children":1534},{"style":373},[1535],{"type":33,"value":1536}," execute_safe_query(query)\n",{"type":24,"tag":276,"props":1538,"children":1539},{"class":278,"line":529},[1540,1544],{"type":24,"tag":276,"props":1541,"children":1542},{"style":476},[1543],{"type":33,"value":919},{"type":24,"tag":276,"props":1545,"children":1546},{"style":373},[1547],{"type":33,"value":1548},"(results)\n",{"type":24,"tag":36,"props":1550,"children":1552},{"id":1551},"buenas-prácticas",[1553],{"type":33,"value":1554},"Buenas prácticas",{"type":24,"tag":150,"props":1556,"children":1557},{},[1558,1563,1568],{"type":24,"tag":154,"props":1559,"children":1560},{},[1561],{"type":33,"value":1562},"Siempre revisa o limita el acceso de los usuarios a consultas críticas o sensibles.",{"type":24,"tag":154,"props":1564,"children":1565},{},[1566],{"type":33,"value":1567},"Implementa validaciones adicionales para asegurar la integridad y seguridad de tu base de datos.",{"type":24,"tag":154,"props":1569,"children":1570},{},[1571],{"type":33,"value":1572},"Utiliza tokens y modelos específicos de OpenAI optimizados para tareas SQL.",{"type":24,"tag":36,"props":1574,"children":1576},{"id":1575},"conclusión",[1577],{"type":33,"value":1578},"Conclusión",{"type":24,"tag":29,"props":1580,"children":1581},{},[1582],{"type":33,"value":1583},"Utilizar GPT para transformar consultas en lenguaje natural a SQL facilita enormemente la accesibilidad a los datos. Implementando este sistema, podrás ofrecer una experiencia de usuario más amigable y eficiente.",{"type":24,"tag":1585,"props":1586,"children":1588},"h1",{"id":1587},"recursos-adicionales",[1589],{"type":33,"value":1590},"Recursos adicionales",{"type":24,"tag":150,"props":1592,"children":1593},{},[1594,1604,1614],{"type":24,"tag":154,"props":1595,"children":1596},{},[1597],{"type":24,"tag":158,"props":1598,"children":1601},{"href":1599,"rel":1600},"https://platform.openai.com/docs/api-reference",[162],[1602],{"type":33,"value":1603},"Documentación API OpenAI",{"type":24,"tag":154,"props":1605,"children":1606},{},[1607],{"type":24,"tag":158,"props":1608,"children":1611},{"href":1609,"rel":1610},"https://docs.sqlalchemy.org/en/20/",[162],[1612],{"type":33,"value":1613},"SQLAlchemy ORM Documentation",{"type":24,"tag":154,"props":1615,"children":1616},{},[1617],{"type":24,"tag":158,"props":1618,"children":1621},{"href":1619,"rel":1620},"https://pypi.org/project/python-dotenv/",[162],[1622],{"type":33,"value":1623},"Python Dotenv",{"type":24,"tag":1625,"props":1626,"children":1627},"style",{},[1628],{"type":33,"value":1629},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}",{"title":7,"searchDepth":192,"depth":192,"links":1631},[1632,1633,1634,1635,1636,1637,1638,1639,1640],{"id":232,"depth":192,"text":235},{"id":256,"depth":192,"text":259},{"id":344,"depth":192,"text":347},{"id":580,"depth":192,"text":583},{"id":927,"depth":192,"text":930},{"id":1289,"depth":192,"text":1292},{"id":1359,"depth":192,"text":1362},{"id":1551,"depth":192,"text":1554},{"id":1575,"depth":192,"text":1578},"content:posts:gpt-sql-integration.md","posts/gpt-sql-integration.md","posts/gpt-sql-integration",{"_path":1645,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":1646,"description":1647,"date":1648,"layout":11,"subtitle":1649,"image":1650,"optimized_image":1650,"category":14,"tags":1651,"author":19,"paginate":6,"body":1656,"_type":200,"_id":2549,"_source":202,"_file":2550,"_stem":2551,"_extension":205},"/posts/java-security-best-practices","Java Security Best Practices: Preventing Vulnerabilities and Threats","Explore essential security strategies for Java applications, addressing common vulnerabilities identified by OWASP Top 10 with practical examples.","2024-08-15T11:00:00.000Z","Practical Strategies to Safeguard Your Java Applications","/assets/img/uploads/java-security-best-practices.png",[14,1652,1653,1654,1655],"java","security","owasp","cybersecurity",{"type":21,"children":1657,"toc":2537},[1658,1666,1672,1677,1731,1736,1742,1747,1870,1875,1881,1886,1989,1995,2000,2123,2128,2134,2139,2164,2169,2175,2180,2196,2201,2207,2212,2295,2300,2306,2311,2431,2436,2442,2447,2480,2485,2491,2496,2500,2533],{"type":24,"tag":25,"props":1659,"children":1660},{},[1661],{"type":24,"tag":29,"props":1662,"children":1663},{},[1664],{"type":33,"value":1665},"Security is critical in software development. Java applications, like all software, face numerous potential vulnerabilities. In this article, we will explore essential security best practices, referencing the OWASP Top 10, and providing practical Java examples to help you protect your applications effectively.",{"type":24,"tag":36,"props":1667,"children":1669},{"id":1668},"understanding-owasp-top-10",[1670],{"type":33,"value":1671},"Understanding OWASP Top 10",{"type":24,"tag":29,"props":1673,"children":1674},{},[1675],{"type":33,"value":1676},"The OWASP Top 10 outlines the most critical web application security risks:",{"type":24,"tag":1678,"props":1679,"children":1680},"ol",{},[1681,1686,1691,1696,1701,1706,1711,1716,1721,1726],{"type":24,"tag":154,"props":1682,"children":1683},{},[1684],{"type":33,"value":1685},"Injection",{"type":24,"tag":154,"props":1687,"children":1688},{},[1689],{"type":33,"value":1690},"Broken Authentication",{"type":24,"tag":154,"props":1692,"children":1693},{},[1694],{"type":33,"value":1695},"Sensitive Data Exposure",{"type":24,"tag":154,"props":1697,"children":1698},{},[1699],{"type":33,"value":1700},"XML External Entities (XXE)",{"type":24,"tag":154,"props":1702,"children":1703},{},[1704],{"type":33,"value":1705},"Broken Access Control",{"type":24,"tag":154,"props":1707,"children":1708},{},[1709],{"type":33,"value":1710},"Security Misconfiguration",{"type":24,"tag":154,"props":1712,"children":1713},{},[1714],{"type":33,"value":1715},"Cross-Site Scripting (XSS)",{"type":24,"tag":154,"props":1717,"children":1718},{},[1719],{"type":33,"value":1720},"Insecure Deserialization",{"type":24,"tag":154,"props":1722,"children":1723},{},[1724],{"type":33,"value":1725},"Using Components with Known Vulnerabilities",{"type":24,"tag":154,"props":1727,"children":1728},{},[1729],{"type":33,"value":1730},"Insufficient Logging and Monitoring",{"type":24,"tag":29,"props":1732,"children":1733},{},[1734],{"type":33,"value":1735},"Let's delve into practical strategies and Java implementations for some of these threats.",{"type":24,"tag":36,"props":1737,"children":1739},{"id":1738},"_1-injection-prevention",[1740],{"type":33,"value":1741},"1. Injection Prevention",{"type":24,"tag":29,"props":1743,"children":1744},{},[1745],{"type":33,"value":1746},"Use Prepared Statements and parameterized queries:",{"type":24,"tag":266,"props":1748,"children":1751},{"className":1749,"code":1750,"language":1652,"meta":7,"style":7},"language-java shiki shiki-themes github-dark github-light","PreparedStatement stmt = connection.prepareStatement(\"SELECT * FROM users WHERE username=? AND password=?\");\nstmt.setString(1, username);\nstmt.setString(2, password);\nResultSet rs = stmt.executeQuery();\n",[1752],{"type":24,"tag":14,"props":1753,"children":1754},{"__ignoreMap":7},[1755,1791,1818,1843],{"type":24,"tag":276,"props":1756,"children":1757},{"class":278,"line":279},[1758,1763,1767,1772,1777,1781,1786],{"type":24,"tag":276,"props":1759,"children":1760},{"style":373},[1761],{"type":33,"value":1762},"PreparedStatement stmt ",{"type":24,"tag":276,"props":1764,"children":1765},{"style":367},[1766],{"type":33,"value":513},{"type":24,"tag":276,"props":1768,"children":1769},{"style":373},[1770],{"type":33,"value":1771}," connection.",{"type":24,"tag":276,"props":1773,"children":1774},{"style":283},[1775],{"type":33,"value":1776},"prepareStatement",{"type":24,"tag":276,"props":1778,"children":1779},{"style":373},[1780],{"type":33,"value":1470},{"type":24,"tag":276,"props":1782,"children":1783},{"style":289},[1784],{"type":33,"value":1785},"\"SELECT * FROM users WHERE username=? AND password=?\"",{"type":24,"tag":276,"props":1787,"children":1788},{"style":373},[1789],{"type":33,"value":1790},");\n",{"type":24,"tag":276,"props":1792,"children":1793},{"class":278,"line":192},[1794,1799,1804,1808,1813],{"type":24,"tag":276,"props":1795,"children":1796},{"style":373},[1797],{"type":33,"value":1798},"stmt.",{"type":24,"tag":276,"props":1800,"children":1801},{"style":283},[1802],{"type":33,"value":1803},"setString",{"type":24,"tag":276,"props":1805,"children":1806},{"style":373},[1807],{"type":33,"value":1470},{"type":24,"tag":276,"props":1809,"children":1810},{"style":476},[1811],{"type":33,"value":1812},"1",{"type":24,"tag":276,"props":1814,"children":1815},{"style":373},[1816],{"type":33,"value":1817},", username);\n",{"type":24,"tag":276,"props":1819,"children":1820},{"class":278,"line":410},[1821,1825,1829,1833,1838],{"type":24,"tag":276,"props":1822,"children":1823},{"style":373},[1824],{"type":33,"value":1798},{"type":24,"tag":276,"props":1826,"children":1827},{"style":283},[1828],{"type":33,"value":1803},{"type":24,"tag":276,"props":1830,"children":1831},{"style":373},[1832],{"type":33,"value":1470},{"type":24,"tag":276,"props":1834,"children":1835},{"style":476},[1836],{"type":33,"value":1837},"2",{"type":24,"tag":276,"props":1839,"children":1840},{"style":373},[1841],{"type":33,"value":1842},", password);\n",{"type":24,"tag":276,"props":1844,"children":1845},{"class":278,"line":423},[1846,1851,1855,1860,1865],{"type":24,"tag":276,"props":1847,"children":1848},{"style":373},[1849],{"type":33,"value":1850},"ResultSet rs ",{"type":24,"tag":276,"props":1852,"children":1853},{"style":367},[1854],{"type":33,"value":513},{"type":24,"tag":276,"props":1856,"children":1857},{"style":373},[1858],{"type":33,"value":1859}," stmt.",{"type":24,"tag":276,"props":1861,"children":1862},{"style":283},[1863],{"type":33,"value":1864},"executeQuery",{"type":24,"tag":276,"props":1866,"children":1867},{"style":373},[1868],{"type":33,"value":1869},"();\n",{"type":24,"tag":29,"props":1871,"children":1872},{},[1873],{"type":33,"value":1874},"This approach prevents SQL injection by ensuring that input data is treated as data, not executable code.",{"type":24,"tag":36,"props":1876,"children":1878},{"id":1877},"_2-secure-authentication",[1879],{"type":33,"value":1880},"2. Secure Authentication",{"type":24,"tag":29,"props":1882,"children":1883},{},[1884],{"type":33,"value":1885},"Implement robust authentication using secure password storage (bcrypt or Argon2):",{"type":24,"tag":266,"props":1887,"children":1889},{"className":1749,"code":1888,"language":1652,"meta":7,"style":7},"import org.springframework.security.crypto.bcrypt.BCrypt;\n\nString hashedPassword = BCrypt.hashpw(password, BCrypt.gensalt());\nif (BCrypt.checkpw(password, hashedPassword)) {\n    // authentication successful\n}\n",[1890],{"type":24,"tag":14,"props":1891,"children":1892},{"__ignoreMap":7},[1893,1905,1912,1949,1972,1981],{"type":24,"tag":276,"props":1894,"children":1895},{"class":278,"line":279},[1896,1900],{"type":24,"tag":276,"props":1897,"children":1898},{"style":367},[1899],{"type":33,"value":381},{"type":24,"tag":276,"props":1901,"children":1902},{"style":373},[1903],{"type":33,"value":1904}," org.springframework.security.crypto.bcrypt.BCrypt;\n",{"type":24,"tag":276,"props":1906,"children":1907},{"class":278,"line":192},[1908],{"type":24,"tag":276,"props":1909,"children":1910},{"emptyLinePlaceholder":449},[1911],{"type":33,"value":452},{"type":24,"tag":276,"props":1913,"children":1914},{"class":278,"line":410},[1915,1920,1924,1929,1934,1939,1944],{"type":24,"tag":276,"props":1916,"children":1917},{"style":373},[1918],{"type":33,"value":1919},"String hashedPassword ",{"type":24,"tag":276,"props":1921,"children":1922},{"style":367},[1923],{"type":33,"value":513},{"type":24,"tag":276,"props":1925,"children":1926},{"style":373},[1927],{"type":33,"value":1928}," BCrypt.",{"type":24,"tag":276,"props":1930,"children":1931},{"style":283},[1932],{"type":33,"value":1933},"hashpw",{"type":24,"tag":276,"props":1935,"children":1936},{"style":373},[1937],{"type":33,"value":1938},"(password, BCrypt.",{"type":24,"tag":276,"props":1940,"children":1941},{"style":283},[1942],{"type":33,"value":1943},"gensalt",{"type":24,"tag":276,"props":1945,"children":1946},{"style":373},[1947],{"type":33,"value":1948},"());\n",{"type":24,"tag":276,"props":1950,"children":1951},{"class":278,"line":423},[1952,1957,1962,1967],{"type":24,"tag":276,"props":1953,"children":1954},{"style":367},[1955],{"type":33,"value":1956},"if",{"type":24,"tag":276,"props":1958,"children":1959},{"style":373},[1960],{"type":33,"value":1961}," (BCrypt.",{"type":24,"tag":276,"props":1963,"children":1964},{"style":283},[1965],{"type":33,"value":1966},"checkpw",{"type":24,"tag":276,"props":1968,"children":1969},{"style":373},[1970],{"type":33,"value":1971},"(password, hashedPassword)) {\n",{"type":24,"tag":276,"props":1973,"children":1974},{"class":278,"line":445},[1975],{"type":24,"tag":276,"props":1976,"children":1978},{"style":1977},"--shiki-default:#6A737D;--shiki-light:#6A737D",[1979],{"type":33,"value":1980},"    // authentication successful\n",{"type":24,"tag":276,"props":1982,"children":1983},{"class":278,"line":455},[1984],{"type":24,"tag":276,"props":1985,"children":1986},{"style":373},[1987],{"type":33,"value":1988},"}\n",{"type":24,"tag":36,"props":1990,"children":1992},{"id":1991},"_3-sensitive-data-exposure",[1993],{"type":33,"value":1994},"3. Sensitive Data Exposure",{"type":24,"tag":29,"props":1996,"children":1997},{},[1998],{"type":33,"value":1999},"Use encryption for sensitive data storage:",{"type":24,"tag":266,"props":2001,"children":2003},{"className":1749,"code":2002,"language":1652,"meta":7,"style":7},"Cipher cipher = Cipher.getInstance(\"AES/GCM/NoPadding\");\nSecretKey key = generateSecretKey();\ncipher.init(Cipher.ENCRYPT_MODE, key);\nbyte[] encryptedData = cipher.doFinal(plainData.getBytes(StandardCharsets.UTF_8));\n",[2004],{"type":24,"tag":14,"props":2005,"children":2006},{"__ignoreMap":7},[2007,2042,2063,2081],{"type":24,"tag":276,"props":2008,"children":2009},{"class":278,"line":279},[2010,2015,2019,2024,2029,2033,2038],{"type":24,"tag":276,"props":2011,"children":2012},{"style":373},[2013],{"type":33,"value":2014},"Cipher cipher ",{"type":24,"tag":276,"props":2016,"children":2017},{"style":367},[2018],{"type":33,"value":513},{"type":24,"tag":276,"props":2020,"children":2021},{"style":373},[2022],{"type":33,"value":2023}," Cipher.",{"type":24,"tag":276,"props":2025,"children":2026},{"style":283},[2027],{"type":33,"value":2028},"getInstance",{"type":24,"tag":276,"props":2030,"children":2031},{"style":373},[2032],{"type":33,"value":1470},{"type":24,"tag":276,"props":2034,"children":2035},{"style":289},[2036],{"type":33,"value":2037},"\"AES/GCM/NoPadding\"",{"type":24,"tag":276,"props":2039,"children":2040},{"style":373},[2041],{"type":33,"value":1790},{"type":24,"tag":276,"props":2043,"children":2044},{"class":278,"line":192},[2045,2050,2054,2059],{"type":24,"tag":276,"props":2046,"children":2047},{"style":373},[2048],{"type":33,"value":2049},"SecretKey key ",{"type":24,"tag":276,"props":2051,"children":2052},{"style":367},[2053],{"type":33,"value":513},{"type":24,"tag":276,"props":2055,"children":2056},{"style":283},[2057],{"type":33,"value":2058}," generateSecretKey",{"type":24,"tag":276,"props":2060,"children":2061},{"style":373},[2062],{"type":33,"value":1869},{"type":24,"tag":276,"props":2064,"children":2065},{"class":278,"line":410},[2066,2071,2076],{"type":24,"tag":276,"props":2067,"children":2068},{"style":373},[2069],{"type":33,"value":2070},"cipher.",{"type":24,"tag":276,"props":2072,"children":2073},{"style":283},[2074],{"type":33,"value":2075},"init",{"type":24,"tag":276,"props":2077,"children":2078},{"style":373},[2079],{"type":33,"value":2080},"(Cipher.ENCRYPT_MODE, key);\n",{"type":24,"tag":276,"props":2082,"children":2083},{"class":278,"line":423},[2084,2089,2094,2098,2103,2108,2113,2118],{"type":24,"tag":276,"props":2085,"children":2086},{"style":367},[2087],{"type":33,"value":2088},"byte",{"type":24,"tag":276,"props":2090,"children":2091},{"style":373},[2092],{"type":33,"value":2093},"[] encryptedData ",{"type":24,"tag":276,"props":2095,"children":2096},{"style":367},[2097],{"type":33,"value":513},{"type":24,"tag":276,"props":2099,"children":2100},{"style":373},[2101],{"type":33,"value":2102}," cipher.",{"type":24,"tag":276,"props":2104,"children":2105},{"style":283},[2106],{"type":33,"value":2107},"doFinal",{"type":24,"tag":276,"props":2109,"children":2110},{"style":373},[2111],{"type":33,"value":2112},"(plainData.",{"type":24,"tag":276,"props":2114,"children":2115},{"style":283},[2116],{"type":33,"value":2117},"getBytes",{"type":24,"tag":276,"props":2119,"children":2120},{"style":373},[2121],{"type":33,"value":2122},"(StandardCharsets.UTF_8));\n",{"type":24,"tag":29,"props":2124,"children":2125},{},[2126],{"type":33,"value":2127},"Always encrypt data at rest and in transit (use HTTPS).",{"type":24,"tag":36,"props":2129,"children":2131},{"id":2130},"_4-security-misconfiguration",[2132],{"type":33,"value":2133},"4. Security Misconfiguration",{"type":24,"tag":29,"props":2135,"children":2136},{},[2137],{"type":33,"value":2138},"Adopt secure defaults, disable debugging, and avoid detailed error messages:",{"type":24,"tag":266,"props":2140,"children":2144},{"className":2141,"code":2142,"language":2143,"meta":7,"style":7},"language-properties shiki shiki-themes github-dark github-light","server.error.include-stacktrace=never\nserver.error.include-message=never\n","properties",[2145],{"type":24,"tag":14,"props":2146,"children":2147},{"__ignoreMap":7},[2148,2156],{"type":24,"tag":276,"props":2149,"children":2150},{"class":278,"line":279},[2151],{"type":24,"tag":276,"props":2152,"children":2153},{},[2154],{"type":33,"value":2155},"server.error.include-stacktrace=never\n",{"type":24,"tag":276,"props":2157,"children":2158},{"class":278,"line":192},[2159],{"type":24,"tag":276,"props":2160,"children":2161},{},[2162],{"type":33,"value":2163},"server.error.include-message=never\n",{"type":24,"tag":29,"props":2165,"children":2166},{},[2167],{"type":33,"value":2168},"Review and secure your Java application configuration regularly.",{"type":24,"tag":36,"props":2170,"children":2172},{"id":2171},"_5-cross-site-scripting-xss",[2173],{"type":33,"value":2174},"5. Cross-Site Scripting (XSS)",{"type":24,"tag":29,"props":2176,"children":2177},{},[2178],{"type":33,"value":2179},"Utilize proper encoding or frameworks that prevent XSS by default, such as Thymeleaf:",{"type":24,"tag":266,"props":2181,"children":2185},{"className":2182,"code":2183,"language":2184,"meta":7,"style":7},"language-html shiki shiki-themes github-dark github-light","\u003Cp th:text=\"${userInput}\">\u003C/p>\n","html",[2186],{"type":24,"tag":14,"props":2187,"children":2188},{"__ignoreMap":7},[2189],{"type":24,"tag":276,"props":2190,"children":2191},{"class":278,"line":279},[2192],{"type":24,"tag":276,"props":2193,"children":2194},{},[2195],{"type":33,"value":2183},{"type":24,"tag":29,"props":2197,"children":2198},{},[2199],{"type":33,"value":2200},"Avoid directly inserting user inputs into HTML without proper sanitization or encoding.",{"type":24,"tag":36,"props":2202,"children":2204},{"id":2203},"_6-insecure-deserialization",[2205],{"type":33,"value":2206},"6. Insecure Deserialization",{"type":24,"tag":29,"props":2208,"children":2209},{},[2210],{"type":33,"value":2211},"Limit deserialization to trusted sources only:",{"type":24,"tag":266,"props":2213,"children":2215},{"className":1749,"code":2214,"language":1652,"meta":7,"style":7},"ObjectInputStream ois = new ObjectInputStream(new FileInputStream(\"trusted_data.ser\"));\nMyObject obj = (MyObject) ois.readObject();\n",[2216],{"type":24,"tag":14,"props":2217,"children":2218},{"__ignoreMap":7},[2219,2269],{"type":24,"tag":276,"props":2220,"children":2221},{"class":278,"line":279},[2222,2227,2231,2236,2241,2245,2250,2255,2259,2264],{"type":24,"tag":276,"props":2223,"children":2224},{"style":373},[2225],{"type":33,"value":2226},"ObjectInputStream ois ",{"type":24,"tag":276,"props":2228,"children":2229},{"style":367},[2230],{"type":33,"value":513},{"type":24,"tag":276,"props":2232,"children":2233},{"style":367},[2234],{"type":33,"value":2235}," new",{"type":24,"tag":276,"props":2237,"children":2238},{"style":283},[2239],{"type":33,"value":2240}," ObjectInputStream",{"type":24,"tag":276,"props":2242,"children":2243},{"style":373},[2244],{"type":33,"value":1470},{"type":24,"tag":276,"props":2246,"children":2247},{"style":367},[2248],{"type":33,"value":2249},"new",{"type":24,"tag":276,"props":2251,"children":2252},{"style":283},[2253],{"type":33,"value":2254}," FileInputStream",{"type":24,"tag":276,"props":2256,"children":2257},{"style":373},[2258],{"type":33,"value":1470},{"type":24,"tag":276,"props":2260,"children":2261},{"style":289},[2262],{"type":33,"value":2263},"\"trusted_data.ser\"",{"type":24,"tag":276,"props":2265,"children":2266},{"style":373},[2267],{"type":33,"value":2268},"));\n",{"type":24,"tag":276,"props":2270,"children":2271},{"class":278,"line":192},[2272,2277,2281,2286,2291],{"type":24,"tag":276,"props":2273,"children":2274},{"style":373},[2275],{"type":33,"value":2276},"MyObject obj ",{"type":24,"tag":276,"props":2278,"children":2279},{"style":367},[2280],{"type":33,"value":513},{"type":24,"tag":276,"props":2282,"children":2283},{"style":373},[2284],{"type":33,"value":2285}," (MyObject) ois.",{"type":24,"tag":276,"props":2287,"children":2288},{"style":283},[2289],{"type":33,"value":2290},"readObject",{"type":24,"tag":276,"props":2292,"children":2293},{"style":373},[2294],{"type":33,"value":1869},{"type":24,"tag":29,"props":2296,"children":2297},{},[2298],{"type":33,"value":2299},"Always validate deserialized data.",{"type":24,"tag":36,"props":2301,"children":2303},{"id":2302},"_7-component-vulnerabilities",[2304],{"type":33,"value":2305},"7. Component Vulnerabilities",{"type":24,"tag":29,"props":2307,"children":2308},{},[2309],{"type":33,"value":2310},"Regularly update libraries and dependencies, and use tools like OWASP Dependency-Check to identify vulnerabilities:",{"type":24,"tag":266,"props":2312,"children":2316},{"className":2313,"code":2314,"language":2315,"meta":7,"style":7},"language-xml shiki shiki-themes github-dark github-light","\u003Cplugin>\n    \u003CgroupId>org.owasp\u003C/groupId>\n    \u003CartifactId>dependency-check-maven\u003C/artifactId>\n    \u003Cversion>9.0.9\u003C/version>\n\u003C/plugin>\n","xml",[2317],{"type":24,"tag":14,"props":2318,"children":2319},{"__ignoreMap":7},[2320,2339,2365,2390,2415],{"type":24,"tag":276,"props":2321,"children":2322},{"class":278,"line":279},[2323,2328,2334],{"type":24,"tag":276,"props":2324,"children":2325},{"style":373},[2326],{"type":33,"value":2327},"\u003C",{"type":24,"tag":276,"props":2329,"children":2331},{"style":2330},"--shiki-default:#85E89D;--shiki-light:#22863A",[2332],{"type":33,"value":2333},"plugin",{"type":24,"tag":276,"props":2335,"children":2336},{"style":373},[2337],{"type":33,"value":2338},">\n",{"type":24,"tag":276,"props":2340,"children":2341},{"class":278,"line":192},[2342,2347,2352,2357,2361],{"type":24,"tag":276,"props":2343,"children":2344},{"style":373},[2345],{"type":33,"value":2346},"    \u003C",{"type":24,"tag":276,"props":2348,"children":2349},{"style":2330},[2350],{"type":33,"value":2351},"groupId",{"type":24,"tag":276,"props":2353,"children":2354},{"style":373},[2355],{"type":33,"value":2356},">org.owasp\u003C/",{"type":24,"tag":276,"props":2358,"children":2359},{"style":2330},[2360],{"type":33,"value":2351},{"type":24,"tag":276,"props":2362,"children":2363},{"style":373},[2364],{"type":33,"value":2338},{"type":24,"tag":276,"props":2366,"children":2367},{"class":278,"line":410},[2368,2372,2377,2382,2386],{"type":24,"tag":276,"props":2369,"children":2370},{"style":373},[2371],{"type":33,"value":2346},{"type":24,"tag":276,"props":2373,"children":2374},{"style":2330},[2375],{"type":33,"value":2376},"artifactId",{"type":24,"tag":276,"props":2378,"children":2379},{"style":373},[2380],{"type":33,"value":2381},">dependency-check-maven\u003C/",{"type":24,"tag":276,"props":2383,"children":2384},{"style":2330},[2385],{"type":33,"value":2376},{"type":24,"tag":276,"props":2387,"children":2388},{"style":373},[2389],{"type":33,"value":2338},{"type":24,"tag":276,"props":2391,"children":2392},{"class":278,"line":423},[2393,2397,2402,2407,2411],{"type":24,"tag":276,"props":2394,"children":2395},{"style":373},[2396],{"type":33,"value":2346},{"type":24,"tag":276,"props":2398,"children":2399},{"style":2330},[2400],{"type":33,"value":2401},"version",{"type":24,"tag":276,"props":2403,"children":2404},{"style":373},[2405],{"type":33,"value":2406},">9.0.9\u003C/",{"type":24,"tag":276,"props":2408,"children":2409},{"style":2330},[2410],{"type":33,"value":2401},{"type":24,"tag":276,"props":2412,"children":2413},{"style":373},[2414],{"type":33,"value":2338},{"type":24,"tag":276,"props":2416,"children":2417},{"class":278,"line":445},[2418,2423,2427],{"type":24,"tag":276,"props":2419,"children":2420},{"style":373},[2421],{"type":33,"value":2422},"\u003C/",{"type":24,"tag":276,"props":2424,"children":2425},{"style":2330},[2426],{"type":33,"value":2333},{"type":24,"tag":276,"props":2428,"children":2429},{"style":373},[2430],{"type":33,"value":2338},{"type":24,"tag":29,"props":2432,"children":2433},{},[2434],{"type":33,"value":2435},"Execute dependency checks regularly in your CI/CD pipeline.",{"type":24,"tag":36,"props":2437,"children":2439},{"id":2438},"_8-logging-and-monitoring",[2440],{"type":33,"value":2441},"8. Logging and Monitoring",{"type":24,"tag":29,"props":2443,"children":2444},{},[2445],{"type":33,"value":2446},"Implement comprehensive logging and monitoring to detect and respond to security incidents:",{"type":24,"tag":266,"props":2448,"children":2450},{"className":1749,"code":2449,"language":1652,"meta":7,"style":7},"logger.warn(\"Failed login attempt for user: {}\", username);\n",[2451],{"type":24,"tag":14,"props":2452,"children":2453},{"__ignoreMap":7},[2454],{"type":24,"tag":276,"props":2455,"children":2456},{"class":278,"line":279},[2457,2462,2467,2471,2476],{"type":24,"tag":276,"props":2458,"children":2459},{"style":373},[2460],{"type":33,"value":2461},"logger.",{"type":24,"tag":276,"props":2463,"children":2464},{"style":283},[2465],{"type":33,"value":2466},"warn",{"type":24,"tag":276,"props":2468,"children":2469},{"style":373},[2470],{"type":33,"value":1470},{"type":24,"tag":276,"props":2472,"children":2473},{"style":289},[2474],{"type":33,"value":2475},"\"Failed login attempt for user: {}\"",{"type":24,"tag":276,"props":2477,"children":2478},{"style":373},[2479],{"type":33,"value":1817},{"type":24,"tag":29,"props":2481,"children":2482},{},[2483],{"type":33,"value":2484},"Aggregate logs centrally, and set up alerts for suspicious activities.",{"type":24,"tag":36,"props":2486,"children":2488},{"id":2487},"conclusion",[2489],{"type":33,"value":2490},"Conclusion",{"type":24,"tag":29,"props":2492,"children":2493},{},[2494],{"type":33,"value":2495},"Adopting these best practices can greatly enhance the security posture of your Java applications. Stay proactive by regularly reviewing security measures, updating dependencies, and monitoring your systems. Security is a continuous process, not a one-time setup.",{"type":24,"tag":1585,"props":2497,"children":2498},{"id":140},[2499],{"type":33,"value":143},{"type":24,"tag":150,"props":2501,"children":2502},{},[2503,2513,2523],{"type":24,"tag":154,"props":2504,"children":2505},{},[2506],{"type":24,"tag":158,"props":2507,"children":2510},{"href":2508,"rel":2509},"https://owasp.org/www-project-top-ten/",[162],[2511],{"type":33,"value":2512},"OWASP Top 10",{"type":24,"tag":154,"props":2514,"children":2515},{},[2516],{"type":24,"tag":158,"props":2517,"children":2520},{"href":2518,"rel":2519},"https://spring.io/projects/spring-security",[162],[2521],{"type":33,"value":2522},"Spring Security",{"type":24,"tag":154,"props":2524,"children":2525},{},[2526],{"type":24,"tag":158,"props":2527,"children":2530},{"href":2528,"rel":2529},"https://owasp.org/www-project-dependency-check/",[162],[2531],{"type":33,"value":2532},"OWASP Dependency Check",{"type":24,"tag":1625,"props":2534,"children":2535},{},[2536],{"type":33,"value":1629},{"title":7,"searchDepth":192,"depth":192,"links":2538},[2539,2540,2541,2542,2543,2544,2545,2546,2547,2548],{"id":1668,"depth":192,"text":1671},{"id":1738,"depth":192,"text":1741},{"id":1877,"depth":192,"text":1880},{"id":1991,"depth":192,"text":1994},{"id":2130,"depth":192,"text":2133},{"id":2171,"depth":192,"text":2174},{"id":2203,"depth":192,"text":2206},{"id":2302,"depth":192,"text":2305},{"id":2438,"depth":192,"text":2441},{"id":2487,"depth":192,"text":2490},"content:posts:java-security-best-practices.md","posts/java-security-best-practices.md","posts/java-security-best-practices",{"_path":2553,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":2554,"description":2555,"date":2556,"layout":11,"subtitle":2557,"image":2558,"optimized_image":2558,"category":14,"tags":2559,"author":19,"paginate":6,"body":2563,"_type":200,"_id":3368,"_source":202,"_file":3369,"_stem":3370,"_extension":205},"/posts/circuit-breaking-resilience","Circuit Breaking in Distributed Systems","An expert guide to understanding and implementing circuit breakers in distributed systems, with practical Java examples using Resilience4j and best practices.","2024-07-15T11:00:00.000Z","Enhancing Resilience with Circuit Breaker Patterns","/assets/img/uploads/circuit-breaking-resilience.png",[14,1652,2560,2561,2562],"resilience","circuit-breaker","distributed-systems",{"type":21,"children":2564,"toc":3356},[2565,2573,2579,2584,2617,2622,2628,2633,2651,2657,2664,2669,2779,2785,2989,2995,3000,3167,3173,3178,3272,3277,3283,3306,3310,3315,3319,3352],{"type":24,"tag":25,"props":2566,"children":2567},{},[2568],{"type":24,"tag":29,"props":2569,"children":2570},{},[2571],{"type":33,"value":2572},"In modern distributed systems, failures are inevitable. The circuit breaker pattern helps maintain system stability by preventing failures from cascading across service boundaries. In this post, we'll dive deep into circuit breakers, explore their importance, and demonstrate practical implementation using Resilience4j in Java.",{"type":24,"tag":36,"props":2574,"children":2576},{"id":2575},"understanding-the-circuit-breaker-pattern",[2577],{"type":33,"value":2578},"Understanding the Circuit Breaker Pattern",{"type":24,"tag":29,"props":2580,"children":2581},{},[2582],{"type":33,"value":2583},"The circuit breaker pattern prevents a service from making repeated calls to an external service that is experiencing issues. It has three main states:",{"type":24,"tag":150,"props":2585,"children":2586},{},[2587,2597,2607],{"type":24,"tag":154,"props":2588,"children":2589},{},[2590,2595],{"type":24,"tag":51,"props":2591,"children":2592},{},[2593],{"type":33,"value":2594},"Closed:",{"type":33,"value":2596}," All requests pass through normally.",{"type":24,"tag":154,"props":2598,"children":2599},{},[2600,2605],{"type":24,"tag":51,"props":2601,"children":2602},{},[2603],{"type":33,"value":2604},"Open:",{"type":33,"value":2606}," Requests fail fast, bypassing the problematic service.",{"type":24,"tag":154,"props":2608,"children":2609},{},[2610,2615],{"type":24,"tag":51,"props":2611,"children":2612},{},[2613],{"type":33,"value":2614},"Half-Open:",{"type":33,"value":2616}," A limited number of requests test if the external service has recovered.",{"type":24,"tag":29,"props":2618,"children":2619},{},[2620],{"type":33,"value":2621},"This mechanism helps improve the resilience and responsiveness of your systems.",{"type":24,"tag":36,"props":2623,"children":2625},{"id":2624},"when-to-use-circuit-breakers",[2626],{"type":33,"value":2627},"When to Use Circuit Breakers",{"type":24,"tag":29,"props":2629,"children":2630},{},[2631],{"type":33,"value":2632},"Circuit breakers are essential when:",{"type":24,"tag":150,"props":2634,"children":2635},{},[2636,2641,2646],{"type":24,"tag":154,"props":2637,"children":2638},{},[2639],{"type":33,"value":2640},"Your system relies on remote calls (microservices architecture, external APIs).",{"type":24,"tag":154,"props":2642,"children":2643},{},[2644],{"type":33,"value":2645},"You need to prevent failure propagation.",{"type":24,"tag":154,"props":2647,"children":2648},{},[2649],{"type":33,"value":2650},"You aim to improve user experience by quickly responding to failures.",{"type":24,"tag":36,"props":2652,"children":2654},{"id":2653},"implementing-circuit-breakers-with-resilience4j",[2655],{"type":33,"value":2656},"Implementing Circuit Breakers with Resilience4j",{"type":24,"tag":2658,"props":2659,"children":2661},"h3",{"id":2660},"step-1-adding-dependencies",[2662],{"type":33,"value":2663},"Step 1: Adding Dependencies",{"type":24,"tag":29,"props":2665,"children":2666},{},[2667],{"type":33,"value":2668},"Add Resilience4j dependencies to your project (Maven example):",{"type":24,"tag":266,"props":2670,"children":2672},{"className":2313,"code":2671,"language":2315,"meta":7,"style":7},"\u003Cdependency>\n    \u003CgroupId>io.github.resilience4j\u003C/groupId>\n    \u003CartifactId>resilience4j-circuitbreaker\u003C/artifactId>\n    \u003Cversion>2.0.2\u003C/version>\n\u003C/dependency>\n",[2673],{"type":24,"tag":14,"props":2674,"children":2675},{"__ignoreMap":7},[2676,2692,2716,2740,2764],{"type":24,"tag":276,"props":2677,"children":2678},{"class":278,"line":279},[2679,2683,2688],{"type":24,"tag":276,"props":2680,"children":2681},{"style":373},[2682],{"type":33,"value":2327},{"type":24,"tag":276,"props":2684,"children":2685},{"style":2330},[2686],{"type":33,"value":2687},"dependency",{"type":24,"tag":276,"props":2689,"children":2690},{"style":373},[2691],{"type":33,"value":2338},{"type":24,"tag":276,"props":2693,"children":2694},{"class":278,"line":192},[2695,2699,2703,2708,2712],{"type":24,"tag":276,"props":2696,"children":2697},{"style":373},[2698],{"type":33,"value":2346},{"type":24,"tag":276,"props":2700,"children":2701},{"style":2330},[2702],{"type":33,"value":2351},{"type":24,"tag":276,"props":2704,"children":2705},{"style":373},[2706],{"type":33,"value":2707},">io.github.resilience4j\u003C/",{"type":24,"tag":276,"props":2709,"children":2710},{"style":2330},[2711],{"type":33,"value":2351},{"type":24,"tag":276,"props":2713,"children":2714},{"style":373},[2715],{"type":33,"value":2338},{"type":24,"tag":276,"props":2717,"children":2718},{"class":278,"line":410},[2719,2723,2727,2732,2736],{"type":24,"tag":276,"props":2720,"children":2721},{"style":373},[2722],{"type":33,"value":2346},{"type":24,"tag":276,"props":2724,"children":2725},{"style":2330},[2726],{"type":33,"value":2376},{"type":24,"tag":276,"props":2728,"children":2729},{"style":373},[2730],{"type":33,"value":2731},">resilience4j-circuitbreaker\u003C/",{"type":24,"tag":276,"props":2733,"children":2734},{"style":2330},[2735],{"type":33,"value":2376},{"type":24,"tag":276,"props":2737,"children":2738},{"style":373},[2739],{"type":33,"value":2338},{"type":24,"tag":276,"props":2741,"children":2742},{"class":278,"line":423},[2743,2747,2751,2756,2760],{"type":24,"tag":276,"props":2744,"children":2745},{"style":373},[2746],{"type":33,"value":2346},{"type":24,"tag":276,"props":2748,"children":2749},{"style":2330},[2750],{"type":33,"value":2401},{"type":24,"tag":276,"props":2752,"children":2753},{"style":373},[2754],{"type":33,"value":2755},">2.0.2\u003C/",{"type":24,"tag":276,"props":2757,"children":2758},{"style":2330},[2759],{"type":33,"value":2401},{"type":24,"tag":276,"props":2761,"children":2762},{"style":373},[2763],{"type":33,"value":2338},{"type":24,"tag":276,"props":2765,"children":2766},{"class":278,"line":445},[2767,2771,2775],{"type":24,"tag":276,"props":2768,"children":2769},{"style":373},[2770],{"type":33,"value":2422},{"type":24,"tag":276,"props":2772,"children":2773},{"style":2330},[2774],{"type":33,"value":2687},{"type":24,"tag":276,"props":2776,"children":2777},{"style":373},[2778],{"type":33,"value":2338},{"type":24,"tag":2658,"props":2780,"children":2782},{"id":2781},"step-2-configuring-circuit-breaker",[2783],{"type":33,"value":2784},"Step 2: Configuring Circuit Breaker",{"type":24,"tag":266,"props":2786,"children":2788},{"className":1749,"code":2787,"language":1652,"meta":7,"style":7},"CircuitBreakerConfig config = CircuitBreakerConfig.custom()\n    .failureRateThreshold(50)\n    .waitDurationInOpenState(Duration.ofMillis(1000))\n    .permittedNumberOfCallsInHalfOpenState(2)\n    .minimumNumberOfCalls(5)\n    .build();\n\nCircuitBreaker circuitBreaker = CircuitBreaker.of(\"myService\", config);\n",[2789],{"type":24,"tag":14,"props":2790,"children":2791},{"__ignoreMap":7},[2792,2819,2845,2881,2905,2930,2946,2953],{"type":24,"tag":276,"props":2793,"children":2794},{"class":278,"line":279},[2795,2800,2804,2809,2814],{"type":24,"tag":276,"props":2796,"children":2797},{"style":373},[2798],{"type":33,"value":2799},"CircuitBreakerConfig config ",{"type":24,"tag":276,"props":2801,"children":2802},{"style":367},[2803],{"type":33,"value":513},{"type":24,"tag":276,"props":2805,"children":2806},{"style":373},[2807],{"type":33,"value":2808}," CircuitBreakerConfig.",{"type":24,"tag":276,"props":2810,"children":2811},{"style":283},[2812],{"type":33,"value":2813},"custom",{"type":24,"tag":276,"props":2815,"children":2816},{"style":373},[2817],{"type":33,"value":2818},"()\n",{"type":24,"tag":276,"props":2820,"children":2821},{"class":278,"line":192},[2822,2827,2832,2836,2841],{"type":24,"tag":276,"props":2823,"children":2824},{"style":373},[2825],{"type":33,"value":2826},"    .",{"type":24,"tag":276,"props":2828,"children":2829},{"style":283},[2830],{"type":33,"value":2831},"failureRateThreshold",{"type":24,"tag":276,"props":2833,"children":2834},{"style":373},[2835],{"type":33,"value":1470},{"type":24,"tag":276,"props":2837,"children":2838},{"style":476},[2839],{"type":33,"value":2840},"50",{"type":24,"tag":276,"props":2842,"children":2843},{"style":373},[2844],{"type":33,"value":499},{"type":24,"tag":276,"props":2846,"children":2847},{"class":278,"line":410},[2848,2852,2857,2862,2867,2871,2876],{"type":24,"tag":276,"props":2849,"children":2850},{"style":373},[2851],{"type":33,"value":2826},{"type":24,"tag":276,"props":2853,"children":2854},{"style":283},[2855],{"type":33,"value":2856},"waitDurationInOpenState",{"type":24,"tag":276,"props":2858,"children":2859},{"style":373},[2860],{"type":33,"value":2861},"(Duration.",{"type":24,"tag":276,"props":2863,"children":2864},{"style":283},[2865],{"type":33,"value":2866},"ofMillis",{"type":24,"tag":276,"props":2868,"children":2869},{"style":373},[2870],{"type":33,"value":1470},{"type":24,"tag":276,"props":2872,"children":2873},{"style":476},[2874],{"type":33,"value":2875},"1000",{"type":24,"tag":276,"props":2877,"children":2878},{"style":373},[2879],{"type":33,"value":2880},"))\n",{"type":24,"tag":276,"props":2882,"children":2883},{"class":278,"line":423},[2884,2888,2893,2897,2901],{"type":24,"tag":276,"props":2885,"children":2886},{"style":373},[2887],{"type":33,"value":2826},{"type":24,"tag":276,"props":2889,"children":2890},{"style":283},[2891],{"type":33,"value":2892},"permittedNumberOfCallsInHalfOpenState",{"type":24,"tag":276,"props":2894,"children":2895},{"style":373},[2896],{"type":33,"value":1470},{"type":24,"tag":276,"props":2898,"children":2899},{"style":476},[2900],{"type":33,"value":1837},{"type":24,"tag":276,"props":2902,"children":2903},{"style":373},[2904],{"type":33,"value":499},{"type":24,"tag":276,"props":2906,"children":2907},{"class":278,"line":445},[2908,2912,2917,2921,2926],{"type":24,"tag":276,"props":2909,"children":2910},{"style":373},[2911],{"type":33,"value":2826},{"type":24,"tag":276,"props":2913,"children":2914},{"style":283},[2915],{"type":33,"value":2916},"minimumNumberOfCalls",{"type":24,"tag":276,"props":2918,"children":2919},{"style":373},[2920],{"type":33,"value":1470},{"type":24,"tag":276,"props":2922,"children":2923},{"style":476},[2924],{"type":33,"value":2925},"5",{"type":24,"tag":276,"props":2927,"children":2928},{"style":373},[2929],{"type":33,"value":499},{"type":24,"tag":276,"props":2931,"children":2932},{"class":278,"line":455},[2933,2937,2942],{"type":24,"tag":276,"props":2934,"children":2935},{"style":373},[2936],{"type":33,"value":2826},{"type":24,"tag":276,"props":2938,"children":2939},{"style":283},[2940],{"type":33,"value":2941},"build",{"type":24,"tag":276,"props":2943,"children":2944},{"style":373},[2945],{"type":33,"value":1869},{"type":24,"tag":276,"props":2947,"children":2948},{"class":278,"line":464},[2949],{"type":24,"tag":276,"props":2950,"children":2951},{"emptyLinePlaceholder":449},[2952],{"type":33,"value":452},{"type":24,"tag":276,"props":2954,"children":2955},{"class":278,"line":472},[2956,2961,2965,2970,2975,2979,2984],{"type":24,"tag":276,"props":2957,"children":2958},{"style":373},[2959],{"type":33,"value":2960},"CircuitBreaker circuitBreaker ",{"type":24,"tag":276,"props":2962,"children":2963},{"style":367},[2964],{"type":33,"value":513},{"type":24,"tag":276,"props":2966,"children":2967},{"style":373},[2968],{"type":33,"value":2969}," CircuitBreaker.",{"type":24,"tag":276,"props":2971,"children":2972},{"style":283},[2973],{"type":33,"value":2974},"of",{"type":24,"tag":276,"props":2976,"children":2977},{"style":373},[2978],{"type":33,"value":1470},{"type":24,"tag":276,"props":2980,"children":2981},{"style":289},[2982],{"type":33,"value":2983},"\"myService\"",{"type":24,"tag":276,"props":2985,"children":2986},{"style":373},[2987],{"type":33,"value":2988},", config);\n",{"type":24,"tag":2658,"props":2990,"children":2992},{"id":2991},"step-3-using-circuit-breaker",[2993],{"type":33,"value":2994},"Step 3: Using Circuit Breaker",{"type":24,"tag":29,"props":2996,"children":2997},{},[2998],{"type":33,"value":2999},"Wrap service calls using the circuit breaker:",{"type":24,"tag":266,"props":3001,"children":3003},{"className":1749,"code":3002,"language":1652,"meta":7,"style":7},"Supplier\u003CString> decoratedSupplier = CircuitBreaker.decorateSupplier(circuitBreaker, () -> myService.remoteCall());\n\nTry.ofSupplier(decoratedSupplier)\n    .recover(throwable -> \"Fallback response\")\n    .onSuccess(result -> System.out.println(\"Response: \" + result));\n",[3004],{"type":24,"tag":14,"props":3005,"children":3006},{"__ignoreMap":7},[3007,3062,3069,3087,3117],{"type":24,"tag":276,"props":3008,"children":3009},{"class":278,"line":279},[3010,3015,3020,3025,3029,3033,3038,3043,3048,3053,3058],{"type":24,"tag":276,"props":3011,"children":3012},{"style":373},[3013],{"type":33,"value":3014},"Supplier\u003C",{"type":24,"tag":276,"props":3016,"children":3017},{"style":367},[3018],{"type":33,"value":3019},"String",{"type":24,"tag":276,"props":3021,"children":3022},{"style":373},[3023],{"type":33,"value":3024},"> decoratedSupplier ",{"type":24,"tag":276,"props":3026,"children":3027},{"style":367},[3028],{"type":33,"value":513},{"type":24,"tag":276,"props":3030,"children":3031},{"style":373},[3032],{"type":33,"value":2969},{"type":24,"tag":276,"props":3034,"children":3035},{"style":283},[3036],{"type":33,"value":3037},"decorateSupplier",{"type":24,"tag":276,"props":3039,"children":3040},{"style":373},[3041],{"type":33,"value":3042},"(circuitBreaker, () ",{"type":24,"tag":276,"props":3044,"children":3045},{"style":367},[3046],{"type":33,"value":3047},"->",{"type":24,"tag":276,"props":3049,"children":3050},{"style":373},[3051],{"type":33,"value":3052}," myService.",{"type":24,"tag":276,"props":3054,"children":3055},{"style":283},[3056],{"type":33,"value":3057},"remoteCall",{"type":24,"tag":276,"props":3059,"children":3060},{"style":373},[3061],{"type":33,"value":1948},{"type":24,"tag":276,"props":3063,"children":3064},{"class":278,"line":192},[3065],{"type":24,"tag":276,"props":3066,"children":3067},{"emptyLinePlaceholder":449},[3068],{"type":33,"value":452},{"type":24,"tag":276,"props":3070,"children":3071},{"class":278,"line":410},[3072,3077,3082],{"type":24,"tag":276,"props":3073,"children":3074},{"style":373},[3075],{"type":33,"value":3076},"Try.",{"type":24,"tag":276,"props":3078,"children":3079},{"style":283},[3080],{"type":33,"value":3081},"ofSupplier",{"type":24,"tag":276,"props":3083,"children":3084},{"style":373},[3085],{"type":33,"value":3086},"(decoratedSupplier)\n",{"type":24,"tag":276,"props":3088,"children":3089},{"class":278,"line":423},[3090,3094,3099,3104,3108,3113],{"type":24,"tag":276,"props":3091,"children":3092},{"style":373},[3093],{"type":33,"value":2826},{"type":24,"tag":276,"props":3095,"children":3096},{"style":283},[3097],{"type":33,"value":3098},"recover",{"type":24,"tag":276,"props":3100,"children":3101},{"style":373},[3102],{"type":33,"value":3103},"(throwable ",{"type":24,"tag":276,"props":3105,"children":3106},{"style":367},[3107],{"type":33,"value":3047},{"type":24,"tag":276,"props":3109,"children":3110},{"style":289},[3111],{"type":33,"value":3112}," \"Fallback response\"",{"type":24,"tag":276,"props":3114,"children":3115},{"style":373},[3116],{"type":33,"value":499},{"type":24,"tag":276,"props":3118,"children":3119},{"class":278,"line":445},[3120,3124,3129,3134,3138,3143,3148,3152,3157,3162],{"type":24,"tag":276,"props":3121,"children":3122},{"style":373},[3123],{"type":33,"value":2826},{"type":24,"tag":276,"props":3125,"children":3126},{"style":283},[3127],{"type":33,"value":3128},"onSuccess",{"type":24,"tag":276,"props":3130,"children":3131},{"style":373},[3132],{"type":33,"value":3133},"(result ",{"type":24,"tag":276,"props":3135,"children":3136},{"style":367},[3137],{"type":33,"value":3047},{"type":24,"tag":276,"props":3139,"children":3140},{"style":373},[3141],{"type":33,"value":3142}," System.out.",{"type":24,"tag":276,"props":3144,"children":3145},{"style":283},[3146],{"type":33,"value":3147},"println",{"type":24,"tag":276,"props":3149,"children":3150},{"style":373},[3151],{"type":33,"value":1470},{"type":24,"tag":276,"props":3153,"children":3154},{"style":289},[3155],{"type":33,"value":3156},"\"Response: \"",{"type":24,"tag":276,"props":3158,"children":3159},{"style":367},[3160],{"type":33,"value":3161}," +",{"type":24,"tag":276,"props":3163,"children":3164},{"style":373},[3165],{"type":33,"value":3166}," result));\n",{"type":24,"tag":36,"props":3168,"children":3170},{"id":3169},"monitoring-and-metrics",[3171],{"type":33,"value":3172},"Monitoring and Metrics",{"type":24,"tag":29,"props":3174,"children":3175},{},[3176],{"type":33,"value":3177},"Resilience4j provides built-in support for metrics and monitoring:",{"type":24,"tag":266,"props":3179,"children":3181},{"className":1749,"code":3180,"language":1652,"meta":7,"style":7},"CircuitBreakerRegistry registry = CircuitBreakerRegistry.ofDefaults();\nregistry.getEventPublisher()\n    .onStateTransition(event -> System.out.println(event.getStateTransition()));\n",[3182],{"type":24,"tag":14,"props":3183,"children":3184},{"__ignoreMap":7},[3185,3211,3228],{"type":24,"tag":276,"props":3186,"children":3187},{"class":278,"line":279},[3188,3193,3197,3202,3207],{"type":24,"tag":276,"props":3189,"children":3190},{"style":373},[3191],{"type":33,"value":3192},"CircuitBreakerRegistry registry ",{"type":24,"tag":276,"props":3194,"children":3195},{"style":367},[3196],{"type":33,"value":513},{"type":24,"tag":276,"props":3198,"children":3199},{"style":373},[3200],{"type":33,"value":3201}," CircuitBreakerRegistry.",{"type":24,"tag":276,"props":3203,"children":3204},{"style":283},[3205],{"type":33,"value":3206},"ofDefaults",{"type":24,"tag":276,"props":3208,"children":3209},{"style":373},[3210],{"type":33,"value":1869},{"type":24,"tag":276,"props":3212,"children":3213},{"class":278,"line":192},[3214,3219,3224],{"type":24,"tag":276,"props":3215,"children":3216},{"style":373},[3217],{"type":33,"value":3218},"registry.",{"type":24,"tag":276,"props":3220,"children":3221},{"style":283},[3222],{"type":33,"value":3223},"getEventPublisher",{"type":24,"tag":276,"props":3225,"children":3226},{"style":373},[3227],{"type":33,"value":2818},{"type":24,"tag":276,"props":3229,"children":3230},{"class":278,"line":410},[3231,3235,3240,3245,3249,3253,3257,3262,3267],{"type":24,"tag":276,"props":3232,"children":3233},{"style":373},[3234],{"type":33,"value":2826},{"type":24,"tag":276,"props":3236,"children":3237},{"style":283},[3238],{"type":33,"value":3239},"onStateTransition",{"type":24,"tag":276,"props":3241,"children":3242},{"style":373},[3243],{"type":33,"value":3244},"(event ",{"type":24,"tag":276,"props":3246,"children":3247},{"style":367},[3248],{"type":33,"value":3047},{"type":24,"tag":276,"props":3250,"children":3251},{"style":373},[3252],{"type":33,"value":3142},{"type":24,"tag":276,"props":3254,"children":3255},{"style":283},[3256],{"type":33,"value":3147},{"type":24,"tag":276,"props":3258,"children":3259},{"style":373},[3260],{"type":33,"value":3261},"(event.",{"type":24,"tag":276,"props":3263,"children":3264},{"style":283},[3265],{"type":33,"value":3266},"getStateTransition",{"type":24,"tag":276,"props":3268,"children":3269},{"style":373},[3270],{"type":33,"value":3271},"()));\n",{"type":24,"tag":29,"props":3273,"children":3274},{},[3275],{"type":33,"value":3276},"Integrate with Prometheus, Grafana, or other observability tools for comprehensive monitoring.",{"type":24,"tag":36,"props":3278,"children":3280},{"id":3279},"best-practices-for-circuit-breaking",[3281],{"type":33,"value":3282},"Best Practices for Circuit Breaking",{"type":24,"tag":150,"props":3284,"children":3285},{},[3286,3291,3296,3301],{"type":24,"tag":154,"props":3287,"children":3288},{},[3289],{"type":33,"value":3290},"Configure sensible thresholds to avoid premature circuit opening.",{"type":24,"tag":154,"props":3292,"children":3293},{},[3294],{"type":33,"value":3295},"Always define fallback mechanisms.",{"type":24,"tag":154,"props":3297,"children":3298},{},[3299],{"type":33,"value":3300},"Regularly monitor circuit breaker states and metrics.",{"type":24,"tag":154,"props":3302,"children":3303},{},[3304],{"type":33,"value":3305},"Test thoroughly to ensure reliability under failure conditions.",{"type":24,"tag":36,"props":3307,"children":3308},{"id":2487},[3309],{"type":33,"value":2490},{"type":24,"tag":29,"props":3311,"children":3312},{},[3313],{"type":33,"value":3314},"Implementing circuit breakers using Resilience4j significantly enhances the resilience of your Java-based distributed systems. This proactive approach ensures your systems gracefully handle failures, maintaining service availability and user satisfaction.",{"type":24,"tag":1585,"props":3316,"children":3317},{"id":140},[3318],{"type":33,"value":143},{"type":24,"tag":150,"props":3320,"children":3321},{},[3322,3332,3342],{"type":24,"tag":154,"props":3323,"children":3324},{},[3325],{"type":24,"tag":158,"props":3326,"children":3329},{"href":3327,"rel":3328},"https://resilience4j.readme.io/docs",[162],[3330],{"type":33,"value":3331},"Resilience4j Official Documentation",{"type":24,"tag":154,"props":3333,"children":3334},{},[3335],{"type":24,"tag":158,"props":3336,"children":3339},{"href":3337,"rel":3338},"https://martinfowler.com/bliki/CircuitBreaker.html",[162],[3340],{"type":33,"value":3341},"Circuit Breaker Pattern by Martin Fowler",{"type":24,"tag":154,"props":3343,"children":3344},{},[3345],{"type":24,"tag":158,"props":3346,"children":3349},{"href":3347,"rel":3348},"https://microservices.io/patterns/reliability/circuit-breaker.html",[162],[3350],{"type":33,"value":3351},"Microservices Patterns by Chris Richardson",{"type":24,"tag":1625,"props":3353,"children":3354},{},[3355],{"type":33,"value":1629},{"title":7,"searchDepth":192,"depth":192,"links":3357},[3358,3359,3360,3365,3366,3367],{"id":2575,"depth":192,"text":2578},{"id":2624,"depth":192,"text":2627},{"id":2653,"depth":192,"text":2656,"children":3361},[3362,3363,3364],{"id":2660,"depth":410,"text":2663},{"id":2781,"depth":410,"text":2784},{"id":2991,"depth":410,"text":2994},{"id":3169,"depth":192,"text":3172},{"id":3279,"depth":192,"text":3282},{"id":2487,"depth":192,"text":2490},"content:posts:circuit-breaking-resilience.md","posts/circuit-breaking-resilience.md","posts/circuit-breaking-resilience",[3372,3373,3374,3375,3379,3383,3387,3391,3392,3396,3400,3404,3408,3412,3416,3420,3424,3428,3431,3435,3439],{"_path":208,"title":209,"date":211},{"_path":1645,"title":1646,"date":1648},{"_path":2553,"title":2554,"date":2556},{"_path":3376,"title":3377,"date":3378},"/posts/maven-and-github-actions","Automating Java Builds with Maven and GitHub Actions CI/CD","2024-06-15T11:00:00.000Z",{"_path":3380,"title":3381,"date":3382},"/posts/pattern-matching-sealed-classes-java21","A Guide to Pattern Matching and Sealed Classes in Java 21","2024-05-15T11:00:00.000Z",{"_path":3384,"title":3385,"date":3386},"/posts/async-java-completable-vs-reactive","Asynchronous Programming in Java: CompletableFuture vs Reactive Streams","2024-04-15T11:00:00.000Z",{"_path":3388,"title":3389,"date":3390},"/posts/mastering-concurrency","Mastering Concurrency in Java: Threads, Executors, and Virtual Threads","2024-03-15T11:00:00.000Z",{"_path":4,"title":8,"date":10},{"_path":3393,"title":3394,"date":3395},"/posts/tdd-testing","Test-Driven Development (TDD) in Java with JUnit and Mockito","2024-01-15T11:00:00.000Z",{"_path":3397,"title":3398,"date":3399},"/posts/single-table-design","Single Table Design","2023-12-27T11:00:00.000Z",{"_path":3401,"title":3402,"date":3403},"/posts/java-17","Exploring Java 17's New Features","2023-10-08T11:00:00.000Z",{"_path":3405,"title":3406,"date":3407},"/posts/hexagonal-architecture","Introduction to Hexagonal Architecture","2023-10-02 18:00:00",{"_path":3409,"title":3410,"date":3411},"/posts/terraform","Unleashing the Power of Infrastructure as Code (IaC)","2023-09-24 18:00:00",{"_path":3413,"title":3414,"date":3415},"/posts/python-virtual-environments","Python Virtual Environments (venv)","2023-09-17 22:00:00",{"_path":3417,"title":3418,"date":3419},"/posts/api-first-principles","API First principles","2023-09-01 22:15:18",{"_path":3421,"title":3422,"date":3423},"/posts/how-to-configure-ssh-key-based-authentication","How To Configure SSH Key-Based Authentication","2021-02-21 03:46:12",{"_path":3425,"title":3426,"date":3427},"/posts/algorithms-binary-search-tree","Algorithms - Binary Search Tree in kotlin","2020-12-06T11:00:00.000Z",{"_path":3429,"title":3430,"date":3427},"/posts/how-to-create-a-twitter-bot","How to create a Twitter bot",{"_path":3432,"title":3433,"date":3434},"/posts/manage-aws-infrastructure-with-terraform","Bootstrap complete Java application infrastructure in AWS with Terraform","2020-12-02T11:00:00.000Z",{"_path":3436,"title":3437,"date":3438},"/posts/algorithms-working-with-trees","Algorithms - Binary tree traversals in kotlin","2020-11-24T11:00:00.000Z",{"_path":3440,"title":3441,"date":3442},"/posts/playing-with-ocr-libraries","Playing with OCR libraries","2020-11-22T11:00:00.000Z",1790510065827]